title: simple shell using execlp goes weirdly
body: <p>I am reading <strong>GNU/Linux application programming the 2nd edition</strong>,you can reach what am reading from <a href="http://books.google.com/books?id=KjEq9Mua5TQC&amp;pg=PA197&amp;lpg=PA197&amp;dq=simpshell.c&amp;source=bl&amp;ots=1TaleVDIpV&amp;sig=u5GVnd1xU4t3SCM7qtp6O1K620c&amp;hl=en&amp;ei=HKheTM-lDYqQuAPPrdiZDA&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=1&amp;ved=0CBIQ6AEwAA#v=onepage&amp;q=simpshell.c&amp;f=false" rel="nofollow noreferrer">here</a>.After I write the code similar to his,but it work strangely:</p>
  
  <pre><code>$ ./shell
  ./shell&gt;&gt;quit
  $ ./shell
  ./shell&gt;&gt;date
  Sun Aug  8 21:19:37 CST 2010
  ./shell&gt;&gt;quit
  $ ./shell
  ./shell&gt;&gt;abc
  execlp failed: No such file or directory
  ./shell&gt;&gt;quit
  ./shell&gt;&gt;quit
  $./shell 
  ./shell&gt;&gt;abcd execlp
  execlp failed: No such file or directory
  ./shell&gt;&gt;quit 
  ./shell&gt;&gt;quit
  </code></pre>
  
  <p>The first and second cases are ok,but the third and forth ones somewhat need two <strong>quit</strong> to quit.This is not what i am expecting.I guess something is wrong with <strong>fork()</strong>,or the <strong>waitpid()</strong>,but this still got unsolved after asking a few people around me.Now is summer time, i.e. summer holiday,I got no more mates to go for.Thanks always.</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  #define LINE_LEN 80
  
  int main(int argc,char* argv[])
  {
  
   pid_t pid;
   char cmd[LINE_LEN+1]={'\0'};
  
   while(1)
   {
    printf("%s&gt;&gt;",argv[0]);
    if(fgets(cmd,sizeof(cmd),stdin)==NULL)
    {
     perror("fgets failed");
     break;
    }
    cmd[strlen(cmd)-1]='\0';
    if(strncmp(cmd,"quit",4)==0)
    {
     break;
    }
    if((pid=fork())==-1)
    {
     perror("fork failed");
     break;
    }else if(pid==0)
    {
     //TODO no option can be specified for cmd
     execlp(cmd,cmd,NULL);
     perror("execlp failed");
    }else
    {
     waitpid(pid,NULL,0);
    }
   }
   return 0;
  }
  </code></pre>