Please help this is to be coded using either C or C++ only I need a place to sta
ID: 642359 • Letter: P
Question
Please help this is to be coded using either C or C++ only I need a place to start and cannot even fathom how to begin this project if someone could just help me get on the right track I would appreciate it immensely!
This assignment is designed to help you become familiar with systems calls related to processes, i.e., fork(), exec(), and wait(). In this assignment, you are going to implement a very simple shell (command-line interpreter). Your shell will read commands from Standard Input and execute the commands. Basically what you have to implement are as follows:
1. First of all, your program must display the prompt before your command line.
2. Your program must read the command and arguments from the standard input. Assume only one command and its arguments.
3. The command
Explanation / Answer
#include <stdio.h>
#include <sys/types.h>
void parse(char *line, char **argv)
{
while (*line != '') { /* if not the end of line ....... */
while (*line == ' ' || *line == ' ' || *line == ' ')
*line++ = ''; /* replace white spaces with 0 */
*argv++ = line; /* save the argument position */
while (*line != '' && *line != ' ' &&
*line != ' ' && *line != ' ')
line++; /* skip the argument until ... */
}
*argv = ''; /* mark the end of argument list */
}
void execute(char **argv)
{
pid_t pid;
int status;
if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failed ");
exit(1);
}
else if (pid == 0) { /* for the child process: */
if (execvp(*argv, argv) < 0) { /* execute the command */
printf("*** ERROR: exec failed ");
exit(1);
}
}
else { /* for the parent: */
while (wait(&status) != pid) /* wait for completion */
;
}
}
void main(void)
{
char line[1024]; /* the input line */
char *argv[64]; /* the command line argument */
while (1) { /* repeat until done .... */
printf("Shell -> "); /* display a prompt */
gets(line); /* read in the command line */
printf(" ");
parse(line, argv); /* parse the line */
if (strcmp(argv[0], "exit") == 0) /* is it an "exit"? */
exit(0); /* exit if it is */
execute(argv); /* otherwise, execute the command */
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.