Refactor git_set_argv0_path() to git_extract_argv0_path()
[git/git.git] / exec_cmd.c
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #define MAX_ARGS 32
5
6 extern char **environ;
7 static const char *argv_exec_path;
8 static const char *argv0_path;
9
10 const char *system_path(const char *path)
11 {
12 static const char *prefix = PREFIX;
13 struct strbuf d = STRBUF_INIT;
14
15 if (is_absolute_path(path))
16 return path;
17
18 strbuf_addf(&d, "%s/%s", prefix, path);
19 path = strbuf_detach(&d, NULL);
20 return path;
21 }
22
23 const char *git_extract_argv0_path(const char *argv0)
24 {
25 const char *slash = argv0 + strlen(argv0);
26
27 while (argv0 <= slash && !is_dir_sep(*slash))
28 slash--;
29
30 if (slash >= argv0) {
31 argv0_path = xstrndup(argv0, slash - argv0);
32 return slash + 1;
33 }
34
35 return argv0;
36 }
37
38 void git_set_argv_exec_path(const char *exec_path)
39 {
40 argv_exec_path = exec_path;
41 }
42
43
44 /* Returns the highest-priority, location to look for git programs. */
45 const char *git_exec_path(void)
46 {
47 const char *env;
48
49 if (argv_exec_path)
50 return argv_exec_path;
51
52 env = getenv(EXEC_PATH_ENVIRONMENT);
53 if (env && *env) {
54 return env;
55 }
56
57 return system_path(GIT_EXEC_PATH);
58 }
59
60 static void add_path(struct strbuf *out, const char *path)
61 {
62 if (path && *path) {
63 if (is_absolute_path(path))
64 strbuf_addstr(out, path);
65 else
66 strbuf_addstr(out, make_nonrelative_path(path));
67
68 strbuf_addch(out, PATH_SEP);
69 }
70 }
71
72 void setup_path(void)
73 {
74 const char *old_path = getenv("PATH");
75 struct strbuf new_path = STRBUF_INIT;
76
77 add_path(&new_path, argv_exec_path);
78 add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
79 add_path(&new_path, system_path(GIT_EXEC_PATH));
80 add_path(&new_path, argv0_path);
81
82 if (old_path)
83 strbuf_addstr(&new_path, old_path);
84 else
85 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
86
87 setenv("PATH", new_path.buf, 1);
88
89 strbuf_release(&new_path);
90 }
91
92 const char **prepare_git_cmd(const char **argv)
93 {
94 int argc;
95 const char **nargv;
96
97 for (argc = 0; argv[argc]; argc++)
98 ; /* just counting */
99 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
100
101 nargv[0] = "git";
102 for (argc = 0; argv[argc]; argc++)
103 nargv[argc + 1] = argv[argc];
104 nargv[argc + 1] = NULL;
105 return nargv;
106 }
107
108 int execv_git_cmd(const char **argv) {
109 const char **nargv = prepare_git_cmd(argv);
110 trace_argv_printf(nargv, "trace: exec:");
111
112 /* execvp() can only ever return if it fails */
113 execvp("git", (char **)nargv);
114
115 trace_printf("trace: exec failed: %s\n", strerror(errno));
116
117 free(nargv);
118 return -1;
119 }
120
121
122 int execl_git_cmd(const char *cmd,...)
123 {
124 int argc;
125 const char *argv[MAX_ARGS + 1];
126 const char *arg;
127 va_list param;
128
129 va_start(param, cmd);
130 argv[0] = cmd;
131 argc = 1;
132 while (argc < MAX_ARGS) {
133 arg = argv[argc++] = va_arg(param, char *);
134 if (!arg)
135 break;
136 }
137 va_end(param);
138 if (MAX_ARGS <= argc)
139 return error("too many args to run %s", cmd);
140
141 argv[argc] = NULL;
142 return execv_git_cmd(argv);
143 }