Question: fix my code #include #include #include #include #include #include #define MAX _ LINE 8 0 / / Maximum length of command #define HISTORY _ SIZE
fix my code #include
#include
#include
#include
#include
#include
#define MAXLINE Maximum length of command
#define HISTORYSIZE Size of history
Function prototypes
void parsecommandchar input char args int background int inputredir, int outputredir, int pipeflag, char inputfile, char outputfile, char pipeargs;
void executecommandchar args int background, int inputredir, int outputredir, char inputfile, char outputfile, int pipeflag, char pipeargs;
void handlehistorychar input char history int historyindex;
void printhistorychar history int historyindex;
void handlepipechar args char args;
int mainvoid
char argsMAXLINE ; Command line arguments
char pipeargsMAXLINE ; Arguments for the second command in a pipe
char historyHISTORYSIZE; Command history
int historyindex ; Current index in history
int shouldrun ; Flag to determine when to exit shell
char inputMAXLINE; User input
int background, inputredir, outputredir, pipeflag; Flags for & inputoutput redirection, and pipe
char inputfile NULL, outputfile NULL; File names for redirection
Initialize history
for int i ; i HISTORYSIZE; i
historyi NULL;
while shouldrun
printfosh;
fflushstdout;
Read the user input
if fgetsinput MAXLINE, stdin
perrorfgets;
exit;
inputstrleninput; Remove newline character
Handle "exit" command
if strcmpinput "exit"
shouldrun ;
continue;
Handle history and builtin commands
if strcmpinput "history"
printhistoryhistory historyindex;
continue;
Handle for repeating the last command
handlehistoryinput history, &historyindex;
Parse the command into args
parsecommandinput args, &background, &inputredir, &outputredir, &pipeflag, &inputfile, &outputfile, pipeargs;
Execute the command
executecommandargs background, inputredir, outputredir, inputfile, outputfile, pipeflag, pipeargs;
Free the history buffer
for int i ; i HISTORYSIZE; i
if historyi freehistoryi;
return ;
Function to parse the input into command and its arguments
void parsecommandchar input char args int background int inputredir, int outputredir, int pipeflag, char inputfile, char outputfile, char pipeargs
background ;
inputredir ;
outputredir ;
pipeflag ;
char token strtokinput;
int i ;
while token NULL
if strcmptoken&
background ;
else if strcmptoken
inputredir ;
token strtokNULL;
inputfile token;
else if strcmptoken
outputredir ;
token strtokNULL;
outputfile token;
else if strcmptoken
pipeflag ;
argsi NULL; Null terminate the first command's args
i ;
token strtokNULL;
while token NULL
pipeargsi token;
token strtokNULL;
pipeargsi NULL; Null terminate the second command's args
break;
else
argsi token;
token strtokNULL;
argsi NULL; Null terminate the args array
Function to handle pipes
void handlepipechar args char args
int fd;
if pipefd
perrorpipe;
exit;
pidt pid fork;
if pid
perrorfork;
exit;
if pid
First child: send output to pipe
closefd; Close unused read end
dupfd STDOUTFILENO; Redirect stdout to pipe
closefd; Close write end after redirecting
execvpargs args;
perrorexecvp; Error handling
exit;
pidt pid fork;
if pid
perrorfork;
exit;
if pid
Second child: receive input from pipe
closefd; Close unused write end
dupfd STDINFILENO; Redirect stdin
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
