}
+ input = skip_whitespace (input);
+
if (pid == 0) {
- char *name = skip_whitespace (input), *space, *slash;
+ char *name = input, *space = 0, *slash;
if ((space = strchr (name, ' '))) {
name = xstrndup (name, space - name);
close (pipefd[0]);
close (pipefd[1]);
- execl (name, (slash = strrchr (name, '/')) ? (slash + 1) : name, skip_whitespace (space), NULL);
+ execlp (name, (slash = strrchr (name, '/')) ? (slash + 1) : name, space ? skip_whitespace (space) : NULL, NULL);
fprintf (stderr, "%s: %s:%lu: %s: %s\n", program_name, filename, line_no, name, strerror (errno));
exit (EXIT_FAILURE);
if (status) {
- fprintf (stderr, "%s\n", lbuf.start);
+ char *p = (lbuf.start = skip_whitespace (lbuf.start));
+ if (*p != '\0') { fprintf (stderr, "%s\n", p); }
+
exit (EXIT_FAILURE);
}
int pipefd[2], pid, status;
+ struct linebuf lbuf;
+ FILE *cmd_output;
+
if (pipe (pipefd) < 0) {
perror ("pipe");
}
+ input = skip_whitespace (input);
+
if (pid == 0) {
- char *name = skip_whitespace (input), *space, *slash;
+ char *name = input, *space = 0, *slash;
if ((space = strchr (name, ' '))) {
name = xstrndup (name, space - name);
}
- execl (name, (slash = strrchr (name, '/')) ? (slash + 1) : name, skip_whitespace (space), NULL);
+ dup2 (pipefd[1], STDOUT_FILENO);
+
+ close (pipefd[0]);
+ close (pipefd[1]);
- fprintf (stderr, "%s: %s:%lu: %s: %s\n", program_name, filename, line_no, name, strerror (errno));
+ execlp (name, (slash = strrchr (name, '/')) ? (slash + 1) : name, space ? skip_whitespace (space) : NULL, NULL);
exit (EXIT_FAILURE);
}
+ close (pipefd[1]);
+
+ if (!(cmd_output = fdopen (pipefd[0], "r"))) {
+
+ perror ("fdopen");
+ exit (EXIT_FAILURE);
+
+ }
+
+ lbuf.start = xmalloc (lbuf.size = 256);
+
+ {
+
+ char *end = lbuf.start + lbuf.size;
+ char *p = lbuf.start;
+
+ while (fgets (p, end - p, cmd_output)) {
+
+ size_t offset;
+ p += strlen (p);
+
+ offset = p - lbuf.start;
+
+ if (end - p >= 80) {
+ continue;
+ }
+
+ lbuf.size *= 2;
+ lbuf.start = xrealloc (lbuf.start, lbuf.size);
+
+ p = lbuf.start + offset;
+ end = lbuf.start + lbuf.size;
+
+ }
+
+ }
+
if (waitpid (pid, &status, 0) == -1) {
perror ("waitpid");