Add a simple getpass() for MinGW
[4msysgit-hv.git] / exec_cmd.c
blob13e036fc37684800d834289cd44ba47c2cf59865
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #define MAX_ARGS 32
6 extern char **environ;
7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 const char *system_path(const char *path)
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;
19 if (is_absolute_path(path))
20 return path;
22 #ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
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;
32 * RUNTIME_PREFIX requested, but prefix computation failed.
33 * Using static fallback.
36 #endif
38 strbuf_addf(&d, "%s/%s", prefix, path);
39 path = strbuf_detach(&d, NULL);
40 return path;
43 const char *git_extract_argv0_path(const char *argv0)
45 const char *slash;
47 if (!argv0 || !*argv0)
48 return NULL;
49 slash = argv0 + strlen(argv0);
51 while (argv0 <= slash && !is_dir_sep(*slash))
52 slash--;
54 if (slash >= argv0) {
55 argv0_path = xstrndup(argv0, slash - argv0);
56 return slash + 1;
59 return argv0;
62 void git_set_argv_exec_path(const char *exec_path)
64 argv_exec_path = exec_path;
66 * Propagate this setting to external programs.
68 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
72 /* Returns the highest-priority, location to look for git programs. */
73 const char *git_exec_path(void)
75 const char *env;
77 if (argv_exec_path)
78 return argv_exec_path;
80 env = getenv(EXEC_PATH_ENVIRONMENT);
81 if (env && *env) {
82 return env;
85 return system_path(GIT_EXEC_PATH);
88 static void add_path(struct strbuf *out, const char *path)
90 if (path && *path) {
91 if (is_absolute_path(path))
92 strbuf_addstr(out, path);
93 else
94 strbuf_addstr(out, make_nonrelative_path(path));
96 strbuf_addch(out, PATH_SEP);
100 void setup_path(void)
102 const char *old_path = getenv("PATH");
103 struct strbuf new_path = STRBUF_INIT;
105 add_path(&new_path, git_exec_path());
106 add_path(&new_path, argv0_path);
108 if (old_path)
109 strbuf_addstr(&new_path, old_path);
110 else
111 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
113 setenv("PATH", new_path.buf, 1);
115 strbuf_release(&new_path);
118 const char **prepare_git_cmd(const char **argv)
120 int argc;
121 const char **nargv;
123 for (argc = 0; argv[argc]; argc++)
124 ; /* just counting */
125 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
127 nargv[0] = "git";
128 for (argc = 0; argv[argc]; argc++)
129 nargv[argc + 1] = argv[argc];
130 nargv[argc + 1] = NULL;
131 return nargv;
134 int execv_git_cmd(const char **argv) {
135 const char **nargv = prepare_git_cmd(argv);
136 trace_argv_printf(nargv, "trace: exec:");
138 /* execvp() can only ever return if it fails */
139 execvp("git", (char **)nargv);
141 trace_printf("trace: exec failed: %s\n", strerror(errno));
143 free(nargv);
144 return -1;
148 int execl_git_cmd(const char *cmd,...)
150 int argc;
151 const char *argv[MAX_ARGS + 1];
152 const char *arg;
153 va_list param;
155 va_start(param, cmd);
156 argv[0] = cmd;
157 argc = 1;
158 while (argc < MAX_ARGS) {
159 arg = argv[argc++] = va_arg(param, char *);
160 if (!arg)
161 break;
163 va_end(param);
164 if (MAX_ARGS <= argc)
165 return error("too many args to run %s", cmd);
167 argv[argc] = NULL;
168 return execv_git_cmd(argv);