title: Pipe: How to create N Pipes?
body: <p>I'm trying to create a shell and I'm having trouble when implementing the pipes. The goal is to type commands on the shell that's created  (e.g. <code>ls -l -a | sort -u &lt; myfile &gt; myfile2 | w</code>) . The number of pipes is random and the code has to support n pipes.</p>
  
  <p>I have something like this:</p>
  
  <pre><code>int fork_pipe(int n)
  {
  
  int i;
  int in=0;
  int fd[2];
  
  for(i=0; i&lt; n; i++)
  {
      pipe(fd);
  
      if(fork()==0)
      {
  
          if(in != 0)
          {
              dup2(in,0);
              close(in);
          }
  
          if(out != 1)
          {
              dup2(out,1)
              close(out);
          }
  
          execpv(argv_current_command,argv_current_command++); //Incompleto
  
          close(fd[1]);
          in = fd[0];
      }
  
  }
  
  if(in!=0)
  {
      dup2(in,0);
  }
  
  execpv(argv_current_command,argv_current_command++);
  
  }
  </code></pre>
  
  <p>and I have a pipe_count to know the number of pipes that will later be send to the fork_pipe function as n.</p>