C reading all data from pipe
<p>My program creates child process and sets pipes to communicate with it. The problem occurs when i try to read data from the pipe. Since child process has ended (i use <code>wait</code> to ensure that) EOF should be on the end of the data stream thus ending the read (As in the man page for <code>pipe</code>). But instead <code>read</code> just freezes and waits for more data to come.
What am i missing here?</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;



void setfd(int *in, int *out) {
    dup2(out[1], 1);
    dup2(in[0], 0);
}

int main(int argc, char *argv[]) {
    int status;
    int pipe2ch[2], pipe2pr[2];
    char *newargv[] = {NULL, NULL};
    newargv[0] = argv[1];

    pipe(pipe2ch);
    pipe(pipe2pr);

    setfd(pipe2pr, pipe2ch);
    int a;
    if (!(a = fork())) {
        setfd(pipe2ch, pipe2pr);
        execve(newargv[0], newargv, NULL);
        exit(1);
    } else {
        printf("hello!\n");
        fflush(stdout);

        char str;

        wait(&amp;status);
        while (read(pipe2pr[0], &amp;str, 1) &gt; 0) {
            fprintf(stderr, "%c", str);
        }
        exit(0);
    }
}
</code></pre>