Question: Instructions Extend your code from the ps 2 - simple shell assignment so that it also handles I / O redirection, allowing the standard output
Instructions
Extend your code from the pssimple shell assignment so that it also handles IO redirection, allowing the standard output to be redirected to an output file. For example:
shell.out cat input output
This command specifies that the input file will be used to read data, and the output will be directed to the specified output file instead of the terminal. To accomplish this, you will need to use the dup system call. Additionally, you should enhance the parser to recognize the and symbols. If these symbols are present, the parser should tie STDINFILENO and STDOUTFILENO to the respective file descriptors.
For additional learning support to assist you in solving this assignment, please refer to the materials listed at the bottom of the page and to the 'Relevant External Materials' page on Canvas.
Submission: Commit the following to your GitHub repository:
Your source code.
a Makefile that compiles and runs your program efficiently.
a README file that details any dependencies and provides stepbystep instructions on using the Makefile to compile and run the program.
Code from ps Simple Shell:
Alena Fisher
#include includeSimpleShellh
using namespace std;
void SimpleShell::executeconst vector& argv
int status;
pidt child;
Spawning a child process
child fork;
Parent process portion
if child
cout getpid : I am a parent process waiting..." endl;
waitpidchild &status, ; Wait for child process to finish
cout "Waiting complete" endl;
Child process portion
else if child
cout "I am a child executing a new environment" endl;
Ensure there's at least one argument
if argvempty
cerr "Error: No command to execute" endl;
exit;
Prepare arguments for exec must end with nullptr
vector args;
for const auto& arg : argv
args.pushbackargcstr;
args.pushbacknullptr;
Execute the ls command. execvp is a function that replaces the current process image
with a new process image. In this case, it is the command stored in args
constcast is used to cast the vector from const char to char const because having 'char
is required by execvp for the second argument.
args.data returns a pointer to the vector
: is returned after attempting to execute the command if it fails. If it fails,
a message is printed. After this the program exits exit
ifexecvpargs constcastargsdata
perrorexec failed";
exit;
else
perrorfork failed"; Error handling if fork fails
exit;
void SimpleShell::parseconst string& line, vector& tokens, const string& delimiter
sizet start ;
sizet end ;
while end line.finddelimiter start string::npos
if end start Ignore empty tokens
tokens.pushbacklinesubstrstart end start;
start end delimiter.length;
if start line.length Add the last token
tokens.pushbacklinesubstrstart;
void SimpleShell::run
while true
string line;
vector tokens;
Print the prompt
cout getpid;
Get input from the keyboard
if getlinecin line
break; Exit the shell if input fails eg EOF
Parse the input into tokens
parseline tokens, ;
if tokensempty
continue; Skip empty input
Execute the user command
executetokens;
int main
SimpleShell shell;
shell.run; Start the shell loop
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
