Start defining a more sophisticated run_command
There are a number of places where we do some variation of fork()+exec() but we also need to setup redirection in the process, much like what run_command does for us already with its option flags. It would be nice to reuse more of the run_command logic, especially as that non-fork API helps us to port to odd platforms like Win32. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Mar 10, 2007 at 03:28 UTC
f1000898d43a30f6a0d3bbde7b4927e97913d010
2 files changed
+27
-7
run-command.c
+18
-7
@@ -2,26 +2,26 @@
2
#include "run-command.h"
3
#include "exec_cmd.h"
4
5
-int run_command_v_opt(const char **argv, int flags)
5
+int run_command(struct child_process *cmd)
6
{
7
pid_t pid = fork();
8
9
if (pid < 0)
10
return -ERR_RUN_COMMAND_FORK;
11
if (!pid) {
12
- if (flags & RUN_COMMAND_NO_STDIN) {
12
+ if (cmd->no_stdin) {
13
int fd = open("/dev/null", O_RDWR);
14
dup2(fd, 0);
15
close(fd);
16
}
17
- if (flags & RUN_COMMAND_STDOUT_TO_STDERR)
17
+ if (cmd->stdout_to_stderr)
18
dup2(2, 1);
19
- if (flags & RUN_GIT_CMD) {
20
- execv_git_cmd(argv);
19
+ if (cmd->git_cmd) {
20
+ execv_git_cmd(cmd->argv);
21
} else {
22
- execvp(argv[0], (char *const*) argv);
22
+ execvp(cmd->argv[0], (char *const*) cmd->argv);
23
}
24
- die("exec %s failed.", argv[0]);
24
+ die("exec %s failed.", cmd->argv[0]);
25
}
26
for (;;) {
27
int status, code;
@@ -46,3 +46,14 @@ int run_command_v_opt(const char **argv, int flags)
46
return 0;
47
}
48
}
49
+
50
+int run_command_v_opt(const char **argv, int opt)
51
+{
52
+ struct child_process cmd;
53
+ memset(&cmd, 0, sizeof(cmd));
54
+ cmd.argv = argv;
55
+ cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
56
+ cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
57
+ cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
58
+ return run_command(&cmd);
59
+}
run-command.h
+9
@@ -10,6 +10,15 @@ enum {
10
ERR_RUN_COMMAND_WAITPID_NOEXIT,
11
};
12
13
+struct child_process {
14
+ const char **argv;
15
+ unsigned no_stdin:1;
16
+ unsigned git_cmd:1; /* if this is to be git sub-command */
17
+ unsigned stdout_to_stderr:1;
18
+};
19
+
20
+int run_command(struct child_process *);
21
+
22
#define RUN_COMMAND_NO_STDIN 1
23
#define RUN_GIT_CMD 2 /*If this is to be git sub-command */
24
#define RUN_COMMAND_STDOUT_TO_STDERR 4