Introduce old redir program
[lcapit-junk-code.git] / books / apue / pipe.c
blobe44f5dd96ab3f6d5905bdce4a290e1de4710043c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
6 int main(int argc, char *argv[])
8 pid_t pid;
9 int err, n, fd[2];
10 char line[1024];
12 err = pipe(fd);
13 if (err) {
14 perror("pipe()");
15 exit(1);
18 pid = fork();
19 if (pid < 0) {
20 perror("fork()");
21 exit(1);
22 } else if (!pid) {
23 /* Child */
24 close(fd[1]);
25 memset(line, '\0', sizeof(line));
26 n = read(fd[0], line, sizeof(line));
27 write(STDOUT_FILENO, line, n);
28 } else {
29 /* Parent */
30 close(fd[0]);
31 write(fd[1], "hello world!\n", 13);
34 exit(0);