Hello, I am trying to get the program to run, it is a simple shell and I am unab
ID: 3867735 • Letter: H
Question
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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 " ., "
#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] != '') {
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);
}
Explanation / Answer
Hi,
Please check below code , Please comment if you need more information
Programe :-
----------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.