Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Teach run-command about stdout redirection

Some potential callers of the run_command family of functions need
to control not only the stdin redirection of the child, but also
the stdout redirection of the child.  This can now be setup much
like the already existing stdin redirection.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information...
commit f4bba25bdc0ecb4ac338de81a2a65af487701833 1 parent 27ebd6e
Shawn O. Pearce authored March 12, 2007 Junio C Hamano committed March 12, 2007
30  run-command.c
@@ -11,7 +11,8 @@ static inline void close_pair(int fd[2])
11 11
 int start_command(struct child_process *cmd)
12 12
 {
13 13
 	int need_in = !cmd->no_stdin && cmd->in < 0;
14  
-	int fdin[2];
  14
+	int need_out = !cmd->stdout_to_stderr && cmd->out < 0;
  15
+	int fdin[2], fdout[2];
15 16
 
16 17
 	if (need_in) {
17 18
 		if (pipe(fdin) < 0)
@@ -20,10 +21,22 @@ int start_command(struct child_process *cmd)
20 21
 		cmd->close_in = 1;
21 22
 	}
22 23
 
  24
+	if (need_out) {
  25
+		if (pipe(fdout) < 0) {
  26
+			if (need_in)
  27
+				close_pair(fdin);
  28
+			return -ERR_RUN_COMMAND_PIPE;
  29
+		}
  30
+		cmd->out = fdout[0];
  31
+		cmd->close_out = 1;
  32
+	}
  33
+
23 34
 	cmd->pid = fork();
24 35
 	if (cmd->pid < 0) {
25 36
 		if (need_in)
26 37
 			close_pair(fdin);
  38
+		if (need_out)
  39
+			close_pair(fdout);
27 40
 		return -ERR_RUN_COMMAND_FORK;
28 41
 	}
29 42
 
@@ -42,6 +55,14 @@ int start_command(struct child_process *cmd)
42 55
 
43 56
 		if (cmd->stdout_to_stderr)
44 57
 			dup2(2, 1);
  58
+		else if (need_out) {
  59
+			dup2(fdout[1], 1);
  60
+			close_pair(fdout);
  61
+		} else if (cmd->out > 1) {
  62
+			dup2(cmd->out, 1);
  63
+			close(cmd->out);
  64
+		}
  65
+
45 66
 		if (cmd->git_cmd) {
46 67
 			execv_git_cmd(cmd->argv);
47 68
 		} else {
@@ -55,6 +76,11 @@ int start_command(struct child_process *cmd)
55 76
 	else if (cmd->in)
56 77
 		close(cmd->in);
57 78
 
  79
+	if (need_out)
  80
+		close(fdout[1]);
  81
+	else if (cmd->out > 1)
  82
+		close(cmd->out);
  83
+
58 84
 	return 0;
59 85
 }
60 86
 
@@ -62,6 +88,8 @@ int finish_command(struct child_process *cmd)
62 88
 {
63 89
 	if (cmd->close_in)
64 90
 		close(cmd->in);
  91
+	if (cmd->close_out)
  92
+		close(cmd->out);
65 93
 
66 94
 	for (;;) {
67 95
 		int status, code;
2  run-command.h
@@ -15,7 +15,9 @@ struct child_process {
15 15
 	const char **argv;
16 16
 	pid_t pid;
17 17
 	int in;
  18
+	int out;
18 19
 	unsigned close_in:1;
  20
+	unsigned close_out:1;
19 21
 	unsigned no_stdin:1;
20 22
 	unsigned git_cmd:1; /* if this is to be git sub-command */
21 23
 	unsigned stdout_to_stderr:1;

0 notes on commit f4bba25

Please sign in to comment.
Something went wrong with that request. Please try again.