Answered step by step
Verified Expert Solution
Link Copied!

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 8.22-8.24 and create your own shell to
1. Maintain variables using a syntax set abc=xyz
2. Report all variables by using the command set with not arguments.
3. Use the value of variable PATH as the path of one additional directory where executable
files may be found.
4. Maintain information on all processes running in the background.
Your new version of shellex.c 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 shellex.c 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.
/* $begin shellmain */
#include "csapp.h"
#define MAXARGS 128
/* function prototypes */
void eval(char*cmdline);
int parseline(char *buf, char **argv);
int builtin_command(char **argv);
int main()
{
char cmdline[MAXLINE]; /* command line */
while (1){
/* read */
printf(">");
Fgets(cmdline, MAXLINE, stdin);
if (feof(stdin))
exit(0);
/* evaluate */
eval(cmdline);
}
}
/* $end shellmain */
/* $begin eval */
/* eval - evaluate a command line */
void eval(char *cmdline)
{
char *argv[MAXARGS]; /* argv for 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)){
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){
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)
{
if (!strcmp(argv[0], "quit"))/* quit command */
exit(0);
if (!strcmp(argv[0],"&"))/* ignore singleton & */
return 1;
return 0; /* not a builtin command */
}
/* $end eval */
/* $begin parseline */
/* parseline - parse the command line and build the argv array */
int parseline(char *buf, char **argv)
{
char *delim; /* points to first space delimiter */
int argc; /* number of args */
int bg; /* background job? */
buf[strlen(buf)-1]=''; /* replace trailing '
' with space */
while (*buf && (*buf ==''))/* ignore leading spaces */
buf++;
/* build the argv list */
argc =0;
while ((delim = strchr(buf,''))){
argv[argc++]= buf;
*delim ='\0';
buf = delim +1;
while (*buf && (*buf ==''))/* ignore spaces */
buf++;
}
argv[argc]= NULL;
if (argc ==0)/* ignore blank line */
return 1;
/* should the job run in the background? */
if ((bg =(*argv[argc-1]=='&'))!=0)
argv[--argc]= NULL;
return bg;
}
/* $end parseline */

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_2

Step: 3

blur-text-image_3

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

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions