Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help , will upvote I am failing test-1 with this code. Here is the failing error this is my code #include #include #include #include

Please help , will upvote

I am failing test-1 with this code. Here is the failing error

image text in transcribed

this is my code

#include

#include

#include

#include

#include

#define MAX_CMD_LENGTH 1024

#define MAX_ARGS 64

char error_message[30] = "An error has occurred ";

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;

}

//To implement the cd command using chdir() system call in the provided code, we need to modify the execute_cmd function to check if the command entered by the user is cd. If it is, then we change the directory using chdir() system call.Here is the modified execute_cmd function

int execute_cmd(char** args, char** path, int num_paths) {

if (args[0] == NULL) {

return 0; // empty command

}

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

if (args[1] == NULL || args[2] != NULL) {

write(STDERR_FILENO, error_message, strlen(error_message));

return 1; // cd takes one argument

}

if (chdir(args[1]) == -1) {

write(STDERR_FILENO, error_message, strlen(error_message));

return 1; // chdir failed

}

return 0;

}

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

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);

return WEXITSTATUS(status);

}

}

//To implement the cd command using the chdir() system call, we need to check if the first argument is cd, and if so, ensure that only one argument is provided. Then we can use the second argument as the path to the directory we want to change to, and call chdir() with that path

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

// initialize path with /bin

char* path[2] = {"/bin", NULL};

int num_paths = 1;

// check if we're in batch mode

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;

}

}

// main loop

char* line = NULL;

size_t line_size = 0;

ssize_t line_len;

while (1) {

// prompt for input

if (input_file == NULL) {

printf("wish> ");

fflush(stdout);

}

// read input

line_len = getline(&line, &line_size, input_file != NULL ? input_file : stdin);

if (line_len == -1) {

// end of file or error

break;

} else if (line_len == 1) {

// empty line

continue;

} else if (strncmp(line, "exit", 4) == 0) {

// exit command

break;

} else {

// parse input

char* args[MAX_ARGS];

//int num_args = parse_input(line, args); unused variable

// execute command

int ret = execute_cmd(args, path, num_paths);

if (ret != 0) {

printf("Command returned with error code %d ", ret);

}

// check for exit command

if (strcmp(args[0], "exit") == 0) {

break;

}

}

// free memory

free(line);

line = NULL;

line_size = 0;

}

if (input_file != NULL) {

fclose(input_file);

}

return 0;

}

//The main changes are in the while loop. We check for the cd command, and if it's detected, we check if there is only one argument. If so, we call chdir() with that argument. If chdir() fails, we print the error message.

//Note that we use the continue statement after processing the cd command to skip the rest of the loop iteration and prompt for input again.

//Also note that we check for the exit command twice - once before executing the command, and once after. This is because the exit command can be entered at any time, and we want to exit immediately if it's detected, rather than waiting for the next input prompt.

-bash: tests-in/1.rc: No such tile or directory aosiaos-vbox: / project-2/shell\$ diff tests/1.re tests-out/1.rc 1 ci 139 aoseaos-vbox: / project-2/shello

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

Students also viewed these Databases questions