add _DEFAULT_SOURCE wherever _BSD_SOURCE was used
[libc-test] / src / functional / spawn.c
1 #ifndef _XOPEN_SOURCE
2 #define _XOPEN_SOURCE 700
3 #endif
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <spawn.h>
10 #include <sys/wait.h>
11 #include "test.h"
12
13 #define TEST(f, x) (void)( \
14         (r = (f)) == (x) || \
15         t_error("%s failed, got %d want %d\n", #f, r, x) )
16
17 #define TEST_E(f) (void)( \
18         (errno = 0), (f) || \
19         t_error("%s failed (errno = %d \"%s\")\n", #f, errno, strerror(errno)) )
20
21 int main(void)
22 {
23         int r;
24         char foo[10];
25         int p[2];
26         pid_t pid;
27         int status;
28         posix_spawn_file_actions_t fa;
29
30         TEST_E(!pipe(p));
31         TEST(posix_spawn_file_actions_init(&fa), 0);
32         TEST(posix_spawn_file_actions_addclose(&fa, p[0]), 0);
33         TEST(posix_spawn_file_actions_adddup2(&fa, p[1], 1), 0);
34         TEST(posix_spawn_file_actions_addclose(&fa, p[1]), 0);
35         TEST(posix_spawnp(&pid, "echo", &fa, 0, (char *[]){"echo","hello",0}, 0), 0);
36         close(p[1]);
37         TEST(waitpid(pid, &status, 0), pid);
38         TEST(read(p[0], foo, sizeof foo), 6);
39         close(p[0]);
40         TEST(posix_spawn_file_actions_destroy(&fa), 0);
41         return t_status;
42 }