Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am trying to get the program to run, it is a simple shell and I am unable to debug it so that I

Hello,

I am trying to get the program to run, it is a simple shell and I am unable to debug it so that I don't get a segmentation error. Any help would be greatly appreciated. Thank you!

#include #include #include #include #define LINE_LEN 80 #define MAX_ARGS 64 #define MAX_ARG_LEN 16 #define MAX_LINE_LEN 80 #define MAX_PATHS 64 #define MAX_PATH_LEN 96 #define WHITESPACE " .,\t "

#ifndef NULL #define NULL 0 #endif

struct command_t { char *name; int argc; char *argv[MAX_ARGS]; };

char *lookupPath(char **, char**); int parseCommand(char *, struct command_t *); int parsePath(char **); void printPrompt(); void readCommand(char *);

int main(int argc, char *argv[ ]) { int i; pid_t pid; int status; char cmdLine[MAX_LINE_LEN]; struct command_t command; char pathv[MAX_PATHS]; parsePath(pathv); // get directory paths from PATH while(1) { printPrompt; readCommand(cmdLine); if(strcmp(cmdLine, "exit") == 0) break; parseCommand(cmdLine, &command); command.name = lookupPath(command.argv, pathv); if(command.name == NULL) { printf(stderr, "%s: command unknown", command.name); continue; } if ((pid = fork()) == 0) { // child process execv(command.name, command.argv); exit(0); } else { waitpid(pid, &status, WUNTRACED); } exit(0);// shell termination } }

char *lookupPath(char **argv, char **dir) { char *result; char pName[MAX_PATH_LEN]; if (*argv[0] == '/') { result = argv[0]; return result; } int i; for(i = 0; i < MAX_PATHS; i++) { // search argument to find command char *str = (char *) malloc(strlen(argv[i])); strcpy(str, argv[i]); if ((result = access(str, F_OK)) == 0) return result; } // else file not found printf(stderr, "%s: command not found ", argv[0]); return NULL; }

int parseCommand(char *cLine, struct command_t *cmd) { int argc; char **clPtr; clPtr = &cLine; // cLine is the command line argc = 0; cmd->argv[argc] = (char *) malloc(MAX_ARG_LEN); while ((cmd ->argv[argc] = strsep(clPtr, WHITESPACE)) != NULL) { cmd->argv[++argc] = (char *) malloc(MAX_ARG_LEN); } cmd->argc = argc-1; cmd->name = (char *) malloc(sizeof(cmd->argv[0])); strcpy (cmd->name, cmd->argv[0]); return 1; }

int parsePath(char **pathv) { char *pathEnvVar; char *thePath; int i, a; for (i = 0; i < MAX_ARGS; i++) { pathv[i] = NULL; } pathEnvVar = (char *) getenv("$PATH"); thePath = (char *) malloc(strlen(pathEnvVar) + 1); strcpy(thePath, pathEnvVar); int count = 0; for ( a = 0; a < MAX_PATHS; a++) { int start = count; // loop to parse thePath. Look for ':' delimiter for (i = 0; i < MAX_ARG_LEN; i++) { // count the number of characters until the : delimiter while (thePath[i] != ':' && thePath[i] != '\0') { count++; } } char *str; str = (char *) malloc(count - start + 1); // load the path term into array for (i = start; i < count; i++) { str = strcat(str, thePath[i]); } pathv[a] = str; count++; // so that the next iteration will skip the : character } return 1; }

void printPrompt() { char ch = '>'; printf("%c ", ch); }

void readCommand(char *buffer) { gets(buffer); }

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

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions