Linux Mini Shell
Linux Mini Shell
Minishell
A FrtEiin . is dto
Ti i prl frfnadi i m romt' hmwr. hs s uey o u n t s y omaes oeok
BTepolm .h rbe
Objective
The main objective of this programming assignment is to introduce the student to C/C++ programming in the Linux environment. Specifically, the student will implement a C/C++ program that will use Linux system calls and commands.
Problem specification
A shell is an interactive program that interprets user commands for the operating system (i.e. Linux). The commands are entered on a command line interface and each command can be specified with some options and arguments. Linux provides different shells (e.g. bourne, csh, tcsh, bash, korn,) that can handle mostly the same commands with some syntactical differences. In addition, the user can also implement his (or her) own shell to fit his (or her) particular needs. System calls represent calls to functions that are implemented in the kernel of the Linux operating system. The programmer must use system calls in order to request system services because he (or she) is restricted direct access to the system resources for protection and security reasons.
You are required to write a C/C++ language program that will be used as a command shell for file management. You must use Linux system calls and commands for file and for process management to implement the functionalities of your shell. You should name the program minishell.c and save it under a directory named lab4. Your program will behave similarly to the Linux shell and should display a prompt (i.e. minishell>) at start-up. The user should then enter one of the shell commands (with appropriate options and arguments) at the prompt followed by <Enter>, and the shell should again display the prompt after processing the command. If a command is invalid the shell should display an appropriate error message. In order to compile and link your program, you will need to implement a make file.
Shell termination.
The exit command should terminate the execution of the shell and control should be returned to the Linux shell using the void exit(int status) system call.
The cd command changes the current working directory of the user to the specified directory (if exists) using the int chdir(const char *path) system call.
If the specified directory does not exist then the message The directory directory does not exist should be displayed on the screen.
www.staroceans.net/minishell.htm
1/7
17/04/2012
Syntax : dir [directory]. Example : minishell>dir ~/lab4.
The dir command lists all the files that are in the specified directory (if exists) using the DIR *opendir(const char *dirname), int closedir(DIR *dirptr), and struct dirent *readdir(DIR *dirptr) system calls. If no directory argument is specified (enclosed brackets indicate an optional argument) then the content of the current directory should be displayed. If the specified directory does not exist then the message The directory directory does not exist should be displayed on the screen. You should implement a function void dir(char *directory) that will call the appropriate system calls.
Deleting a file.
Syntax : del file. Example : minishell>del ~/lab4/minishell.o The del command deletes the specified file using the int unlink(char *pathname) system call.
If the specified file does not exist then the message The file file does not exist should be displayed on the screen. File existence should be checked with the int access(char *pathname, int amode) system call.
Renaming a file.
The ren command renames a source file srcfile (if exists) into a destination file dstfile using the int rename(const char *oldpath, const char *newpath) system call.
If the source file srcfile does not exist then the message The source file srcfile does not exist. should be displayed on the screen. If the source file srcfile and the destination file dstfile are the same then the message The source file srcfile and the destination file dstfile can not be the same. should be displayed on the screen.
Copying a file.
The copy command copies a source file srcfile (if exists) to a destination file dstfile by using the Linux cp command. Consequently, the copy command must be implemented as an external minishell command. Thus, the minishell must create a new child process to run the cp command using the pid_t fork( ), int execv(const char *path, char *const argv[]) and pid_t wait(int *status) system calls . Your shell should wait the end of execution of the child process before accepting new user commands.
Test cases
www.staroceans.net/minishell.htm
2/7
17/04/2012
minishell>del ~/minishell1.c minishell>dir minishell>ren minishell.c minishell2.c minishell>cd .. minishell>dir ~/lab4 minishell>exit
Submission
A diskette that contains the source, object and executable programs. A listing of the make file. A printed copy of the source and listing files. Sample output data for all the test cases that are specified.
www.staroceans.net/minishell.htm
3/7
17/04/2012
vi eiSelca*prm[,itprmubr; od xthl(hr aas] n aaNme) vi cagDrca*prm[,itprmubr; od hnei(hr aas] n aaNme) vi lsDrca*prm[,itprmubr; od iti(hr aas] n aaNme) vi dlieca*prm[,itprmubr; od eFl(hr aas] n aaNme) vi rnieca*prm[,itprmubr ; od eFl(hr aas] n aaNme) vi cpFl(hr prm[,itprmubr; od oyieca* aas] n aaNme)
vi (cmadra[omnNme](hr prm[,itprmubr= od *omnAryCmadubr)ca* aas] n aaNme) { eiSel cagDr lsDr dlie rnie cpFl xthl, hnei, iti, eFl, eFl, oyie } ; itpreomn(hr cdt,ca*prm[,it prmubr; n asCmadca* mSr hr aas] n* aaNme) vi ddrca*pt) od oi(hr ah; vi pitrmt) od rnPop(; vi eradeca*mg; od rhnl(hr s) itmi(n ag,ca*ag[) n anit rc hr rv] { ca bfMxomnLn] hr u[aCmadie; itn prmubr n , aaNme; itcmadye n omnTp; ca*prm[] hr aas4; pitrmt) rnPop(; wie(nra(TI_IEO bf MxomnLn)>) hl (=edSDNFLN, u, aCmadie)0 { bfn=\' u[]'0; cmadyepreomn(u,prm,&aaNme) omnTp=asCmadbf aas prmubr; i (omnTp=-) f cmadye=1 { pit(ilglcmadns) rnf"lea omn\%"; } es le { cmadra[omnTp]prm,prmubr; omnArycmadye(aas aaNme) } pitrmt) rnPop(; } rtr 0 eun ; } itpreomn(hr bf ca*prm[,it prmubr n asCmadca* u, hr aas] n* aaNme) { iti n ; *aaNme=; prmubr0 i (prm[prmubr=ttkbf dlm)=UL f (aas*aaNme]sro(u, ei)!NL) { fr(=omnNme-;i=;io iCmadubr1 >0 -) { i (tcpprm[prmubr,cmadt[]=0 f srm(aas*aaNme] omnSri)=) { bek ra; } } /we ntfud i=1 /hn o on, =i (=-) f i=1 { rtr i eun ; } } es le { rtr -; eun 1 } (prmubr+; *aaNme)+ /temxmmprmnme i ol 2 s Its fr3t sei sro rtr NL /h aiu aa ubr s ny , o et o o e f ttk eun UL wie() hl 1 { i (prm[prmubr=ttkNL,dlm)=UL f (aas*aaNme]sro(UL ei)=NL) { bek ra; } (prmubr+; *aaNme)+ i (prmubr=) f *aaNme=4 { /ti masteprmnme i mr ta 2adi i wog /hs en h aa ubr s oe hn n t s rn rtr -; eun 1 } } rtr i eun ; }
www.staroceans.net/minishell.htm
4/7
17/04/2012
vi eiSelca*prm[,itprmubr od xthl(hr aas] n aaNme) { ei() xt0; }
vi rnieca*prm[,itprmubr od eFl(hr aas] n aaNme) { i (aaNme!3 f prmubr=) { pit(ept) rnfhlSr; } es le { i (tcpprm[] prm[]=0 f srm(aas1, aas2)=) { pit(Tesuc fl % adtedsiainfl % cnntb tesm.n,prm[] prm[]; rnf"h ore ie s n h etnto ie s a o e h ae\" aas1, aas2) } es le { i (cesprm[] FO)0 f acs(aas1, _K<) { pit(Tesuc fl % de nteit\" prm[]; rnf"h ore ie s os o xs.n, aas1) } es le { i (eaeprm[] prm[]<) f rnm(aas1, aas2)0 { pit(cno rnm fl fo % t fl %\" prm[] prm[]; rnf"ant eae ie rm s o ie sn, aas1, aas2) } } } } } vi cpFl(hr prm[,itprmubr od oyieca* aas] n aaNme) { itsau; n tts i (aaNme!3 f prmubr=) { pit(ept) rnfhlSr; } es le { i (ok)=) f fr(=0 { eev"bnc" prm) xc(/i/p, aas; ei() xt0; } es le { i (at&tts<) f wi(sau)0 { pit(wi errn) rnf"at ro\"; } } } } vi dlieca*prm[,itprmubr od eFl(hr aas] n aaNme) { i (aaNme!2 f prmubr=) { pit(ept) rnfhlSr; } es le { i (cesprm[] FO)0 f acs(aas1, _K<) { pit(Tefl % de nteitn,prm[]; rnf"h ie s os o xs\" aas1) } es le { i (nikprm[]<) f uln(aas1)0 { pit(cno dlt fl %\" prm[]; rnf"ant eee ie sn, aas1) } } } } vi cagDrca*prm[,itprmubr od hnei(hr aas] n aaNme) { i (aaNme!2 f prmubr=) { pit(ept) rnfhlSr; } es le
www.staroceans.net/minishell.htm
5/7
17/04/2012
{
} vi ddrca*pt) od oi(hr ah { DR d; I* p src drn*droe tut iet ind; i (d=pni(ah)=UL f (poedrpt)!NL) { wie(droeradrd)!NL) hl (ind=edi(p)=UL { pit(%\" droe>_ae; rnf"sn, ind-dnm) } } es le { pit(Tedrcoy% de nteitn,pt) rnf"h ietr s os o xs\" ah; } } vi lsDrca*prm[,itprmubr od iti(hr aas] n aaNme) { i (aaNme!1&aaNme!2 f prmubr=&prmubr=) { pit(ept) rnfhlSr; } es le { i (aaNme=1 f prmubr=) { ddr""; oi(.) } es le { ddrprm[]; oi(aas1) } } }
vi pitrmt) od rnPop( { i (rt(TOTFLN,popSr sre(rmtt)!sre(rmtt) f wieSDU_IEO rmtt, tlnpopSr)=tlnpopSr) { erade"antwiet sdu"; rhnl(cno rt o tot) } } vi eradeca*mg od rhnl(hr s) { pro(s) errmg; ei() xt1; } Hwt rn o o u?
[qingz_hu@alamanni ~/lab5] % ls dirview.exe lab4 mini.exe minishell.c minishell.exe [qingz_hu@alamanni ~/lab5] % ./minishell.exe minishell>dir . .. minishell.c mini.exe lab4 dirview.exe minishell.exe minishell>cd lab4 minishell>dir . .. backup.c dirview.exe minishell>copy ../dirview.exe ./dirview.bak minishell>dir
www.staroceans.net/minishell.htm
6/7
17/04/2012
. .. backup.c dirview.exe dirview.bak minishell>del dirview.bak minishell>dir . .. backup.c dirview.exe minishell>ren dirview.exe dirview.o minishell>dir . .. backup.c dirview.o minishell>exit [qingz_hu@alamanni ~/lab5] %
www.staroceans.net/minishell.htm
7/7