MinGW tests: override 'pwd', too
[git/mingw/4msysgit/wingit-dll.git] / exec_cmd.c
blob0de03c2bed2d934218a6eb767c92e1a74d075483
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 static const char *prefix;
14 if (is_absolute_path(path)) {
15 return path;
18 #ifdef RUNTIME_PREFIX
19 assert(argv0_path);
20 assert(is_absolute_path(argv0_path));
22 if (!prefix) {
23 const char *strip[] = {
24 GIT_EXEC_PATH,
25 BINDIR,
28 const char **s;
30 for (s = strip; *s; s++) {
31 const char *sargv = argv0_path + strlen(argv0_path);
32 const char *ss = *s + strlen(*s);
33 while (argv0_path < sargv && *s < ss
34 && (*sargv == *ss ||
35 (is_dir_sep(*sargv) && is_dir_sep(*ss)))) {
36 sargv--;
37 ss--;
39 if (*s == ss) {
40 struct strbuf d = STRBUF_INIT;
41 /* We also skip the trailing directory separator. */
42 assert(sargv - argv0_path - 1 >= 0);
43 strbuf_add(&d, argv0_path, sargv - argv0_path - 1);
44 prefix = strbuf_detach(&d, NULL);
45 break;
50 if (!prefix) {
51 prefix = PREFIX;
52 fprintf(stderr, "RUNTIME_PREFIX requested, "
53 "but prefix computation failed. "
54 "Using static fallback '%s'.\n", prefix);
56 #else
57 prefix = PREFIX;
58 #endif
60 struct strbuf d = STRBUF_INIT;
61 strbuf_addf(&d, "%s/%s", prefix, path);
62 path = strbuf_detach(&d, NULL);
63 return path;
66 const char *git_extract_argv0_path(const char *argv0)
68 const char *slash = argv0 + strlen(argv0);
71 --slash;
72 while (slash >= argv0 && !is_dir_sep(*slash));
74 if (slash >= argv0) {
75 argv0_path = xstrndup(argv0, slash - argv0);
76 return slash + 1;
79 return argv0;
82 void git_set_argv_exec_path(const char *exec_path)
84 argv_exec_path = exec_path;
88 /* Returns the highest-priority, location to look for git programs. */
89 const char *git_exec_path(void)
91 const char *env;
93 if (argv_exec_path)
94 return argv_exec_path;
96 env = getenv(EXEC_PATH_ENVIRONMENT);
97 if (env && *env) {
98 return env;
101 return system_path(GIT_EXEC_PATH);
104 static void add_path(struct strbuf *out, const char *path)
106 if (path && *path) {
107 if (is_absolute_path(path))
108 strbuf_addstr(out, path);
109 else
110 strbuf_addstr(out, make_nonrelative_path(path));
112 strbuf_addch(out, PATH_SEP);
116 void setup_path(void)
118 const char *old_path = getenv("PATH");
119 struct strbuf new_path = STRBUF_INIT;
121 add_path(&new_path, git_exec_path());
122 add_path(&new_path, argv0_path);
124 if (old_path)
125 strbuf_addstr(&new_path, old_path);
126 else
127 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
129 setenv("PATH", new_path.buf, 1);
131 strbuf_release(&new_path);
134 const char **prepare_git_cmd(const char **argv)
136 int argc;
137 const char **nargv;
139 for (argc = 0; argv[argc]; argc++)
140 ; /* just counting */
141 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
143 nargv[0] = "git";
144 for (argc = 0; argv[argc]; argc++)
145 nargv[argc + 1] = argv[argc];
146 nargv[argc + 1] = NULL;
147 return nargv;
150 int execv_git_cmd(const char **argv) {
151 const char **nargv = prepare_git_cmd(argv);
152 trace_argv_printf(nargv, "trace: exec:");
154 /* execvp() can only ever return if it fails */
155 execvp("git", (char **)nargv);
157 trace_printf("trace: exec failed: %s\n", strerror(errno));
159 free(nargv);
160 return -1;
164 int execl_git_cmd(const char *cmd,...)
166 int argc;
167 const char *argv[MAX_ARGS + 1];
168 const char *arg;
169 va_list param;
171 va_start(param, cmd);
172 argv[0] = cmd;
173 argc = 1;
174 while (argc < MAX_ARGS) {
175 arg = argv[argc++] = va_arg(param, char *);
176 if (!arg)
177 break;
179 va_end(param);
180 if (MAX_ARGS <= argc)
181 return error("too many args to run %s", cmd);
183 argv[argc] = NULL;
184 return execv_git_cmd(argv);