git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Junio C Hamano <junkio@cox.net>
Cc: git@vger.kernel.org
Subject: [PATCH 1/4] Simplify closing two fds at once in run-command.c
Date: Mon, 12 Mar 2007 14:37:28 -0400	[thread overview]
Message-ID: <20070312183728.GA15996@spearce.org> (raw)

I started hacking on a change to add stdout redirection support to
the run_command family, but found I was using a lot of close calls
on two pipes in an array (such as for pipe).  So I'm doing a tiny
bit of refactoring first to make the next set of changes clearer.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 run-command.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/run-command.c b/run-command.c
index 03ff7bc..bef9775 100644
--- a/run-command.c
+++ b/run-command.c
@@ -2,6 +2,12 @@
 #include "run-command.h"
 #include "exec_cmd.h"
 
+static inline void close_pair(int fd[2])
+{
+	close(fd[0]);
+	close(fd[1]);
+}
+
 int start_command(struct child_process *cmd)
 {
 	int need_in = !cmd->no_stdin && cmd->in < 0;
@@ -16,10 +22,8 @@ int start_command(struct child_process *cmd)
 
 	cmd->pid = fork();
 	if (cmd->pid < 0) {
-		if (need_in) {
-			close(fdin[0]);
-			close(fdin[1]);
-		}
+		if (need_in)
+			close_pair(fdin);
 		return -ERR_RUN_COMMAND_FORK;
 	}
 
@@ -30,8 +34,7 @@ int start_command(struct child_process *cmd)
 			close(fd);
 		} else if (need_in) {
 			dup2(fdin[0], 0);
-			close(fdin[0]);
-			close(fdin[1]);
+			close_pair(fdin);
 		} else if (cmd->in) {
 			dup2(cmd->in, 0);
 			close(cmd->in);
-- 
1.5.0.3.985.gcf0b4

                 reply	other threads:[~2007-03-12 18:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070312183728.GA15996@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).