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]);
36 if (!posix_spawn_file_actions_init(&fa)) {
37 for (FILE *l = *__ofl_lock(); l; l=l->next)
38 if (l->pipe_pid && posix_spawn_file_actions_addclose(&fa, l->fd))
40 if (!posix_spawn_file_actions_adddup2(&fa, p[1-op], 1-op)) {
41 if (!(e = posix_spawn(&pid, "/bin/sh", &fa, 0,
42 (char *[]){ "sh", "-c", (char *)cmd, 0 }, __environ))) {
43 posix_spawn_file_actions_destroy(&fa);
45 if (!strchr(mode, 'e'))
46 fcntl(p[op], F_SETFD, 0);
47 __syscall(SYS_close, p[1-op]);
54 posix_spawn_file_actions_destroy(&fa);
57 __syscall(SYS_close, p[1-op]);