Question: can you please fix this code, it has some issues and I cant run it #include #include #include #include #include #define MAX_CMD_LENGTH 1024 #define MAX_ARGS

can you please fix this code, it has some issues and I cant run it

#include #include #include #include #include

#define MAX_CMD_LENGTH 1024 #define MAX_ARGS 64 #define MAX_STACK_SIZE 1024 char error_message[30] = "An error has occurred ";

int main(int argc, char** argv) {

FILE* input_file = NULL; if (argc > 2) { write(STDERR_FILENO, error_message, strlen(error_message)); return 1; } else if (argc == 2) { input_file = fopen(argv[1], "r"); if (input_file == NULL) { write(STDERR_FILENO, error_message, strlen(error_message)); return 1; } }

while (1) { // prompt for input if (input_file == NULL) { printf("wish> "); fflush(stdout); } // read input

char *buffer; size_t bufsize = 32; size_t characters;

buffer = (char *)malloc(bufsize * sizeof(char)); if( buffer == NULL) { perror("Unable to allocate buffer"); exit(1); }

characters = getline(&buffer,&bufsize,stdin); printf("%ld characters were read. ",characters); // printf("You typed: '%s' ",buffer); // int init_size = strlen(buffer); char delim[] = " "; char strArray[100][100]; int cnt=0; char *ptr = strtok(buffer, delim);

while(ptr != NULL) { // printf("%s ", ptr); // ptr = strtok(NULL, delim);

strcpy(strArray[cnt], ptr); // printf("%s ", strArray[cnt]); ptr = strtok(NULL, delim); cnt++; }

if (strcmp(strArray[0],"cd")==0){

}

if (strcmp(strArray[0],"exit")>0){ exit(1);

} } return 0; }

int parse_input(char* input, char** args) { int i = 0; char* token = strtok(input, " \t "); while (token != NULL) { args[i++] = token; token = strtok(NULL, " \t "); } args[i] = NULL; return i; }

int stack[MAX_STACK_SIZE]; int top = -1; void push(int value) { if (top == MAX_STACK_SIZE - 1) { printf("Stack overflow "); exit(1); } stack[++top] = value; }

int pop() { if (top == -1) { printf("Stack underflow "); exit(1); } return stack[top--]; } int execute_cmd(char** args, char** path, int num_paths, int stack_size) { if (args[0] == NULL) { return 0; // empty command }

if (strcmp(args[0], "cd") == 0) { // ... cd implementation ... }else if (strcmp(args[0], "push") == 0) { if (args[1] == NULL) { printf("Usage: push "); return 1; } int value = atoi(args[1]); push(value); } else if (strcmp(args[0], "pop") == 0) { if (top == -1) { printf("Stack underflow "); return 1; } int value = pop(); printf("%d ", value); } else {

pid_t pid = fork(); if (pid == -1) { write(STDERR_FILENO, error_message, strlen(error_message)); return 1; } else if (pid == 0) { // child process int i; for (i = 0; i < num_paths; i++) { char cmd[MAX_CMD_LENGTH]; snprintf(cmd, sizeof(cmd), "%s/%s", path[i], args[0]); execv(cmd, args); } // if we reach here, execv failed write(STDERR_FILENO, error_message, strlen(error_message)); exit(1); } else { // parent process int status; waitpid(pid, &status, 0); if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // command executed successfully return 0; } else { // command not found or exited with error write(STDERR_FILENO, error_message, strlen(error_message)); return 1; } } } return 0;

// free memory free(line); line = NULL; line_size = 0; { //if (input_file != NULL) { fclose(input_file); } 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!