Simplify closing two fds at once in run-command.c
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> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Mar 12, 2007 at 14:37 UTC
9dc09c766463333ca51311a2db86fd67f4fec19b
1 file changed
+9
-6
run-command.c
+9
-6
@@ -2,6 +2,12 @@
2
#include "run-command.h"
3
#include "exec_cmd.h"
4
5
+static inline void close_pair(int fd[2])
6
+{
7
+ close(fd[0]);
8
+ close(fd[1]);
9
+}
10
+
11
int start_command(struct child_process *cmd)
12
{
13
int need_in = !cmd->no_stdin && cmd->in < 0;
@@ -16,10 +22,8 @@ int start_command(struct child_process *cmd)
22
23
cmd->pid = fork();
24
if (cmd->pid < 0) {
19
- if (need_in) {
20
- close(fdin[0]);
21
- close(fdin[1]);
22
- }
25
+ if (need_in)
26
+ close_pair(fdin);
27
return -ERR_RUN_COMMAND_FORK;
28
}
29
@@ -30,8 +34,7 @@ int start_command(struct child_process *cmd)
34
close(fd);
35
} else if (need_in) {
36
dup2(fdin[0], 0);
33
- close(fdin[0]);
34
- close(fdin[1]);
37
+ close_pair(fdin);
38
} else if (cmd->in) {
39
dup2(cmd->in, 0);
40
close(cmd->in);