git_exec_path: avoid Coverity warning about unfree()d result
[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
48         slash = find_last_dir_sep(argv0);
49
50         if (slash) {
51                 argv0_path = xstrndup(argv0, slash - argv0);
52                 return slash + 1;
53         }
54
55         return argv0;
56 }
57
58 void git_set_argv_exec_path(const char *exec_path)
59 {
60         argv_exec_path = exec_path;
61         /*
62          * Propagate this setting to external programs.
63          */
64         setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
65 }
66
67
68 /* Returns the highest-priority, location to look for git programs. */
69 const char *git_exec_path(void)
70 {
71         const char *env;
72         static char *system_exec_path;
73
74         if (argv_exec_path)
75                 return argv_exec_path;
76
77         env = getenv(EXEC_PATH_ENVIRONMENT);
78         if (env && *env) {
79                 return env;
80         }
81
82         if (!system_exec_path)
83                 system_exec_path = system_path(GIT_EXEC_PATH);
84         return system_exec_path;
85 }
86
87 static void add_path(struct strbuf *out, const char *path)
88 {
89         if (path && *path) {
90                 strbuf_add_absolute_path(out, path);
91                 strbuf_addch(out, PATH_SEP);
92         }
93 }
94
95 void setup_path(void)
96 {
97         const char *old_path = getenv("PATH");
98         struct strbuf new_path = STRBUF_INIT;
99
100         add_path(&new_path, git_exec_path());
101
102         if (old_path)
103                 strbuf_addstr(&new_path, old_path);
104         else
105                 strbuf_addstr(&new_path, _PATH_DEFPATH);
106
107         setenv("PATH", new_path.buf, 1);
108
109         strbuf_release(&new_path);
110 }
111
112 const char **prepare_git_cmd(struct argv_array *out, const char **argv)
113 {
114         argv_array_push(out, "git");
115         argv_array_pushv(out, argv);
116         return out->argv;
117 }
118
119 int execv_git_cmd(const char **argv) {
120         struct argv_array nargv = ARGV_ARRAY_INIT;
121
122         prepare_git_cmd(&nargv, argv);
123         trace_argv_printf(nargv.argv, "trace: exec:");
124
125         /* execvp() can only ever return if it fails */
126         sane_execvp("git", (char **)nargv.argv);
127
128         trace_printf("trace: exec failed: %s\n", strerror(errno));
129
130         argv_array_clear(&nargv);
131         return -1;
132 }
133
134
135 int execl_git_cmd(const char *cmd,...)
136 {
137         int argc;
138         const char *argv[MAX_ARGS + 1];
139         const char *arg;
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 }