Question: shell.c below is a stripped down shell program. It will run a Unix command without any arguments. Copy and save the program as shell.c. Modify
shell.c below is a stripped down shell program. It will run a Unix command without any arguments. Copy
and save the program as shell.c. Modify the program so that it can execute any shell command. It should
be able to take at least 2 arguments. Usually, you need to parse the command line you enter. Using execvp
instead of execlp will probably help. It should work on any UNIX command consisting of 0-2 arguments
after the command entered by the user, and be error free. The following are a few test cases that the user
could enter.
$ cp file1 file2
$ ls -l
$ who
char *strtok(char *str, const char *delim)
is a useful function for parsing the input string. You
can see below how it used " " as a delimiter to get everything before that character. You can specify " " as
a delimiter to split the string into arguments. The first time you use it, give it a string as the first parameter
to tokenize. This will return the first token. You can then use it subsequent times with NULL as the first
parameter and it will return subsequent tokens in order. After the last token was returned, it returns NULL.
For example if you wrote:
char
input [ ] = " Hello World ! " ;
strtok ( input , " " ) ;
strtok (NULL, " " ) ;
strtok (NULL, " " ) ;
...the first call would return "Hello". The second call would return "World!". And the third call would
return NULL.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
