gitk: work around ridiculous command line restriction on Windows
[git/dscho.git] / exec_cmd.c
blob8207dd2c3578217e8484bc12c6ee0471002bed98
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #define MAX_ARGS 32
6 extern char **environ;
7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 const char *system_path(const char *path)
12 #ifdef RUNTIME_PREFIX
13 static const char *prefix;
14 #else
15 static const char *prefix = PREFIX;
16 #endif
17 struct strbuf d = STRBUF_INIT;
19 if (is_absolute_path(path))
20 return path;
22 #ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
26 if (!prefix &&
27 !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
28 !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
29 !(prefix = strip_path_suffix(argv0_path, "git"))) {
30 prefix = PREFIX;
31 fprintf(stderr, "RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
35 #endif
37 strbuf_addf(&d, "%s/%s", prefix, path);
38 path = strbuf_detach(&d, NULL);
39 return path;
42 const char *git_extract_argv0_path(const char *argv0)
44 const char *slash;
46 if (!argv0 || !*argv0)
47 return NULL;
48 slash = argv0 + strlen(argv0);
50 while (argv0 <= slash && !is_dir_sep(*slash))
51 slash--;
53 if (slash == argv0) {
54 argv0 = lookup_program_in_path(argv0);
55 slash = argv0 + strlen(argv0);
56 while (argv0 <= slash && !is_dir_sep(*slash))
57 slash--;
60 if (slash >= argv0) {
61 argv0_path = xstrndup(argv0, slash - argv0);
62 return slash + 1;
65 return argv0;
68 void git_set_argv_exec_path(const char *exec_path)
70 argv_exec_path = exec_path;
72 * Propagate this setting to external programs.
74 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
78 /* Returns the highest-priority, location to look for git programs. */
79 const char *git_exec_path(void)
81 const char *env;
83 if (argv_exec_path)
84 return argv_exec_path;
86 env = getenv(EXEC_PATH_ENVIRONMENT);
87 if (env && *env) {
88 return env;
91 return system_path(GIT_EXEC_PATH);
94 static void add_path(struct strbuf *out, const char *path)
96 if (path && *path) {
97 if (is_absolute_path(path))
98 strbuf_addstr(out, path);
99 else
100 strbuf_addstr(out, make_nonrelative_path(path));
102 strbuf_addch(out, PATH_SEP);
106 void setup_path(void)
108 const char *old_path = getenv("PATH");
109 struct strbuf new_path = STRBUF_INIT;
111 add_path(&new_path, git_exec_path());
112 add_path(&new_path, argv0_path);
114 if (old_path)
115 strbuf_addstr(&new_path, old_path);
116 else
117 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
119 setenv("PATH", new_path.buf, 1);
121 strbuf_release(&new_path);
124 const char **prepare_git_cmd(const char **argv)
126 int argc;
127 const char **nargv;
129 for (argc = 0; argv[argc]; argc++)
130 ; /* just counting */
131 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
133 nargv[0] = "git";
134 for (argc = 0; argv[argc]; argc++)
135 nargv[argc + 1] = argv[argc];
136 nargv[argc + 1] = NULL;
137 return nargv;
140 int execv_git_cmd(const char **argv) {
141 const char **nargv = prepare_git_cmd(argv);
142 trace_argv_printf(nargv, "trace: exec:");
144 /* execvp() can only ever return if it fails */
145 execvp("git", (char **)nargv);
147 trace_printf("trace: exec failed: %s\n", strerror(errno));
149 free(nargv);
150 return -1;
154 int execl_git_cmd(const char *cmd,...)
156 int argc;
157 const char *argv[MAX_ARGS + 1];
158 const char *arg;
159 va_list param;
161 va_start(param, cmd);
162 argv[0] = cmd;
163 argc = 1;
164 while (argc < MAX_ARGS) {
165 arg = argv[argc++] = va_arg(param, char *);
166 if (!arg)
167 break;
169 va_end(param);
170 if (MAX_ARGS <= argc)
171 return error("too many args to run %s", cmd);
173 argv[argc] = NULL;
174 return execv_git_cmd(argv);
177 char *lookup_program_in_path(const char *program)
179 struct strbuf buf = STRBUF_INIT;
180 const char *path = getenv("PATH");
182 if (!path || !*path)
183 return NULL;
185 for (;;) {
186 const char *colon = strchrnul(path, PATH_SEP);
188 strbuf_setlen(&buf, 0);
189 strbuf_addf(&buf, "%.*s/%s",
190 (int)(colon - path), path, program);
191 if (!access(buf.buf, X_OK))
192 return strbuf_detach(&buf, NULL);
193 if (!*colon)
194 return NULL;
195 path = colon + 1;