title: Creating multiple pipe in C
body: <p>I'm writing a simple shell and I'd like to change my program to add the possibility of multiple pipe commands like "echo foo | cat | cat | cat | cat | wc". I have written for two commands but for multiple i can't.</p>
  
  <p>Here is the source code of my program:</p>
  
  <pre><code>if (pid == 0) // in the child process 
   {  
     for (i = 0; i &lt; command; i++)  // for each cmd
     {
       if (argv[i][0] == '|') 
       {
         j = i; 
       }
     }
     if (j &gt; 0) 
     {
       if (pipe(p)) 
       {
         fprintf(stderr, "pipe");
         exit(1);
       }
       argv[j] = NULL;
       if (fork() == 0) // child
       { 
          j = -1;
          close(p[0]);
          dup2(p[1],1);
          close(p[1]);
       } 
  
       // parent 
       close(p[1]);  
       dup2(p[0], 0);
       close(p[0]);                              
     }
     for (i = 0; dirs[i] != 0; i++) 
     {
       snprintf(pathname, sizeof(pathname), "%s/%s", dirs[i], argv[j+1]);
       execv(pathname, &amp;argv[j+1]);
     }
   }
   else 
   {  
     while (wait(0) != pid) // parent: wait child
   }
  </code></pre>
  
  <p>Thank you in advance for help. </p>