Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include csapp.h #define MAXARGS 1 2 8 void eval ( char * cmdline ) ; int parseline ( char * buf , char *

#include "csapp.h"#define MAXARGS128void eval(char*cmdline);int parseline(char *buf, char **argv);int builtin_command(char **argv);int main(){ char cmdline[MAXLINE]; while (1){ printf(">"); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0); eval(cmdline);}}void eval(char *cmdline){ char *argv[MAXARGS]; char buf[MAXLINE];
int bg; pid_t pid; strcpy(buf, cmdline); bg = parseline(buf, argv); if (argv[0]== NULL) return; if (!builtin_command(argv)){ if ((pid = Fork())==0){ if (execve(argv[0], argv, environ)<0){ printf("%s: Command not found.
", argv[0]); exit(0);}} if (!bg){ int status; if (waitpid(pid, &status, 0)<0) unix_error("waitfg: waitpid error");} else printf("%d %s", pid, cmdline);} return;}int builtin_command(char **argv){ if (!strcmp(argv[0], "quit")) exit(0); if (!strcmp(argv[0],"&")) return 1; return 0;}int parseline(char *buf, char **argv){ char *delim; int argc; int bg; buf[strlen(buf)-1]=''; while (*buf && (*buf =='')) buf++; argc =0; while ((delim = strchr(buf,''))){ argv[argc++]= buf;*delim ='\0'; buf = delim +1; while (*buf && (*buf =='')) buf++;} argv[argc]= NULL; if (argc ==0) return 1; if ((bg =(*argv[argc-1]=='&'))!=0) argv[--argc]= NULL; return bg;}
edit this code so that Your new version must maintain variables using a local array of strings abc=xyz. It should not rely on the real shell to keep these strings. Which means that at the top of it there must be a declaration like char *vars[10] to have an array that will contain up to (for example)10 strings in the form abc=xyz. When the user types a command like set path=/home/ck47/progs your shell should find an empty element in local array vars[], call malloc() and store the string there. When the user types a command to run a program from the disk, say > test arg1 arg2 your shell should use execve() to find the program in the current directory. If the program in not found, execve() returns error and your shell should insert the PATH value before the filename (by creating a new string and concatenating) and call execve() with that new string. So, in the above example, either program test is executed or program /home/ck47/progs/test is executed.

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions