{"thread":{"id":"7213","subject":"[PATCH 2/4] Teach run-command about stdout redirection","startedAt":"2007-03-12T18:37:45Z","lastAt":"2007-03-12T18:37:45Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"36934","messageId":"20070312183745.GB15996@spearce.org","threadId":"7213","inReplyTo":null,"subject":"[PATCH 2/4] Teach run-command about stdout redirection","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-12T18:37:45Z","receivedAt":"2007-03-12T18:37:45Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Some potential callers of the run_command family of functions need\nto control not only the stdin redirection of the child, but also\nthe stdout redirection of the child.  This can now be setup much\nlike the already existing stdin redirection.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n run-command.c |   30 +++++++++++++++++++++++++++++-\n run-command.h |    2 ++\n 2 files changed, 31 insertions(+), 1 deletions(-)\n\ndiff --git a/run-command.c b/run-command.c\nindex bef9775..d20a6ef 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -11,7 +11,8 @@ static inline void close_pair(int fd[2])\n int start_command(struct child_process *cmd)\n {\n \tint need_in = !cmd->no_stdin && cmd->in < 0;\n-\tint fdin[2];\n+\tint need_out = !cmd->stdout_to_stderr && cmd->out < 0;\n+\tint fdin[2], fdout[2];\n \n \tif (need_in) {\n \t\tif (pipe(fdin) < 0)\n@@ -20,10 +21,22 @@ int start_command(struct child_process *cmd)\n \t\tcmd->close_in = 1;\n \t}\n \n+\tif (need_out) {\n+\t\tif (pipe(fdout) < 0) {\n+\t\t\tif (need_in)\n+\t\t\t\tclose_pair(fdin);\n+\t\t\treturn -ERR_RUN_COMMAND_PIPE;\n+\t\t}\n+\t\tcmd->out = fdout[0];\n+\t\tcmd->close_out = 1;\n+\t}\n+\n \tcmd->pid = fork();\n \tif (cmd->pid < 0) {\n \t\tif (need_in)\n \t\t\tclose_pair(fdin);\n+\t\tif (need_out)\n+\t\t\tclose_pair(fdout);\n \t\treturn -ERR_RUN_COMMAND_FORK;\n \t}\n \n@@ -42,6 +55,14 @@ int start_command(struct child_process *cmd)\n \n \t\tif (cmd->stdout_to_stderr)\n \t\t\tdup2(2, 1);\n+\t\telse if (need_out) {\n+\t\t\tdup2(fdout[1], 1);\n+\t\t\tclose_pair(fdout);\n+\t\t} else if (cmd->out > 1) {\n+\t\t\tdup2(cmd->out, 1);\n+\t\t\tclose(cmd->out);\n+\t\t}\n+\n \t\tif (cmd->git_cmd) {\n \t\t\texecv_git_cmd(cmd->argv);\n \t\t} else {\n@@ -55,6 +76,11 @@ int start_command(struct child_process *cmd)\n \telse if (cmd->in)\n \t\tclose(cmd->in);\n \n+\tif (need_out)\n+\t\tclose(fdout[1]);\n+\telse if (cmd->out > 1)\n+\t\tclose(cmd->out);\n+\n \treturn 0;\n }\n \n@@ -62,6 +88,8 @@ int finish_command(struct child_process *cmd)\n {\n \tif (cmd->close_in)\n \t\tclose(cmd->in);\n+\tif (cmd->close_out)\n+\t\tclose(cmd->out);\n \n \tfor (;;) {\n \t\tint status, code;\ndiff --git a/run-command.h b/run-command.h\nindex ff09067..1c9126b 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -15,7 +15,9 @@ struct child_process {\n \tconst char **argv;\n \tpid_t pid;\n \tint in;\n+\tint out;\n \tunsigned close_in:1;\n+\tunsigned close_out:1;\n \tunsigned no_stdin:1;\n \tunsigned git_cmd:1; /* if this is to be git sub-command */\n \tunsigned stdout_to_stderr:1;\n-- \n1.5.0.3.985.gcf0b4\n"}]}