Make the tab width used for whitespace checks configurable
[git/mjg.git] / exec_cmd.c
blobbf225706ee377b89035eb21f76f9957cfaf6363b
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;
31 trace_printf("RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
35 #endif
37 strbuf_addf(&d, "%s/%s", prefix, path);
38 path = strbuf_detach(&d, NULL);
39 return path;
42 const char *git_extract_argv0_path(const char *argv0)
44 const char *slash;
46 if (!argv0 || !*argv0)
47 return NULL;
48 slash = argv0 + strlen(argv0);
50 while (argv0 <= slash && !is_dir_sep(*slash))
51 slash--;
53 if (slash >= argv0) {
54 argv0_path = xstrndup(argv0, slash - argv0);
55 return slash + 1;
58 return argv0;
61 void git_set_argv_exec_path(const char *exec_path)
63 argv_exec_path = exec_path;
65 * Propagate this setting to external programs.
67 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
71 /* Returns the highest-priority, location to look for git programs. */
72 const char *git_exec_path(void)
74 const char *env;
76 if (argv_exec_path)
77 return argv_exec_path;
79 env = getenv(EXEC_PATH_ENVIRONMENT);
80 if (env && *env) {
81 return env;
84 return system_path(GIT_EXEC_PATH);
87 static void add_path(struct strbuf *out, const char *path)
89 if (path && *path) {
90 if (is_absolute_path(path))
91 strbuf_addstr(out, path);
92 else
93 strbuf_addstr(out, make_nonrelative_path(path));
95 strbuf_addch(out, PATH_SEP);
99 void setup_path(void)
101 const char *old_path = getenv("PATH");
102 struct strbuf new_path = STRBUF_INIT;
104 add_path(&new_path, git_exec_path());
105 add_path(&new_path, argv0_path);
107 if (old_path)
108 strbuf_addstr(&new_path, old_path);
109 else
110 strbuf_addstr(&new_path, _PATH_DEFPATH);
112 setenv("PATH", new_path.buf, 1);
114 strbuf_release(&new_path);
117 const char **prepare_git_cmd(const char **argv)
119 int argc;
120 const char **nargv;
122 for (argc = 0; argv[argc]; argc++)
123 ; /* just counting */
124 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
126 nargv[0] = "git";
127 for (argc = 0; argv[argc]; argc++)
128 nargv[argc + 1] = argv[argc];
129 nargv[argc + 1] = NULL;
130 return nargv;
133 int execv_git_cmd(const char **argv) {
134 const char **nargv = prepare_git_cmd(argv);
135 trace_argv_printf(nargv, "trace: exec:");
137 /* execvp() can only ever return if it fails */
138 execvp("git", (char **)nargv);
140 trace_printf("trace: exec failed: %s\n", strerror(errno));
142 free(nargv);
143 return -1;
147 int execl_git_cmd(const char *cmd,...)
149 int argc;
150 const char *argv[MAX_ARGS + 1];
151 const char *arg;
152 va_list param;
154 va_start(param, cmd);
155 argv[0] = cmd;
156 argc = 1;
157 while (argc < MAX_ARGS) {
158 arg = argv[argc++] = va_arg(param, char *);
159 if (!arg)
160 break;
162 va_end(param);
163 if (MAX_ARGS <= argc)
164 return error("too many args to run %s", cmd);
166 argv[argc] = NULL;
167 return execv_git_cmd(argv);