(Solved) : Input Test Cases Need Hard Coded Note Use Sample Program Provided Base Code Implement Part Q36572574 . . .
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.
For C/C++ in a Unix Environment
A Simple Shell to run One Command with File Redirectionwhere we use “{” for input, “}” for output, “}}” forappend.
Design and implement a simple shell program to handle “onecommand” with file redirection. Here we use the followingfile-redirection symbols as follow: “{” for input, “}” for output,”}}” for append. For example, “ls } out.txt” will output the resultof the command to a file named out.txt.
For example (and for your test cases)
1. whoami
2. date
3. pwd
4. ls
5. ls }p1out1.txt
6. wc {p1out1.txt
7. cat {p1out1.txt
8. date }p1out2.txt
9. date }}p1out2.txt
10. cat {p1out2.txt
11. wc { p1out1.txt }p1out4.txt
12. cat { p1out4.txt
Provide a Makefile file to compile your program.
Your Program Name: a3part1.c
Sample program for your base code.
/* World’s simplest shell – shell0.c */
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.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 *linefer, size_t linelen) {
printf(“myShell> “);
returnfgets(linefer, linelen, stdin);
}
int main(int argc, char **argv) {
charline[1024];
pid_t pid;
int status;
if(signal(SIGINT, sig_int) == SIG_ERR) {
fprintf(stderr, “signal error: %sn”, strerror(errno));
exit(1);
}
while(getinput(line, sizeof(line))) {
line[strlen(line) – 1] = ‘ ‘;
printf(” Input command is: %s n”, line);
if (strcmp(line, “exit”) == 0) /* is it an “exit”? */
exit(0);
if((pid=fork()) == -1) {
fprintf(stderr, “shell: can’t fork: %sn”,
strerror(errno));
continue;
} else if (pid == 0) {
/* child */
execlp(line, line, (char *)0);
fprintf(stderr, “shell: couldn’t exec %s: %sn”,
line, strerror(errno));
exit(EX_DATAERR);
}
if ((pid=waitpid(pid, &status, 0)) < 0)
fprintf(stderr, “shell: waitpid error: %sn”,
strerror(errno));
}
exit(EX_OK);
}
Sample code for your base code for file-redirection – { forinput, } for output, and }} for append.
void redirect(char* redirection, char* file){
int fdout;
//checks ifread, write, append for stdin/stdout
if(!strcmp(redirection, “{“)){
if ((fdout = open(file, O_CREAT|O_RDONLY)) < 0) {
perror(file); /* open failed */
}
dup2(fdout,0);
close(fdout);
}
elseif(!strcmp(redirection, “}”)){
if ((fdout = open(file, O_CREAT|O_WRONLY)) < 0) {
perror(file); /* open failed */
}
dup2(fdout,1);
close(fdout);
}
elseif(!strcmp(redirection, “}}”)){
if ((fdout = open(file, O_CREAT|O_APPEND|O_WRONLY)) < 0) {
perror(file); /* open failed */
}
dup2(fdout,1);
close(fdout);
}
}
Expert Answer
Answer to Input Test Cases Need Hard Coded Note Use Sample Program Provided Base Code Implement Part Q36572574 . . .
OR