title: fork() and execvp() commands in c
body: <p>I am writing a C program. All code is in while loop but I did not add it yet. It takes input from user like start <code>ls -l</code> and run <code>ls -l</code>. And wait new command. If user write "wait" and there is a process that is not end, It waits until the child process is completed and print child process id that is just completed and take new command. My wait command does not working, and I need your help. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include  &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;unistd.h&gt;
  
  
  int main (int argc, char **argv) 
  {   
  
      char command[1024];
      char *cmd;
      char *ptr1;
      char *ptr2 = (char*)malloc(10 * sizeof(char));
      char *ptr3;
      char *pos;
      char *args[3];
      char tmp[1024];
      int count = 0;
      int cstatus;
      pid_t  child;
      pid_t c;
  
  
      printf("newShell&gt; ");
      fgets( command, 1024, stdin );
  
  
      if ((pos=strchr(command, '\n')) != NULL)
              *pos = '\0';
  
      if (strlen(command) &gt; 1024)
      {
          printf("Command is too long\n");
          exit(1);    
      }
  
      strcpy(tmp,command);
  
      ptr1 = strtok(tmp," ");
      cmd = ptr1;
  
  
      while(ptr1 != NULL) 
      {   
          ptr1 = strtok(NULL, " ");
  
  
          if (ptr1 == NULL) break;
  
          if (count == 0)
          {
              ptr3 = ptr1;
  
              count = 1;
          }
  
          else
              strcat(strcat(ptr2,ptr1)," ");
          }
  
      ptr2[strlen(ptr2) - 1] = '\0';
  
      args[0] = ptr3;
      args[1] = ptr2;
      args[2] = NULL;
  
      if (strcmp(cmd,"start") == 0)
      {
          if ((child = fork()) == 0) 
          { 
              printf("\nProcess %ld is started\n", (long) getpid());
              execvp(args[0], args); 
  
              fprintf(stderr, "execvp() couldn't do the process\n");
              exit(1);
          }
  
          if (child == (pid_t)(-1)) 
          {
              fprintf(stderr, "Fork error.\n"); exit(1);
          }
      }
  
      else if (strcmp(cmd,"wait") == 0)
      {   
  
          if (child &gt; 0)
          {
              c = wait(&amp;cstatus); 
              printf("Process %ld completed with code: %d \n",
              (long) c, cstatus);
          }
          else
              printf("There is no process\n");
      }   
  
  
  }
  </code></pre>
  
  <p>Edit: </p>
  
  <p>Should i change that part with: </p>
  
  <pre><code>child = fork();
  
      if (child == (pid_t)(-1)) 
      {
              fprintf(stderr, "Fork failed.\n"); 
              exit(1);
      }
  
      if (strcmp(cmd,"start") == 0)
      {
          if (child == 0) 
          { 
              printf("\nProcess %ld is started\n", (long) getpid());
              execvp(args[0], args); 
  
              fprintf(stderr, "execvp() couldn't do the process\n");
              exit(1);
  
              continue;
          }
  
      else if (strcmp(cmd,"wait") == 0)
  
          c = wait(&amp;cstatus); 
          printf("Parent: Child  %ld exited with status = %d\n",
          (long) c, cstatus);
          continue;
  
  
      }
  </code></pre>