title: Problem with pipe and redirection, create own shell
body: <p>I'm trying to recreate the shell implementation (<code>bash --posix</code>) of &quot;<code>|</code>&quot; and &quot;<code>&gt;</code>&quot; in c.</p>
  <p>But I have trouble to find my problems with <code>fd</code>. When I do the following command: <code>echo heyy | wc &gt; txt.txt | wc</code> everything is going smoothly but one in two the last <code>wc</code> display the good answer <code>(0 0 0)</code> or the false answer (result of the previous <code>wc</code>).</p>
  <p><code>t_command</code> is a list of <code>cmd</code>. Each node have a <code>char **tab</code> with the command, for example:</p>
  <pre><code>-first node is: tab[0] = echo , tab[1]=heyy
  -third node is: tab[0] = wc
  </code></pre>
  <p>etc..</p>
  <pre><code>{
      int         count;
      t_command   *tmp;
  
      tmp = all_cmd;
      count = 0;
      while (tmp /* &amp;&amp; (tmp-&gt;type == WORD || tmp-&gt;type == PIPE) */)
      {
          if (tmp-&gt;type == WORD)
              count++;
          tmp = tmp-&gt;next;
      }
      return (count);
  }
  
  
  pid_t   fork_pipe(int fd_0, int fd_1)
  {
      pid_t   pid;
  
      pid = fork();
      if (pid &lt; 0)
          return (-1);
      if (pid == 0)
      {
          if (fd_0 != 0)
          {
              dup2(fd_0, STDIN_FILENO);
              close(fd_0);
          }
          if (fd_1 != 1)
          {
              dup2(fd_1, STDOUT_FILENO);
              close(fd_1);
          }
      }
      return (pid);
  }
  
  
  
  int execute_last(t_command *all_cmd, char **env, int fd_0, int fd_1)
  {
      int pid;
  
      pid = fork_pipe(fd_0, fd_1);
      if (pid == 0)
          ft_exec(env, all_cmd-&gt;cmd_to_exec);
      return (0);
  }
  
  int execute_pipe(t_command *all_cmd, char **env, int nb_cmd)
  {
      int     fd[2];
      int     fd_0;
      int     fd_1;
      int     i;
      pid_t   pid;
  
      i = 0;
      fd_0 = STDIN_FILENO;
      while (all_cmd &amp;&amp; i &lt; nb_cmd - 1)
      {
          
          if (pipe(fd) &lt; 0)
              return (-1);
          if(all_cmd-&gt;next &amp;&amp; all_cmd-&gt;next-&gt;type == CHV_R)
          {
              fd_1 = open(all_cmd-&gt;next-&gt;next-&gt;cmd_to_exec[0], O_WRONLY | O_CREAT | O_TRUNC, 00777);
              execute_last(all_cmd, env, fd_0, fd_1);
              if (all_cmd-&gt;next-&gt;next)
                  all_cmd = all_cmd-&gt;next-&gt;next;
              i += 2;
          }
          else
          {
              fd_1 = fd[1];
              pid = fork_pipe(fd_0, fd_1);
              if (pid == 0)
                  ft_exec(env, all_cmd-&gt;cmd_to_exec);
              close(fd[1]);
              fd_0 = fd[0];
          }
          if (all_cmd &amp;&amp; all_cmd-&gt;next &amp;&amp; all_cmd-&gt;next-&gt;next)
              all_cmd = all_cmd-&gt;next-&gt;next;
          i++;
      }
      execute_last(all_cmd, env, fd_0, STDOUT_FILENO);
      wait_pipe(nb_cmd);
      return (0);
  } ```
  </code></pre>