title: Piping to redirect stdout of one program to stdin of another
body: <p>We have to write a C program that will essentially redirect stdin of one command/program to another command/program stdout, using pipes. If the program on the command line was passed <code>./a.out ls -l \; more</code>, it should redirect the stdout of <code>ls -l</code> to <code>more</code>, with the <code>\;</code> being the delimiter. This program should work for any command/program that is in our path so: <code>./a.out cat filename.c \; more</code>, should be the same as typing:  <code>cat filename.c | more</code>. </p>
  
  <p>My problem is that my program can't seem to exec properly or that the pipes are not working as expected. Basically I'm just getting no output aside from the print statement debugging I have placed. To be specific, the program prints: Exec... and then ERROR, which is all in the parent code.</p>
  
  <pre><code>  #include &lt;fcntl.h&gt;
    #include &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;unistd.h&gt;
    #include &lt;sys/types.h&gt;
    #include &lt;sys/wait.h&gt;
    void pipeit(char * pro1, char * pro2, char * p1args[], char * p2args[])
    {
       pid_t pid;
       int fd[2];
       int st;
       pipe(fd);
       pid = fork();
  
       if(pid &lt; 0)
       {
          printf("Error Forking...\n");
          exit(-1);
       }
       else if(pid == 0)
       {
          dup2(fd[1],1);
          close(fd[0]);
          close(1);
          printf("Exec 1...\n");
          execv(pro1, p1args);
          printf("ERROR\n");
       }
       else
       {
          waitpid(pid,&amp;st,0);
          if(st&lt;0)
          {
             printf("Child Error\n");   
          }
          dup2(fd[0],0);
          close(fd[1]);
          close(0);
          printf("Exec...\n");
          execv(pro2,p2args);
          printf("ERROR\n");
       }
       return;
    }
    /* THIS IS JUST COMMAND LINE PARSING */
    int main(int argc, char * argv[])
    {
       int i = 1;
       char * pro1;
       char * pro2;
       char * first[argc+1];
       char * second[argc+1];
       while(i&lt;argc &amp;&amp; argv[i][0] != ';')
       {
          if(i == 1)
          {
             pro1 = argv[i];
          }
          else
          {
             first[i] = argv[i];
          }
          i++; 
       }
       first[i] = NULL;
       while(i&lt;argc)
       {
          if(argv[i][0] == ';')
          {
             i++;
             pro2 = argv[i];
          }
          else
          {
             second[i] = argv[i];
          }
          i++; 
       }
       second[i] = NULL;
       pipeit(pro1,pro2,first, second);
       return 0;
    }
  </code></pre>