title: Implementing unlimited pipes in C with just one loop and one pipe
body: <p>I'm trying to build a minishell and I've mostly everything but pipes working. I've read a lot of answers in this site and many others, but I'm not able to find my particular problem.</p>
  <p>The sequence should end when the right-most process ends, this is: <code>sleep 3 | sleep 3 | sleep 0</code> should immediately end, and not wait for any of the sleep 3 processes.</p>
  <p>*argvv is a pointer to the first process (*argvv would be <code>sleep 3</code>, *(argvv + 1) is the next <code>sleep 3</code>, etc. I don't know what's going on, but something is wrong with file descriptors and everything breaks after a test.</p>
  <p>For instance, when the input is <code>sh&gt; ls | wc | wc</code>, the output is</p>
  <pre><code>sh&gt; ls | wc | wc
  sh&gt;    1       3      25
  </code></pre>
  <p>, then it waits for some input. It's not until I press enter again it finishes. After that, <code>sleep 3</code> does not work anymore.</p>
  <p>Any help would be welcome, thanks a lot!</p>
  <p><strong>Edit:</strong></p>
  <p>Ok, I edited the code and everything seems to work fine, except sleep 3 | sleep 0 lasts 3 seconds, instead of immediately ending. I don't know how to fix this.</p>
  <pre class="lang-c prettyprint-override"><code>void execute(char ***argvv)
  {
      pid_t pid;
      int pd[2];
      int in = 0;
  
      while (*argvv != NULL) {
          pipe(pd);
          
          if ((pid = fork()) &lt; 0)
          {
              perror();
              exit(1);
          }
          else if (pid == 0) {
              dup2(in, 0);
              if (*(argvv + 1) != NULL)
                  dup2(pf[1], 1);
              close(pd[0]);
              close(pd[1]);
              execvp((*argvv)[0], *argvv);
              perror();
              exit(1);
          }
  
          else
          {
              close(pd[1]);
              in = pd[0];
  
              if (*(argvv + 1) == NULL)
                  close(pd[0]);
              argvv++;
          }
          wait(&amp;pid);
      }
  
  }
  </code></pre>