6 #include "stdio_impl.h"
9 extern char **__environ;
11 FILE *popen(const char *cmd, const char *mode)
16 posix_spawn_file_actions_t fa;
20 } else if (*mode == 'w') {
27 if (pipe2(p, O_CLOEXEC)) return NULL;
28 f = fdopen(p[op], mode);
30 __syscall(SYS_close, p[0]);
31 __syscall(SYS_close, p[1]);
37 if (!posix_spawn_file_actions_init(&fa)) {
38 if (!posix_spawn_file_actions_adddup2(&fa, p[1-op], 1-op)) {
39 if (!(e = posix_spawn(&pid, "/bin/sh", &fa, 0,
40 (char *[]){ "sh", "-c", (char *)cmd, 0 }, __environ))) {
41 posix_spawn_file_actions_destroy(&fa);
43 if (!strchr(mode, 'e'))
44 fcntl(p[op], F_SETFD, 0);
45 __syscall(SYS_close, p[1-op]);
50 posix_spawn_file_actions_destroy(&fa);
53 __syscall(SYS_close, p[1-op]);