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>maint
parent
1358e7d670
commit
9dc09c7664
|
@ -2,6 +2,12 @@
|
||||||
#include "run-command.h"
|
#include "run-command.h"
|
||||||
#include "exec_cmd.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 start_command(struct child_process *cmd)
|
||||||
{
|
{
|
||||||
int need_in = !cmd->no_stdin && cmd->in < 0;
|
int need_in = !cmd->no_stdin && cmd->in < 0;
|
||||||
|
@ -16,10 +22,8 @@ int start_command(struct child_process *cmd)
|
||||||
|
|
||||||
cmd->pid = fork();
|
cmd->pid = fork();
|
||||||
if (cmd->pid < 0) {
|
if (cmd->pid < 0) {
|
||||||
if (need_in) {
|
if (need_in)
|
||||||
close(fdin[0]);
|
close_pair(fdin);
|
||||||
close(fdin[1]);
|
|
||||||
}
|
|
||||||
return -ERR_RUN_COMMAND_FORK;
|
return -ERR_RUN_COMMAND_FORK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,8 +34,7 @@ int start_command(struct child_process *cmd)
|
||||||
close(fd);
|
close(fd);
|
||||||
} else if (need_in) {
|
} else if (need_in) {
|
||||||
dup2(fdin[0], 0);
|
dup2(fdin[0], 0);
|
||||||
close(fdin[0]);
|
close_pair(fdin);
|
||||||
close(fdin[1]);
|
|
||||||
} else if (cmd->in) {
|
} else if (cmd->in) {
|
||||||
dup2(cmd->in, 0);
|
dup2(cmd->in, 0);
|
||||||
close(cmd->in);
|
close(cmd->in);
|
||||||
|
|
Loading…
Reference in New Issue