Use of popen in pipes in c
<p>i am taking the command like ls as input and executing the command using popen and storing the result in the buffer. However it is not printing all the contents of the command. Please help me.
PS It was able to work when the whole code was in main. I have tried gdb but i am not able to do the debugging.</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;



void process_command(char * command, char * buffer)
{
    int     fd[2], nbytes;
    pid_t   childpid;
    char    readbuffer[1025];
    FILE *fp = NULL;
    pipe(fd);


    if((childpid = fork()) == -1)
    {
            perror("fork");
            exit(1);
    }

    int b = 0;
    int status = 0;

    if(childpid == 0)
    {
            /* Child process closes up input side of pipe */
            close(fd[0]);
            fp = popen(command,"r");

            /* Send "string" through the output side of pipe */
          while((b = fread(readbuffer,1,1024,fp)) &gt; 0)  
               write(fd[1], readbuffer, b);

          status = pclose(fp);

     }

    else
    {
            /* Parent process closes up output side of pipe */
            close(fd[1]);
            waitpid(childpid,&amp;status,0);
            /* Read in a string from the pipe */
           do
          {
            nbytes = read(fd[0], buffer, sizeof(buffer));
          }while(nbytes == -1);
          buffer[nbytes] = '\0';  
    printf("Received string: %s", buffer);
    }


}


#define MAX 1024

int main(void)
{
  char command[MAX] ;
  char buffer[MAX];
  scanf("%s",command);
  process_command(command,buffer);

    return(0);
}  
</code></pre>