+/*
+ * TODO: Windows version
+ * this implements a unique bi-directional popen-like function that
+ * allows reading data from both stdout and stderr. And writing to
+ * stdin :)
+ *
+ * Example of use:
+ * FILE *handles[3] = task_popen("ls", "-l", "r");
+ * if (!handles) { perror("failed to open stdin/stdout/stderr to ls");
+ * // handles[0] = stdin
+ * // handles[1] = stdout
+ * // handles[2] = stderr
+ *
+ * task_pclose(handles); // to close
+ */
+#ifndef _WIN32
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <dirent.h>
+#include <unistd.h>
+typedef struct {
+ FILE *handles[3];
+ int pipes [3];
+
+ int stderr_fd;
+ int stdout_fd;
+ int pid;
+} popen_t;
+
+static FILE ** task_popen(const char *command, const char *mode) {
+ int inhandle [2];
+ int outhandle [2];
+ int errhandle [2];
+ int trypipe;
+
+ popen_t *data = (popen_t*)mem_a(sizeof(popen_t));
+
+ /*
+ * Parse the command now into a list for execv, this is a pain
+ * in the ass.
+ */
+ char *line = (char*)command;
+ char **argv = NULL;
+ {
+
+ while (*line != '\0') {
+ while (*line == ' ' || *line == '\t' || *line == '\n')
+ *line++ = '\0';
+ vec_push(argv, line);
+
+ while (*line != '\0' && *line != ' ' &&
+ *line != '\t' && *line != '\n') line++;
+ }
+ vec_push(argv, '\0');
+ }
+
+
+ if ((trypipe = pipe(inhandle)) < 0) goto task_popen_error_0;
+ if ((trypipe = pipe(outhandle)) < 0) goto task_popen_error_1;
+ if ((trypipe = pipe(errhandle)) < 0) goto task_popen_error_2;
+
+ if ((data->pid = fork()) > 0) {
+ /* parent */
+ close(inhandle [0]);
+ close(outhandle [1]);
+ close(errhandle [1]);
+
+ data->pipes [0] = inhandle [1];
+ data->pipes [1] = outhandle[0];
+ data->pipes [2] = errhandle[0];
+
+ data->handles[0] = fdopen(inhandle [1], "w");
+ data->handles[1] = fdopen(outhandle[0], mode);
+ data->handles[2] = fdopen(errhandle[0], mode);
+
+ /* sigh */
+ if (argv)
+ vec_free(argv);
+ return data->handles;
+ } else if (data->pid == 0) {
+ /* child */
+ close(inhandle [1]);
+ close(outhandle[0]);
+ close(errhandle[0]);
+
+ /* see piping documentation for this sillyness :P */
+ dup2(inhandle [0], 0);
+ dup2(outhandle[1], 1);
+ dup2(errhandle[1], 2);
+
+ execvp(*argv, argv);
+ exit(EXIT_FAILURE);
+ } else {
+ /* fork failed */
+ goto task_popen_error_3;
+ }
+
+task_popen_error_3: close(errhandle[0]), close(errhandle[1]);
+task_popen_error_2: close(outhandle[0]), close(outhandle[1]);
+task_popen_error_1: close(inhandle [0]), close(inhandle [1]);
+task_popen_error_0:
+
+ vec_free(argv);
+ return NULL;
+}
+
+static int task_pclose(FILE **handles) {
+ popen_t *data = (popen_t*)handles;
+ int status = 0;
+
+ close(data->pipes[0]); /* stdin */
+ close(data->pipes[1]); /* stdout */
+ close(data->pipes[2]); /* stderr */
+
+ waitpid(data->pid, &status, 0);
+
+ mem_d(data);
+
+ return status;
+}
+#else
+ typedef struct {
+ FILE *handles[3];
+ char name_err[L_tmpnam];
+ char name_out[L_tmpnam];
+ } popen_t;
+
+ static FILE **task_popen(const char *command, const char *mode) {
+ char *cmd = NULL;
+ popen_t *open = (popen_t*)mem_a(sizeof(popen_t));
+
+ tmpnam(open->name_err);
+ tmpnam(open->name_out);
+
+ (void)mode; /* excluded */
+
+ util_asprintf(&cmd, "%s -redirout=%s -redirerr=%s", command, open->name_out, open->name_err);
+
+ system(cmd); /* HACK */
+ open->handles[0] = NULL;
+ open->handles[1] = fs_file_open(open->name_out, "r");
+ open->handles[2] = fs_file_open(open->name_err, "r");
+
+ mem_d(cmd);
+
+ return open->handles;
+ }
+
+ static void task_pclose(FILE **files) {
+ popen_t *open = ((popen_t*)files);
+ fs_file_close(files[1]);
+ fs_file_close(files[2]);
+ remove(open->name_err);
+ remove(open->name_out);
+
+ mem_d(open);
+ }
+#endif /*! _WIN32 */