tools/configurator: fix compile error with -O2
[ccan] / ccan / read_write_all / test / run-write_all.c
1 #include <ccan/read_write_all/read_write_all.h>
2 #include <ccan/tap/tap.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <signal.h>
6 #include <limits.h>
7 #include <sys/wait.h>
8 #include <err.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12
13 static ssize_t test_write(int fd, const void *buf, size_t count);
14 #define write test_write
15 #include <ccan/read_write_all/read_write_all.c>
16 #undef write
17
18 static ssize_t write_return;
19
20 static ssize_t test_write(int fd, const void *buf, size_t count)
21 {
22         (void)fd;
23         (void)buf;
24
25         if (write_return == 0) {
26                 errno = ENOSPC;
27                 return 0;
28         }
29
30         if (write_return < 0) {
31                 errno = -write_return;
32                 /* Don't return EINTR more than once! */
33                 if (errno == EINTR)
34                         write_return = count;
35                 return -1;
36         }
37
38         if (write_return < count)
39                 return write_return;
40         return count;
41 }
42
43 #define BUFSZ 1024
44
45 int main(void)
46 {
47         char *buffer;
48
49         buffer = malloc(BUFSZ);
50         plan_tests(8);
51
52         write_return = -ENOSPC;
53         ok1(!write_all(100, buffer, BUFSZ));
54         ok1(errno == ENOSPC);
55
56         write_return = -EINTR;
57         ok1(write_all(100, buffer, BUFSZ));
58         ok1(errno == EINTR);
59
60         write_return = 1;
61         errno = 0;
62         ok1(write_all(100, buffer, BUFSZ));
63         ok1(errno == 0);
64
65         write_return = BUFSZ;
66         ok1(write_all(100, buffer, BUFSZ));
67         ok1(errno == 0);
68         free(buffer);
69
70         return exit_status();
71 }