{"thread":{"id":"7212","subject":"[PATCH 3/4] Teach run-command to redirect stdout to /dev/null","startedAt":"2007-03-12T18:37:55Z","lastAt":"2007-03-12T18:37:55Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"36933","messageId":"20070312183755.GC15996@spearce.org","threadId":"7212","inReplyTo":null,"subject":"[PATCH 3/4] Teach run-command to redirect stdout to /dev/null","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-12T18:37:55Z","receivedAt":"2007-03-12T18:37:55Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Some run-command callers may wish to just discard any data that\nis sent to stdout from the child.  This is a lot like our existing\nno_stdin support, we just open /dev/null and duplicate the descriptor\ninto position.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n run-command.c |   26 ++++++++++++++++++--------\n run-command.h |    1 +\n 2 files changed, 19 insertions(+), 8 deletions(-)\n\ndiff --git a/run-command.c b/run-command.c\nindex d20a6ef..eff523e 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -8,12 +8,19 @@ static inline void close_pair(int fd[2])\n \tclose(fd[1]);\n }\n \n+static inline void dup_devnull(int to)\n+{\n+\tint fd = open(\"/dev/null\", O_RDWR);\n+\tdup2(fd, to);\n+\tclose(fd);\n+}\n+\n int start_command(struct child_process *cmd)\n {\n-\tint need_in = !cmd->no_stdin && cmd->in < 0;\n-\tint need_out = !cmd->stdout_to_stderr && cmd->out < 0;\n+\tint need_in, need_out;\n \tint fdin[2], fdout[2];\n \n+\tneed_in = !cmd->no_stdin && cmd->in < 0;\n \tif (need_in) {\n \t\tif (pipe(fdin) < 0)\n \t\t\treturn -ERR_RUN_COMMAND_PIPE;\n@@ -21,6 +28,9 @@ int start_command(struct child_process *cmd)\n \t\tcmd->close_in = 1;\n \t}\n \n+\tneed_out = !cmd->no_stdout\n+\t\t&& !cmd->stdout_to_stderr\n+\t\t&& cmd->out < 0;\n \tif (need_out) {\n \t\tif (pipe(fdout) < 0) {\n \t\t\tif (need_in)\n@@ -41,11 +51,9 @@ int start_command(struct child_process *cmd)\n \t}\n \n \tif (!cmd->pid) {\n-\t\tif (cmd->no_stdin) {\n-\t\t\tint fd = open(\"/dev/null\", O_RDWR);\n-\t\t\tdup2(fd, 0);\n-\t\t\tclose(fd);\n-\t\t} else if (need_in) {\n+\t\tif (cmd->no_stdin)\n+\t\t\tdup_devnull(0);\n+\t\telse if (need_in) {\n \t\t\tdup2(fdin[0], 0);\n \t\t\tclose_pair(fdin);\n \t\t} else if (cmd->in) {\n@@ -53,7 +61,9 @@ int start_command(struct child_process *cmd)\n \t\t\tclose(cmd->in);\n \t\t}\n \n-\t\tif (cmd->stdout_to_stderr)\n+\t\tif (cmd->no_stdout)\n+\t\t\tdup_devnull(1);\n+\t\telse if (cmd->stdout_to_stderr)\n \t\t\tdup2(2, 1);\n \t\telse if (need_out) {\n \t\t\tdup2(fdout[1], 1);\ndiff --git a/run-command.h b/run-command.h\nindex 1c9126b..3680ef9 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -19,6 +19,7 @@ struct child_process {\n \tunsigned close_in:1;\n \tunsigned close_out:1;\n \tunsigned no_stdin:1;\n+\tunsigned no_stdout:1;\n \tunsigned git_cmd:1; /* if this is to be git sub-command */\n \tunsigned stdout_to_stderr:1;\n };\n-- \n1.5.0.3.985.gcf0b4\n"}]}