Remove unnecessary argc parameter from run_command_v.
[git/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
b1bf95bb 4
9b0b5093 5int run_command_v_opt(const char **argv, int flags)
b1bf95bb
JW
6{
7 pid_t pid = fork();
8
9 if (pid < 0)
10 return -ERR_RUN_COMMAND_FORK;
11 if (!pid) {
128aed68
DB
12 if (flags & RUN_COMMAND_NO_STDIO) {
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, 0);
15 dup2(fd, 1);
77cb17e9
MO
16 close(fd);
17 }
18 if (flags & RUN_GIT_CMD) {
19 execv_git_cmd(argv);
20 } else {
21 execvp(argv[0], (char *const*) argv);
128aed68 22 }
19614330 23 die("exec %s failed.", argv[0]);
b1bf95bb
JW
24 }
25 for (;;) {
26 int status, code;
6f002f98 27 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 28
6f002f98 29 if (waiting < 0) {
b1bf95bb
JW
30 if (errno == EINTR)
31 continue;
6f002f98 32 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
33 return -ERR_RUN_COMMAND_WAITPID;
34 }
6f002f98 35 if (waiting != pid)
b1bf95bb
JW
36 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
37 if (WIFSIGNALED(status))
38 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
39
40 if (!WIFEXITED(status))
41 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
42 code = WEXITSTATUS(status);
43 if (code)
44 return -code;
45 return 0;
46 }
47}
48
9b0b5093 49int run_command_v(const char **argv)
128aed68 50{
9b0b5093 51 return run_command_v_opt(argv, 0);
128aed68
DB
52}
53
b1bf95bb
JW
54int run_command(const char *cmd, ...)
55{
56 int argc;
9201c707 57 const char *argv[MAX_RUN_COMMAND_ARGS];
b1bf95bb
JW
58 const char *arg;
59 va_list param;
60
b1bf95bb 61 va_start(param, cmd);
19614330 62 argv[0] = (char*) cmd;
b1bf95bb
JW
63 argc = 1;
64 while (argc < MAX_RUN_COMMAND_ARGS) {
65 arg = argv[argc++] = va_arg(param, char *);
66 if (!arg)
67 break;
68 }
69 va_end(param);
70 if (MAX_RUN_COMMAND_ARGS <= argc)
71 return error("too many args to run %s", cmd);
9b0b5093 72 return run_command_v_opt(argv, 0);
b1bf95bb 73}