@matheusbsbs / matheus-bagliano / commits / 4f41b61148
Fork 0

run-command: Allow stderr to be a caller supplied pipe

Like .out, .err may now be set to a file descriptor > 0, which is a writable pipe/socket/file that the child's stderr will be redirected into. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Feb 5, 2010 at 12:57 UTC 4f41b611481bad08319966f7787fc7c4c7bfaa52
3 files changed +10 -2
Documentation/technical/api-run-command.txt
+1 -1
@@ -135,7 +135,7 @@ stderr as follows:
135
136 .in: The FD must be readable; it becomes child's stdin.
137 .out: The FD must be writable; it becomes child's stdout.
138 - .err > 0 is not supported.
138 + .err: The FD must be writable; it becomes child's stderr.
139
140 The specified FD is closed by start_command(), even if it fails to
141 run the sub-process!
run-command.c
+8
@@ -94,6 +94,9 @@ fail_pipe:
94 else if (need_err) {
95 dup2(fderr[1], 2);
96 close_pair(fderr);
97 + } else if (cmd->err > 1) {
98 + dup2(cmd->err, 2);
99 + close(cmd->err);
100 }
101
102 if (cmd->no_stdout)
@@ -156,6 +159,9 @@ fail_pipe:
159 } else if (need_err) {
160 s2 = dup(2);
161 dup2(fderr[1], 2);
162 + } else if (cmd->err > 2) {
163 + s2 = dup(2);
164 + dup2(cmd->err, 2);
165 }
166
167 if (cmd->no_stdout) {
@@ -228,6 +234,8 @@ fail_pipe:
234
235 if (need_err)
236 close(fderr[1]);
237 + else if (cmd->err)
238 + close(cmd->err);
239
240 return 0;
241 }
run-command.h
+1 -1
@@ -18,7 +18,7 @@ struct child_process {
18 * - Specify > 0 to set a channel to a particular FD as follows:
19 * .in: a readable FD, becomes child's stdin
20 * .out: a writable FD, becomes child's stdout/stderr
21 - * .err > 0 not supported
21 + * .err: a writable FD, becomes child's stderr
22 * The specified FD is closed by start_command(), even in case
23 * of errors!
24 */