{"thread":{"id":"7211","subject":"[PATCH 1/4] Simplify closing two fds at once in run-command.c","startedAt":"2007-03-12T18:37:28Z","lastAt":"2007-03-12T18:37:28Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"36932","messageId":"20070312183728.GA15996@spearce.org","threadId":"7211","inReplyTo":null,"subject":"[PATCH 1/4] Simplify closing two fds at once in run-command.c","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-12T18:37:28Z","receivedAt":"2007-03-12T18:37:28Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"I started hacking on a change to add stdout redirection support to\nthe run_command family, but found I was using a lot of close calls\non two pipes in an array (such as for pipe).  So I'm doing a tiny\nbit of refactoring first to make the next set of changes clearer.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n run-command.c |   15 +++++++++------\n 1 files changed, 9 insertions(+), 6 deletions(-)\n\ndiff --git a/run-command.c b/run-command.c\nindex 03ff7bc..bef9775 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -2,6 +2,12 @@\n #include \"run-command.h\"\n #include \"exec_cmd.h\"\n \n+static inline void close_pair(int fd[2])\n+{\n+\tclose(fd[0]);\n+\tclose(fd[1]);\n+}\n+\n int start_command(struct child_process *cmd)\n {\n \tint need_in = !cmd->no_stdin && cmd->in < 0;\n@@ -16,10 +22,8 @@ int start_command(struct child_process *cmd)\n \n \tcmd->pid = fork();\n \tif (cmd->pid < 0) {\n-\t\tif (need_in) {\n-\t\t\tclose(fdin[0]);\n-\t\t\tclose(fdin[1]);\n-\t\t}\n+\t\tif (need_in)\n+\t\t\tclose_pair(fdin);\n \t\treturn -ERR_RUN_COMMAND_FORK;\n \t}\n \n@@ -30,8 +34,7 @@ int start_command(struct child_process *cmd)\n \t\t\tclose(fd);\n \t\t} else if (need_in) {\n \t\t\tdup2(fdin[0], 0);\n-\t\t\tclose(fdin[0]);\n-\t\t\tclose(fdin[1]);\n+\t\t\tclose_pair(fdin);\n \t\t} else if (cmd->in) {\n \t\t\tdup2(cmd->in, 0);\n \t\t\tclose(cmd->in);\n-- \n1.5.0.3.985.gcf0b4\n"}]}