1570af302Sopenharmony_ci#ifndef _XOPEN_SOURCE
2570af302Sopenharmony_ci#define _XOPEN_SOURCE 700
3570af302Sopenharmony_ci#endif
4570af302Sopenharmony_ci#include <stdlib.h>
5570af302Sopenharmony_ci#include <unistd.h>
6570af302Sopenharmony_ci#include <stdio.h>
7570af302Sopenharmony_ci#include <errno.h>
8570af302Sopenharmony_ci#include <string.h>
9570af302Sopenharmony_ci#include <spawn.h>
10570af302Sopenharmony_ci#include <sys/wait.h>
11570af302Sopenharmony_ci#include "test.h"
12570af302Sopenharmony_ci
13570af302Sopenharmony_ci#define TEST(f, x) (void)( \
14570af302Sopenharmony_ci	(r = (f)) == (x) || \
15570af302Sopenharmony_ci	t_error("%s failed, got %d want %d\n", #f, r, x) )
16570af302Sopenharmony_ci
17570af302Sopenharmony_ci#define TEST_E(f) (void)( \
18570af302Sopenharmony_ci	(errno = 0), (f) || \
19570af302Sopenharmony_ci	t_error("%s failed (errno = %d \"%s\")\n", #f, errno, strerror(errno)) )
20570af302Sopenharmony_ci
21570af302Sopenharmony_ciint main(void)
22570af302Sopenharmony_ci{
23570af302Sopenharmony_ci	int r;
24570af302Sopenharmony_ci	char foo[10];
25570af302Sopenharmony_ci	int p[2];
26570af302Sopenharmony_ci	pid_t pid;
27570af302Sopenharmony_ci	int status;
28570af302Sopenharmony_ci	posix_spawn_file_actions_t fa;
29570af302Sopenharmony_ci
30570af302Sopenharmony_ci	TEST_E(!pipe(p));
31570af302Sopenharmony_ci	TEST(posix_spawn_file_actions_init(&fa), 0);
32570af302Sopenharmony_ci	TEST(posix_spawn_file_actions_addclose(&fa, p[0]), 0);
33570af302Sopenharmony_ci	TEST(posix_spawn_file_actions_adddup2(&fa, p[1], 1), 0);
34570af302Sopenharmony_ci	TEST(posix_spawn_file_actions_addclose(&fa, p[1]), 0);
35570af302Sopenharmony_ci	TEST(posix_spawnp(&pid, "echo", &fa, 0, (char *[]){"echo","hello",0}, 0), 0);
36570af302Sopenharmony_ci	close(p[1]);
37570af302Sopenharmony_ci	TEST(waitpid(pid, &status, 0), pid);
38570af302Sopenharmony_ci	TEST(read(p[0], foo, sizeof foo), 6);
39570af302Sopenharmony_ci	close(p[0]);
40570af302Sopenharmony_ci	TEST(posix_spawn_file_actions_destroy(&fa), 0);
41570af302Sopenharmony_ci	return t_status;
42570af302Sopenharmony_ci}
43