Question
Improve the shell program we studied in class to support output redirection: Using the current version, if we type at the command prompt > cat
Improve the shell program we studied in class to support output redirection: Using the current version, if we type at the command prompt > cat filename.txt we will see the contents of the file at the terminal. If we type
> cat filename.txt > out.txt the current shell will pass the > and out.txt as arguments to the program, cat in this example. Modify the shell so that it treats the > and out.txt as stdout redirection and does not pass them as arguments to the program being executed.
Make you changes in shellex.c, highlight them and explain them in detail. Include the modified (and highlighted) shellex.c in your single pdf file with your answers.
Notes:
Call parseline - now you have argv[]
Scan argv[]
if you find > filename
store filename as output file name
Remove > filename from argv[]
Assume that the symbol > followed by a filename will appear at the end of the command line (they will be the last two strings in argv[]).
fork()
In child
if output file name exists
open()
dup2()
execve()
/* $begin shellmain */
#include "csapp.h"
#define MAXARGS 128
/* function prototypes */
void eval(char *cmdline);
int parseline(char *buf, char **argv);
int builtin_command(char **argv);
int main() {
char cmdline[MAXLINE];
/* Command line */
while (1) {
/* Read */
printf("> ");
Fgets(cmdline, MAXLINE, stdin);
if (feof(stdin))
exit(0);
/* Evaluate */
eval(cmdline);
}
}
/* $end shellmain */
/* $begin eval */
/* eval - Evaluate a command line */
void eval(char *cmdline) {
char *argv[MAXARGS]; /* Argument list execve() */
char buf[MAXLINE]; /* Holds modified command line */
int bg; /* Should the job run in bg or fg? */
pid_t pid; /* Process id */
strcpy(buf, cmdline);
bg = parseline(buf, argv);
if (argv[0] == NULL)
return; /* Ignore empty lines */
if (!builtin_command(argv)) {
if ((pid = Fork()) == 0) { /* Child runs user job */
if (execve(argv[0], argv, environ) < 0) {
printf("%s: Command not found. ", argv[0]);
exit(0);
}
}
/* Parent waits for foreground job to terminate */
if (!bg) { int status; if (waitpid(pid, &status, 0) < 0) unix_error("waitfg: waitpid error"); } else printf("%d %s", pid, cmdline); } return; } /* If first arg is a builtin command, run it and return true */ int builtin_command(char **argv) { if (!strcmp(argv[0], "quit")) /* quit command */ exit(0); if (!strcmp(argv[0], "&")) /* Ignore singleton & */ return 1; return 0; /* Not a builtin command */ } /* $end eval */ /* $begin parseline */ /* parseline - Parse the command line and build the argv array */ int parseline(char *buf, char **argv) { char *delim; /* Points to first space delimiter */ int argc; /* Number of args */ int bg; /* Background job? */ buf[strlen(buf)-1] = ' '; /* Replace trailing ' ' with space */ while (*buf && (*buf == ' ')) /* Ignore leading spaces */ buf++; /* Build the argv list */ argc = 0; while ((delim = strchr(buf, ' '))) { argv[argc++] = buf; *delim = '\0'; buf = delim + 1; while (*buf && (*buf == ' ')) /* Ignore spaces */ buf++; } argv[argc] = NULL; if (argc == 0) /* Ignore blank line */ return 1; /* Should the job run in the background? */ if ((bg = (*argv[argc-1] == '&')) != 0) argv[--argc] = NULL; return bg; } /* $end parseline */
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