refs.c: make sure leading directories exist before writing a ref.
[git/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
3#include <sys/wait.h>
4
19614330 5int run_command_v(int argc, char **argv)
b1bf95bb
JW
6{
7 pid_t pid = fork();
8
9 if (pid < 0)
10 return -ERR_RUN_COMMAND_FORK;
11 if (!pid) {
12 execvp(argv[0], (char *const*) argv);
19614330 13 die("exec %s failed.", argv[0]);
b1bf95bb
JW
14 }
15 for (;;) {
16 int status, code;
17 int retval = waitpid(pid, &status, 0);
18
19 if (retval < 0) {
20 if (errno == EINTR)
21 continue;
22 error("waitpid failed (%s)", strerror(retval));
23 return -ERR_RUN_COMMAND_WAITPID;
24 }
25 if (retval != pid)
26 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
27 if (WIFSIGNALED(status))
28 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
29
30 if (!WIFEXITED(status))
31 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
32 code = WEXITSTATUS(status);
33 if (code)
34 return -code;
35 return 0;
36 }
37}
38
39int run_command(const char *cmd, ...)
40{
41 int argc;
19614330 42 char *argv[MAX_RUN_COMMAND_ARGS];
b1bf95bb
JW
43 const char *arg;
44 va_list param;
45
b1bf95bb 46 va_start(param, cmd);
19614330 47 argv[0] = (char*) cmd;
b1bf95bb
JW
48 argc = 1;
49 while (argc < MAX_RUN_COMMAND_ARGS) {
50 arg = argv[argc++] = va_arg(param, char *);
51 if (!arg)
52 break;
53 }
54 va_end(param);
55 if (MAX_RUN_COMMAND_ARGS <= argc)
56 return error("too many args to run %s", cmd);
19614330 57 return run_command_v(argc, argv);
b1bf95bb 58}