Broken Pipes in a C program that simulates ls | sort | wc -l
<p>I need to create a C program to simulate this command using exec and unnamed pipes:</p>

<pre><code>ls | sort | wc -l
</code></pre>

<p>But how can i do this? I have only begin to study pipes and I have tried really bad</p>

<pre><code>int main( int argc, char** argv )
{
    int fd1[2], fd2[2],fd3[2], pid;

    createPipe(fd1);
    createPipe(fd2);
    createPipe(fd3);

    pid=babyMaker();

    if (pid == 0)
    {
    dup2(fd2[0],0);
    dup2(fd3[1],1);
    execlp("wc","wc","-l",NULL);
    }

    pid=babyMaker();

    if (pid == 0)
    {   
    dup2(fd2[1], 1);
    dup2(fd1[0], STDIN_FILENO);
    execlp("sort", "sort", NULL);
    }

pid=babyMaker();

if (pid == 0)
{
    dup2(fd1[1], 1);
    execlp("ls", "ls", "-la", NULL);
}

char string[BUFFER_SIZE];
int bytesReaded=read(fd3[0],string,BUFFER_SIZE);
string[bytesReaded-1]=0;
printf("%s\n",string);

wait(NULL);

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

<p>EDIT: Added my code</p>