title: exec failed with fork() and execvp()
body: <p>I am currently working on a program in which I have to write a shell in C. I am having trouble getting the fork() section of my program to work. Here is my code:</p>
  <pre><code>void execute_func(char** tok)
  {
      pid_t pid = fork();
      
      if (pid == -1)
      {
          printf(&quot;\nERROR: forking child process failed\n&quot;);
          return;
      }
      else if (pid == 0)
      {
          if (execvp(tok[0], tok) &lt; 0)
          {
              printf(&quot;ERROR: exec failed\n&quot;);
          }
          exit(0);
      }
      else
      {
          wait(NULL);
          return;
      }
  }
  </code></pre>
  <p>for example, if I am to type in any sort of function such as &quot;ls&quot; or &quot;wc&quot; it gives me the &quot;ERROR: exec failed&quot; message, which means that the fork() is not running correctly. This could be a small issue in my understanding of fork() but I am completely stumped.</p>
  <p>here is my whole program:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/wait.h&gt;
  
  char str[129];
  enum {NOT_FOUND=0,FOUND};
  enum {false=0,true};
  static char *ptr;
  const char *del;
  int ReadLine(char *, int , FILE *);
  
  
  
  
  char *mystrtok(char* string,const char *delim)
  {
      int j,flag=NOT_FOUND;
      char *p;
      if(string != NULL)
      {
          ptr=string;
          p=string;
      }
      else
      {
          if(*ptr == '\0')
          return NULL;
  
          p=ptr;
      }
  
      while(*ptr != '\0')
      {
          del=delim;
          while(*del != '\0')
          {
              if(*ptr == *del)
              {
                  if(ptr == p)
                  {
                      p++;
                      ptr++;
                  }
                  else
                  {
                      *ptr='\0';
                      ptr++;
  
                      return p;
                  }
              }
              else
              {
                  del++;
              }
          }
          ptr++;
      }
      return p;
  }
  
  void execute_func(char** tok)
  {
      pid_t pid = fork();
      
      if (pid == -1)
      {
          printf(&quot;\nERROR: forking child process failed\n&quot;);
          return;
      }
      else if (pid == 0)
      {
          if (execvp(tok[0], tok) &lt; 0)
          {
              printf(&quot;ERROR: exec failed\n&quot;);
          }
          exit(0);
      }
      else
      {
          wait(NULL);
          return;
      }
  }
      
  
  
  int main()
  {
      int i;
      char *p_str,*token;
      char delim[10];
      delim[0] = ' ';
      delim[1] = '\t';
      delim[2] = '\n';
      delim[3] = '\0';
      char cwd[1024];
      char *tok[129];
  
      while(1)
      {
          tok[0] = NULL;
          fflush(stdin);
          fflush(stdout);
          printf(&quot;\n Enter a string to tokenize: &quot;);
          //    printf(&quot;\n before scan&quot;);
          fflush(stdin);
          //    printf(&quot;\n fflush&quot;);
          ReadLine(str, 128, stdin);
          /*    scanf(&quot;%[^\n]&quot;,str); */
          printf(&quot;\n after scan&quot;);
  
          for (i = 1, p_str = str; ; i++, p_str = NULL)
          {
              token = mystrtok(p_str,delim);
              if (token == NULL)
              break;
              printf(&quot;%d: %s\n&quot;,i,token);
              tok[i-1] = token;
              printf(&quot;%s\n&quot;,tok[i-1]);
          }
          if(tok[0] != NULL)
          {
              if(strcmp(tok[0],&quot;cd&quot;) == 0)
              {
                  if (chdir(tok[1]) != 0)
                      perror(&quot;chdir() error()&quot;);
  
                  getcwd(cwd, sizeof(cwd));
                  printf(&quot;current working directory is: %s\n&quot;, cwd);
              }
  
              else if(strcmp(tok[0],&quot;pwd&quot;) == 0)
                  if (getcwd(cwd, sizeof(cwd)) == NULL)
                      perror(&quot;getcwd() error&quot;);
                  else
                      printf(&quot;current working directory is: %s\n&quot;, cwd);
  
              else if(strcmp(tok[0],&quot;exit&quot;) == 0)
                  exit(3);
  
              else
              {
                  execute_func(tok);
              }
          }
      }       
  }
  
  
  int ReadLine(char *buff, int size, FILE *fp)
  {
      buff[0] = '\0';
      buff[size - 1] = '\0';             /* mark end of buffer */
      char *tmp;
  
      if (fgets(buff, size, fp) == NULL) 
      {
          *buff = '\0';                   /* EOF */
          return false;
      }
      else 
      {
          /* remove newline */
          if ((tmp = strrchr(buff, '\n')) != NULL) 
          {
              *tmp = '\0';
          }
      }
  return true;
  }
  
  </code></pre>