title: "Command not found" error with UNIX shell
body: <p>I've been working on a C program which simulates a shell via the terminal. I'm stuck on the pipe where I use a temporary file.</p>
  
  <p>My problem is that during the execution of a command like <code>ls | wc</code> it works 9 times on 10. Moreover if I execute this command in the terminal (real, not mine) about 1/10 of the time I get an error message that the wc command does not exist. Huh?</p>
  
  <p>My code looks fine so I suspect the error is coming from my UNIX setup (terminal or core, I don't know). But I'm not sure. Does this look like a code error or an OS problem?</p>
  
  <pre><code>/**************************************/
  /*           test parsing             */
  /**************************************/
  
  #include&lt;unistd.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;sys/wait.h&gt;
  #include&lt;sys/stat.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include &lt;errno.h&gt;
  
  char *delimiteurs = ";&amp;&lt;&gt;|";
  char *eltsCommande[10];
  char *eltsoutput[10];
  char *eltsinput[10];
  char *eltstube[10];
  char mot[50];
  int symbole, status, c;
  
  void parsing(){
      int i=0;
      int cmot=0;
      while(1){
          c = getchar();
          if      (c == '\n') {symbole = 0;return;}
          else if (c == ';')  {symbole = 1;return;}
          else if (c == '&amp;')  {symbole = 2;return;}
          else if (c == '&lt;')  {symbole = 3;return;}
          else if (c == '&gt;')  {symbole = 4;return;}
          else if (c == '|')  {symbole = 5;return;}
          else if (c == EOF)  {symbole = 7;return;}
          else if (c != ' ') {
              symbole = 10;
              while(c != '\n' &amp;&amp; !strchr(delimiteurs,c)){
                  i=0;
                  while(c != 32 ){
                      if((c != '\n') &amp;&amp; !strchr(delimiteurs,c)){
                      mot[i]=c;i++;
                      c=getchar();
                      }
                      else {break;}
                  }
                  break;
              }
              while(c == ' ')
              {
                  c=getchar();
              }
              ungetc(c,stdin);
              mot[i]=0;
              eltsCommande[cmot++]=strdup(mot);
              fflush(stdout);
              if(c == '\n' || strchr(delimiteurs,c))
              {
                  eltsCommande[cmot]=0;
                  return;
              }
          }
      }
  
  }
  
  int main(int argc, char* argv[]) {
      pid_t pid, fid;
      int background = 0;
      int status;
      char car;
      int i, j, k, l;
      int p, p2;
      int execute=1;
      int output=0;
      int input=0;
      int tube=0;
      int fd[2];
      int fich;
      while(1){
          if(execute==1){
              if(symbole==0){
                  printf("Entrez une commande&gt; ");
              }    
              for (j=0;j&lt;10;j++){
                  eltsCommande[j]=NULL;
              }            
              execute=0;    
              background=0;
          }
          fflush(stdout);
          parsing();
          switch (symbole) {
              case 0 :                            // SYMBOLE : \n
                  p=fork();
                  for(k=0;k&lt;10;k++){
                       printf("ELEMENT COMMANDE %d : %s \n", k, eltsCommande[k]);
                       //printf("ELEMENT COMMANDE TUBE %d : %s \n", k, eltstube[k]);
                       //printf("ELEMENT COMMANDE INPUT %d : %s \n", k, eltsinput[k]);
                       //printf("ELEMENT COMMANDE OUTPUT %d : %s \n", k, eltsoutput[k]);
                  }
  
                  if(p==0){                    //fils
                      if(tube==1){//printf("\n\n\n");
                                fich = open("fichtmp",O_RDONLY,0640);
                          close(0); //fermeture clavier
                          dup(fich); //fichier devient entrée 0
                          execvp(eltsCommande[0], eltsCommande);
                          close(fich); //fermeture fichier
                            }
                            else if(output==0 &amp;&amp; input==0){        //pas de redirection
                            printf("truc1");
                          execvp(eltsCommande[0], eltsCommande);    
                      }else if(output==1){                //dans le cas d'une redirection
                      printf("truc2");
                          close(1);
                          int filew = creat(eltsCommande[0], 0644);
                          execvp(eltsoutput[0], eltsoutput);
                      }
                      else if(input==1){
                      printf("truc3");
                          close(0);
                          int filer = open(eltsCommande[0], O_RDONLY);
                          execvp(eltsinput[0], eltsinput);
                      }/*
                      if(tube==1){
                          int f2 = fork();
                          pipe(fd);
                          switch (f2){
                          case -1 : printf("loose");
                          case 0: close(fd[0]);
                              close(1);
                              dup(fd[1]);
                              close(fd[1]);
                                   execvp(eltstube[0], eltstube); break;
                                 case 1: wait(&amp;status);
                              close(fd[1]);
                              close(0);
                              dup(fd[0]); 
                              close(fd[0]);
                                execvp(eltsCommande[0], eltsCommande); break;
                            default : printf("err");
                            }*/
                      return 0;
                  }else{                            //pere
                      if(background==0){                //pas de bg on attend le fils
                          waitpid(p, NULL, 0);
                      }                    
                      /*if(tube==1){
                          close(0);
                                  dup(fd[0]);
                                 close(fd[1]);
                                execvp(eltsCommande[0], eltsCommande);
                      }*/
                      output=0;
                      input=0;            
                      execute=1;
                      tube=0;
                  }
                  break;
              case 1:                                // SYMBOLE : ;
                  p=fork();                        
                  if(p==0){                         //fils
                      if(output==0 &amp;&amp; input==0 &amp;&amp; tube==0){        //pas de redirection
                          execvp(eltsCommande[0], eltsCommande);    
                      }else if(output==1){                //dans le cas d'une redirection
                          close(1);
                          int filew = creat(eltsCommande[0], 0644);
                          execvp(eltsoutput[0], eltsoutput);
                      }
                      else if(input==1){
                          close(0);
                          int filer = open(eltsCommande[0], O_RDONLY);
                          execvp(eltsinput[0], eltsinput);
                      }
                      return 0;                
                  }else{                            //pere
                      waitpid(p, NULL, 0);    
                      output=0;
                      input=0;
                      execute=1;    
                  }
                  break;
              case 2:                                // SYMBOLE : &amp;
                  background=1;
                  break;
              case 3:                                // SYMBOLE : &lt;
  
                        if(input==0){
                            input=1;
                            execute=1;
                            for (l=0;l&lt;10;l++){
                                eltsinput[l]=eltsCommande[l];
                            }
                        }
                        break;
              case 4:                                // SYMBOLE : &gt;
                  if(output==0){
                      output=1;
                      execute=1;
                      for (l=0;l&lt;10;l++){
                          eltsoutput[l]=eltsCommande[l];
                      }
                  }
                  break;
              case 5:                                // SYMBOLE : |
                  //if(tube==0){
                      /*for (l=0;l&lt;10;l++){
                          eltstube[l]=eltsCommande[l];
                      }*/
                      p2=fork();
                      if(p2==0){
                          if(tube==0){
                              freopen( "fichtmp", "w", stdout );
                              execvp(eltsCommande[0], eltsCommande);
                          }
                          return(0);
                      }
                      else{    if(background==0){            // SANS MOD BG ATTENDRE FIN FILS
                              waitpid(p2, NULL, 0);
                          }
                          tube=1;                
                          execute=1;    
                      }
  
                  break;
              default: 
                  printf("");
  
          }
      }
      return 0 ;
  }
  </code></pre>