Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/pipe-manpages.c

raw
1 /* { dg-require-fork "" } */
2
3 /* Example of "pipe" from release 5.13 of the Linux man-pages project.
4
5 Copyright (C) 2005, 2008, Michael Kerrisk <[email protected]>
6 (A few fragments remain from an earlier (1992) version by
7 Drew Eckhardt <[email protected]>.)
8
9 Permission is granted to make and distribute verbatim copies of this
10 manual provided the copyright notice and this permission notice are
11 preserved on all copies.
12
13 Permission is granted to copy and distribute modified versions of this
14 manual under the conditions for verbatim copying, provided that the
15 entire resulting derived work is distributed under the terms of a
16 permission notice identical to this one.
17
18 Since the Linux kernel and libraries are constantly changing, this
19 manual page may be incorrect or out-of-date. The author(s) assume no
20 responsibility for errors or omissions, or for damages resulting from
21 the use of the information contained herein. The author(s) may not
22 have taken the same level of care in the production of this manual,
23 which is licensed free of charge, as they might when working
24 professionally.
25
26 Formatted or processed versions of this manual, if unaccompanied by
27 the source, must acknowledge the copyright and authors of this work.
28
29 */
30
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37
38 int
39 main(int argc, char *argv[])
40 {
41 int pipefd[2];
42 pid_t cpid;
43 char buf;
44
45 if (argc != 2) {
46 fprintf(stderr, "Usage: %s <string>\n", argv[0]);
47 exit(EXIT_FAILURE);
48 }
49
50 if (pipe(pipefd) == -1) {
51 perror("pipe");
52 exit(EXIT_FAILURE);
53 }
54
55 cpid = fork();
56 if (cpid == -1) {
57 perror("fork");
58 exit(EXIT_FAILURE);
59 }
60
61 if (cpid == 0) { /* Child reads from pipe */
62 close(pipefd[1]); /* Close unused write end */
63
64 while (read(pipefd[0], &buf, 1) > 0)
65 write(STDOUT_FILENO, &buf, 1);
66
67 write(STDOUT_FILENO, "\n", 1);
68 close(pipefd[0]);
69 _exit(EXIT_SUCCESS);
70
71 } else { /* Parent writes argv[1] to pipe */
72 close(pipefd[0]); /* Close unused read end */
73 write(pipefd[1], argv[1], strlen(argv[1]));
74 close(pipefd[1]); /* Reader will see EOF */
75 wait(NULL); /* Wait for child */
76 exit(EXIT_SUCCESS);
77 }
78 }