2 #include "run-command.h"
4 #include "spawn-pipe.h"
6 int run_command_v_opt(const char **argv
, int flags
)
9 int fd_i
[2] = { -1, -1 };
10 int fd_o
[2] = { -1, -1 };
12 if (flags
& RUN_COMMAND_NO_STDIN
) {
14 fd_i
[0] = open("/dev/null", O_RDWR
);
16 fd_i
[0] = open("nul", O_RDWR
);
19 if (flags
& RUN_COMMAND_STDOUT_TO_STDERR
)
22 if (flags
& RUN_GIT_CMD
) {
23 pid
= spawnv_git_cmd(argv
, fd_i
, fd_o
);
25 pid
= spawnvpe_pipe(argv
[0], argv
, environ
, fd_i
, fd_o
);
28 return -ERR_RUN_COMMAND_FORK
;
31 pid_t waiting
= waitpid(pid
, &status
, 0);
36 error("waitpid failed (%s)", strerror(errno
));
37 return -ERR_RUN_COMMAND_WAITPID
;
40 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
41 if (WIFSIGNALED(status
))
42 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
44 if (!WIFEXITED(status
))
45 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
46 code
= WEXITSTATUS(status
);
53 int run_command_v(const char **argv
)
55 return run_command_v_opt(argv
, 0);
58 static int run_command_va_opt(int opt
, const char *cmd
, va_list param
)
61 const char *argv
[MAX_RUN_COMMAND_ARGS
];
64 argv
[0] = (char*) cmd
;
66 while (argc
< MAX_RUN_COMMAND_ARGS
) {
67 arg
= argv
[argc
++] = va_arg(param
, char *);
71 if (MAX_RUN_COMMAND_ARGS
<= argc
)
72 return error("too many args to run %s", cmd
);
73 return run_command_v_opt(argv
, opt
);
76 int run_command_opt(int opt
, const char *cmd
, ...)
81 va_start(params
, cmd
);
82 r
= run_command_va_opt(opt
, cmd
, params
);
87 int run_command(const char *cmd
, ...)
92 va_start(params
, cmd
);
93 r
= run_command_va_opt(0, cmd
, params
);