title: Multipipe function breaking stdin in c?
body: <p>Some friends and I are trying to implement a complete shell, and I'm in charge of the multipipe function.</p>
  
  <p>The problem is that just after the function ends and return, the <code>read()</code> in the main loop seems to endlessly read 0 bytes and cause an infinite loop. However, the function outputs the intended result.</p>
  
  <p>After spending around a week trying to debug my function we came to the following conclusion: somehow, I broke <code>STDIN</code>.</p>
  
  <p>What could possibly cause this?</p>
  
  <p>Here is the full function minus error checking for clarity purpose.</p>
  
  <pre><code>int do_pipe( t_parser *paser, int fd, t_info *info, int j )
  {
      int    status;
      pid_t  pid;
      int    pipefd[ 2 ];
  
      if ( j == 0 )
      {
          while ( parser-&gt;next != NULL )
              parser = parser-&gt;next;
  
          fd = 1;
      }
  
      if ( parser-&gt;prev )
          pipe( pipefd );
  
      pid = fork();
  
      if ( pid == 0 )
      {
          if ( parser-&gt;prev )
          {
              close( pipefd[1] );
              dup2( pipefd[0], 0 );
              close( pipefd[0] );
          }
  
          if ( parser-&gt;next )
          {
              if ( parser-&gt;prev )
                  close( pipefd[0] );
  
              dup2( fd, 1 );
              close( fd );
          }
  
          my_execve( parser, info-&gt;env );
      }
      else
      {
          if ( parser-&gt;prev )
              close( pipefd[0] );
  
          if ( fd != 1 )
              close( fd );
  
          j++;
  
          if ( parser-&gt;prev )
              do_pipe( parser-&gt;prev, pipefd[1], info, j );
              waitpid( pid, &amp;status, 0 );
      }
  
      return ( 0 );
  }
  </code></pre>