Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could someone add to the following C code so that the following is true (code is below): Command Execution - Once the shell understands what

Could someone add to the following C code so that the following is true (code is below):

Command Execution - Once the shell understands what commands to execute it is time to implement the execution of simple commands. Since the execution of another program involves creating another process, you will have to use the fork() system call to create another process. Once you have created the new child process, that process must use the execvp() system call to execute the program. Finally, the parent (shell) process must wait for the child process to complete before releasing the childs resources using the waitpid() system call. However, the execvp() system call may return if there is an error. If it does, your shell should print an error, reset, and prompt for new input. Here is an example: prompt$ lalala -a Error: Command could not be executed prompt$

Built-ins - Not all commands are actually programs, and your shell must implement two built-in commands. In other words, if you encounter any of these two commands, do not execute them using fork(), exec(), and waitpid(). Instead, your shell should call a subroutine that implements the following functionality.:

exit terminates your running shell process and prints 'exit'. prompt$ exit exit (shell exits)

cd [PATH] Changes the present working directory. You will need to use the chdir() system call and update the PWD environmental variable with setenv(). prompt$ pwd /user/name/os/project1 prompt$ cd .. prompt$ pwd /user/name/os prompt$ cd project1 prompt$ pwd /usr/name/os/project1

showpid shows the last 5 child process IDs created by your shell. prompt$ showpid 4987 4992 5001 5002 5004

Better Prompt - Lets make your shell more usable. Modify the prompt so that it displays the current working directory before the $ sign: o /home/xkcd/$. Make the prompt a different color. Optionally, you can make other parts of your shell different colors as well.

CODE:

#include

#include

#include

#include

#include

#define MAX_LINE 80

int main()

{

char str[100];

char *p;

char *args[MAX_LINE/2+1];

int count=0,pid,i;

printf("$ ");

fgets(str, 120, stdin);

while(strcmp(str, "exit ")!=0)

{

if(strcmp(str,"help ") == 0)

{

printf("enter Linux commands, or exit to exit ");

printf("$ ");

}

else

{

//fgets(str, 1000, stdin);

printf("$ ");

count=0;

//fgets(buff,sizeof(buff),stdin);

//eliminate /n in buff

str[strlen(str)-1]='\0';

//printf("%s ",buff);

//alocate memory for 10 array of strings

for(i = 0; i < 10; i++)

{

args[i] = (char*)malloc(20*sizeof(char));

}

//now make a argument list and add to string array

p=strtok(str," ");

strcpy(args[count++],p);

while(p!=NULL)

{

p = strtok(NULL," ");

if(p == NULL)

break;

strcpy(args[count++],p);

}

//after building list of commands , execute command using execvp , for that create child process

args[count]=NULL;

pid = fork();

if(strcmp(str,"exit") == 0)

{

exit(0);

}

if(pid == 0) //child process

{

//execvp(args[0],args);

if(strcmp(str,"help") == 0)

{

exit(0);

}

execvp(args[0],args);

perror("ps error: ");

printf("After execvp failed ");

exit(0);

}

else //parent process

{

//wait for chidl to finish

wait(0);

//printf("Finished executing user command %s ",buff);

if(strcmp(str,"exit") == 0)

{

exit(0);

}

str[0]='\0';

count=0;

*args=NULL;

}

}

fflush(stdin);

fgets(str, 1000, stdin);

}

}

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

Students also viewed these Databases questions

Question

Describe the business culture in Germany.

Answered: 1 week ago

Question

Interpret Du Pont Model for ADvTECH for 2020 and 2019 years

Answered: 1 week ago

Question

undertake an initial analysis of your data;

Answered: 1 week ago

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago