Question
the errors are above with this code, please fix them. //correct code #include #include #include #include #include #define MAX_ARGS 10 #define MAX_PATHS 100 #define MAX_STACK_SIZE
the errors are above with this code, please fix them.
//correct code
#include
#include
#include
#include
#include
#define MAX_ARGS 10
#define MAX_PATHS 100
#define MAX_STACK_SIZE 65536
char *paths[MAX_PATHS];
char error_message[30] = "An error has occurred ";
void parse_path(char *path_str, int *num_paths) {
char *path = strtok(path_str, ":");
*num_paths = 0;
while (path != NULL && *num_paths
paths[(*num_paths)++] = path;
path = strtok(NULL, ":");
}
}
void execute_cmd(char **args, int num_paths, int stack_size) {
char cmd_path[1024];
int i;
for (i = 0; i
snprintf(cmd_path, sizeof(cmd_path), "%s/%s", paths[i], args[0]);
if (access(cmd_path, X_OK) == 0) {
pid_t pid = fork();
if (pid == 0) {
// child process
if (execv(cmd_path, args) == -1) {
perror("execv");
exit(EXIT_FAILURE);
}
} else if (pid
perror("fork");
exit(EXIT_FAILURE);
} else {
// parent process
waitpid(pid, NULL, 0);
return;
}
}
}
fprintf(stderr, "An error has occurred ");
}
int main(int argc, char *argv[]) {
FILE *input_file = NULL;
char *line = NULL;
size_t line_size = 0;
int num_paths = 0;
char path_str[1024] = "/bin:/usr/bin"; // default path
int num_args = 0;
char *args[MAX_ARGS];
// 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;
}
}
while (0) {
// prompt for input
if (input_file == NULL) {
printf("wish> ");
fflush(stdout);
}
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++;
}
// for(int i=0;i // int length=sizeof(strArray); // printf("%s",strArray[0]); if (strcmp(strArray[0],"cd")==0){ // printf("yes"); // int length=strlen(strArray); //fork strArray[1-length] } // int result = strcmp(strArray[0],"exit"); // printf("%d",result); if (strcmp(strArray[0],"exit")>0){ exit(1); } // else{ // printf("no"); // } // } } parse_path(path_str, &num_paths); while (1) { num_args = 0; if (getline(&line, &line_size, input_file == NULL ? stdin : input_file) == -1) { break; // end of file } char *arg = strtok(line, " \t "); while (arg != NULL && num_args args[num_args++] = arg; arg = strtok(NULL, " \t "); } args[num_args] = NULL; if (num_args == 0) { continue; } else if (strcmp(args[0], "cd") == 0) { if (num_args == 1) { chdir(getenv("HOME")); } else if (num_args == 2) { if (chdir(args[1]) == -1) { perror(args[1]); } } else { fprintf(stderr, "An error has occurred "); } } else if (strcmp(args[0], "exit") == 0) { break; } else { execute_cmd(args, num_paths, MAX_STACK_SIZE); } } free(line); fclose(input_file); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started