summaryrefslogtreecommitdiff
path: root/popen3/popen3.c
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2012-09-06 14:51:09 +0200
committerTobias Klauser <tklauser@distanz.ch>2012-09-06 14:51:09 +0200
commitf81e1ad6d8e7ebaa52e98b274a4012cf8fa44862 (patch)
tree4d3383361f2832a9bc832ef8063339222596d28c /popen3/popen3.c
parentfe9fc3eaaa31ed3fd7cc7db47dfb70dcc6166969 (diff)
Add popen3 based on popenRWE (http://www.jukie.net/bart/blog/popenRWE)HEADmaster
Diffstat (limited to 'popen3/popen3.c')
-rw-r--r--popen3/popen3.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/popen3/popen3.c b/popen3/popen3.c
new file mode 100644
index 0000000..121ee95
--- /dev/null
+++ b/popen3/popen3.c
@@ -0,0 +1,106 @@
+/*
+ * popen() for stdin, stdout and stderr.
+ *
+ * Based on popenRWE() by Bart Trojanowski.
+ *
+ * Copyright (C) 2012 Tobias Klauser <tklauser@distanz.ch>
+ * Copyright (C) 2009-2010 Bart Trojanowski <bart@jukie.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/**
+ * Pipe stream to an application (like popen() but for std{in,out,err}).
+ *
+ * After successful return:
+ * - write to pipes[0] will go to stdin of the child
+ * - read from pipes[1] to get stdout of the child
+ * - read from pipes[2] to get stderr of the child
+ *
+ * @param cmd external command to execute
+ * @param pipes int array of three to write FDs for std{in,out,err} to
+ * @return PID of the executed application on success, -1 otherwise
+ */
+pid_t popen3(const char *cmd, int *pipes)
+{
+ int in[2], out[2], err[2];
+ pid_t pid;
+ int ret;
+
+ /* Open pipes to stdin, stdout, stderr */
+ ret = pipe(in);
+ if (ret < 0)
+ goto err_in;
+
+ ret = pipe(out);
+ if (ret < 0)
+ goto err_out;
+
+ ret = pipe(err);
+ if (ret < 0)
+ goto err_err;
+
+ pid = fork();
+ if (pid > 0) { /* parent */
+ close(in[0]);
+ close(out[1]);
+ close(err[1]);
+ pipes[0] = in[1];
+ pipes[1] = out[0];
+ pipes[2] = err[0];
+ return pid;
+ } else if (pid == 0) { /* child */
+ close(in[1]);
+ close(out[0]);
+ close(err[0]);
+ dup2(in[0], 0);
+ dup2(out[1], 1);
+ dup2(err[1], 2);
+
+ /* just emulate what popen() does */
+ execl("/bin/sh", "sh", "-c", cmd, NULL);
+ exit(EXIT_FAILURE);
+ } else
+ goto err_fork;
+
+ return pid;
+
+err_fork:
+ close(err[0]);
+ close(err[1]);
+err_err:
+ close(out[0]);
+ close(out[1]);
+err_out:
+ close(in[0]);
+ close(in[1]);
+err_in:
+ return ret;
+}
+
+/**
+ * Close pipes previously opened with popen3.
+ *
+ * @param pid
+ * @param pipes
+ * @return
+ */
+int pclose3(pid_t pid, int *pipes)
+{
+ int ret, status;
+ close(pipes[0]);
+ close(pipes[1]);
+ close(pipes[2]);
+ ret = waitpid(pid, &status, 0);
+ if (ret == 0)
+ return status;
+ else
+ return ret;
+}
include/dt-bindings/reset?id=efdf5aa8f175c18def6407bb2b51643e4da88259'>ARM: STi: DT: Move reset controller constants into common locationPhilipp Zabel3-0/+139 2015-07-16clk: tegra: Add DFLL DVCO reset control for Tegra124Paul Walmsley1-0/+12 2015-05-30clk: qcom: Add support for NSS/GMAC clocks and resetsStephen Boyd1-0/+43 2015-03-23dt-bindings: Add #defines for MSM8916 clocks and resetsGeorgi Djakov1-0/+108 2014-07-15clk: qcom: Add support for APQ8064 multimedia clocksStephen Boyd1-0/+8 2014-07-15clk: qcom: Add support for IPQ8064's global clock controller (GCC)Kumar Gala1-0/+132 2014-07-15clk: qcom: Add APQ8084 Multimedia Clock Controller (MMCC) supportGeorgi Djakov1-0/+64 2014-07-11clk: qcom: Add APQ8084 Global Clock Controller supportGeorgi Djakov1-0/+109 2014-07-11clk: qcom: Fully support apq8064 global clock controlStephen Boyd1-0/+16 2014-06-07Merge tag 'clk-for-linus-3.16' of git://git.linaro.org/people/mike.turquette/...Linus Torvalds1-1/+1 2014-05-05ARM: socfpga: dts: add reset-controllerSteffen Trumtrar1-0/+90 2014-04-30clk: qcom: Various fixes for MSM8960's global clock controllerKumar Gala1-1/+1 2014-01-16clk: qcom: Add support for MSM8660's global clock controller (GCC)Stephen Boyd1-0/+134 2014-01-16clk: qcom: Add support for MSM8974's multimedia clock controller (MMCC)Stephen Boyd1-0/+62 2014-01-16clk: qcom: Add support for MSM8974's global clock controller (GCC)Stephen Boyd1-0/+96 2014-01-16clk: qcom: Add support for MSM8960's multimedia clock controller (MMCC)Stephen Boyd1-0/+93 2014-01-16clk: qcom: Add support for MSM8960's global clock controller (GCC)Stephen Boyd1-0/+118