Merge branch 'sf/diff'
[git/git.git] / exec_cmd.c
CommitLineData
77cb17e9
MO
1#include "cache.h"
2#include "exec_cmd.h"
3#define MAX_ARGS 32
4
5extern char **environ;
6static const char *builtin_exec_path = GIT_EXEC_PATH;
7static const char *current_exec_path = NULL;
8
9void git_set_exec_path(const char *exec_path)
10{
11 current_exec_path = exec_path;
12}
13
14
15/* Returns the highest-priority, location to look for git programs. */
962554c6 16const char *git_exec_path(void)
77cb17e9
MO
17{
18 const char *env;
19
20 if (current_exec_path)
21 return current_exec_path;
22
23 env = getenv("GIT_EXEC_PATH");
2b601626 24 if (env && *env) {
77cb17e9
MO
25 return env;
26 }
27
28 return builtin_exec_path;
29}
30
31
9201c707 32int execv_git_cmd(const char **argv)
77cb17e9
MO
33{
34 char git_command[PATH_MAX + 1];
d6859901 35 int i;
77cb17e9
MO
36 const char *paths[] = { current_exec_path,
37 getenv("GIT_EXEC_PATH"),
38 builtin_exec_path };
39
b4f2a6ac 40 for (i = 0; i < ARRAY_SIZE(paths); ++i) {
d6859901
DL
41 size_t len;
42 int rc;
77cb17e9 43 const char *exec_dir = paths[i];
9201c707
JH
44 const char *tmp;
45
2b601626 46 if (!exec_dir || !*exec_dir) continue;
77cb17e9
MO
47
48 if (*exec_dir != '/') {
49 if (!getcwd(git_command, sizeof(git_command))) {
50 fprintf(stderr, "git: cannot determine "
d6859901
DL
51 "current directory: %s\n",
52 strerror(errno));
53 break;
77cb17e9
MO
54 }
55 len = strlen(git_command);
56
57 /* Trivial cleanup */
58 while (!strncmp(exec_dir, "./", 2)) {
59 exec_dir += 2;
60 while (*exec_dir == '/')
61 exec_dir++;
62 }
d6859901
DL
63
64 rc = snprintf(git_command + len,
65 sizeof(git_command) - len, "/%s",
66 exec_dir);
67 if (rc < 0 || rc >= sizeof(git_command) - len) {
68 fprintf(stderr, "git: command name given "
69 "is too long.\n");
70 break;
71 }
77cb17e9 72 } else {
d6859901
DL
73 if (strlen(exec_dir) + 1 > sizeof(git_command)) {
74 fprintf(stderr, "git: command name given "
75 "is too long.\n");
76 break;
77 }
77cb17e9
MO
78 strcpy(git_command, exec_dir);
79 }
80
81 len = strlen(git_command);
d6859901
DL
82 rc = snprintf(git_command + len, sizeof(git_command) - len,
83 "/git-%s", argv[0]);
84 if (rc < 0 || rc >= sizeof(git_command) - len) {
77cb17e9
MO
85 fprintf(stderr,
86 "git: command name given is too long.\n");
87 break;
88 }
89
90 /* argv[0] must be the git command, but the argv array
91 * belongs to the caller, and my be reused in
92 * subsequent loop iterations. Save argv[0] and
93 * restore it on error.
94 */
95
96 tmp = argv[0];
97 argv[0] = git_command;
98
99 /* execve() can only ever return if it fails */
9201c707 100 execve(git_command, (char **)argv, environ);
77cb17e9 101
77cb17e9
MO
102 argv[0] = tmp;
103 }
104 return -1;
105
106}
107
108
9201c707 109int execl_git_cmd(const char *cmd,...)
77cb17e9
MO
110{
111 int argc;
9201c707
JH
112 const char *argv[MAX_ARGS + 1];
113 const char *arg;
77cb17e9
MO
114 va_list param;
115
116 va_start(param, cmd);
117 argv[0] = cmd;
118 argc = 1;
119 while (argc < MAX_ARGS) {
120 arg = argv[argc++] = va_arg(param, char *);
121 if (!arg)
122 break;
123 }
124 va_end(param);
125 if (MAX_ARGS <= argc)
126 return error("too many args to run %s", cmd);
127
128 argv[argc] = NULL;
129 return execv_git_cmd(argv);
130}