run-command: Allow stderr to be a caller supplied pipe
[gitweb.git] / run-command.c
index cf2d8f7fae1356e50736cb9d599625df79738a2a..bfd231243d2293b1803fc3c3614f67279e959362 100644 (file)
@@ -94,6 +94,9 @@ int start_command(struct child_process *cmd)
                else if (need_err) {
                        dup2(fderr[1], 2);
                        close_pair(fderr);
+               } else if (cmd->err > 1) {
+                       dup2(cmd->err, 2);
+                       close(cmd->err);
                }
 
                if (cmd->no_stdout)
@@ -156,6 +159,9 @@ int start_command(struct child_process *cmd)
        } else if (need_err) {
                s2 = dup(2);
                dup2(fderr[1], 2);
+       } else if (cmd->err > 2) {
+               s2 = dup(2);
+               dup2(cmd->err, 2);
        }
 
        if (cmd->no_stdout) {
@@ -228,6 +234,8 @@ int start_command(struct child_process *cmd)
 
        if (need_err)
                close(fderr[1]);
+       else if (cmd->err)
+               close(cmd->err);
 
        return 0;
 }