4 #include "spawn-pipe.h"
8 static const char *current_exec_path
;
10 static const char *builtin_exec_path(void)
21 len
= strlen(_pgmptr
);
25 p
= ep
= xmalloc(len
+1);
28 /* copy program name, turn '\\' into '/', skip last part */
30 if (*q
== '\\' || *q
== '/') {
39 ep
[0] = '.', ep
[1] = '\0';
44 void git_set_exec_path(const char *exec_path
)
46 current_exec_path
= exec_path
;
50 /* Returns the highest-priority, location to look for git programs. */
51 const char *git_exec_path(void)
55 if (current_exec_path
)
56 return current_exec_path
;
58 env
= getenv(EXEC_PATH_ENVIRONMENT
);
63 return builtin_exec_path();
67 int execv_git_cmd(const char **argv
)
69 char git_command
[PATH_MAX
+ 1];
71 const char *paths
[] = { current_exec_path
,
72 getenv(EXEC_PATH_ENVIRONMENT
),
73 builtin_exec_path() };
75 for (i
= 0; i
< ARRAY_SIZE(paths
); ++i
) {
78 const char *exec_dir
= paths
[i
];
81 if (!exec_dir
|| !*exec_dir
) continue;
84 if (*exec_dir
!= '/' && exec_dir
[1] != ':') {
86 if (*exec_dir
!= '/') {
88 if (!getcwd(git_command
, sizeof(git_command
))) {
89 fprintf(stderr
, "git: cannot determine "
90 "current directory: %s\n",
94 len
= strlen(git_command
);
97 while (!prefixcmp(exec_dir
, "./")) {
99 while (*exec_dir
== '/')
103 rc
= snprintf(git_command
+ len
,
104 sizeof(git_command
) - len
, "/%s",
106 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
107 fprintf(stderr
, "git: command name given "
112 if (strlen(exec_dir
) + 1 > sizeof(git_command
)) {
113 fprintf(stderr
, "git: command name given "
117 strcpy(git_command
, exec_dir
);
120 len
= strlen(git_command
);
121 rc
= snprintf(git_command
+ len
, sizeof(git_command
) - len
,
123 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
125 "git: command name given is too long.\n");
129 /* argv[0] must be the git command, but the argv array
130 * belongs to the caller, and my be reused in
131 * subsequent loop iterations. Save argv[0] and
132 * restore it on error.
136 argv
[0] = git_command
;
138 trace_argv_printf(argv
, -1, "trace: exec:");
140 /* execve() can only ever return if it fails */
141 execve(git_command
, (char **)argv
, environ
);
143 trace_printf("trace: exec failed: %s\n", strerror(errno
));
152 int execl_git_cmd(const char *cmd
,...)
155 const char *argv
[MAX_ARGS
+ 1];
159 va_start(param
, cmd
);
162 while (argc
< MAX_ARGS
) {
163 arg
= argv
[argc
++] = va_arg(param
, char *);
168 if (MAX_ARGS
<= argc
)
169 return error("too many args to run %s", cmd
);
172 return execv_git_cmd(argv
);
175 int spawnve_git_cmd(const char **argv
, int pin
[2], int pout
[2], char **envp
)
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 **new_argv
;
186 for (i
= 0; i
< ARRAY_SIZE(paths
); ++i
) {
188 const char *exec_dir
= paths
[i
];
190 if (!exec_dir
|| !*exec_dir
) continue;
193 if (*exec_dir
!= '/' && exec_dir
[1] != ':') {
195 if (*exec_dir
!= '/') {
197 if (!getcwd(p
[i
], sizeof(p
[i
]))) {
198 fprintf(stderr
, "git: cannot determine "
199 "current directory: %s\n",
205 /* Trivial cleanup */
206 while (!strncmp(exec_dir
, "./", 2)) {
208 while (*exec_dir
== '/')
212 rc
= snprintf(p
[i
] + len
,
213 sizeof(p
[i
]) - len
, "/%s",
215 if (rc
< 0 || rc
>= sizeof(p
[i
]) - len
) {
216 fprintf(stderr
, "git: command name given "
221 if (strlen(exec_dir
) + 1 > sizeof(p
[i
])) {
222 fprintf(stderr
, "git: command name given "
226 strcpy(p
[i
], exec_dir
);
232 for (rc
= 0;argv
[rc
];rc
++);
234 new_argv
= malloc(sizeof(char*)*(rc
+2)); /* git command plus null */
236 new_argv
[1] = argv
[0];
237 memcpy(&new_argv
[2], &argv
[1], sizeof(char*)*rc
);
239 trace_argv_printf(new_argv
, -1, "trace: exec:");
241 pid
= spawnvppe_pipe(new_argv
[0], new_argv
, envp
, usedpaths
,
250 int spawnv_git_cmd(const char **argv
, int pin
[2], int pout
[2])
252 return spawnve_git_cmd(argv
, pin
, pout
, environ
);