Question
Continue extending our msh.c shell from below. This week we will add only one small feature: redirection. This feature will allow commands to use files
Continue extending our msh.c shell from below. This week we will add only one small feature: redirection. This feature will allow commands to use files for input and output, instead of standard input and standard output. To direct a commands output to a file, the syntax > outfile is used. To read a commands input from a file, the syntax < infile is used.
NOTE: Your extended version of msh should do what last weeks msh could, plus should be able to handle commands like the following:
***** Code should handle commands like here ******
$ ./msh
msh> ls -l > temp.txt
msh> sort < temp.txt > temp-sorted.txt
************************************************************
The result of these commands should be that the sorted output of ls -l is in file temp-sorted.txt.
Your shell builtins (like cd and help) do not have to handle redirection.
Only one new Linux command is needed: dup2. You will use dup2 for both input and output redirection.
The basic idea is that if you see redirection on the command line, you open the file or files, and then use dup2.
Also, you need to handle the ">" and "<" characters that appear on the command line. Think about whether or not you put them into an array of tokens.
------------ Please provide screen shots of redirection working ---------------------
- Add "redirection" code below
******************* msh.c***********************
#include
/* * A simple shell */
#define MAX_BUF 160 #define MAX_TOKS 100
int main(int argc, char *argv[]) { char *pos; char *tok; char *path; char s[MAX_BUF]; char *toks[MAX_TOKS]; time_t rawtime; struct tm *timeinfo; static const char prompt[] = "msh> "; FILE *infile;
/* * process command line options */
if (argc > 2) { fprintf(stderr, "msh: usage: msh [file] "); exit(EXIT_FAILURE); } if (argc == 2) { /* read from script supplied on the command line */ infile = fopen(argv[1], "r"); if (infile == NULL) { fprintf(stderr, "msh: cannot open script '%s'. ", argv[1]); exit(EXIT_FAILURE); } } else { infile = stdin; }
while (1) { // prompt for input, if interactive input if (infile == stdin) { printf(prompt); }
/* * read a line of input and break it into tokens */
// read input char *status = fgets(s, MAX_BUF-1, infile);
// exit if ^d or "exit" entered if (status == NULL || strcmp(s, "exit ") == 0) { if (status == NULL && infile == stdin) { printf(" "); } exit(EXIT_SUCCESS); }
// remove any trailing newline if ((pos = strchr(s, ' ')) != NULL) { *pos = '\0'; }
// break input line into tokens char *rest = s; int num_toks = 0; while ((tok = strtok_r(rest, " ", &rest)) != NULL && num_toks < MAX_TOKS) { toks[num_toks] = tok; num_toks++; } if (num_toks == MAX_TOKS) { fprintf(stderr, "msh: too many tokens"); exit(EXIT_FAILURE); } toks[num_toks] = NULL;
/* * process a command */
// do nothing if no tokens found in input if (num_toks == 0) { continue; }
// if a shell built-in command, then run it if (strcmp(toks[0], "help") == 0) { // help printf("enter a Linux command, or 'exit' to quit "); continue; } if (strcmp(toks[0], "today") == 0) { // today time(&rawtime); timeinfo = localtime(&rawtime); printf("Current local time: %s", asctime(timeinfo)); continue; } if (strcmp(toks[0], "cd") == 0) { // cd if (num_toks == 1) { path = getenv("HOME"); } else { path = toks[1]; } int cd_status = chdir(path); if (cd_status != 0) { printf("msh: %s: %s ", toks[0], strerror(errno)); } continue; }
// not a built-in, so fork a process that will run the command int rc = fork(); if (rc < 0) { fprintf(stderr, "msh: fork failed "); exit(1); } if (rc == 0) { // child process: run the command indicated by toks[0] execvp(toks[0], toks); /* if execvp returns than an error occurred */ printf("msh: %s: %s ", toks[0], strerror(errno)); exit(1); } else { // parent process: wait for child to terminate wait(NULL); } } }
******************************************************************
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started