@matheusbsbs / matheus-bagliano / commits / e4507ae84e
Fork 0

Teach run-command to redirect stdout to /dev/null

Some run-command callers may wish to just discard any data that is sent to stdout from the child. This is a lot like our existing no_stdin support, we just open /dev/null and duplicate the descriptor into position. 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 e4507ae84ed04c835b66f77195a0549b72f99c39
2 files changed +19 -8
run-command.c
+18 -8
@@ -8,12 +8,19 @@ static inline void close_pair(int fd[2])
8 close(fd[1]);
9 }
10
11 +static inline void dup_devnull(int to)
12 +{
13 + int fd = open("/dev/null", O_RDWR);
14 + dup2(fd, to);
15 + close(fd);
16 +}
17 +
18 int start_command(struct child_process *cmd)
19 {
13 - int need_in = !cmd->no_stdin && cmd->in < 0;
14 - int need_out = !cmd->stdout_to_stderr && cmd->out < 0;
20 + int need_in, need_out;
21 int fdin[2], fdout[2];
22
23 + need_in = !cmd->no_stdin && cmd->in < 0;
24 if (need_in) {
25 if (pipe(fdin) < 0)
26 return -ERR_RUN_COMMAND_PIPE;
@@ -21,6 +28,9 @@ int start_command(struct child_process *cmd)
28 cmd->close_in = 1;
29 }
30
31 + need_out = !cmd->no_stdout
32 + && !cmd->stdout_to_stderr
33 + && cmd->out < 0;
34 if (need_out) {
35 if (pipe(fdout) < 0) {
36 if (need_in)
@@ -41,11 +51,9 @@ int start_command(struct child_process *cmd)
51 }
52
53 if (!cmd->pid) {
44 - if (cmd->no_stdin) {
45 - int fd = open("/dev/null", O_RDWR);
46 - dup2(fd, 0);
47 - close(fd);
48 - } else if (need_in) {
54 + if (cmd->no_stdin)
55 + dup_devnull(0);
56 + else if (need_in) {
57 dup2(fdin[0], 0);
58 close_pair(fdin);
59 } else if (cmd->in) {
@@ -53,7 +61,9 @@ int start_command(struct child_process *cmd)
61 close(cmd->in);
62 }
63
56 - if (cmd->stdout_to_stderr)
64 + if (cmd->no_stdout)
65 + dup_devnull(1);
66 + else if (cmd->stdout_to_stderr)
67 dup2(2, 1);
68 else if (need_out) {
69 dup2(fdout[1], 1);
run-command.h
+1
@@ -19,6 +19,7 @@ struct child_process {
19 unsigned close_in:1;
20 unsigned close_out:1;
21 unsigned no_stdin:1;
22 + unsigned no_stdout:1;
23 unsigned git_cmd:1; /* if this is to be git sub-command */
24 unsigned stdout_to_stderr:1;
25 };