-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipedemo.c
40 lines (33 loc) · 828 Bytes
/
pipedemo.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
39
40
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
int len, i, apipe[2];
char buf[BUFSIZ];
if( pipe(apipe) == -1 ) {
perror("could not make pipe");
exit(1);
}
printf("Got a pipe! It is file description : {%d %d}\n", apipe[0], apipe[1]);
while(fgets(buf, BUFSIZ, stdin)) {
len = strlen(buf);
if(write(apipe[1], buf, len) != len) {
perror("writing to pipe");
break;
}
for(i = 0; i < len; i++) {
buf[i] = 'X';
}
len = read(apipe[0], buf, BUFSIZ);
if(len == -1) {
perror("reading from pipe");
break;
}
if(write(1, buf, len) != len) {
perror("writing to stdout");
break;
}
}
}