small fix for ccan/take/take.c
[ccan] / ccan / pipecmd / test / run-fdleak.c
1 #include <ccan/pipecmd/pipecmd.h>
2 /* Include the C files directly. */
3 #include <ccan/pipecmd/pipecmd.c>
4 #include <ccan/tap/tap.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8
9 int main(int argc, char *argv[])
10 {
11         pid_t child;
12         int outfd, status;
13         char buf[5] = "test";
14
15         /* We call ourselves, to test pipe. */
16         if (argc == 2) {
17                 if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf))
18                                 exit(1);
19                 exit(0);
20         }
21
22         /* This is how many tests you plan to run */
23         plan_tests(13);
24         child = pipecmd(&outfd, NULL, NULL, argv[0], "out", NULL);
25         if (!ok1(child > 0))
26                 exit(1);
27         ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
28         ok1(memcmp(buf, "test", sizeof(buf)) == 0);
29         ok1(waitpid(child, &status, 0) == child);
30         ok1(WIFEXITED(status));
31         ok1(WEXITSTATUS(status) == 0);
32
33         /* No leaks! */
34         ok1(close(outfd) == 0);
35         ok1(close(outfd) == -1 && errno == EBADF);
36         ok1(close(++outfd) == -1 && errno == EBADF);
37         ok1(close(++outfd) == -1 && errno == EBADF);
38         ok1(close(++outfd) == -1 && errno == EBADF);
39         ok1(close(++outfd) == -1 && errno == EBADF);
40         ok1(close(++outfd) == -1 && errno == EBADF);
41
42         /* This exits depending on whether all tests passed */
43         return exit_status();
44 }