7 static const char *argv_exec_path;
9 static const char *builtin_exec_path(void)
20 len = strlen(_pgmptr);
24 p = ep = xmalloc(len+1);
27 /* copy program name, turn '\\' into '/', skip last part */
29 if (*q == '\\' || *q == '/') {
38 ep[0] = '.', ep[1] = '\0';
43 void git_set_argv_exec_path(const char *exec_path)
45 argv_exec_path = exec_path;
49 /* Returns the highest-priority, location to look for git programs. */
50 const char *git_exec_path(void)
55 return argv_exec_path;
57 env = getenv(EXEC_PATH_ENVIRONMENT);
62 return builtin_exec_path();
65 static void add_path(struct strbuf *out, const char *path)
68 if (is_absolute_path(path))
69 strbuf_addstr(out, path);
71 strbuf_addstr(out, make_absolute_path(path));
73 strbuf_addch(out, PATH_SEP);
77 void setup_path(const char *cmd_path)
79 const char *old_path = getenv("PATH");
80 struct strbuf new_path;
82 strbuf_init(&new_path, 0);
84 add_path(&new_path, argv_exec_path);
85 add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
86 add_path(&new_path, builtin_exec_path());
87 add_path(&new_path, cmd_path);
90 strbuf_addstr(&new_path, old_path);
92 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
94 setenv("PATH", new_path.buf, 1);
96 strbuf_release(&new_path);
99 int execv_git_cmd(const char **argv)
104 for (argc = 0; argv[argc]; argc++)
105 ; /* just counting */
106 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
109 for (argc = 0; argv[argc]; argc++)
110 nargv[argc + 1] = argv[argc];
111 nargv[argc + 1] = NULL;
112 trace_argv_printf(nargv, "trace: exec:");
114 /* execvp() can only ever return if it fails */
115 execvp("git", (char **)nargv);
117 trace_printf("trace: exec failed: %s\n", strerror(errno));
124 int execl_git_cmd(const char *cmd,...)
127 const char *argv[MAX_ARGS + 1];
131 va_start(param, cmd);
134 while (argc < MAX_ARGS) {
135 arg = argv[argc++] = va_arg(param, char *);
140 if (MAX_ARGS <= argc)
141 return error("too many args to run %s", cmd);
144 return execv_git_cmd(argv);