title: Having issues with pipe, fork, dup2
body: <p>I am using pipes, fork , dup2 to implement “ls | more” or “ls | sort” etc. 
  I am just not able to understand the issue here.
  When I run my program, I get this error:</p>
  
  <pre><code>./a.out  
  Missing filename ("less --help" for help)
  </code></pre>
  
  <p>Why am I getting "less" ??</p>
  
  <p>What is wrong with this code ? If I change “more” to “ls” again, it works fine. I mean, its like doing ls | ls. </p>
  
  <pre><code>#define STDIN 0
  #define STDOUT 1
  
  int main()
  {
     int fd[2];
     int pid;
     char *lschar[20]={"ls",NULL};
     char *morechar[20]={"more",NULL};
     pid = fork();
     if (pid == 0) {
     /* child */
       int cpid;
       cpid = fork();
       if(cpid == 0) {
         //printf("\n in ls \n");
         pipe(fd);
         dup2(fd[1], STDOUT);
         close(fd[0]);
         close (fd[1]);
         execvp("ls",lschar);
       } else if(cpid&gt;0) {
         waitpid(cpid, NULL,0);
         dup2(fd[0],STDIN);
         close(fd[0]);
         close(fd[1]);
         execvp("more", morechar);
       }
     } else if (pid &gt; 0) {
       /* Parent */
       waitpid(pid, NULL,0);
     }
     return 0;
  }
  </code></pre>
  
  <p>Appreciate your help. </p>