(Solved) : Input Test Cases Need Hard Coded Note Use Sample Program Provided Base Code Implement Part Q36572620 . . .
ALL INPUT/TEST CASES NEED TO BE HARD CODED IN
Note. You should use the sample program (provided below) as yourbase code to implement this part.
So just build off sample program below if thats easier foryou
For C/C++ in a Unix Environment
Design and implement a C/C++ program to handle N commands inpipe. In addition, the shell program should do the following tasksin a loop: (1) to get input command(s) in pipe from the keyboard(stdin), to be processed and (2) to run N commands in pipe.
Warning: Donot use any “system(…)” functioncall throughout this assignment.
Your test case is hard-coded in the program.
For example (and for your test cases)
1. whoami
2. date
3. pwd
4. ls
5. ls /etc| head
6. ls /etc| head | wc
7. ls -l /etc |head | cat | wc
Provide a Makefile file to compile your program.
Your Program Name: a3part4.c
Sample Program. The following sample shell program is in a loop(1) to get a command from stdin (keyboard), (2) to parse it, and(3) to run the command.
/*—————————————————————–*/
/* PROGRAM simple shell.cprogam */
/*—————————————————————–*/
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
void sig_int(int signo) {
printf(“nCaught SIGINT!n”);
}
char *getinput(char *buffer, size_t buflen) {
printf(“myShell> “);
returnfgets(buffer, buflen, stdin);
}
/*—————————————————————–*/
/* FUNCTIONparse: */
/*—————————————————————–*/
void parse(char *line, char **argv)
{
while (*line != ‘ ‘){ /* if not the end of line……. */
while(*line == ‘ ‘ || *line == ‘t’ || *line == ‘n’)
*line++ =’ ‘; /* replace white spaces with0 */
*argv++ =line; /* savethe argument position */
while(*line != ‘ ‘ && *line != ‘ ‘ &&
*line != ‘t’ && *line != ‘n’)
line++; /* skip the argument until … */
}
*argv =’ ‘; /* mark the end of argument list */
}
/*—————————————————————–*/
/* FUNCTIONexecute: */
/*—————————————————————–*/
void execute(char **argv)
{
pid_t pid;
int status;
if ((pid = fork()) < 0){ /* fork a childprocess */
printf(“*** ERROR: forking child process failedn”);
exit(1);
}
else if (pid == 0){ /* for thechild process: */
if(execvp(*argv, argv) < 0) { /* executethe command */
printf(“*** ERROR: exec failedn”);
exit(1);
}
}
else{ /* for the parent: */
while(wait(&status) != pid) /* waitfor completion */
;
}
}
/*—————————————————————–*/
/* The main program startshere */
/*—————————————————————–*/
void main(void)
{
charline[1024]; /* the inputline */
char*argv[64]; /* the command line argument */
size_t linelen;
if(signal(SIGINT, sig_int) == SIG_ERR) {
fprintf(stderr, “signal error: %sn”, strerror(errno));
exit(1);
}
while (1){ /* repeat until done…. */
getinput(line, sizeof(line));
line[strlen(line) – 1] = ‘ ‘;
printf(“Input command is: %s n”, line);
parse(line, argv); /* parse theline */
if(strcmp(argv[0], “exit”) == 0) /* is it an”exit”? */
exit(0); /* exit if itis */
execute(argv); /* otherwise, execute the command */
}
}
Expert Answer
Answer to Input Test Cases Need Hard Coded Note Use Sample Program Provided Base Code Implement Part Q36572620 . . .
OR