]> git.ipfire.org Git - thirdparty/git.git/blob - exec_cmd.c
ewah: convert to REALLOC_ARRAY, etc
[thirdparty/git.git] / exec_cmd.c
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #include "argv-array.h"
5 #define MAX_ARGS 32
6
7 static const char *argv_exec_path;
8 static const char *argv0_path;
9
10 char *system_path(const char *path)
11 {
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;
18
19 if (is_absolute_path(path))
20 return xstrdup(path);
21
22 #ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
25
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 trace_printf("RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
34 }
35 #endif
36
37 strbuf_addf(&d, "%s/%s", prefix, path);
38 return strbuf_detach(&d, NULL);
39 }
40
41 const char *git_extract_argv0_path(const char *argv0)
42 {
43 const char *slash;
44
45 if (!argv0 || !*argv0)
46 return NULL;
47 slash = argv0 + strlen(argv0);
48
49 while (argv0 <= slash && !is_dir_sep(*slash))
50 slash--;
51
52 if (slash >= argv0) {
53 argv0_path = xstrndup(argv0, slash - argv0);
54 return slash + 1;
55 }
56
57 return argv0;
58 }
59
60 void git_set_argv_exec_path(const char *exec_path)
61 {
62 argv_exec_path = exec_path;
63 /*
64 * Propagate this setting to external programs.
65 */
66 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
67 }
68
69
70 /* Returns the highest-priority, location to look for git programs. */
71 const char *git_exec_path(void)
72 {
73 const char *env;
74
75 if (argv_exec_path)
76 return argv_exec_path;
77
78 env = getenv(EXEC_PATH_ENVIRONMENT);
79 if (env && *env) {
80 return env;
81 }
82
83 return system_path(GIT_EXEC_PATH);
84 }
85
86 static void add_path(struct strbuf *out, const char *path)
87 {
88 if (path && *path) {
89 strbuf_add_absolute_path(out, path);
90 strbuf_addch(out, PATH_SEP);
91 }
92 }
93
94 void setup_path(void)
95 {
96 const char *old_path = getenv("PATH");
97 struct strbuf new_path = STRBUF_INIT;
98
99 add_path(&new_path, git_exec_path());
100
101 if (old_path)
102 strbuf_addstr(&new_path, old_path);
103 else
104 strbuf_addstr(&new_path, _PATH_DEFPATH);
105
106 setenv("PATH", new_path.buf, 1);
107
108 strbuf_release(&new_path);
109 }
110
111 const char **prepare_git_cmd(struct argv_array *out, const char **argv)
112 {
113 argv_array_push(out, "git");
114 argv_array_pushv(out, argv);
115 return out->argv;
116 }
117
118 int execv_git_cmd(const char **argv) {
119 struct argv_array nargv = ARGV_ARRAY_INIT;
120
121 prepare_git_cmd(&nargv, argv);
122 trace_argv_printf(nargv.argv, "trace: exec:");
123
124 /* execvp() can only ever return if it fails */
125 sane_execvp("git", (char **)nargv.argv);
126
127 trace_printf("trace: exec failed: %s\n", strerror(errno));
128
129 argv_array_clear(&nargv);
130 return -1;
131 }
132
133
134 int execl_git_cmd(const char *cmd,...)
135 {
136 int argc;
137 const char *argv[MAX_ARGS + 1];
138 const char *arg;
139 va_list param;
140
141 va_start(param, cmd);
142 argv[0] = cmd;
143 argc = 1;
144 while (argc < MAX_ARGS) {
145 arg = argv[argc++] = va_arg(param, char *);
146 if (!arg)
147 break;
148 }
149 va_end(param);
150 if (MAX_ARGS <= argc)
151 return error("too many args to run %s", cmd);
152
153 argv[argc] = NULL;
154 return execv_git_cmd(argv);
155 }