Question: this code does not have pipes. please add pips beased on the following instructions. Before your shell forks a new process to call execvp (

this code does not have pipes. please add pips beased on the following instructions. Before your shell forks a new process to call execvp(), it should parse the input string and separate it into a collection of substrings representing the executable file and any command-line arguments. If the user entered an empty line, report an error and fetch a new line of input. Your code must handle at least four command-line arguments (in addition to the name of the executable file itself) for each command.
You should store pointers to the substrings in an array (similar to the argv array passed to main()) and pass this array of arguments to execvp(). Note that the number of command-line arguments is variable; this is indicated in the array by including a NULL pointer in the array after the last substring. (This means that if the user specifies N substrings, your array must hold N +1 pointers where the last pointer is NULL.) If the user enters the exit command, your shell should terminate (returning to the regular shell).
Note: your shell does not need to support cd (change directory).
Piping - You shell must also support piping. An example of using pipes in the Linux shell would be cat myfile.txt | wc -l which would copy the file myfile.txt to standard output which is redirected as the standard input to wc (wordcount) which will then display how many lines were in the file myfile.txt. The pipe character | seperates different commands and the output (stdout) of the program on the left becomes the input (stdin) for the program on the right. There is no limit to the number of commands that can be piped together. (Just limited by the command line length specified above).
Hint: get your shell working without pipes first, then go back and add pipes. While this may require a little more work to change your code to support pipes, it ensures you do the rest of the shell correctly so you are not trying to debug multiple things at once. Some functions you will need include pipe and dup2.
Your program must also accept a command line argument which is the prefix prompt. If no value is specified use > as the prompt. #include
#include
#include
#include
#include
#include
#define MAX_INPUT_SIZE 177
#define MAX_ARGS 5
void execute_command(char *args[]){
pid_t pid;
int status;
pid = fork();
if (pid ==0){
// Child process
if (execvp(args[0], args)==-1){
perror("Error executing command");
exit(EXIT_FAILURE);
}
} else if (pid <0){
perror("Error forking process");
} else {
// Parent process
waitpid(pid, &status, 0);
printf("Child PID: %d, Return result: %d
", pid, WEXITSTATUS(status));
}
}
int main(int argc, char *argv[]){
char input[MAX_INPUT_SIZE];
char *args[MAX_ARGS];
char *token;
const char delim[]="";
if (argc >1){
printf("%s ", argv[1]);
} else {
printf(">");
}
while (fgets(input, sizeof(input), stdin)!= NULL){
if (input[0]=='
'){
fprintf(stderr, "Error: Empty command
");
if (argc >1){
printf("%s ", argv[1]);
} else {
printf(">");
}
continue;
}
input[strcspn(input,"
")]=0; // Remove newline character
int i =0;
token = strtok(input, delim);
while (token != NULL && i < MAX_ARGS -1){
args[i]= token;
token = strtok(NULL, delim);
i++;
}
args[i]= NULL; // Set the last element of the array to NULL
if (strcmp(args[0], "exit")==0){
break;
}
execute_command(args);
if (argc >1){
printf("%s ", argv[1]);
} else {
printf(">");
}
}
return 0;
}

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!