From eb08b8579355c6962e187f27e6c69a8e5e837d2c Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 7 Oct 2013 13:31:29 -0500 Subject: [PATCH] Forgot these files --- src/core/popenRWE.c | 93 ++++++++++++++++++++++++++++++++++++++++++ src/core/popenRWE.h | 7 ++++ 2 files changed, 100 insertions(+) create mode 100644 src/core/popenRWE.c create mode 100644 src/core/popenRWE.h --- /dev/null +++ b/src/core/popenRWE.c @@ -0,0 +1,93 @@ +/** + * Copyright 2009-2010 Bart Trojanowski + * Licensed under GPLv2, or later, at your choosing. + * + * bidirectional popen() call + * + * @param rwepipe - int array of size three + * @param exe - program to run + * @param argv - argument list + * @return pid or -1 on error + * + * The caller passes in an array of three integers (rwepipe), on successful + * execution it can then write to element 0 (stdin of exe), and read from + * element 1 (stdout) and 2 (stderr). + */ + +/* Modified by Stephen Paul Weber to take a path and pass it to sh -c */ + +#include +#include +#include + +#include "popenRWE.h" + +int popenRWE(int *rwepipe, const char *path) { + int in[2]; + int out[2]; + int err[2]; + int pid; + int rc; + const char *argv[4] = {"sh", "-c", NULL, NULL}; + argv[2] = path; + + rc = pipe(in); + if (rc<0) + goto error_in; + + rc = pipe(out); + if (rc<0) + goto error_out; + + rc = pipe(err); + if (rc<0) + goto error_err; + + pid = fork(); + if (pid > 0) { /* parent */ + close(in[0]); + close(out[1]); + close(err[1]); + rwepipe[0] = in[1]; + rwepipe[1] = out[0]; + rwepipe[2] = err[0]; + return pid; + } else if (pid == 0) { /* child */ + close(in[1]); + close(out[0]); + close(err[0]); + close(0); + dup(in[0]); + close(1); + dup(out[1]); + close(2); + dup(err[1]); + + execvp(argv[0], (char**)argv); + exit(1); + } else + goto error_fork; + + return pid; + +error_fork: + close(err[0]); + close(err[1]); +error_err: + close(out[0]); + close(out[1]); +error_out: + close(in[0]); + close(in[1]); +error_in: + return -1; +} + +int pcloseRWE(int pid, int *rwepipe) { + int rc, status; + close(rwepipe[0]); + close(rwepipe[1]); + close(rwepipe[2]); + rc = waitpid(pid, &status, 0); + return status; +} --- /dev/null +++ b/src/core/popenRWE.h @@ -0,0 +1,7 @@ +#ifndef __POPEN_RWE +#define __POPEN_RWE + +int popenRWE(int *rwepipe, const char *path); +int pcloseRWE(int pid, int *rwepipe); + +#endif