Make the pager work.
[4msysgit-hv.git] / exec_cmd.c
blob68c39e348cba6aef870705a1455b35186ae6e599
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 *builtin_exec_path = GIT_EXEC_PATH;
8 static const char *current_exec_path;
10 void git_set_exec_path(const char *exec_path)
12 current_exec_path = exec_path;
16 /* Returns the highest-priority, location to look for git programs. */
17 const char *git_exec_path(void)
19 const char *env;
21 if (current_exec_path)
22 return current_exec_path;
24 env = getenv(EXEC_PATH_ENVIRONMENT);
25 if (env && *env) {
26 return env;
29 return builtin_exec_path;
33 int execv_git_cmd(const char **argv)
35 char git_command[PATH_MAX + 1];
36 int i;
37 const char *paths[] = { current_exec_path,
38 getenv(EXEC_PATH_ENVIRONMENT),
39 builtin_exec_path };
41 for (i = 0; i < ARRAY_SIZE(paths); ++i) {
42 size_t len;
43 int rc;
44 const char *exec_dir = paths[i];
45 const char *tmp;
47 if (!exec_dir || !*exec_dir) continue;
49 #ifdef __MINGW32__
50 if (*exec_dir != '/' && exec_dir[1] != ':') {
51 #else
52 if (*exec_dir != '/') {
53 #endif
54 if (!getcwd(git_command, sizeof(git_command))) {
55 fprintf(stderr, "git: cannot determine "
56 "current directory: %s\n",
57 strerror(errno));
58 break;
60 len = strlen(git_command);
62 /* Trivial cleanup */
63 while (!strncmp(exec_dir, "./", 2)) {
64 exec_dir += 2;
65 while (*exec_dir == '/')
66 exec_dir++;
69 rc = snprintf(git_command + len,
70 sizeof(git_command) - len, "/%s",
71 exec_dir);
72 if (rc < 0 || rc >= sizeof(git_command) - len) {
73 fprintf(stderr, "git: command name given "
74 "is too long.\n");
75 break;
77 } else {
78 if (strlen(exec_dir) + 1 > sizeof(git_command)) {
79 fprintf(stderr, "git: command name given "
80 "is too long.\n");
81 break;
83 strcpy(git_command, exec_dir);
86 len = strlen(git_command);
87 rc = snprintf(git_command + len, sizeof(git_command) - len,
88 "/git-%s", argv[0]);
89 if (rc < 0 || rc >= sizeof(git_command) - len) {
90 fprintf(stderr,
91 "git: command name given is too long.\n");
92 break;
95 /* argv[0] must be the git command, but the argv array
96 * belongs to the caller, and my be reused in
97 * subsequent loop iterations. Save argv[0] and
98 * restore it on error.
101 tmp = argv[0];
102 argv[0] = git_command;
104 trace_argv_printf(argv, -1, "trace: exec:");
106 /* execve() can only ever return if it fails */
107 execve(git_command, (char **)argv, environ);
109 trace_printf("trace: exec failed: %s\n", strerror(errno));
111 argv[0] = tmp;
113 return -1;
118 int execl_git_cmd(const char *cmd,...)
120 int argc;
121 const char *argv[MAX_ARGS + 1];
122 const char *arg;
123 va_list param;
125 va_start(param, cmd);
126 argv[0] = cmd;
127 argc = 1;
128 while (argc < MAX_ARGS) {
129 arg = argv[argc++] = va_arg(param, char *);
130 if (!arg)
131 break;
133 va_end(param);
134 if (MAX_ARGS <= argc)
135 return error("too many args to run %s", cmd);
137 argv[argc] = NULL;
138 return execv_git_cmd(argv);