2 #include "run-command.h"
5 int start_command(struct child_process *cmd)
7 int need_in = !cmd->no_stdin && cmd->in < 0;
12 return -ERR_RUN_COMMAND_PIPE;
23 return -ERR_RUN_COMMAND_FORK;
28 int fd = open("/dev/null", O_RDWR);
40 if (cmd->stdout_to_stderr)
43 execv_git_cmd(cmd->argv);
45 execvp(cmd->argv[0], (char *const*) cmd->argv);
47 die("exec %s failed.", cmd->argv[0]);
58 int finish_command(struct child_process *cmd)
65 pid_t waiting = waitpid(cmd->pid, &status, 0);
70 error("waitpid failed (%s)", strerror(errno));
71 return -ERR_RUN_COMMAND_WAITPID;
73 if (waiting != cmd->pid)
74 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
75 if (WIFSIGNALED(status))
76 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
78 if (!WIFEXITED(status))
79 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
80 code = WEXITSTATUS(status);
87 int run_command(struct child_process *cmd)
89 int code = start_command(cmd);
92 return finish_command(cmd);
95 int run_command_v_opt(const char **argv, int opt)
97 struct child_process cmd;
98 memset(&cmd, 0, sizeof(cmd));
100 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
101 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
102 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
103 return run_command(&cmd);