aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2017-08-29 14:12:58 +0100
committerRon Yorston <rmy@pobox.com>2017-08-30 17:17:14 +0100
commit8cd264102aa2d954be9bbdbf84d3f4ab75189904 (patch)
tree11fc8c5c534753eaa474eb86122cec7d9cec4198
parent97e2c4a05e8efa0e3e3a3c256daa05676dc5484e (diff)
downloadbusybox-w32-8cd264102aa2d954be9bbdbf84d3f4ab75189904.tar.gz
busybox-w32-8cd264102aa2d954be9bbdbf84d3f4ab75189904.tar.bz2
busybox-w32-8cd264102aa2d954be9bbdbf84d3f4ab75189904.zip
win32: add a function to find executable on PATH
The function find_executable is more generic than needed here: it can search for all matches on PATH whereas we only want the first. Implement find_first_executable to do that.
-rw-r--r--win32/process.c64
1 files changed, 30 insertions, 34 deletions
diff --git a/win32/process.c b/win32/process.c
index e47fbac1e..07c2e49df 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -173,6 +173,21 @@ quote_arg(const char *arg)
173 return q; 173 return q;
174} 174}
175 175
176static char *
177find_first_executable(const char *name)
178{
179 char *tmp, *path = getenv("PATH");
180 char *exe_path = NULL;
181
182 if (path) {
183 tmp = path = xstrdup(path);
184 exe_path = find_executable(name, &tmp);
185 free(path);
186 }
187
188 return exe_path;
189}
190
176static intptr_t 191static intptr_t
177spawnveq(int mode, const char *path, char *const *argv, char *const *env) 192spawnveq(int mode, const char *path, char *const *argv, char *const *env)
178{ 193{
@@ -238,7 +253,6 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv, char *con
238 (fullpath=file_is_win32_executable(int_path)) != NULL) { 253 (fullpath=file_is_win32_executable(int_path)) != NULL) {
239 new_argv[0] = fullpath ? fullpath : int_path; 254 new_argv[0] = fullpath ? fullpath : int_path;
240 ret = spawnveq(mode, new_argv[0], new_argv, envp); 255 ret = spawnveq(mode, new_argv[0], new_argv, envp);
241 free(fullpath);
242 } else 256 } else
243#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE 257#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE
244 if (find_applet_by_name(int_name) >= 0) { 258 if (find_applet_by_name(int_name) >= 0) {
@@ -246,22 +260,16 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv, char *con
246 ret = mingw_spawn_applet(mode, new_argv, envp); 260 ret = mingw_spawn_applet(mode, new_argv, envp);
247 } else 261 } else
248#endif 262#endif
249 { 263 if ((fullpath=find_first_executable(int_name)) != NULL) {
250 char *path = xstrdup(getenv("PATH"));
251 char *tmp = path;
252
253 fullpath = find_executable(int_name, &tmp);
254 free(path);
255 if (!fullpath) {
256 free(new_argv);
257 errno = ENOENT;
258 return -1;
259 }
260 new_argv[0] = fullpath; 264 new_argv[0] = fullpath;
261 ret = spawnveq(mode, fullpath, new_argv, envp); 265 ret = spawnveq(mode, fullpath, new_argv, envp);
262 free(fullpath); 266 }
267 else {
268 errno = ENOENT;
269 ret = -1;
263 } 270 }
264 271
272 free(fullpath);
265 free(new_argv); 273 free(new_argv);
266 return ret; 274 return ret;
267} 275}
@@ -269,36 +277,24 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv, char *con
269static intptr_t 277static intptr_t
270mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp) 278mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp)
271{ 279{
272 intptr_t ret; 280 char *prog;
273 281
274#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE 282#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE
275 if (find_applet_by_name(cmd) >= 0) 283 if (find_applet_by_name(cmd) >= 0)
276 return mingw_spawn_applet(mode, argv, envp); 284 return mingw_spawn_applet(mode, argv, envp);
277 else 285 else
278#endif 286#endif
279 if (strchr(cmd, '/') || strchr(cmd, '\\')) 287 if (strchr(cmd, '/') || strchr(cmd, '\\')) {
280 return mingw_spawn_interpreter(mode, cmd, argv, envp); 288 return mingw_spawn_interpreter(mode, cmd, argv, envp);
281 else { 289 }
282 char *tmp, *path = getenv("PATH"); 290 else if ((prog=find_first_executable(cmd)) != NULL) {
283 char *prog; 291 intptr_t ret = mingw_spawn_interpreter(mode, prog, argv, envp);
284
285 if (!path) {
286 errno = ENOENT;
287 return -1;
288 }
289
290 /* executable_exists() does not return new file name */
291 tmp = path = xstrdup(path);
292 prog = find_executable(cmd, &tmp);
293 free(path);
294 if (!prog) {
295 errno = ENOENT;
296 return -1;
297 }
298 ret = mingw_spawn_interpreter(mode, prog, argv, envp);
299 free(prog); 292 free(prog);
293 return ret;
300 } 294 }
301 return ret; 295
296 errno = ENOENT;
297 return -1;
302} 298}
303 299
304pid_t FAST_FUNC 300pid_t FAST_FUNC