err: fix warn-unused-result warning in test (-O2)
[ccan] / ccan / err / test / run.c
1 #include <ccan/err/err.c>
2 #include <ccan/tap/tap.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10
11 #define BUFFER_MAX 1024
12
13 int main(int argc, char *argv[])
14 {
15         int pfd[2];
16         const char *base;
17
18         plan_tests(24);
19
20         err_set_progname(argv[0]);
21
22         /* In case it only prints out the basename of argv[0]. */
23         base = strrchr(argv[0], '/');
24         if (base)
25                 base++;
26         else
27                 base = argv[0];
28
29         /* Test err() in child */
30         if (pipe(pfd))
31                 abort();
32         fflush(stdout);
33         if (fork()) {
34                 char buffer[BUFFER_MAX+1];
35                 unsigned int i;
36                 int status;
37
38                 /* We are parent. */
39                 close(pfd[1]);
40                 for (i = 0; i < BUFFER_MAX; i++) {
41                         if (read(pfd[0], buffer + i, 1) == 0) {
42                                 buffer[i] = '\0';
43                                 ok1(strstr(buffer, "running err:"));
44                                 ok1(strstr(buffer, strerror(ENOENT)));
45                                 ok1(strstr(buffer, base));
46                                 ok1(buffer[i-1] == '\n');
47                                 break;
48                         }
49                 }
50                 close(pfd[0]);
51                 ok1(wait(&status) != -1);
52                 ok1(WIFEXITED(status));
53                 ok1(WEXITSTATUS(status) == 17);
54         } else {
55                 close(pfd[0]);
56                 dup2(pfd[1], STDERR_FILENO);
57                 errno = ENOENT;
58                 err(17, "running %s", "err");
59                 abort();
60         }
61
62         /* Test errx() in child */
63         if (pipe(pfd))
64                 abort();
65         fflush(stdout);
66         if (fork()) {
67                 char buffer[BUFFER_MAX+1];
68                 unsigned int i;
69                 int status;
70
71                 /* We are parent. */
72                 close(pfd[1]);
73                 for (i = 0; i < BUFFER_MAX; i++) {
74                         if (read(pfd[0], buffer + i, 1) == 0) {
75                                 buffer[i] = '\0';
76                                 ok1(strstr(buffer, "running errx\n"));
77                                 ok1(strstr(buffer, base));
78                                 break;
79                         }
80                 }
81                 close(pfd[0]);
82                 ok1(wait(&status) != -1);
83                 ok1(WIFEXITED(status));
84                 ok1(WEXITSTATUS(status) == 17);
85         } else {
86                 close(pfd[0]);
87                 dup2(pfd[1], STDERR_FILENO);
88                 errx(17, "running %s", "errx");
89                 abort();
90         }
91
92
93         /* Test warn() in child */
94         if (pipe(pfd))
95                 abort();
96         fflush(stdout);
97         if (fork()) {
98                 char buffer[BUFFER_MAX+1];
99                 unsigned int i;
100                 int status;
101
102                 /* We are parent. */
103                 close(pfd[1]);
104                 for (i = 0; i < BUFFER_MAX; i++) {
105                         if (read(pfd[0], buffer + i, 1) == 0) {
106                                 buffer[i] = '\0';
107                                 ok1(strstr(buffer, "running warn:"));
108                                 ok1(strstr(buffer, strerror(ENOENT)));
109                                 ok1(strstr(buffer, base));
110                                 ok1(buffer[i-1] == '\n');
111                                 break;
112                         }
113                 }
114                 close(pfd[0]);
115                 ok1(wait(&status) != -1);
116                 ok1(WIFEXITED(status));
117                 ok1(WEXITSTATUS(status) == 17);
118         } else {
119                 close(pfd[0]);
120                 dup2(pfd[1], STDERR_FILENO);
121                 errno = ENOENT;
122                 warn("running %s", "warn");
123                 exit(17);
124         }
125
126         /* Test warnx() in child */
127         if (pipe(pfd))
128                 abort();
129         fflush(stdout);
130         if (fork()) {
131                 char buffer[BUFFER_MAX+1];
132                 unsigned int i;
133                 int status;
134
135                 /* We are parent. */
136                 close(pfd[1]);
137                 for (i = 0; i < BUFFER_MAX; i++) {
138                         if (read(pfd[0], buffer + i, 1) == 0) {
139                                 buffer[i] = '\0';
140                                 ok1(strstr(buffer, "running warnx\n"));
141                                 ok1(strstr(buffer, base));
142                                 break;
143                         }
144                 }
145                 close(pfd[0]);
146                 ok1(wait(&status) != -1);
147                 ok1(WIFEXITED(status));
148                 ok1(WEXITSTATUS(status) == 17);
149         } else {
150                 close(pfd[0]);
151                 dup2(pfd[1], STDERR_FILENO);
152                 warnx("running %s", "warnx");
153                 exit(17);
154         }
155         return exit_status();
156 }
157