title: ls | grep in shell written in C
body: <p>I am trying to make my own shell in C.  It uses one pipe and the input (for now) is static. I execute commands using execvp.
  Everything is fine except when I run the command <code>ls |grep ".c"</code> I get no results. Can anyone show me where is the problem and find a solution.</p>
  
  <p>The shell so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  
  int   p[2];
  int pid;
  int r;
  
  main()
  {
      char *ls[] = {"ls", NULL};
      char *grep[] = {"grep", "\".c\"", NULL};
  
      pipe(p);
  
      pid = fork();
      if (pid  != 0) {
              // Parent: Output is to child via pipe[1]
  
              // Change stdout to pipe[1]
              dup2(p[1], 1);
              close(p[0]);
  
              r = execvp("ls", ls);
      } else {
              // Child: Input is from pipe[0] and output is via stdout.
              dup2(p[0], 0);
              close(p[1]);
  
              r = execvp("grep", grep);
              close(p[0]);
      }
  
      return r;
  }
  </code></pre>