Question: Modify that minishell.c to do the following: 1 . put commands ended by an & into background mode and report when they finish. 2 .

Modify that minishell.c to do the following:
1. put commands ended by an & into background mode and report when they finish.
2. properly interpret the shell cd command
3. include an appropriate perror statement after each and every system call.
4. fix the minishell so that if the exec system call fails, the child process is correctly terminated.
#include
#include
#include
#include
#include
#include
#include
#define NV 20/* max number of command tokens */
#define NL 100/* input buffer size */
char line[NL]; /* command input buffer */
/*
shell prompt
*/
prompt(void)
{
fprintf(stdout,"
msh>");
fflush(stdout);
}
main(int argk, char *argv[], char *envp[])
/* argk - number of arguments */
/* argv - argument vector from command line */
/* envp - environment pointer */
{
int frkRtnVal; /* value returned by fork sys call */
int wpid; /* value returned by wait */
char *v[NV]; /* array of pointers to command line tokens */
char *sep ="\t
";/* command line token separators */
int i; /* parse index */
/* prompt for and process one command line at a time */
while (1){/* do Forever */
prompt();
fgets(line, NL, stdin);
fflush(stdin);
if (feof(stdin)){/* non-zero on EOF */
fprintf(stderr, "EOF pid %d feof %d ferror %d
", getpid(),
feof(stdin), ferror(stdin));
exit(0);
}
if (line[0]=='#'|| line[0]=='
'|| line[0]=='\000')
continue; /* to prompt */
v[0]= strtok(line, sep);
for (i =1; i < NV; i++){
v[i]= strtok(NULL, sep);
if (v[i]== NULL)
break;
}
/* assert i is number of tokens +1*/
/* fork a child process to exec the command in v[0]*/
switch (frkRtnVal = fork()){
case -1: /* fork returns error to parent process */
{
break;
}
case 0: /* code executed only by child process */
{
execvp(v[0], v);
}
default: /* code executed only by parent process */
{
wpid = wait(0);
printf("%s done
", v[0]);
break;
}
}/* switch */
}/* while */
}/* main */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!