| 1 |
From eb08b8579355c6962e187f27e6c69a8e5e837d2c Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephen Paul Weber <[email protected]> |
| 3 |
Date: Mon, 7 Oct 2013 13:31:29 -0500 |
| 4 |
Subject: [PATCH] Forgot these files |
| 5 |
|
| 6 |
--- |
| 7 |
src/core/popenRWE.c | 93 ++++++++++++++++++++++++++++++++++++++++++ |
| 8 |
src/core/popenRWE.h | 7 ++++ |
| 9 |
2 files changed, 100 insertions(+) |
| 10 |
create mode 100644 src/core/popenRWE.c |
| 11 |
create mode 100644 src/core/popenRWE.h |
| 12 |
|
| 13 |
--- /dev/null |
| 14 |
+++ b/src/core/popenRWE.c |
| 15 |
@@ -0,0 +1,93 @@ |
| 16 |
+/** |
| 17 |
+ * Copyright 2009-2010 Bart Trojanowski <[email protected]> |
| 18 |
+ * Licensed under GPLv2, or later, at your choosing. |
| 19 |
+ * |
| 20 |
+ * bidirectional popen() call |
| 21 |
+ * |
| 22 |
+ * @param rwepipe - int array of size three |
| 23 |
+ * @param exe - program to run |
| 24 |
+ * @param argv - argument list |
| 25 |
+ * @return pid or -1 on error |
| 26 |
+ * |
| 27 |
+ * The caller passes in an array of three integers (rwepipe), on successful |
| 28 |
+ * execution it can then write to element 0 (stdin of exe), and read from |
| 29 |
+ * element 1 (stdout) and 2 (stderr). |
| 30 |
+ */ |
| 31 |
+ |
| 32 |
+/* Modified by Stephen Paul Weber to take a path and pass it to sh -c */ |
| 33 |
+ |
| 34 |
+#include <unistd.h> |
| 35 |
+#include <stdlib.h> |
| 36 |
+#include <sys/wait.h> |
| 37 |
+ |
| 38 |
+#include "popenRWE.h" |
| 39 |
+ |
| 40 |
+int popenRWE(int *rwepipe, const char *path) { |
| 41 |
+ int in[2]; |
| 42 |
+ int out[2]; |
| 43 |
+ int err[2]; |
| 44 |
+ int pid; |
| 45 |
+ int rc; |
| 46 |
+ const char *argv[4] = {"sh", "-c", NULL, NULL}; |
| 47 |
+ argv[2] = path; |
| 48 |
+ |
| 49 |
+ rc = pipe(in); |
| 50 |
+ if (rc<0) |
| 51 |
+ goto error_in; |
| 52 |
+ |
| 53 |
+ rc = pipe(out); |
| 54 |
+ if (rc<0) |
| 55 |
+ goto error_out; |
| 56 |
+ |
| 57 |
+ rc = pipe(err); |
| 58 |
+ if (rc<0) |
| 59 |
+ goto error_err; |
| 60 |
+ |
| 61 |
+ pid = fork(); |
| 62 |
+ if (pid > 0) { /* parent */ |
| 63 |
+ close(in[0]); |
| 64 |
+ close(out[1]); |
| 65 |
+ close(err[1]); |
| 66 |
+ rwepipe[0] = in[1]; |
| 67 |
+ rwepipe[1] = out[0]; |
| 68 |
+ rwepipe[2] = err[0]; |
| 69 |
+ return pid; |
| 70 |
+ } else if (pid == 0) { /* child */ |
| 71 |
+ close(in[1]); |
| 72 |
+ close(out[0]); |
| 73 |
+ close(err[0]); |
| 74 |
+ close(0); |
| 75 |
+ dup(in[0]); |
| 76 |
+ close(1); |
| 77 |
+ dup(out[1]); |
| 78 |
+ close(2); |
| 79 |
+ dup(err[1]); |
| 80 |
+ |
| 81 |
+ execvp(argv[0], (char**)argv); |
| 82 |
+ exit(1); |
| 83 |
+ } else |
| 84 |
+ goto error_fork; |
| 85 |
+ |
| 86 |
+ return pid; |
| 87 |
+ |
| 88 |
+error_fork: |
| 89 |
+ close(err[0]); |
| 90 |
+ close(err[1]); |
| 91 |
+error_err: |
| 92 |
+ close(out[0]); |
| 93 |
+ close(out[1]); |
| 94 |
+error_out: |
| 95 |
+ close(in[0]); |
| 96 |
+ close(in[1]); |
| 97 |
+error_in: |
| 98 |
+ return -1; |
| 99 |
+} |
| 100 |
+ |
| 101 |
+int pcloseRWE(int pid, int *rwepipe) { |
| 102 |
+ int rc, status; |
| 103 |
+ close(rwepipe[0]); |
| 104 |
+ close(rwepipe[1]); |
| 105 |
+ close(rwepipe[2]); |
| 106 |
+ rc = waitpid(pid, &status, 0); |
| 107 |
+ return status; |
| 108 |
+} |
| 109 |
--- /dev/null |
| 110 |
+++ b/src/core/popenRWE.h |
| 111 |
@@ -0,0 +1,7 @@ |
| 112 |
+#ifndef __POPEN_RWE |
| 113 |
+#define __POPEN_RWE |
| 114 |
+ |
| 115 |
+int popenRWE(int *rwepipe, const char *path); |
| 116 |
+int pcloseRWE(int pid, int *rwepipe); |
| 117 |
+ |
| 118 |
+#endif |