Question: SO far Tests Failed: Test running command with & , Test history , Test invalid command, Test UNIX pipe, Test redirection of input , Test

SO far Tests Failed: Test running command with & , Test history , Test invalid command, Test UNIX pipe, Test redirection of input , Test redirection of out >
prog.cpp file
#include
#include
#include
#include
#include
#include
#include
#define MAX_LINE 80
int main(void){
char *args[MAX_LINE/2+1];
int should_run =1;
char history[MAX_LINE];
while (should_run){
printf("osh>");
fflush(stdout);
char input[MAX_LINE];
fgets(input, MAX_LINE, stdin);
// Check for history command
if (strcmp(input,"!!
")==0){
if (strlen(history)==0){
printf("No commands in history
");
continue;
} else {
strcpy(input, history); // Use the last command
printf("%s
", history); // Echo the history command
}
} else {
strcpy(history, input); // Save command to history
}
// Tokenize the input
char *token = strtok(input,"
");
int i =0;
int background =0;
while (token != NULL){
args[i++]= token;
token = strtok(NULL,"
");
}
args[i]= NULL;
if (i >0 && strcmp(args[i -1],"&")==0){
background =1;
args[i -1]= NULL;
}
// Exit command
if (strcmp(args[0], "exit")==0){
should_run =0;
continue;
}
pid_t pid = fork();
if (pid 0){
perror("Fork failed");
exit(1);
} else if (pid ==0){
// Child process
execvp(args[0], args);
perror("exec failed");
exit(1);
} else {
// Parent process
if (!background){
wait(NULL);
}
}
}
return 0;
}
Include commands got deleted so i included photo
SO far Tests Failed: Test running command with &

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 Programming Questions!