git_exec_path: avoid Coverity warning about unfree()d result
[git/git.git] / exec_cmd.c
CommitLineData
77cb17e9
MO
1#include "cache.h"
2#include "exec_cmd.h"
575ba9d6 3#include "quote.h"
20574f55 4#include "argv-array.h"
77cb17e9
MO
5#define MAX_ARGS 32
6
384df833 7static const char *argv_exec_path;
e1464ca7 8static const char *argv0_path;
77cb17e9 9
59362e56 10char *system_path(const char *path)
2de9de5e 11{
35fb0e86
SP
12#ifdef RUNTIME_PREFIX
13 static const char *prefix;
14#else
026fa0d5 15 static const char *prefix = PREFIX;
35fb0e86 16#endif
026fa0d5
SP
17 struct strbuf d = STRBUF_INIT;
18
19 if (is_absolute_path(path))
59362e56 20 return xstrdup(path);
026fa0d5 21
35fb0e86
SP
22#ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
25
024aa7d8
JS
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"))) {
35fb0e86 30 prefix = PREFIX;
aa094570 31 trace_printf("RUNTIME_PREFIX requested, "
35fb0e86
SP
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
34 }
35#endif
36
026fa0d5 37 strbuf_addf(&d, "%s/%s", prefix, path);
59362e56 38 return strbuf_detach(&d, NULL);
2de9de5e
SP
39}
40
4dd47c3b 41const char *git_extract_argv0_path(const char *argv0)
e1464ca7 42{
2cd72b0b
SP
43 const char *slash;
44
45 if (!argv0 || !*argv0)
46 return NULL;
4dd47c3b 47
f4598233 48 slash = find_last_dir_sep(argv0);
4dd47c3b 49
f4598233 50 if (slash) {
4dd47c3b
SH
51 argv0_path = xstrndup(argv0, slash - argv0);
52 return slash + 1;
53 }
54
55 return argv0;
e1464ca7
JS
56}
57
384df833 58void git_set_argv_exec_path(const char *exec_path)
77cb17e9 59{
384df833 60 argv_exec_path = exec_path;
c90d565a
JS
61 /*
62 * Propagate this setting to external programs.
63 */
64 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
77cb17e9
MO
65}
66
67
68/* Returns the highest-priority, location to look for git programs. */
962554c6 69const char *git_exec_path(void)
77cb17e9
MO
70{
71 const char *env;
c9bb5d10 72 static char *system_exec_path;
77cb17e9 73
384df833
SP
74 if (argv_exec_path)
75 return argv_exec_path;
77cb17e9 76
d4ebc36c 77 env = getenv(EXEC_PATH_ENVIRONMENT);
2b601626 78 if (env && *env) {
77cb17e9
MO
79 return env;
80 }
81
c9bb5d10
JS
82 if (!system_exec_path)
83 system_exec_path = system_path(GIT_EXEC_PATH);
84 return system_exec_path;
77cb17e9
MO
85}
86
511707d4
SP
87static void add_path(struct strbuf *out, const char *path)
88{
89 if (path && *path) {
9610decf 90 strbuf_add_absolute_path(out, path);
80ba074f 91 strbuf_addch(out, PATH_SEP);
511707d4
SP
92 }
93}
94
e1464ca7 95void setup_path(void)
511707d4
SP
96{
97 const char *old_path = getenv("PATH");
f285a2d7 98 struct strbuf new_path = STRBUF_INIT;
511707d4 99
8e346283 100 add_path(&new_path, git_exec_path());
511707d4
SP
101
102 if (old_path)
103 strbuf_addstr(&new_path, old_path);
104 else
cb6a22c0 105 strbuf_addstr(&new_path, _PATH_DEFPATH);
511707d4
SP
106
107 setenv("PATH", new_path.buf, 1);
108
109 strbuf_release(&new_path);
110}
77cb17e9 111
20574f55 112const char **prepare_git_cmd(struct argv_array *out, const char **argv)
77cb17e9 113{
20574f55
JK
114 argv_array_push(out, "git");
115 argv_array_pushv(out, argv);
116 return out->argv;
4933e5eb
SP
117}
118
119int execv_git_cmd(const char **argv) {
20574f55
JK
120 struct argv_array nargv = ARGV_ARRAY_INIT;
121
122 prepare_git_cmd(&nargv, argv);
123 trace_argv_printf(nargv.argv, "trace: exec:");
575ba9d6 124
511707d4 125 /* execvp() can only ever return if it fails */
20574f55 126 sane_execvp("git", (char **)nargv.argv);
77cb17e9 127
511707d4 128 trace_printf("trace: exec failed: %s\n", strerror(errno));
575ba9d6 129
20574f55 130 argv_array_clear(&nargv);
511707d4 131 return -1;
77cb17e9
MO
132}
133
134
9201c707 135int execl_git_cmd(const char *cmd,...)
77cb17e9
MO
136{
137 int argc;
9201c707
JH
138 const char *argv[MAX_ARGS + 1];
139 const char *arg;
77cb17e9
MO
140 va_list param;
141
142 va_start(param, cmd);
143 argv[0] = cmd;
144 argc = 1;
145 while (argc < MAX_ARGS) {
146 arg = argv[argc++] = va_arg(param, char *);
147 if (!arg)
148 break;
149 }
150 va_end(param);
151 if (MAX_ARGS <= argc)
152 return error("too many args to run %s", cmd);
153
154 argv[argc] = NULL;
155 return execv_git_cmd(argv);
156}