gitk: force focus to main window
[git/platforms.git] / exec_cmd.c
blob2a8e48b048e9db1d9912be826b0ce2f6662f1be0
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #include "spawn-pipe.h"
5 #define MAX_ARGS 32
7 static const char *current_exec_path;
9 static const char *builtin_exec_path(void)
11 #ifndef __MINGW32__
12 return GIT_EXEC_PATH;
13 #else
14 int len;
15 char *p, *q, *sl;
16 static char *ep;
17 if (ep)
18 return ep;
20 len = strlen(_pgmptr);
21 if (len < 2)
22 return ep = ".";
24 p = ep = xmalloc(len+1);
25 q = _pgmptr;
26 sl = NULL;
27 /* copy program name, turn '\\' into '/', skip last part */
28 while ((*p = *q)) {
29 if (*q == '\\' || *q == '/') {
30 *p = '/';
31 sl = p;
33 p++, q++;
35 if (sl)
36 *sl = '\0';
37 else
38 ep[0] = '.', ep[1] = '\0';
39 return ep;
40 #endif
43 void git_set_exec_path(const char *exec_path)
45 current_exec_path = exec_path;
49 /* Returns the highest-priority, location to look for git programs. */
50 const char *git_exec_path(void)
52 const char *env;
54 if (current_exec_path)
55 return current_exec_path;
57 env = getenv(EXEC_PATH_ENVIRONMENT);
58 if (env && *env) {
59 return env;
62 return builtin_exec_path();
66 int execv_git_cmd(const char **argv)
68 char git_command[PATH_MAX + 1];
69 int i;
70 const char *paths[] = { current_exec_path,
71 getenv(EXEC_PATH_ENVIRONMENT),
72 builtin_exec_path() };
74 for (i = 0; i < ARRAY_SIZE(paths); ++i) {
75 size_t len;
76 int rc;
77 const char *exec_dir = paths[i];
78 const char *tmp;
80 if (!exec_dir || !*exec_dir) continue;
82 #ifdef __MINGW32__
83 if (*exec_dir != '/' && exec_dir[1] != ':') {
84 #else
85 if (*exec_dir != '/') {
86 #endif
87 if (!getcwd(git_command, sizeof(git_command))) {
88 fprintf(stderr, "git: cannot determine "
89 "current directory: %s\n",
90 strerror(errno));
91 break;
93 len = strlen(git_command);
95 /* Trivial cleanup */
96 while (!prefixcmp(exec_dir, "./")) {
97 exec_dir += 2;
98 while (*exec_dir == '/')
99 exec_dir++;
102 rc = snprintf(git_command + len,
103 sizeof(git_command) - len, "/%s",
104 exec_dir);
105 if (rc < 0 || rc >= sizeof(git_command) - len) {
106 fprintf(stderr, "git: command name given "
107 "is too long.\n");
108 break;
110 } else {
111 if (strlen(exec_dir) + 1 > sizeof(git_command)) {
112 fprintf(stderr, "git: command name given "
113 "is too long.\n");
114 break;
116 strcpy(git_command, exec_dir);
119 len = strlen(git_command);
120 rc = snprintf(git_command + len, sizeof(git_command) - len,
121 "/git-%s", argv[0]);
122 if (rc < 0 || rc >= sizeof(git_command) - len) {
123 fprintf(stderr,
124 "git: command name given is too long.\n");
125 break;
128 /* argv[0] must be the git command, but the argv array
129 * belongs to the caller, and my be reused in
130 * subsequent loop iterations. Save argv[0] and
131 * restore it on error.
134 tmp = argv[0];
135 argv[0] = git_command;
137 trace_argv_printf(argv, -1, "trace: exec:");
139 /* execve() can only ever return if it fails */
140 execve(git_command, (const char **) argv, (const char **) environ);
142 trace_printf("trace: exec failed: %s\n", strerror(errno));
144 argv[0] = tmp;
146 return -1;
151 int execl_git_cmd(const char *cmd,...)
153 int argc;
154 const char *argv[MAX_ARGS + 1];
155 const char *arg;
156 va_list param;
158 va_start(param, cmd);
159 argv[0] = cmd;
160 argc = 1;
161 while (argc < MAX_ARGS) {
162 arg = argv[argc++] = va_arg(param, char *);
163 if (!arg)
164 break;
166 va_end(param);
167 if (MAX_ARGS <= argc)
168 return error("too many args to run %s", cmd);
170 argv[argc] = NULL;
171 return execv_git_cmd(argv);
174 int spawnv_git_cmd(const char **argv, int pin[2], int pout[2])
176 char cmd[100];
177 int i, rc;
178 pid_t pid;
179 const char *paths[] = { current_exec_path,
180 getenv(EXEC_PATH_ENVIRONMENT),
181 builtin_exec_path() };
182 char p[3][PATH_MAX + 1];
183 char *usedpaths[4], **up = usedpaths;
184 const char *tmp;
186 for (i = 0; i < ARRAY_SIZE(paths); ++i) {
187 size_t len;
188 const char *exec_dir = paths[i];
190 if (!exec_dir || !*exec_dir) continue;
192 #ifdef __MINGW32__
193 if (*exec_dir != '/' && exec_dir[1] != ':') {
194 #else
195 if (*exec_dir != '/') {
196 #endif
197 if (!getcwd(p[i], sizeof(p[i]))) {
198 fprintf(stderr, "git: cannot determine "
199 "current directory: %s\n",
200 strerror(errno));
201 return -1;
203 len = strlen(p[i]);
205 /* Trivial cleanup */
206 while (!strncmp(exec_dir, "./", 2)) {
207 exec_dir += 2;
208 while (*exec_dir == '/')
209 exec_dir++;
212 rc = snprintf(p[i] + len,
213 sizeof(p[i]) - len, "/%s",
214 exec_dir);
215 if (rc < 0 || rc >= sizeof(p[i]) - len) {
216 fprintf(stderr, "git: command name given "
217 "is too long.\n");
218 return -1;
220 } else {
221 if (strlen(exec_dir) + 1 > sizeof(p[i])) {
222 fprintf(stderr, "git: command name given "
223 "is too long.\n");
224 return -1;
226 strcpy(p[i], exec_dir);
228 *up++ = p[i];
230 *up = NULL;
232 rc = snprintf(cmd, sizeof(cmd), "git-%s", argv[0]);
233 if (rc < 0 || rc >= sizeof(cmd)) {
234 fprintf(stderr,
235 "git: command name given is too long.\n");
236 return -1;
239 /* argv[0] must be the git command, but the argv array
240 * belongs to the caller. Save argv[0] and
241 * restore it later.
244 tmp = argv[0];
245 argv[0] = cmd;
247 trace_argv_printf(argv, -1, "trace: exec:");
249 pid = spawnvppe_pipe(cmd, argv, (const char**) environ, usedpaths,
250 pin, pout);
252 argv[0] = tmp;
253 return pid;