-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
38 lines (31 loc) · 826 Bytes
/
pipe.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define oops(m, x) {perror(m); exit(x);}
int main(int ac, char **av)
{
int thepipe[2], newfd, pid;
if ( ac != 3 ) {
fprintf(stderr, "useage: pipe cmd1 cmd2\n");
exit(1);
}
if( pipe(thepipe) == -1 )
oops("Can`t get a pipe", 1);
if( (pid = fork()) == -1 ) {
oops("Can`t fork", 2);
}
if( pid > 0 ) {
close(thepipe[1]);
if( dup2(thepipe[0], 0) == -1 )
oops("could not redirect stdin", 3);
close(thepipe[0]);
execlp(av[2], av[2], NULL);
oops(av[2], 4);
}
close(thepipe[0]);
if( dup2(thepipe[1], 1) == -1 )
oops("could not redirect stdout", 4);
close(thepipe[1]);
execlp(av[1], av[1], NULL);
oops(av[1], 5);
}