Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

$ ./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.

- Add "redirection" code below

******************* msh.c***********************

#include #include #include #include #include #include #include

#define MAX_BUF 1000 #define MAX_TOKS 100

int main(int argc, char* argv[]) { char *pos; char *tok; char s[MAX_BUF]; char *tokens[MAX_TOKS]; time_t rawt; struct tm *timeinfo; FILE* fileptr; if(argc > 1){ fileptr = fopen(argv[1], "r"); } while (1) { if(argc == 1){ printf("msh> "); } char *input; if(argc == 1){ input = fgets(s, 1000, stdin); }else{ input = fgets(s, 1000, fileptr); } if (input == NULL || strcmp(s, "exit ") == 0) { if (input == NULL) { if(argc == 1) printf(" "); } exit(0); } if ((pos = strchr(s, ' ')) != NULL) { *pos = '\0'; } char *remain = s; int i = 0; while ((tok = strtok_r(remain, " ", &remain)) != NULL && i < MAX_TOKS) { tokens[i] = tok; i++; } tokens[i] = NULL; if (i == 0) { continue; } if (strcmp(tokens[0], "help") == 0) { printf("enter a Linux command, or 'exit' to quit "); continue; } if (strcmp(tokens[0], "today") == 0) { time(&rawt); timeinfo = localtime(&rawt); printf("%02d/%02d/%4d ", 1 + timeinfo->tm_mon, timeinfo->tm_mday, 1900 + timeinfo->tm_year); continue; }else if(strcmp(tokens[0], "cd") == 0){ if(tokens[2] != NULL){ printf("msh: cd: Need only one or no arguments "); }else if(tokens[1] == NULL){ int value = chdir(getenv("HOME")); if(value == -1){ printf("msh: cd %s is bad path ", tokens[1]); } }else{ int value = chdir(tokens[1]); if(value == -1){ printf("msh: cd: %s is a bad path ", tokens[1]); } } continue; } int rc = fork(); if (rc < 0) { fprintf(stderr, "fork failed "); exit(1); } if (rc == 0) { execvp(tokens[0], tokens); printf("msh: %s: %s ", tokens[0], strerror(errno)); exit(1); } else { wait(NULL); } } }

****************************************************************

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 Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

1. What are the purposes of negative messages? (LO 10-1)

Answered: 1 week ago