Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this C function. We are modifiying the existing C code called msh.c given below but leave the existing functions there. We

Please help me with this C function. We are modifiying the existing C code called msh.c given below but leave the existing functions there. We are adding a new function for when 'cd' command is called and also to allow the code to read input from a file instead of standard input. We are to use chdir and getenv for the cd command.

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 had asked this question before but got completely wrong answers that were handwritten on paper. Please post the correct code.

Instructions/Requirements:

image text in transcribed

This is the msh.c code to add the new functions to:

#include  #include  #include  #include  #include  /* * A very simple shell */ #define MAX_BUF 160 #define MAX_TOKS 100 int main() { char *pos; char *tok; char s[MAX_BUF]; char *toks[MAX_TOKS]; time_t rawtime; struct tm *timeinfo; static const char prompt[] = "msh> "; while (1) { /* prompt for input */ printf(prompt); /* read input */ char *status = fgets(s, MAX_BUF-1, stdin); /* exit if ^d or "exit" entered */ if (status == NULL || strcmp(s, "exit ") == 0) { if (status == NULL) { printf(" "); } exit(EXIT_SUCCESS); } /* remove any trailing newline */ if ((pos = strchr(s, ' ')) != NULL) { *pos = '\0'; } /* break input line into tokens */ char *rest = s; int i = 0; while ((tok = strtok_r(rest, " ", &rest)) != NULL && i tm_mon, timeinfo->tm_mday, 1900 + timeinfo->tm_year); continue; } /* otherwise fork a process to run the command */ int rc = fork(); if (rc   a. Add a "built-in, command 'cd' in your msh shell. This command should work in two ways, Just like in bash: 1) if no argument is supplied, change to the user's home directory, and 2) if an argument is supplied, change to the directory specified by the argument. Hint: to get the user's home directory, your code will have to read the value of the HOME environment variable. Investigate the 'getenv library function and 'chdir' system call. Here is the sort of output your new msh should produce $ ./msh msh> 1s backup Makefile msh msh.c msh-hw-2.c README.txt temp msh> pwod /home/CLASSES/brunsglenn/ctests/msh msh> cd backup msh> pwd /home/CLASSES/brunsglenn/ctests/msh/backup msh> cd .. msh> pwid /home/CLASSES/brunsglenn/ctests/msh msh> cd baz msh: cd: bad path msh> cd msh> pwod /home/CLASSES/brunsglenn msh> exit b. Allow msh to read input from a file rather than from standard input. If msh is invoked from bash with a command- line argument, then msh should attempt to open the file specified by the command-line argument, and use that file as input. Your msh should terminate when the end of the file is reached (note that ctrl-d means "end-of-file" in bash) However, msh should still be able to be invoked without a command-line argument (as shown in the previous part of this exercise) Hint: you are probably already using function fgets to read input from the user. Notice that the last argument of fgets is a "stream", of type FILE *. When you call fgets you are probably supplying stdin as the input, which tells fgets to get input from the keyboard. Look at the man pages for functions fgets and fopen Here is an example of how msh should work with a filename on the command line backup Makefile msh msh.c msh-hw-2.c README.txt temp $ cat temp $ ./msh temp backup Makefile msh msh.c msh-hw-2.c README.txt temp Note that no prompts are produced when msh is run from a script

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

Icdt 88 2nd International Conference On Database Theory Bruges Belgium August 31 September 2 1988 Proceedings Lncs 326

Authors: Marc Gyssens ,Jan Paredaens ,Dirk Van Gucht

1st Edition

3540501711, 978-3540501718

More Books

Students also viewed these Databases questions

Question

Assess three steps in the selection process.

Answered: 1 week ago

Question

Identify the steps in job analysis.

Answered: 1 week ago