title: Basic shell that takes multiple word command
body: <p>I am trying to implement a shell that takes a command ; however, I can not get it to work properly. For example, if I type &quot;ls -a&quot;, I get this:</p>
  <p>invalid option -- '
  '</p>
  <p>Try 'ls --help' for more information.</p>
  <p>I have probably made some bad mistakes as I am a beginner so please forgive me. Also, I will put the code that reads in the command into a function. Its just like this for testing- thanks.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stddef.h&gt;
  
  int main()
  {
      pid_t       pid;
      int         status;
      char* token;
      char* argv[20];
      char input[100];
  
      printf(&quot;AP&gt; &quot;);
  
      while (1)
      {
          fgets(input, 100, stdin);
          token = strtok(input, &quot; &quot;);
  
          int i = 0;
          //walk through other tokens
          while (token != NULL) {
              argv[i] = malloc(strlen(token) + 1);
              strncpy(argv[i], token, strlen(token));
              //argv[i] = token;
              i++;
              token = strtok(NULL, &quot; &quot;);
          }
          argv[i] = NULL; //argv ends with NULL
  
          pid = fork();
  
          if (pid &lt; 0)
          {
              perror(&quot;fork error&quot;);
              return EXIT_FAILURE;
          }
  
          else if (pid == 0)
          {
              // child process
              execvp(argv[0], argv);
              perror(&quot;execl error&quot;);
              return EXIT_FAILURE;
          }
  
          else {
              // parent process
              if (waitpid(pid, &amp;status, 0)&lt;0)
              {
                  perror(&quot;waitpid error&quot;);
                  return EXIT_FAILURE;
              }
          }
          printf(&quot;AP&gt; &quot;);
      }
  
      return 0;
  }
  </code></pre>