Question: Instructions Enhance your code from the ps 3 - simple shell with I / O redirection assignment to include the ability to send the output

Instructions
Enhance your code from the ps3-simple shell with I/O redirection assignment to include the ability to send the output from one program as input to another program. This functionality is supported by a mechanism called a pipe.
SimpleShell.cpp:
// Including header file path #include "../include/SimpleShell.h"// Including all necessary libraries #include #include #include #include #include #include #include #include #include #include using namespace std; void SimpleShell::execute(const vector& argv){// Checking if there are no commands to execute if(argv.empty()){ cerr << "Error: No command to execute" << endl; return; }// Preventing execution of the shell itself if(argv[0]=="./bin/SimpleShell"){ cerr << "Error: Cannot execute the shell itself." << endl; return; }// Variables to store the status of the child process and the child process ID, respectively int status; pid_t child; // Creating a new process child = fork(); if(child >0){// Waiting for the child process to finish waitpid(child, &status, 0); } else if(child ==0){// Child process if(argv.empty()){ cerr << "Error: No command to execute" << endl; _exit(1); } vector args; // Vector to hold command arguments string input_file; // String to hold input file name string output_file; // String to hold output file name // Parsing the command line arguments for(size_t i =0; i < argv.size(); ++i){ if(argv[i]=="<"){// Input redirection if(i +1< argv.size()){ input_file = argv[i +1]; // Getting the input file name ++i; // Skipping the file name in the next iteration } else { cerr << "Error: No input file specified." << endl; _exit(1); }} else if (argv[i]==">"){// Output redirection if(i +1< argv.size()){ output_file = argv[i +1]; // Getting the output file ++i; // Skipping the file name in the next iteration } else { cerr << "Error: No output file specified." << endl; _exit(1); }} else {// Adding command arguments to vector args.push_back(argv[i]); }}// Handling input redirection if(!input_file.empty()){ int fd_in = open(input_file.c_str(), O_RDONLY); // Opening the input file if(fd_in <0){ perror("Error opening input file"); _exit(1); } dup2(fd_in, STDIN_FILENO); // Redirecting standard input to the input file close(fd_in); // Closing the input file descriptor }// Handling output redirection if(!output_file.empty()){ int fd_out = open(output_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); // Opening the output file if(fd_out <0){ perror("Error opening output file"); _exit(1); } dup2(fd_out, STDOUT_FILENO); // Redirecting the standard output to the output file close(fd_out); // Close the output file descriptor }// Vector to hold strings for execvp vector exec_args; for(const auto& arg : args){ exec_args.push_back(arg.c_str()); // Convert each string to const char*} exec_args.push_back(nullptr); // Terminate the argument list // Execute the command using execvp if(execvp(exec_args[0], const_cast(exec_args.data()))==-1){ perror("execvp failed"); _exit(1); }}// Handling fork failure else { perror("fork failed"); exit(1); }}// Method to parse a line of input into tokens based on a given delimeter void SimpleShell::parse(const string& line, vector& tokens, const string& delimeter){ size_t start =0; // Starting position for each substring search size_t end =0; // Ending position for substring search // Loop to find and extract tokens separated by a delimiter while((end = line.find(delimeter, start))!= string::npos){ if(end != start){ tokens.push_back(line.substr(start, end - start)); // Add token to the vector } start = end + delimeter.length(); // Move start position to the next character }// Add the last token if there is any remaining text if(start < line.length()){ tokens.push_back(line.substr(start)); }}// Main loop for running the shell void SimpleSh

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!