format-patch: resurrect extra headers from config
[git/spearce.git] / exec_cmd.c
blobc1539d12ce9e350811b5b110206f5577a151e8ef
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #define MAX_ARGS 32
5 extern char **environ;
6 static const char *builtin_exec_path = GIT_EXEC_PATH;
7 static const char *current_exec_path = NULL;
9 void git_set_exec_path(const char *exec_path)
11 current_exec_path = exec_path;
15 /* Returns the highest-priority, location to look for git programs. */
16 const char *git_exec_path(void)
18 const char *env;
20 if (current_exec_path)
21 return current_exec_path;
23 env = getenv("GIT_EXEC_PATH");
24 if (env && *env) {
25 return env;
28 return builtin_exec_path;
32 int execv_git_cmd(const char **argv)
34 char git_command[PATH_MAX + 1];
35 int i;
36 const char *paths[] = { current_exec_path,
37 getenv("GIT_EXEC_PATH"),
38 builtin_exec_path };
40 for (i = 0; i < ARRAY_SIZE(paths); ++i) {
41 size_t len;
42 int rc;
43 const char *exec_dir = paths[i];
44 const char *tmp;
46 if (!exec_dir || !*exec_dir) continue;
48 if (*exec_dir != '/') {
49 if (!getcwd(git_command, sizeof(git_command))) {
50 fprintf(stderr, "git: cannot determine "
51 "current directory: %s\n",
52 strerror(errno));
53 break;
55 len = strlen(git_command);
57 /* Trivial cleanup */
58 while (!strncmp(exec_dir, "./", 2)) {
59 exec_dir += 2;
60 while (*exec_dir == '/')
61 exec_dir++;
64 rc = snprintf(git_command + len,
65 sizeof(git_command) - len, "/%s",
66 exec_dir);
67 if (rc < 0 || rc >= sizeof(git_command) - len) {
68 fprintf(stderr, "git: command name given "
69 "is too long.\n");
70 break;
72 } else {
73 if (strlen(exec_dir) + 1 > sizeof(git_command)) {
74 fprintf(stderr, "git: command name given "
75 "is too long.\n");
76 break;
78 strcpy(git_command, exec_dir);
81 len = strlen(git_command);
82 rc = snprintf(git_command + len, sizeof(git_command) - len,
83 "/git-%s", argv[0]);
84 if (rc < 0 || rc >= sizeof(git_command) - len) {
85 fprintf(stderr,
86 "git: command name given is too long.\n");
87 break;
90 /* argv[0] must be the git command, but the argv array
91 * belongs to the caller, and my be reused in
92 * subsequent loop iterations. Save argv[0] and
93 * restore it on error.
96 tmp = argv[0];
97 argv[0] = git_command;
99 /* execve() can only ever return if it fails */
100 execve(git_command, (char **)argv, environ);
102 argv[0] = tmp;
104 return -1;
109 int execl_git_cmd(const char *cmd,...)
111 int argc;
112 const char *argv[MAX_ARGS + 1];
113 const char *arg;
114 va_list param;
116 va_start(param, cmd);
117 argv[0] = cmd;
118 argc = 1;
119 while (argc < MAX_ARGS) {
120 arg = argv[argc++] = va_arg(param, char *);
121 if (!arg)
122 break;
124 va_end(param);
125 if (MAX_ARGS <= argc)
126 return error("too many args to run %s", cmd);
128 argv[argc] = NULL;
129 return execv_git_cmd(argv);