Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Start with program shellex.c from figures 8 . 2 2 - 8 . 2 4 and create your own shell to 1 . Maintain variables
Start with program shellex.c from figures and create your own shell to
Maintain variables using a syntax set abcxyz
Report all variables by using the command set with not arguments.
Use the value of variable PATH as the path of one additional directory where executable
files may be found.
Maintain information on all processes running in the background.
Your new version of shellex.c must maintain variables using a local array of strings abcxyz It should not rely on the real shell to keep these strings. Which means that at the top of shellex.c there must be a declaration like char vars to have an array that will contain up to for example strings in the form abcxyz When the user types a command like set pathhomeckprogs 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 arg arg 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 homeckprogstest is executed.
$begin shellmain
#include "csapp.h
#define MAXARGS
function prototypes
void evalcharcmdline;
int parselinechar buf char argv;
int builtincommandchar argv;
int main
char cmdlineMAXLINE; command line
while
read
printf;
Fgetscmdline MAXLINE, stdin;
if feofstdin
exit;
evaluate
evalcmdline;
$end shellmain
$begin eval
eval evaluate a command line
void evalchar cmdline
char argvMAXARGS; argv for execve
char bufMAXLINE; holds modified command line
int bg; should the job run in bg or fg
pidt pid; process id
strcpybuf cmdline;
bg parselinebuf argv;
if argv NULL
return; ignore empty lines
if builtincommandargv
if pid Fork child runs user job
if execveargv argv, environ
printfs: Command not found.
argv;
exit;
parent waits for foreground job to terminate
if bg
int status;
if waitpidpid &status,
unixerrorwaitfg: waitpid error";
else
printfd s pid, cmdline;
return;
if first arg is a builtin command, run it and return true
int builtincommandchar argv
if strcmpargv "quit" quit command
exit;
if strcmpargv& ignore singleton &
return ;
return ; not a builtin command
$end eval
$begin parseline
parseline parse the command line and build the argv array
int parselinechar buf char argv
char delim; points to first space delimiter
int argc; number of args
int bg; background job?
bufstrlenbuf; replace trailing
with space
while buf && buf ignore leading spaces
buf;
build the argv list
argc ;
while delim strchrbuf
argvargc buf;
delim ;
buf delim ;
while buf && buf ignore spaces
buf;
argvargc NULL;
if argc ignore blank line
return ;
should the job run in the background?
if bg argvargc&
argvargc NULL;
return bg;
$end parseline
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