1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| void main(int argc, char ** argv, char ** envp)
{
int pid;
int pc[2]; /* Parent to child pipe */
int cp[2]; /* Child to parent pipe */
char ch;
int incount = 0, outcount = 0;
/* Make pipes */
if( pipe(pc) < 0)
{
perror("Can't make pipe");
exit(1);
}
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. */
dup( pc[0]); /* Make stdin come from read
end of pipe. */
close( pc[1]);
close( cp[0]);
execvp(argv[1], argv + 1, envp);
perror("No exec");
signal(getppid(), SIGQUIT);
exit(1);
default:
/* Parent. */
/* Close what we don't need. */
printf("Input to child:\n");
while( read(0, &ch, 1) > 0 )
{
write(pc[1],&ch, 1);
write(1, &ch, 1);
incount ++;
}
close(pc[1]);
printf("\nOutput from child:\n");
close(cp[1]);
while( read(cp[0], &ch, 1) == 1)
{
write(1, &ch, 1);
outcount++;
}
printf("\n\nTotal characters in: %d\n",incount);
printf("Total characters out: %d\n", outcount);
exit(0);
}
} |
Partager