title: Shell program with pipes in C
body: <p>I have a problem with pipes. My program is a Shell program in C. I want to execute for example <code>ls | wc</code>, but what I get after running is: </p>
  
  <blockquote>
    <p>ls: cannot access |: no such file or directory ls: cannot access wc: no such file or directory. </p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>#include &lt;stdlib.h&gt;   
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/types.h&gt;
  
  #define MAX_CMD_LENGTH 100
  
  #define MAX_NUM_PARAMS 10
  
  int parsecmd(char* cmd, char** params) { //split cmd into array of params
      int i,n=-1;
      for(i=0; i&lt;MAX_NUM_PARAMS; i++) {
          params[i] = strsep(&amp;cmd, " ");
          n++;
          if(params[i] == NULL) break;
      }
      return(n);
  };
  
  int executecmd(char** params) {
      pid_t pid = fork(); //fork process
  
      if (pid == -1) { //error
          char *error = strerror(errno);
          printf("error fork!!\n");
          return 1;
      } else if (pid == 0) { // child process
          execvp(params[0], params); //exec cmd
          char *error = strerror(errno);
          printf("unknown command\n");
          return 0;
      } else { // parent process
          int childstatus;
          waitpid(pid, &amp;childstatus, 0);    
          return 1;
      }
  };
  
  int execpipe (char ** argv1, char ** argv2) {
      int fds[2];
      pipe(fds);
      int i;
      pid_t pid = fork();
      for (i=0; i&lt;2; i++) {
          if (pid == -1) { //error
              char *error = strerror(errno);
              printf("error fork!!\n");
              return 1;
           } else
               if (pid == 0) {
                   if(i ==0){
                       close(fds[1]);
                       dup2(fds[0], 0);
                       close(fds[0]);
                       execvp(argv1[0], argv1);
                       char *error = strerror(errno);
                       printf("unknown command\n");
                       return 0;
                   } else if(i == 1) { 
                       close(fds[0]);
                       dup2(fds[1], 1);
                       close(fds[1]);
                       execvp(argv2[0], argv2);
                       char *error = strerror(errno);
                       printf("unknown command\n");
                       return 0;
                   }
              } else { // parent process
                  int childstatus;
                  waitpid(pid, &amp;childstatus, 0);
                  return 1;
              }
      } // end for
  };
  
  
  int main() {    
      char cmd[MAX_CMD_LENGTH+1];    
      char * params[MAX_NUM_PARAMS+1];    
      char * argv1[MAX_NUM_PARAMS+1];    
      char * argv2[MAX_NUM_PARAMS+1];    
      int k, y, x;    
      int f = 1;    
      while(1) {
          printf("$"); //prompt    
          if(fgets(cmd, sizeof(cmd), stdin) == NULL) break; //read command, ctrl+D exit       
          if(cmd[strlen(cmd)-1] == '\n') { //remove newline char    
              cmd[strlen(cmd)-1] = '\0';    
          }    
          int j=parsecmd(cmd, params); //split cmd into array of params           
          if (strcmp(params[0], "exit") == 0) break; //exit   
          for (k=0; k &lt;j; k++) { //elegxos gia uparksi pipes    
              if (strcmp(params[k], "|") == 0) {    
                  f = 0; y = k;      
                 printf("pipe found\n");
              }               
          }
          if (f==0) {
              for (x=0; x&lt;k; x++) {    
                 argv1[x]=params[x];
              }     
              int z = 0;     
              for (x=k+1; x&lt; j; x++) {     
                  argv2[z]=params[x];
                  z++;
              }     
              if (execpipe(argv1, argv2) == 0) break;    
           } else if (f==1) {     
               if (executecmd(params) == 0) break;
           }
      } // end while
      return 0;
  }
  </code></pre>