Please help!
this is the assign: The purpose of this assignment is to make sure that the students can work effectively with the fork, pipe, and
dup, system calls. It should also grant some insight into how the shell enables input and output redirection
behind the scenes.
I am almost done with the code, but I don't know how to split the arguments when the user types it in the command line. For example, "ls -a", I have no idea what to do in order to split the text in the command line. This is what the suggestions are according to the assignment.
Possibilities include the C function strtok or the C++ istringstream
object.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
usingnamespace std;
int main()
{
// Creating command1 & command2
string command1, command2;
// quit = exit program
char quit[] = "quit";
// User entering command #1
cout << "command1? ";
cin >> command1;
// if "quit" is not typed in command line, user will enter command #2
while (command1 != "quit")
{
// command #2
cout << "command2? ";
cin >> command2;
// Creating my pipe
int pipefd[2];
int rs = pipe(pipefd);
// If pipe fails
if (rs == -1)
{
perror("Piping has fail!");
// Exit the program
exit(EXIT_FAILURE);
}
// Creating my fork to 2 processes
rs = fork();
// If fork fails
if (rs == -1)
{
perror("Piping has fail");
// Exit the program
exit(EXIT_FAILURE);
}
//Once fork, child is created
if (rs == 0)
{
// Always close the read end of the pipe
// to avoid ForkBomb, the write end will
// be left open
close(pipefd[0]);
// Closing stdout
close(1);
// Creating a duplicate (dup) to place into
// the write end of the pipe to stdout(1)
dup(pipefd[1]);
// Closing the wight end of the pipe
close(pipefd[1]);
// Command1 will be run using execlp
rs = execlp(command1.c_str(), command1.c_str(), (char*)NULL);
// If execlp fails display message to user
if (rs == -1)
{
perror("Execlp has failed!");
// Exit the program
exit(EXIT_FAILURE);
}
}
// This is the parent process
else
{
// Closing write end of the pipe and leaving
// read open
close(pipefd[1]);
// Close stdout
close(0);
// Creating a duplicate (dup) to place into
// the read end of the pipe to stdin(0)
dup(pipefd[0]);
// Closing the read end of the pipe
close(pipefd[0]);
// Forking into 2 processes again
rs = fork();
// If fork() fails display message to user
if (rs == 1)
{
perror("Parent fork has failed!");
exit(EXIT_FAILTURE);
}
// You are in child process #2
// Here we will run command2
if (rs == 0)
{
//Running command2
rs == execlp(command2.c_str(), command2.c_str(), (char*)NULL);
// If execlp fails display message to user
if (rs == -1)
{
perror("Child2 execlp has faild");
exit(EXIT_FAILURE);
}
}
// Inside the parent process
else
{
wait(NULL);
wait(NULL);
break;
}
}
}
return 0;
}