Question: #include csapp.h #include #define MAXARGS 128 /* Function prototypes */ void eval(char *cmdline); int parseline(char *buf, char **argv); int builtin_command(char **argv); int main() { char
#include "csapp.h" #include
/* Function prototypes */ void eval(char *cmdline); int parseline(char *buf, char **argv); int builtin_command(char **argv);
int main() { char cmdline[MAXLINE]; /* Command line */ printf("Welcome to my Shell. "); while (1) { /* Read */ printf("newsh$ "); Fgets(cmdline, MAXLINE, stdin);
// Evaluate eval(cmdline); if (feof(stdin)) exit(0); else system(cmdline); } } /* $end shellmain */
/* $begin eval */ /* eval - Evaluate a command line */ void eval(char *cmdline) {
char *argv[MAXARGS]; /* Argument list execve() */ char buf[MAXLINE]; /* Holds modified command line */ int bg; /* Should the job run in bg or fg? */ pid_t pid; /* Process id */
strcpy(buf, cmdline); bg = parseline(buf, argv); if (argv[0] == NULL) return; /* Ignore empty lines */ if(builtin_command(argv)) { builtin_command(argv);
}
else if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* Child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found. ", argv[0]); exit(0); } }
/* Parent waits for foreground job to terminate */ if (!bg) { printf("%d %s", pid, cmdline); int status; if (waitpid(pid, &status, 0) < 0) unix_error("waitfg: waitpid error"); } else printf("%d %s", pid, cmdline); } return; }
/* If first arg is a builtin command, run it and return true */ int builtin_command(char **argv) { char cwd[256]; if (!strcmp(argv[0], "quit")) /* quit command */ exit(0); if (!strcmp(argv[0], "&")) /* Ignore singleton & */ return 1; if(strcmp(argv[0], "cd")) { if(getcwd(cwd, sizeof(cwd)) != NULL) chdir(cwd); return 1; } //if(strcmp(argv[0], "bp")) {
return 0; /* Not a builtin command */ } /* $end eval */
/* $begin parseline */ /* parseline - Parse the command line and build the argv array */
I am trying to implement my own shell but I am having trouble implementing the cd command. I have included some of my code. What do I need to use to get cd to work?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
