summaryrefslogtreecommitdiff
path: root/libc/misc/popen.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc/misc/popen.c')
-rw-r--r--libc/misc/popen.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/libc/misc/popen.c b/libc/misc/popen.c
new file mode 100644
index 0000000..c848ca4
--- /dev/null
+++ b/libc/misc/popen.c
@@ -0,0 +1,42 @@
+
+#include <stdio.h>
+
+
+FILE * popen(command, rw)
+char * command;
+char * rw;
+{
+ int pipe_fd[2];
+ int pid, reading;
+
+ if( pipe(pipe_fd) < 0 ) return NULL;
+ reading = (rw[0] == 'r');
+
+ pid = vfork();
+ if( pid < 0 ) { close(pipe_fd[0]); close(pipe_fd[1]); return NULL; }
+ if( pid == 0 )
+ {
+ close(pipe_fd[!reading]);
+ close(reading);
+ if( pipe_fd[reading] != reading )
+ {
+ dup2(pipe_fd[reading], reading);
+ close(pipe_fd[reading]);
+ }
+
+ execl("/bin/sh", "sh", "-c", command, (char*)0);
+ _exit(255);
+ }
+
+ close(pipe_fd[reading]);
+ return fdopen(pipe_fd[!reading], rw);
+}
+
+int pclose(fd)
+FILE *fd;
+{
+ int waitstat;
+ if( fclose(fd) != 0 ) return EOF;
+ wait(&waitstat);
+}
+