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 MAX_LINE 80// Maximum length of command
#define HISTORY_SIZE 10// Size of history
// Function prototypes
void parse_command(char *input, char **args, int *background, int *input_redir, int *output_redir, int *pipe_flag, char **input_file, char **output_file, char **pipe_args);
void execute_command(char **args, int background, int input_redir, int output_redir, char *input_file, char *output_file, int pipe_flag, char **pipe_args);
void handle_history(char *input, char **history, int *history_index);
void print_history(char **history, int history_index);
void handle_pipe(char **args1, char **args2);
int main(void){
char *args[MAX_LINE /2+1]; // Command line arguments
char *pipe_args[MAX_LINE /2+1]; // Arguments for the second command in a pipe
char *history[HISTORY_SIZE]; // Command history
int history_index =0; // Current index in history
int should_run =1; // Flag to determine when to exit shell
char input[MAX_LINE]; // User input
int background, input_redir, output_redir, pipe_flag; // Flags for '&', input/output redirection, and pipe
char *input_file = NULL, *output_file = NULL; // File names for redirection
// Initialize history
for (int i =0; i < HISTORY_SIZE; i++){
history[i]= NULL;
}
while (should_run){
printf("osh>");
fflush(stdout);
// Read the user input
if (!fgets(input, MAX_LINE, stdin)){
perror("fgets");
exit(1);
}
input[strlen(input)-1]='\0'; // Remove newline character
// Handle "exit" command
if (strcmp(input, "exit")==0){
should_run =0;
continue;
}
// Handle history and built-in commands
if (strcmp(input, "history")==0){
print_history(history, history_index);
continue;
}
// Handle '!!' for repeating the last command
handle_history(input, history, &history_index);
// Parse the command into args[]
parse_command(input, args, &background, &input_redir, &output_redir, &pipe_flag, &input_file, &output_file, pipe_args);
// Execute the command
execute_command(args, background, input_redir, output_redir, input_file, output_file, pipe_flag, pipe_args);
}
// Free the history buffer
for (int i =0; i < HISTORY_SIZE; i++){
if (history[i]) free(history[i]);
}
return 0;
}
// Function to parse the input into command and its arguments
void parse_command(char *input, char **args, int *background, int *input_redir, int *output_redir, int *pipe_flag, char **input_file, char **output_file, char ***pipe_args){
*background =0;
*input_redir =0;
*output_redir =0;
*pipe_flag =0;
char *token = strtok(input,"");
int i =0;
while (token != NULL){
if (strcmp(token,"&")==0){
*background =1;
} else if (strcmp(token,"<")==0){
*input_redir =1;
token = strtok(NULL,"");
*input_file = token;
} else if (strcmp(token,">")==0){
*output_redir =1;
token = strtok(NULL,"");
*output_file = token;
} else if (strcmp(token,"|")==0){
*pipe_flag =1;
args[i]= NULL; // Null terminate the first command's args
i =0;
token = strtok(NULL,"");
while (token != NULL){
(*pipe_args)[i++]= token;
token = strtok(NULL,"");
}
(*pipe_args)[i]= NULL; // Null terminate the second command's args
break;
} else {
args[i++]= token;
}
token = strtok(NULL,"");
}
args[i]= NULL; // Null terminate the args array
}
// Function to handle pipes
void handle_pipe(char **args1, char **args2){
int fd[2];
if (pipe(fd)==-1){
perror("pipe");
exit(1);
}
pid_t pid1= fork();
if (pid1<0){
perror("fork");
exit(1);
}
if (pid1==0){
// First child: send output to pipe
close(fd[0]); // Close unused read end
dup2(fd[1], STDOUT_FILENO); // Redirect stdout to pipe
close(fd[1]); // Close write end after redirecting
execvp(args1[0], args1);
perror("execvp"); // Error handling
exit(1);
}
pid_t pid2= fork();
if (pid2<0){
perror("fork");
exit(1);
}
if (pid2==0){
// Second child: receive input from pipe
close(fd[1]); // Close unused write end
dup2(fd[0], STDIN_FILENO); // Redirect stdin

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!