This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26
27 #include "macros.h"
28
29 int
30 main (int argc, char **argv)
31 {
32 size_t len;
33 char *cmd;
34 int i;
35
36
37 if (argc > 1)
38 {
39 if (*argv[1] == 'r')
40 ASSERT (putchar ('c') == 'c');
41 else
42 ASSERT (getchar () == 'p');
43
44 return 42;
45 }
46
47
48
49 len = strlen (argv[0]);
50 cmd = malloc (len + 3);
51 ASSERT (cmd);
52
53 strcpy (cmd, argv[0]);
54 cmd[len] = ' ';
55 cmd[len + 2] = '\0';
56 for (i = 0; i < 2; i++)
57 {
58 FILE *child;
59 int status;
60
61 if (i)
62 {
63 ASSERT (fclose (stdin) == 0);
64 ASSERT (fclose (stdout) == 0);
65 }
66
67 cmd[len + 1] = 'r';
68 ASSERT (child = popen (cmd, "r"));
69 ASSERT (fgetc (child) == 'c');
70 status = pclose (child);
71 ASSERT (WIFEXITED (status));
72 ASSERT (WEXITSTATUS (status) == 42);
73 if (i)
74 {
75 ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
76 ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
77 }
78
79 cmd[len + 1] = 'w';
80 ASSERT (child = popen (cmd, "w"));
81 ASSERT (fputc ('p', child) == 'p');
82 status = pclose (child);
83 ASSERT (WIFEXITED (status));
84 ASSERT (WEXITSTATUS (status) == 42);
85 if (i)
86 {
87 ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
88 ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
89 }
90 }
91 free (cmd);
92 return 0;
93 }