+/*
+ * 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;
+
+FILE ** task_popen(const char *command, const char *mode) {
+ int inhandle [2];
+ int outhandle [2];
+ int errhandle [2];
+ int trypipe;
+
+ popen_t *data = 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 */
+ close(0), dup(inhandle [0]);
+ close(1), dup(outhandle[1]);
+ close(2), dup(errhandle[1]);
+
+ 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:
+
+ if (argv)
+ vec_free(argv);
+ return NULL;
+}
+
+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
+# define _WIN32_LEAN_AND_MEAN
+# define popen _popen
+# define pclose _pclose
+# include <windows.h>
+# include <io.h>
+# include <fcntl.h>
+ /*
+ * Bidirectional piping implementation for windows using CreatePipe and DuplicateHandle +
+ * other hacks.
+ */
+
+ typedef struct {
+ int __dummy;
+ /* TODO: implement */
+ } popen_t;
+
+ FILE **task_popen(const char *command, const char *mode) {
+ (void)command;
+ (void)mode;
+
+ /* TODO: implement */
+ return NULL;
+ }
+
+ void task_pclose(FILE **files) {
+ /* TODO: implement */
+ (void)files;
+ return;
+ }
+
+# ifdef __MINGW32__
+ /* mingw32 has dirent.h */
+# include <dirent.h>
+# elif defined (_WIN32)
+ /*
+ * visual studio lacks dirent.h it's a posix thing
+ * so we emulate it with the WinAPI.
+ */
+
+ struct dirent {
+ long d_ino;
+ unsigned short d_reclen;
+ unsigned short d_namlen;
+ char d_name[FILENAME_MAX];
+ };
+
+ typedef struct {
+ struct _finddata_t dd_dta;
+ struct dirent dd_dir;
+ long dd_handle;
+ int dd_stat;
+ char dd_name[1];
+ } DIR;
+
+ DIR *opendir(const char *name) {
+ DIR *dir = (DIR*)mem_a(sizeof(DIR) + strlen(name));
+ if (!dir)
+ return NULL;
+
+ strcpy(dir->dd_name, name);
+ return dir;
+ }
+
+ int closedir(DIR *dir) {
+ FindClose((HANDLE)dir->dd_handle);
+ mem_d ((void*)dir);
+ return 0;
+ }
+
+ struct dirent *readdir(DIR *dir) {
+ WIN32_FIND_DATA info;
+ struct dirent *data;
+ int rets;
+
+ if (!dir->dd_handle) {
+ char *dirname;
+ if (*dir->dd_name) {
+ size_t n = strlen(dir->dd_name);
+ if ((dirname = (char*)mem_a(n + 5) /* 4 + 1 */)) {
+ strcpy(dirname, dir->dd_name);
+ strcpy(dirname + n, "\\*.*"); /* 4 + 1 */
+ }
+ } else {
+ if (!(dirname = util_strdup("\\*.*")))
+ return NULL;
+ }
+
+ dir->dd_handle = (long)FindFirstFile(dirname, &info);
+ mem_d(dirname);
+ rets = !(!dir->dd_handle);
+ } else if (dir->dd_handle != -11) {
+ rets = FindNextFile ((HANDLE)dir->dd_handle, &info);
+ } else {
+ rets = 0;
+ }
+
+ if (!rets)
+ return NULL;
+
+ if ((data = (struct dirent*)mem_a(sizeof(struct dirent)))) {
+ strncpy(data->d_name, info.cFileName, FILENAME_MAX - 1);
+ data->d_name[FILENAME_MAX - 1] = '\0'; /* terminate */
+ data->d_namlen = strlen(data->d_name);
+ }
+ return data;
+ }
+
+ /*
+ * Visual studio also lacks S_ISDIR for sys/stat.h, so we emulate this as well
+ * which is not hard at all.
+ */
+# undef S_ISDIR /* undef just incase */
+# define S_ISDIR(X) ((X)&_S_IFDIR)
+# endif
+#endif
+