nvmd... got it sorta.
Code:
// create the pipes
int fds_poci[2], fds_pico[2];
if (0 != pipe(fds_poci) || 0 != pipe(fds_pico)) return 1;
// create the child
if (0 == fork()) {
// in the child process
dup2(fds_poci[0], 0); // copy reading end to stdin
dup2(fds_pico[1], 1); // copy writing end to stdout
// close the pipes as they're now redundant
close(fds_poci[0]);
close(fds_poci[1]);
close(fds_pico[0]);
close(fds_pico[1]);
execl("/bin/ps", "ps", "-U root", NULL);
return 2; // hopefully never gets here
}
// in the parent process
close(fds_poci[0]); // close reading end
close(fds_pico[1]); // close writing end
// read from child and write to stdout
int n;
char buffer[2048];
while (0 < (n = read(fds_pico[0], buffer, 2048))) {
write(1, buffer, n);
}
close(fds_poci[1]);
close(fds_pico[0]);
__________________