From 4066aff5e481941585c5958460c39a1b1399ce88 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 9 May 2012 15:16:44 +0100 Subject: Use win32_execable_file() in test, which and execable.c --- coreutils/test.c | 26 +++++++++----------------- debianutils/which.c | 23 ----------------------- include/mingw.h | 2 ++ libbb/execable.c | 31 +++++++++---------------------- win32/mingw.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 62 deletions(-) diff --git a/coreutils/test.c b/coreutils/test.c index ccfa923da..80f540c22 100644 --- a/coreutils/test.c +++ b/coreutils/test.c @@ -648,24 +648,16 @@ static int filstat(char *nm, enum token mode) #undef W_OK #define W_OK S_IWRITE if (mode == FILEX) { - int len = strlen(nm), ret; - if (len >= 4 && - (!strcmp(nm+len-4,".exe") || - !strcmp(nm+len-4,".com"))) - ret = stat(nm, &s); - else { - char *exepath; - exepath = malloc(len+5); - memcpy(exepath, nm, len); - memcpy(exepath+len, ".exe", 5); - ret = stat(exepath, &s); - if (ret < 0) { - memcpy(exepath+len, ".exe", 5); - ret = stat(exepath, &s); - } - free(exepath); + char *p; + + if (execable_file(nm)) { + return 1; } - return ret >= 0; + else if ((p=win32_execable_file(nm))) { + free(p); + return 1; + } + return 0; } #endif diff --git a/debianutils/which.c b/debianutils/which.c index 2b5804e00..bcca95331 100644 --- a/debianutils/which.c +++ b/debianutils/which.c @@ -21,29 +21,6 @@ #include "libbb.h" -#if ENABLE_PLATFORM_MINGW32 -static char *win32_execable_file(const char *p) -{ - char *path; - int len = strlen(p) + 5; - - if ( (path=malloc(len)) != NULL ) { - memcpy(path, p, len); - memcpy(path+len, ".exe", 5); - if (execable_file(path)) { - return path; - } - memcpy(path+len, ".com", 5); - if (execable_file(path)) { - return path; - } - free(path); - } - - return NULL; -} -#endif - int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int which_main(int argc UNUSED_PARAM, char **argv) { diff --git a/include/mingw.h b/include/mingw.h index 1ef13c638..fdcd7f709 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -386,3 +386,5 @@ char **env_setenv(char **env, const char *name); const char *get_busybox_exec_path(void); void init_winsock(void); + +char *win32_execable_file(const char *p); diff --git a/libbb/execable.c b/libbb/execable.c index c2a7bf0ca..ae61a8800 100644 --- a/libbb/execable.c +++ b/libbb/execable.c @@ -35,6 +35,9 @@ int FAST_FUNC execable_file(const char *name) char* FAST_FUNC find_execable(const char *filename, char **PATHp) { char *p, *n; +#if ENABLE_PLATFORM_MINGW32 + char *w; +#endif p = *PATHp; while (p) { @@ -47,29 +50,13 @@ char* FAST_FUNC find_execable(const char *filename, char **PATHp) *PATHp = n; return p; } - if (ENABLE_PLATFORM_MINGW32) { - int len = strlen(p); - if (len > 4 && - (!strcasecmp(p+len-4, ".exe") || - !strcasecmp(p+len-4, ".com"))) - ; /* nothing, already tested by execable_file() */ - else { - char *np = xmalloc(len+4+1); - memcpy(np, p, len); - memcpy(np+len, ".exe", 5); - if (execable_file(np)) { - *PATHp = n; - free(p); - return np; - } - memcpy(np+len, ".com", 5); - if (execable_file(np)) { - *PATHp = n; - free(p); - return np; - } - } +#if ENABLE_PLATFORM_MINGW32 + else if ((w=win32_execable_file(p))) { + *PATHp = n; + free(p); + return w; } +#endif free(p); } p = n; diff --git a/win32/mingw.c b/win32/mingw.c index a6c969596..7dda6cd1b 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -830,3 +830,35 @@ int mingw_access(const char *name, int mode) return -1; } + +/* check if path can be made into an executable by adding a suffix; + * return an allocated string containing the path if it can; + * return NULL if not. + * + * if path already has a suffix don't even bother trying + */ +char *win32_execable_file(const char *p) +{ + char *path; + int len = strlen(p); + + if (len > 4 && (!strcasecmp(p+len-4, ".exe") || + !strcasecmp(p+len-4, ".com"))) { + return NULL; + } + + if ( (path=malloc(len+5)) != NULL ) { + memcpy(path, p, len); + memcpy(path+len, ".exe", 5); + if (execable_file(path)) { + return path; + } + memcpy(path+len, ".com", 5); + if (execable_file(path)) { + return path; + } + free(path); + } + + return NULL; +} -- cgit v1.2.3-55-g6feb