Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this C function. We are creating a custom shell, most of it is done I just need to add one more

Please help me with this C function. We are creating a custom shell, most of it is done I just need to add one more function. We are modifiying the existing C code given below but leave the existing functions there. We are adding a function for input and output redirection for files using 'dup2'

We are compiling it using gcc. The code for msh.c is given below after the instructions. Please post screenshots of your code and output since it is easier to see the format that way. Thank You! I asked this question before, but got completely wrong answers that were handwritten. Please post the correct solution.

Instructions/Requirements:image text in transcribed

The code I have so far:

#include #include #include #include #include #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  

3. msh. We will continue extending our 'msh' shell. 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 command's output to a file, the syntax ">outfile" is used. To read a command's input from a file, the syntax " infile" is used. Your extended version of msh should do what last week's msh could, plus should be able to handle commands like the following $ ./msh msh ls -1 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. It is only required to handle redirection commands after other commands. Shell builtins (like 'cd' and 'help') do not have to handle redirection. We will discuss how to implement redirection in class. Only one new Linux command is needed: dup2. The basic idea is that if you see redirection on the command line, you open the file or files, and then use dup2

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 Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions