aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-03-16 10:47:09 +0000
committerRon Yorston <rmy@pobox.com>2023-03-16 10:47:09 +0000
commit184edf9bd672bc93856157098d528eab48948ba9 (patch)
tree183531c00207b293de9a609384e0ef9ff7dbfac5
parent385decd6bf62c116565ece1e0992ff7a79d48474 (diff)
downloadbusybox-w32-184edf9bd672bc93856157098d528eab48948ba9.tar.gz
busybox-w32-184edf9bd672bc93856157098d528eab48948ba9.tar.bz2
busybox-w32-184edf9bd672bc93856157098d528eab48948ba9.zip
win32: code shrink detection of executables
Add a function, file_is_win32_exe(), to detect if a path refers to an executable. It tries adding extensions if necessary. Use this in a number of places to replace common code of the form path = alloc_ext_space(cmd); if (add_win32_extension(path) || file_is_executable(path)) Saves 32-48 bytes.
-rw-r--r--debianutils/which.c11
-rw-r--r--include/mingw.h3
-rw-r--r--libbb/executable.c10
-rw-r--r--win32/mingw.c34
-rw-r--r--win32/process.c11
5 files changed, 41 insertions, 28 deletions
diff --git a/debianutils/which.c b/debianutils/which.c
index b2cd4de18..4af6daf4f 100644
--- a/debianutils/which.c
+++ b/debianutils/which.c
@@ -12,9 +12,8 @@
12//config: which is used to find programs in your PATH and 12//config: which is used to find programs in your PATH and
13//config: print out their pathnames. 13//config: print out their pathnames.
14 14
15// NOTE: For WIN32 this applet is NOEXEC as alloc_ext_space() and 15// NOTE: For WIN32 this applet is NOEXEC as file_is_win32_exe() and
16// find_executable() both allocate memory. And find_executable() 16// find_executable() both allocate memory.
17// calls alloc_ext_space().
18 17
19//applet:IF_PLATFORM_MINGW32(IF_WHICH(APPLET_NOEXEC(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))) 18//applet:IF_PLATFORM_MINGW32(IF_WHICH(APPLET_NOEXEC(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which)))
20//applet:IF_PLATFORM_POSIX(IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))) 19//applet:IF_PLATFORM_POSIX(IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which)))
@@ -87,9 +86,9 @@ int which_main(int argc UNUSED_PARAM, char **argv)
87 } 86 }
88#else 87#else
89 if (has_path(*argv)) { 88 if (has_path(*argv)) {
90 char *path = alloc_ext_space(*argv); 89 char *path = file_is_win32_exe(*argv);
91 90
92 if (add_win32_extension(path) || file_is_executable(path)) { 91 if (path) {
93 missing = 0; 92 missing = 0;
94 puts(bs_to_slash(path)); 93 puts(bs_to_slash(path));
95 } 94 }
@@ -103,11 +102,9 @@ int which_main(int argc UNUSED_PARAM, char **argv)
103# endif 102# endif
104 { 103 {
105 argv[0] = (char *)name; 104 argv[0] = (char *)name;
106 free(path);
107 goto try_PATH; 105 goto try_PATH;
108 } 106 }
109 } 107 }
110 free(path);
111#endif 108#endif
112 } else { 109 } else {
113 char *path; 110 char *path;
diff --git a/include/mingw.h b/include/mingw.h
index e49483307..7e6109f2e 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -559,7 +559,9 @@ void init_codepage(void);
559int has_bat_suffix(const char *p); 559int has_bat_suffix(const char *p);
560int has_exe_suffix(const char *p); 560int has_exe_suffix(const char *p);
561int has_exe_suffix_or_dot(const char *name); 561int has_exe_suffix_or_dot(const char *name);
562char *alloc_ext_space(const char *path);
562int add_win32_extension(char *p); 563int add_win32_extension(char *p);
564char *file_is_win32_exe(const char *name);
563 565
564char *bs_to_slash(char *p) FAST_FUNC; 566char *bs_to_slash(char *p) FAST_FUNC;
565void slash_to_bs(char *p) FAST_FUNC; 567void slash_to_bs(char *p) FAST_FUNC;
@@ -578,7 +580,6 @@ int unc_root_len(const char *dir);
578int root_len(const char *path); 580int root_len(const char *path);
579const char *get_system_drive(void); 581const char *get_system_drive(void);
580const char *need_system_drive(const char *path); 582const char *need_system_drive(const char *path);
581char *alloc_ext_space(const char *path);
582int chdir_system_drive(void); 583int chdir_system_drive(void);
583char *xabsolute_path(char *path); 584char *xabsolute_path(char *path);
584char *get_drive_cwd(const char *path, char *buffer, int size); 585char *get_drive_cwd(const char *path, char *buffer, int size);
diff --git a/libbb/executable.c b/libbb/executable.c
index 770aedc0c..606bec986 100644
--- a/libbb/executable.c
+++ b/libbb/executable.c
@@ -57,10 +57,12 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp)
57 ); 57 );
58#if ENABLE_PLATFORM_MINGW32 58#if ENABLE_PLATFORM_MINGW32
59 { 59 {
60 char *w = alloc_ext_space(p); 60 char *w = file_is_win32_exe(p);
61 ex = add_win32_extension(w) || file_is_executable(w); 61 ex = w != NULL;
62 free(p); 62 if (ex) {
63 p = w; 63 free(p);
64 p = w;
65 }
64 } 66 }
65#else 67#else
66 ex = file_is_executable(p); 68 ex = file_is_executable(p);
diff --git a/win32/mingw.c b/win32/mingw.c
index c7eeea088..0a1af6b72 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -2001,6 +2001,15 @@ int has_exe_suffix_or_dot(const char *name)
2001 return last_char_is(name, '.') || has_win_suffix(name, 0); 2001 return last_char_is(name, '.') || has_win_suffix(name, 0);
2002} 2002}
2003 2003
2004/* Copy path to an allocated string long enough to allow a file extension
2005 * to be added. */
2006char *alloc_ext_space(const char *path)
2007{
2008 char *s = xmalloc(strlen(path) + 5);
2009 strcpy(s, path);
2010 return s;
2011}
2012
2004/* Check if path can be made into an executable by adding a suffix. 2013/* Check if path can be made into an executable by adding a suffix.
2005 * The suffix is added to the end of the argument which must be 2014 * The suffix is added to the end of the argument which must be
2006 * long enough to allow this. 2015 * long enough to allow this.
@@ -2024,6 +2033,22 @@ int add_win32_extension(char *p)
2024 return FALSE; 2033 return FALSE;
2025} 2034}
2026 2035
2036/*
2037 * Determine if a path represents a WIN32 executable, adding a suffix
2038 * if necessary. Returns an allocated string if it does, NULL if not.
2039 */
2040char *
2041file_is_win32_exe(const char *name)
2042{
2043 char *path = alloc_ext_space(name);
2044
2045 if ((add_win32_extension(path) || file_is_executable(path)))
2046 return path;
2047
2048 free(path);
2049 return NULL;
2050}
2051
2027char * FAST_FUNC bs_to_slash(char *str) 2052char * FAST_FUNC bs_to_slash(char *str)
2028{ 2053{
2029 char *p; 2054 char *p;
@@ -2193,15 +2218,6 @@ const char *need_system_drive(const char *path)
2193 return NULL; 2218 return NULL;
2194} 2219}
2195 2220
2196/* Copy path to an allocated string long enough to allow a file extension
2197 * to be added. */
2198char *alloc_ext_space(const char *path)
2199{
2200 char *s = xmalloc(strlen(path) + 5);
2201 strcpy(s, path);
2202 return s;
2203}
2204
2205int chdir_system_drive(void) 2221int chdir_system_drive(void)
2206{ 2222{
2207 const char *sd = get_system_drive(); 2223 const char *sd = get_system_drive();
diff --git a/win32/process.c b/win32/process.c
index 0585f66a6..a0678f50d 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -328,14 +328,12 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv,
328 } 328 }
329#endif 329#endif
330 330
331 path = alloc_ext_space(interp.path); 331 path = file_is_win32_exe(interp.path);
332 if ((add_win32_extension(path) || file_is_executable(path))) { 332 if (path) {
333 new_argv[0] = path; 333 new_argv[0] = path;
334 ret = mingw_spawn_interpreter(mode, path, new_argv, envp, level); 334 ret = mingw_spawn_interpreter(mode, path, new_argv, envp, level);
335 goto done; 335 goto done;
336 } 336 }
337 free(path);
338 path = NULL;
339 337
340 if (unix_path(interp.path)) { 338 if (unix_path(interp.path)) {
341 if ((path = find_first_executable(interp.name)) != NULL) { 339 if ((path = find_first_executable(interp.name)) != NULL) {
@@ -363,13 +361,12 @@ mingw_spawnvp(int mode, const char *cmd, char *const *argv)
363 return mingw_spawn_applet(mode, argv, NULL); 361 return mingw_spawn_applet(mode, argv, NULL);
364#endif 362#endif
365 if (has_path(cmd)) { 363 if (has_path(cmd)) {
366 path = alloc_ext_space(cmd); 364 path = file_is_win32_exe(cmd);
367 if (add_win32_extension(path) || file_is_executable(path)) { 365 if (path) {
368 ret = mingw_spawn_interpreter(mode, path, argv, NULL, 0); 366 ret = mingw_spawn_interpreter(mode, path, argv, NULL, 0);
369 free(path); 367 free(path);
370 return ret; 368 return ret;
371 } 369 }
372 free(path);
373 if (unix_path(cmd)) 370 if (unix_path(cmd))
374 cmd = bb_basename(cmd); 371 cmd = bb_basename(cmd);
375 } 372 }