title: Implementing Pipes in a C shell (Unix)
body: <p>Basically I have created a shell using standard POSIX commands, I want to be able to Implement Piping as well. Right now it handles commands correctly, and can do background processing with &amp;. But I need to be able to pipe using | and >> as well.
  For example something like this:
  cat file1 file2 >> file3
  cat file1 file2 | more
  more file1 | grep stuff</p>
  
  <p>Here is the code I have currently. I also want to AVOID "SYSTEM" calls. I know U need to use dup2, but the way I did my code is a bit odd, so im hoping if someone can tell me if it is feasible to implement pipes in this code? thanks! I know dup2 is used, but also im def. confused at how to implement >> as WELL as |</p>
  
  <pre><code>#include &lt;sys/wait.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt; 
  #include &lt;stdio.h&gt;
  
  using namespace std;
  
  
  void Execute(char* command[],bool BG)
  {
  //Int Status is Used Purely for the waitpid, fork() is set up like normal.
      int status;
      pid_t pid = fork();
  
  
      switch(pid)
      {
          case  0:
              execvp(command[0], command);
  
              if(execvp(command[0], command) == -1)
              {
                  cout &lt;&lt; "Command Not Found" &lt;&lt; endl;
                  exit(0);
              }
  
            default:
              if(BG == 0)
              {
                      waitpid(pid, &amp;status, 0);
  //Debug             cout &lt;&lt; "DEBUG:Child Finished" &lt;&lt; endl;
              }
  
  
      }
  
  }
  
  
  bool ParseArg(char* prompt, char* command[], char Readin[],bool BG)
  {
  
      fprintf(stderr, "myshell&gt;");
          cin.getline(Readin,50);
      prompt = strtok(Readin, " ");
      int i = 0;
  
      while(prompt != NULL)
      {
          command[i] = prompt;
          if(strcmp(command[i], "&amp;") == 0){
  //Debug        cout &lt;&lt; "&amp; found";
          command[i] = NULL;
          return true;
      }
  //Debug        cout &lt;&lt; command[i] &lt;&lt; " ";
          i++;
          prompt = strtok(NULL, " ");
  
      }
      return false;
  }
  
  void Clean(char* command[])
  {
  //Clean Array
          for(int a=0; a &lt; 50; a++)
          {
               command[a] = NULL;
          }
  }
  
  
  
  int main()
  {
     char* prompt;
     char* command[50];
     char Readin[50]; 
     bool BG = false;
  
  
  
     while(command[0] != NULL)
     {
  
          Clean(command);
         BG = ParseArg(prompt, command, Readin, BG);
         if(strcmp(command[0], "exit") == 0 || strcmp(command[0], "quit") == 0 )
         {
               break;
         }
  
      else
      {
              Execute(command,BG);
      }
  
     }
  
     return 1;
  
  }
  </code></pre>