7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 static const char *builtin_exec_path(void)
21 len = strlen(_pgmptr);
25 p = ep = xmalloc(len+1);
28 /* copy program name, turn '\\' into '/', skip last part */
30 if (*q == '\\' || *q == '/') {
39 ep[0] = '.', ep[1] = '\0';
44 const char *system_path(const char *path)
46 if (!is_absolute_path(path) && argv0_path) {
47 struct strbuf d = STRBUF_INIT;
48 strbuf_addf(&d, "%s/%s", argv0_path, path);
49 path = strbuf_detach(&d, NULL);
54 void git_set_argv0_path(const char *path)
59 void git_set_argv_exec_path(const char *exec_path)
61 argv_exec_path = exec_path;
65 /* Returns the highest-priority, location to look for git programs. */
66 const char *git_exec_path(void)
71 return argv_exec_path;
73 env = getenv(EXEC_PATH_ENVIRONMENT);
78 return builtin_exec_path();
81 static void add_path(struct strbuf *out, const char *path)
84 if (is_absolute_path(path))
85 strbuf_addstr(out, path);
87 strbuf_addstr(out, make_absolute_path(path));
89 strbuf_addch(out, PATH_SEP);
95 const char *old_path = getenv("PATH");
96 struct strbuf new_path;
98 strbuf_init(&new_path, 0);
100 add_path(&new_path, argv_exec_path);
101 add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
102 add_path(&new_path, builtin_exec_path());
103 add_path(&new_path, argv0_path);
106 strbuf_addstr(&new_path, old_path);
108 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
110 setenv("PATH", new_path.buf, 1);
112 strbuf_release(&new_path);
115 int execv_git_cmd(const char **argv)
120 for (argc = 0; argv[argc]; argc++)
121 ; /* just counting */
122 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
125 for (argc = 0; argv[argc]; argc++)
126 nargv[argc + 1] = argv[argc];
127 nargv[argc + 1] = NULL;
128 trace_argv_printf(nargv, "trace: exec:");
130 /* execvp() can only ever return if it fails */
131 execvp("git", (char **)nargv);
133 trace_printf("trace: exec failed: %s\n", strerror(errno));
140 int execl_git_cmd(const char *cmd,...)
143 const char *argv[MAX_ARGS + 1];
147 va_start(param, cmd);
150 while (argc < MAX_ARGS) {
151 arg = argv[argc++] = va_arg(param, char *);
156 if (MAX_ARGS <= argc)
157 return error("too many args to run %s", cmd);
160 return execv_git_cmd(argv);