#include #include int main(int argc, char ** argv, char ** envp) { int pid; int cp[2]; /* Child to parent pipe */ char ch; int incount = 0, outcount = 0; if( pipe(cp) < 0) { perror("Can't make pipe"); exit(1); } /* Create a child to run command. */ switch( pid = fork() ) { case -1: perror("Can't fork"); exit(1); case 0: /* Child. */ close(1); /* Close current stdout. */ dup( cp[1]); /* Make stdout go to write end of pipe. */ close(0); /* Close current stdin. */ close( cp[0]); execvp(argv[1], argv + 1); perror("No exec"); exit(1); default: /* Parent. */ printf("\nOutput from child:\n"); close(cp[1]); while( read(cp[0], &ch, 1) == 1) { write(1, &ch, 1); outcount++; } exit(0); } exit(0); }