title: C Programming - execlp() help?
body: <p>I'm creating a shell in C for learning purposes, and so far I've gotten to the point where you can input a string via fgets(), the string is broken down into "chunks", and then these chunks are passed to execlp(). The first chunk being the name of the command, and subsequent chunks being the commands arguments.</p>
  
  <p>Everything works just fine, except the execlp() call. But I don't see what I'm doing wrong, this all looks legit to me, according to the man pages! </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  
  #define MAX_CHUNKS 10
  
  /*==========================================================================
   *  Given a string, Break it down into chunks. Separated by ' ', skipping \n
   * ========================================================================*/
  int break_down_string(char *input_string, char *pointer_array[MAX_CHUNKS])
  {
          char *p = input_string, buffer[100]={0};//Initialize buffer to zero's.
          short int index = 0, space_count = 0, i;
  
  
      strncat(p, " ", 1);
  
      while (*p != '\0')
      {   
          if (index == MAX_CHUNKS) break; //End if MAX_CHUNKS chunks taken from string.
          if (*p == '\n'){ //Skip newline characters.
              p++;
              continue;
              }
  
          if (*p == ' ') //Space Detected 
          {
              if (space_count == 0)
              {
                  pointer_array[index] = (char *)malloc(sizeof(char) * strlen(buffer) +1);
                  strncpy(pointer_array[index], buffer, strlen(buffer));
                  strncat(pointer_array[index], "\0", 1);
                  bzero(buffer, sizeof(buffer));
                  index++;
              }
              space_count = 1;
          }
          else //Non-Space Detected
          {
              if (space_count &gt; 0) space_count = 0;
              strncat(buffer, p, 1);
          }
          p++;
      }
  
  pointer_array[index] = NULL; //Set end pointer to NULL for execlp().
  
  return 0;
  }
  
  
  
  /*--------------------------------MAIN()-----------------------------------*/
  int main(void)
  {
      char buffer[100];
      char *pointer_array[MAX_CHUNKS]; //Array which will hold string chunks
  
      fgets(buffer, sizeof(buffer), stdin); 
  
      break_down_string(buffer, pointer_array);
  
      if (fork() == 0)
      {
          printf("Child process!\n");
          execlp(pointer_array[0], (pointer_array+1), NULL);
      }
      else
      {
          printf("Parent process!\n");
      }
  
  return 0;
  }
  </code></pre>
  
  <p>Help would be greatly appreciated, I'm really stuck here!</p>