aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2012-05-09 15:16:44 +0100
committerRon Yorston <rmy@pobox.com>2012-05-09 15:16:44 +0100
commit4066aff5e481941585c5958460c39a1b1399ce88 (patch)
tree87f3996a592298e744faa6b17b0af17c3786510a
parentf2459f232790aab0434d1cc6471ea62bc193e636 (diff)
downloadbusybox-w32-4066aff5e481941585c5958460c39a1b1399ce88.tar.gz
busybox-w32-4066aff5e481941585c5958460c39a1b1399ce88.tar.bz2
busybox-w32-4066aff5e481941585c5958460c39a1b1399ce88.zip
Use win32_execable_file() in test, which and execable.c
-rw-r--r--coreutils/test.c26
-rw-r--r--debianutils/which.c23
-rw-r--r--include/mingw.h2
-rw-r--r--libbb/execable.c31
-rw-r--r--win32/mingw.c32
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)
648#undef W_OK 648#undef W_OK
649#define W_OK S_IWRITE 649#define W_OK S_IWRITE
650 if (mode == FILEX) { 650 if (mode == FILEX) {
651 int len = strlen(nm), ret; 651 char *p;
652 if (len >= 4 && 652
653 (!strcmp(nm+len-4,".exe") || 653 if (execable_file(nm)) {
654 !strcmp(nm+len-4,".com"))) 654 return 1;
655 ret = stat(nm, &s);
656 else {
657 char *exepath;
658 exepath = malloc(len+5);
659 memcpy(exepath, nm, len);
660 memcpy(exepath+len, ".exe", 5);
661 ret = stat(exepath, &s);
662 if (ret < 0) {
663 memcpy(exepath+len, ".exe", 5);
664 ret = stat(exepath, &s);
665 }
666 free(exepath);
667 } 655 }
668 return ret >= 0; 656 else if ((p=win32_execable_file(nm))) {
657 free(p);
658 return 1;
659 }
660 return 0;
669 } 661 }
670#endif 662#endif
671 663
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 @@
21 21
22#include "libbb.h" 22#include "libbb.h"
23 23
24#if ENABLE_PLATFORM_MINGW32
25static char *win32_execable_file(const char *p)
26{
27 char *path;
28 int len = strlen(p) + 5;
29
30 if ( (path=malloc(len)) != NULL ) {
31 memcpy(path, p, len);
32 memcpy(path+len, ".exe", 5);
33 if (execable_file(path)) {
34 return path;
35 }
36 memcpy(path+len, ".com", 5);
37 if (execable_file(path)) {
38 return path;
39 }
40 free(path);
41 }
42
43 return NULL;
44}
45#endif
46
47int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 24int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
48int which_main(int argc UNUSED_PARAM, char **argv) 25int which_main(int argc UNUSED_PARAM, char **argv)
49{ 26{
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);
386 386
387const char *get_busybox_exec_path(void); 387const char *get_busybox_exec_path(void);
388void init_winsock(void); 388void init_winsock(void);
389
390char *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)
35char* FAST_FUNC find_execable(const char *filename, char **PATHp) 35char* FAST_FUNC find_execable(const char *filename, char **PATHp)
36{ 36{
37 char *p, *n; 37 char *p, *n;
38#if ENABLE_PLATFORM_MINGW32
39 char *w;
40#endif
38 41
39 p = *PATHp; 42 p = *PATHp;
40 while (p) { 43 while (p) {
@@ -47,29 +50,13 @@ char* FAST_FUNC find_execable(const char *filename, char **PATHp)
47 *PATHp = n; 50 *PATHp = n;
48 return p; 51 return p;
49 } 52 }
50 if (ENABLE_PLATFORM_MINGW32) { 53#if ENABLE_PLATFORM_MINGW32
51 int len = strlen(p); 54 else if ((w=win32_execable_file(p))) {
52 if (len > 4 && 55 *PATHp = n;
53 (!strcasecmp(p+len-4, ".exe") || 56 free(p);
54 !strcasecmp(p+len-4, ".com"))) 57 return w;
55 ; /* nothing, already tested by execable_file() */
56 else {
57 char *np = xmalloc(len+4+1);
58 memcpy(np, p, len);
59 memcpy(np+len, ".exe", 5);
60 if (execable_file(np)) {
61 *PATHp = n;
62 free(p);
63 return np;
64 }
65 memcpy(np+len, ".com", 5);
66 if (execable_file(np)) {
67 *PATHp = n;
68 free(p);
69 return np;
70 }
71 }
72 } 58 }
59#endif
73 free(p); 60 free(p);
74 } 61 }
75 p = n; 62 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)
830 830
831 return -1; 831 return -1;
832} 832}
833
834/* check if path can be made into an executable by adding a suffix;
835 * return an allocated string containing the path if it can;
836 * return NULL if not.
837 *
838 * if path already has a suffix don't even bother trying
839 */
840char *win32_execable_file(const char *p)
841{
842 char *path;
843 int len = strlen(p);
844
845 if (len > 4 && (!strcasecmp(p+len-4, ".exe") ||
846 !strcasecmp(p+len-4, ".com"))) {
847 return NULL;
848 }
849
850 if ( (path=malloc(len+5)) != NULL ) {
851 memcpy(path, p, len);
852 memcpy(path+len, ".exe", 5);
853 if (execable_file(path)) {
854 return path;
855 }
856 memcpy(path+len, ".com", 5);
857 if (execable_file(path)) {
858 return path;
859 }
860 free(path);
861 }
862
863 return NULL;
864}