Question: Description: Writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either

Description: Writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is quit, the program should immediately exit. If quit is not found, each of these lines of input will be treated as a command line to be executed. These two commands should be executed as if the user had typed command1 | command2 at the shell prompt, meaning that the standard output of the first command gets redirected into the standard input of the second command.You will need to be able to split the text entered by the user into the different command line arguments that comprise it,using the C function strtok, or a C++ istringstream object. You do not need to worry about escaping special characters when splitting the string for the purposes of this assignment; the split is based on spaces alone. After these commands have both finished executing, your program should prompt for another pair of input commands to run, repeating the process over and over again until the user gives quit as input.

Requirements: can't use the system function.

Make your own child processes, do the appropriate changes to the file descriptors in each of them, and have them use one of the exec calls to run the commands given by the user.

Use the pipe system call to create the file descriptors used to communicate between the two programs that are run.

If an error occurs at any point, there should be an appropriate error message printed to the standard error stream. Your program must be able to handle user-specified commands that are up to 127 characters long.

Your program must be able to handle user-specified commands that contain up to five commandline arguments. Make sure to close unused pipe file descriptors in all process. If you do not, your program is likely to stall.

Do not forget that this program involves a loop. After both programs have finished running (but not until), the program should go back to the beginning and prompt for two more commands to run.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I'm not sure if this is what my instructor wanted me do, but I put my code below it's not pretty but it does the job but am I doing the exec part correct?

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

using namespace std;

int main(int argc, char const *argv[]) { //While loop to continue processing commands until "exit" command is entered while(true) { char buffer[127]; string command; string argument1, argument2, argument3, argument4, argument5; //Prompt user for command and place entered command, options, and arguments into buffer cout << "Enter command: "; cin.getline(buffer,127); //If user entered "exit" command, break out of loop if(!strcmp(buffer, "quit")) { break; } //Begin process to divide inputed string into command and options/arguments //Define deliminator and new array for command, options, and arguments to be placed into const char delim[2] = " "; char * section[5]; int curSect = 0; char *split; split = strtok(buffer, delim); //Place pieces of buffer (separated by whitespace) into section array and corresponding command, options, and arguments arrays while (split != NULL) {

section[curSect] = (char *) malloc(strlen(split) + 1); strcpy(section[curSect], split); //First string entered by user is command if (curSect == 0) { command = section[curSect]; } //The options and arguments are separated into their own strings else if (curSect == 1) { argument1 = section[curSect]; } else if (curSect == 2) { argument2 = section[curSect]; } else if (curSect == 3) { argument3 = section[curSect]; }

else if (curSect == 4) { argument4 = section[curSect]; } else if (curSect == 5) { argument5 = section[curSect]; } curSect++; split = strtok(NULL, delim); } //Fork process int pid, rs, status; pid = fork(); //If fork fails, exit with error message if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); }

if (pid == 0) { //Set of if statements to determine how many options and/or arguments have been entered and pass the proper number of arguments to execlp //If no options or arguments were entered by user if (curSect == 1) { rs = execlp(command.c_str(), command.c_str(), (char*) NULL); if (rs == -1) { perror(command.c_str()); exit(EXIT_FAILURE); } } //If one option or argument was entered by user if (curSect == 2) { rs = execlp(command.c_str(), command.c_str(), argument1.c_str(), (char*) NULL); if (rs == -1) { perror(command.c_str()); exit(EXIT_FAILURE); } } //If two options and/or arguments were entered by user if (curSect == 3) { rs = execlp(command.c_str(), command.c_str(), argument1.c_str(), argument2.c_str(), (char*) NULL); if (rs == -1) { perror(command.c_str()); exit(EXIT_FAILURE); } } //If three options and/or arguments were entered by user if (curSect == 4) { rs = execlp(command.c_str(), command.c_str(), argument1.c_str(), argument2.c_str(), argument3.c_str(), (char*) NULL); if (rs == -1) {

perror(command.c_str()); exit(EXIT_FAILURE); } } //If four options and/or arguments were entered by user if (curSect == 5) { rs = execlp(command.c_str(), command.c_str(), argument1.c_str(), argument2.c_str(), argument3.c_str(), argument4.c_str(), (char*) NULL); if (rs == -1) { perror(command.c_str()); exit(EXIT_FAILURE); } } }

// Parent process: wait for child to end else { wait(&status);

}

} return 0; }

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 Databases Questions!