+ int so_size = 0, se_size = 0;
+ int so_fd[2], se_fd[2];
+ int pid, r, quit, i;
+ fd_set rset, rset2;
+ char buf[256];
+
+ if (stdoutput) *stdoutput = NULL;
+ if (stderror) *stderror = NULL;
+
+ if (verbose) {
+ printf ("%s", argv[0]);
+ for (i = 1; argv[i] != NULL; ++i)
+ printf (" %s", argv[i]);
+ printf ("\n");
+ }
+
+ if (pipe (so_fd) == -1 || pipe (se_fd) == -1) {
+ perror ("pipe");
+ return -1;
+ }
+
+ pid = fork ();
+ if (pid == -1) {
+ perror ("fork");
+ close (so_fd[0]);
+ close (so_fd[1]);
+ close (se_fd[0]);
+ close (se_fd[1]);
+ return -1;
+ }
+
+ if (pid == 0) { /* Child process. */
+ close (0);
+ close (so_fd[0]);
+ close (se_fd[0]);
+ dup2 (so_fd[1], 1);
+ dup2 (se_fd[1], 2);
+ close (so_fd[1]);
+ close (se_fd[1]);
+
+ execvp (argv[0], argv);
+ perror (argv[0]);
+ _exit (1);
+ }
+
+ /* Parent process. */
+ close (so_fd[1]);
+ close (se_fd[1]);
+
+ FD_ZERO (&rset);
+ FD_SET (so_fd[0], &rset);
+ FD_SET (se_fd[0], &rset);
+
+ quit = 0;
+ while (quit < 2) {
+ rset2 = rset;
+ r = select (MAX (so_fd[0], se_fd[0]) + 1, &rset2, NULL, NULL, NULL);
+ if (r == -1) {
+ perror ("select");
+ close (so_fd[0]);
+ close (se_fd[0]);
+ waitpid (pid, NULL, 0);
+ return -1;
+ }
+
+ if (FD_ISSET (so_fd[0], &rset2)) { /* something on stdout */
+ r = read (so_fd[0], buf, sizeof buf);
+ if (r == -1) {
+ perror ("read");
+ close (so_fd[0]);
+ close (se_fd[0]);
+ waitpid (pid, NULL, 0);
+ return -1;
+ }
+ if (r == 0) { FD_CLR (so_fd[0], &rset); quit++; }
+
+ if (r > 0 && stdoutput) {
+ so_size += r;
+ *stdoutput = realloc (*stdoutput, so_size);
+ if (*stdoutput == NULL) {
+ perror ("realloc");
+ *stdoutput = NULL;
+ continue;
+ }
+ memcpy (*stdoutput + so_size - r, buf, r);
+ }
+ }
+
+ if (FD_ISSET (se_fd[0], &rset2)) { /* something on stderr */
+ r = read (se_fd[0], buf, sizeof buf);
+ if (r == -1) {
+ perror ("read");
+ close (so_fd[0]);
+ close (se_fd[0]);
+ waitpid (pid, NULL, 0);
+ return -1;
+ }
+ if (r == 0) { FD_CLR (se_fd[0], &rset); quit++; }
+
+ if (r > 0 && stderror) {
+ se_size += r;
+ *stderror = realloc (*stderror, se_size);
+ if (*stderror == NULL) {
+ perror ("realloc");
+ *stderror = NULL;
+ continue;
+ }
+ memcpy (*stderror + se_size - r, buf, r);
+ }
+ }
+ }
+
+ close (so_fd[0]);
+ close (se_fd[0]);
+
+ /* Make sure the output buffers are \0-terminated. Also remove any
+ * trailing \n characters from the error buffer (not from stdout).
+ */
+ if (stdoutput) {
+ *stdoutput = realloc (*stdoutput, so_size+1);
+ if (*stdoutput == NULL) {
+ perror ("realloc");
+ *stdoutput = NULL;
+ } else
+ (*stdoutput)[so_size] = '\0';
+ }
+ if (stderror) {
+ *stderror = realloc (*stderror, se_size+1);
+ if (*stderror == NULL) {
+ perror ("realloc");
+ *stderror = NULL;
+ } else {
+ (*stderror)[se_size] = '\0';
+ se_size--;
+ while (se_size >= 0 && (*stderror)[se_size] == '\n')
+ (*stderror)[se_size--] = '\0';
+ }
+ }
+
+ /* Get the exit status of the command. */
+ waitpid (pid, &r, 0);
+
+ if (WIFEXITED (r)) {
+ if (WEXITSTATUS (r) == 0)
+ return 0;
+ else
+ return -1;
+ } else
+ return -1;