From c2df77a517e8651a07e323aaa18c6caa2d77eb1b Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 9 Feb 2019 16:57:14 -0500 Subject: merge POSIX.2 identifiers --- src/stdio/popen.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/stdio/popen.c (limited to 'src/stdio/popen.c') diff --git a/src/stdio/popen.c b/src/stdio/popen.c new file mode 100644 index 00000000..cd1a6572 --- /dev/null +++ b/src/stdio/popen.c @@ -0,0 +1,62 @@ +#include +#include "string.h" +#include "errno.h" +#include "unistd.h" +#include "wchar.h" +#include "__nonstd.h" + +FILE * popen(const char * command, const char * mode) +{ + __ASSERT_NONNULL(command); + __ASSERT_NONNULL(mode); + + int direction = 0; + if (!strcmp(mode, "w")) { + direction = 1; + } else if (strcmp(mode, "r")) { + errno = EINVAL; + return NULL; + } + + int pd[2]; + if (pipe(pd) == -1) { + return NULL; + } + + pid_t child = fork(); + if (child == -1) { + close(pd[0]); + close(pd[1]); + return NULL; + } else if (child == 0) { + if (direction == 1) { + dup2(pd[0], STDIN_FILENO); + } else { + dup2(pd[1], STDOUT_FILENO); + } + close(pd[0]); + close(pd[1]); + execl("/bin/sh", "sh", "-c", command, (char *)0); + return NULL; + } + + FILE *p = NULL; + if (direction == 1) { + p = fdopen(pd[1], "w"); + } else { + p = fdopen(pd[0], "r"); + } + + if (p == NULL) { + return NULL; + } + + fwide(p, -1); + p->pipe_pid = pid; + return p; +} + +/* +POSIX(2) +*/ + -- cgit v1.2.1