Question: shell.c below is a stripped down shell program. It will run a Unix command without any arguments. Modify the program so it will run a
shell.c below is a stripped down shell program. It will run a Unix command without any arguments. Modify the program so it will run a Unix command with any number of arguments.
#include
// ex "cp file1 file2" // ex "ls -a -l" // ex "ls -a -l -o ...etc.."
int main() // main (int argc, char* argv[]) { int PID; char lineGot[256]; char *cmd; // stores command from user while (1){ // infinite loop until user inputs e printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes ) // enter string "cp file1 file2" --> lineGot="cp file1 file2 " // char c = ' ' (change line)
cmd = strtok(lineGot, " "); // Get the string without the // (lineGot, ' ') is wrong
if( strcmp(cmd, "e") == 0 ) // loop terminates when "e" is typed exit (0); // creates a new process. Parent gets the child's process ID. Child gets 0. if ( (PID=fork()) > 0) { wait(NULL); } else if (PID == 0) /* child process */ { execlp (cmd, cmd, NULL); /* exec cannot return. If so do the following */ fprintf (stderr, "Cannot execute %s ", cmd); exit(1); /* exec failed */ } else if ( PID == -1) { fprintf (stderr, "Cannot create a new process "); exit (2); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
