how to wait until a child blocks?



Hi,

the following few lines are doing what I want, i.e.
printing a prompt _before_ waiting for new input, but
_after_ the content of "someFile" has been printed:

#include <unistd.h>
#include <malloc.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
int main(void) {
char *line = 0;
size_t n = 0;
FILE *pp = popen("cat someFile -", "w");
timespec ts = {0, 10000000};
nanosleep(&ts, 0);
while(true) {
fprintf(stderr, "> ");
fflush(stderr);
getline (&line, &n, stdin);
if(!strcmp(line, "END\n")) {
break;
}
fprintf(pp, line);
}
fflush(stdout);
pclose(pp);
free(line);
}

E.g., with someFile containing:

Hi
Bye

I get:

$./a.out
Hi
Bye
> END

END was typed in to end the program.

But when I remove the clumsy nanosleep hack, I get

$./a.out
> Hi
Bye
END

How could this be achieved in a clean way? I would need to somehow
wait until the process being created by popen() blocks as it waits
for additional input being typed in. How could this be done?

Thanks for any help,

Christof
.



Relevant Pages