Question: Update the shell program (shell.c) so that it handles I/O redirection and pipes similar to the way the shell (bash) of your Linux account handles

Update the shell program (shell.c) so that it handles I/O redirection and pipes similar to the way the shell (bash) of your Linux account handles as following. (1) The I/O redirection commands you must handle are: filename sends standard output to the given file To make parsing easier, you can assume that there is no space between the file name and the characters < and >. You can also assume that I/O redirection appears after the command and all options for the command.

For example, the following command lines are legal: ls -l -F -a >list ps aux >plist You don't have to worry about command lines like: ls -l >list -F -a ps >plist aux cat >list1 >list (2) The command with one pipe you must handle is like: cmd1 | cmd2 which executes the command 'cmd1', whose output is taken as the input to the command 'cmd2', whose output is in turn taken as the input to the command 'cmd3', which produces output to the screen. For example, the following command lines are legal: ls -l -F -a |cat cat proj13.c | grep printf (3) Bonus : Your project works as bash with both I/O redirections and any number of pipes. The combination of (1) and (2) such as the command cat plist You can assume that input redirections can only happen before the first '|', and output redirections can only happen after the last '|'. As in (1), you can also assume that the I/O redirections always appear after the options of the commands.

Shell.c:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define BUFFER_LENGTH 1024

using namespace std;

int main(){

//set the variables

char line[BUFFER_LENGTH];

char* argv[100];

char* path= "/bin/";

char execmd[20];

int arguments;

while(1){

//begin the shell command line

printf("My shell>> ");

if(!fgets(line, BUFFER_LENGTH, stdin)){

size_t length = strlen(line);

if (line[length - 1] == ' ')

line[length - 1] = '\0';

}

if(strcmp(line, "exit")==0){

break;

}

char *token;

token = strtok(line," ");

int i=0;

while(token!=NULL){

argv[i]=token;

token = strtok(NULL," ");

i++;

}

argv[i]=NULL;

arguments=i;

for(i=0; i

printf("%s ", argv[i]);

}

strcpy(execmd, path);

strcat(execmd, argv[0]);

for(i=0; i

if(execmd[i]==' '){

execmd[i]='\0';

}

}

int pid = fork();

if(pid==0){

execvp(execmd,argv);

fprintf(stderr, "Child process could not do execvp ");

//exit the child

}else{

wait(NULL);

printf("Child exited ");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!