4 #include "spawn-pipe.h"
7 static const char *current_exec_path
;
9 static const char *builtin_exec_path(void)
20 len
= strlen(_pgmptr
);
24 p
= ep
= xmalloc(len
+1);
27 /* copy program name, turn '\\' into '/', skip last part */
29 if (*q
== '\\' || *q
== '/') {
38 ep
[0] = '.', ep
[1] = '\0';
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)
54 if (current_exec_path
)
55 return current_exec_path
;
57 env
= getenv(EXEC_PATH_ENVIRONMENT
);
62 return builtin_exec_path();
66 int execv_git_cmd(const char **argv
)
68 char git_command
[PATH_MAX
+ 1];
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
) {
77 const char *exec_dir
= paths
[i
];
80 if (!exec_dir
|| !*exec_dir
) continue;
83 if (*exec_dir
!= '/' && exec_dir
[1] != ':') {
85 if (*exec_dir
!= '/') {
87 if (!getcwd(git_command
, sizeof(git_command
))) {
88 fprintf(stderr
, "git: cannot determine "
89 "current directory: %s\n",
93 len
= strlen(git_command
);
96 while (!prefixcmp(exec_dir
, "./")) {
98 while (*exec_dir
== '/')
102 rc
= snprintf(git_command
+ len
,
103 sizeof(git_command
) - len
, "/%s",
105 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
106 fprintf(stderr
, "git: command name given "
111 if (strlen(exec_dir
) + 1 > sizeof(git_command
)) {
112 fprintf(stderr
, "git: command name given "
116 strcpy(git_command
, exec_dir
);
119 len
= strlen(git_command
);
120 rc
= snprintf(git_command
+ len
, sizeof(git_command
) - len
,
122 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
124 "git: command name given is too long.\n");
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.
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
));
151 int execl_git_cmd(const char *cmd
,...)
154 const char *argv
[MAX_ARGS
+ 1];
158 va_start(param
, cmd
);
161 while (argc
< MAX_ARGS
) {
162 arg
= argv
[argc
++] = va_arg(param
, char *);
167 if (MAX_ARGS
<= argc
)
168 return error("too many args to run %s", cmd
);
171 return execv_git_cmd(argv
);
174 int spawnv_git_cmd(const char **argv
, int pin
[2], int pout
[2])
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
;
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 rc
= snprintf(cmd
, sizeof(cmd
), "git-%s", argv
[0]);
233 if (rc
< 0 || rc
>= sizeof(cmd
)) {
235 "git: command name given is too long.\n");
239 /* argv[0] must be the git command, but the argv array
240 * belongs to the caller. Save argv[0] and
247 trace_argv_printf(argv
, -1, "trace: exec:");
249 pid
= spawnvppe_pipe(cmd
, argv
, (const char**) environ
, usedpaths
,