Question: C Programming. We will continue extending our msh shell. First, try running cd in your msh and see what you get. Probably your current directory
C Programming. We will continue extending our msh shell. First, try running cd in your msh and see what you get. Probably your current directory did not change. Think about why this is so, then extend your msh in the two following ways:
Add a built-in command cd in your msh shell. This command should work in two ways, just like in bash: 1) if no argument is supplied, change to the users home directory, and 2) if an argument is supplied, change to the directory specified by the argument. Hint: to get the users home directory, your code will have to read the value of the HOME environment variable. Investigate the getenv library function and chdir system call. Here is the sort of output your new msh should produce:
$ ./msh
msh> ls
backup Makefile msh msh.c msh-hw-2.c README.txt temp
msh> pwd
/home/CLASSES/brunsglenn/ctests/msh
msh> cd backup
msh> pwd
/home/CLASSES/brunsglenn/ctests/msh/backup
msh> cd ..
msh> pwd
/home/CLASSES/brunsglenn/ctests/msh
msh> cd
msh> pwd
/home/CLASSES/brunsglenn
msh> exit
$
Allow msh to read input from a file rather than from user input. If msh is invoked from bash with a command-line argument, then msh should attempt to open the file specified by the command-line argument, and use that file as input. Your msh should terminate when the end of the file is reached. However, msh should still be able to be invoked without a command-line argument (as shown in the previous part of this exercise). Here is an example of how msh should work with a filename on the command line:
$ ls
backup Makefile msh msh.c msh-hw-2.c README.txt temp
$ cat temp
ls
$ ./msh temp
backup Makefile msh msh.c msh-hw-2.c README.txt temp
$
Note that no prompts are produced when msh is run from a script.
BELOW IS THE CODE I CURRENTLY HAVE: It can handle all commands, including 'cd' and 'ls', but cannot handle the files. This is where I need help.
#include
int main(int argc, char* argv[])//Taking in user input, reads in commands { char entry[MAX_BIN_SIZE]; char ** entryAry; char** tokens; int rc; char cwd[100]; char * pos; char * aryCMD; char fileChars[100]; int count = 0; while(1) { printf("msh>"); fgets(entry,MAX_BIN_SIZE,stdin); //Getting user input //Comparison of homebrew commands if(entry[0] == 't' && entry[1]=='o'&& entry[2]=='d' && entry[3]=='a' && entry[4]=='y') todayFunc(); else if(entry[0]=='e' && entry[1]=='x' && entry[2]=='i' && entry[3]=='t' && entry[4] == ' ') { exit(0); break; } else if(entry[0]=='h' && entry[1]=='e' && entry[2]=='l' && entry[3]=='p') { printf("Enter linux commands or 'exit' to exit. :) "); } //if no self-made cmds are true then fork to //process linux cmds and return to parent operation after tokens = tokenize(entry); if(strcmp(tokens[0],"cd") == 0 && tokens[1] != NULL)//Checking for cd and home env beyond this point { getcwd(cwd,100); strcat(cwd,"/"); strcat(cwd,tokens[1]); printf("Value of CWD: %s",cwd); if((pos = strchr(cwd,' ')) != NULL) { *pos = '\0'; } chdir(cwd); printf("%s ",cwd); continue; } else if(strcmp(tokens[0],"cd") == 0) { //strcat(cwd,getenv("HOME")); if((pos = strchr(cwd,' ')) != NULL) { *pos = '\0'; } chdir(getenv("HOME")); continue; } else { rc = fork(); if(rc < 0) { fprintf(stderr,"fork failed "); } else if(rc == 0) { tokens = tokenize(entry); if( execvp(tokens[0],tokens) == -1) { fprintf(stderr,"cmd failed "); exit(0); } } else { wait(NULL); } } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
