aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2020-08-23 10:16:12 +0100
committerRon Yorston <rmy@pobox.com>2020-08-23 10:16:12 +0100
commit64ecd10486934c12336dac84c67a1939dce0e096 (patch)
tree73e8bc6b07176b84295fd07f19828292a240f693
parentd6b557547551dd80a389f361a995a97ef5930a63 (diff)
downloadbusybox-w32-64ecd10486934c12336dac84c67a1939dce0e096.tar.gz
busybox-w32-64ecd10486934c12336dac84c67a1939dce0e096.tar.bz2
busybox-w32-64ecd10486934c12336dac84c67a1939dce0e096.zip
win32: code shrink Unix-style path handling
Replace auto_add_system_drive() with alloc_system_drive() which leaves space for a possible filename extension. This makes it possible to drop alloc_win32_extension() and auto_win32_extension(). Saves 144 bytes.
-rw-r--r--debianutils/which.c36
-rw-r--r--include/mingw.h10
-rw-r--r--libbb/executable.c12
-rw-r--r--libbb/lineedit.c3
-rw-r--r--shell/ash.c27
-rw-r--r--win32/mingw.c30
-rw-r--r--win32/process.c28
7 files changed, 50 insertions, 96 deletions
diff --git a/debianutils/which.c b/debianutils/which.c
index 384e2cfec..1d23786b8 100644
--- a/debianutils/which.c
+++ b/debianutils/which.c
@@ -45,10 +45,8 @@ int which_main(int argc UNUSED_PARAM, char **argv)
45 45
46 do { 46 do {
47 int missing = 1; 47 int missing = 1;
48#if ENABLE_PLATFORM_MINGW32
49 char *p;
50 48
51# if ENABLE_FEATURE_SH_STANDALONE 49#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE
52 if (strcmp(*argv, "busybox") == 0 && 50 if (strcmp(*argv, "busybox") == 0 &&
53 is_suffixed_with(bb_busybox_exec_path, "busybox.exe")) { 51 is_suffixed_with(bb_busybox_exec_path, "busybox.exe")) {
54 missing = 0; 52 missing = 0;
@@ -63,44 +61,36 @@ int which_main(int argc UNUSED_PARAM, char **argv)
63 if (!option_mask32) /* -a not set */ 61 if (!option_mask32) /* -a not set */
64 break; 62 break;
65 } 63 }
66# endif
67#endif 64#endif
68 65
69 /* If file contains a slash don't use PATH */
70#if !ENABLE_PLATFORM_MINGW32 66#if !ENABLE_PLATFORM_MINGW32
67 /* If file contains a slash don't use PATH */
71 if (strchr(*argv, '/')) { 68 if (strchr(*argv, '/')) {
69 if (file_is_executable(*argv)) {
70 missing = 0;
71 puts(*argv);
72 }
72#else 73#else
73 if (has_path(*argv)) { 74 if (has_path(*argv)) {
74# if ENABLE_FEATURE_SH_STANDALONE 75# if ENABLE_FEATURE_SH_STANDALONE
75 const char *name = bb_basename(*argv); 76 const char *name = bb_basename(*argv);
76 int is_unix_path = unix_path(*argv);
77# endif 77# endif
78 *argv = auto_add_system_drive(*argv); 78 char *path = alloc_system_drive(*argv);
79 if ((p=auto_win32_extension(*argv)) != NULL) { 79
80 missing = 0; 80 if (add_win32_extension(path) || file_is_executable(path)) {
81 puts(bs_to_slash(p));
82 }
83 else
84#endif
85 if (file_is_executable(*argv)) {
86 missing = 0; 81 missing = 0;
87#if ENABLE_PLATFORM_MINGW32 82 puts(bs_to_slash(path));
88 puts(bs_to_slash(auto_string(xstrdup(*argv))));
89#else
90 puts(*argv);
91#endif
92 } 83 }
93#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE 84# if ENABLE_FEATURE_SH_STANDALONE
94 else if (is_unix_path && find_applet_by_name(name) >= 0) { 85 else if (unix_path(*argv) && find_applet_by_name(name) >= 0) {
95 missing = 0; 86 missing = 0;
96 puts(name); 87 puts(name);
97 } 88 }
89# endif
98#endif 90#endif
99 } else { 91 } else {
100 char *path; 92 char *path;
101#if !ENABLE_PLATFORM_MINGW32
102 char *p; 93 char *p;
103#endif
104 94
105 path = env_path; 95 path = env_path;
106 /* NOFORK NB: xmalloc inside find_executable(), must have no allocs above! */ 96 /* NOFORK NB: xmalloc inside find_executable(), must have no allocs above! */
diff --git a/include/mingw.h b/include/mingw.h
index b34d8772c..79cf6c783 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -521,16 +521,8 @@ void init_codepage(void);
521int has_bat_suffix(const char *p); 521int has_bat_suffix(const char *p);
522int has_exe_suffix(const char *p); 522int has_exe_suffix(const char *p);
523int has_exe_suffix_or_dot(const char *name); 523int has_exe_suffix_or_dot(const char *name);
524char *alloc_win32_extension(const char *p);
525int add_win32_extension(char *p); 524int add_win32_extension(char *p);
526 525
527static inline char *auto_win32_extension(const char *p)
528{
529 extern char *auto_string(char *str) FAST_FUNC;
530 char *s = alloc_win32_extension(p);
531 return s ? auto_string(s) : NULL;
532}
533
534char *bs_to_slash(char *p) FAST_FUNC; 526char *bs_to_slash(char *p) FAST_FUNC;
535void slash_to_bs(char *p) FAST_FUNC; 527void slash_to_bs(char *p) FAST_FUNC;
536size_t remove_cr(char *p, size_t len) FAST_FUNC; 528size_t remove_cr(char *p, size_t len) FAST_FUNC;
@@ -548,7 +540,7 @@ int unc_root_len(const char *dir);
548int root_len(const char *path); 540int root_len(const char *path);
549const char *get_system_drive(void); 541const char *get_system_drive(void);
550const char *need_system_drive(const char *path); 542const char *need_system_drive(const char *path);
551char *auto_add_system_drive(const char *path); 543char *alloc_system_drive(const char *path);
552int chdir_system_drive(void); 544int chdir_system_drive(void);
553char *xabsolute_path(char *path); 545char *xabsolute_path(char *path);
554char *get_drive_cwd(const char *path, char *buffer, int size); 546char *get_drive_cwd(const char *path, char *buffer, int size);
diff --git a/libbb/executable.c b/libbb/executable.c
index 0a0769ef3..f549a7aae 100644
--- a/libbb/executable.c
+++ b/libbb/executable.c
@@ -49,20 +49,16 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp)
49 49
50 n = strchr(p, PATH_SEP); 50 n = strchr(p, PATH_SEP);
51 if (n) *n = '\0'; 51 if (n) *n = '\0';
52#if ENABLE_PLATFORM_MINGW32
53 p = auto_add_system_drive(p);
54#endif
55 p = concat_path_file( 52 p = concat_path_file(
56 p[0] ? p : ".", /* handle "::" case */ 53 p[0] ? p : ".", /* handle "::" case */
57 filename 54 filename
58 ); 55 );
59 if (n) *n++ = PATH_SEP; 56 if (n) *n++ = PATH_SEP;
60#if ENABLE_PLATFORM_MINGW32 57#if ENABLE_PLATFORM_MINGW32
61 if ((w=alloc_win32_extension(p))) { 58 w = alloc_system_drive(p);
62 free(p); 59 add_win32_extension(w);
63 p = w; 60 free(p);
64 /* following test will succeed */ 61 p = w;
65 }
66#endif 62#endif
67 ex = file_is_executable(p); 63 ex = file_is_executable(p);
68 if (ex) { 64 if (ex) {
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index f6577e372..b35da1874 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -913,8 +913,9 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
913 struct dirent *next; 913 struct dirent *next;
914 struct stat st; 914 struct stat st;
915 char *found; 915 char *found;
916
916#if ENABLE_PLATFORM_MINGW32 917#if ENABLE_PLATFORM_MINGW32
917 char *lpath = auto_add_system_drive(paths[i]); 918 char *lpath = auto_string(alloc_system_drive(paths[i]));
918 dir = opendir(lpath); 919 dir = opendir(lpath);
919#else 920#else
920 dir = opendir(paths[i]); 921 dir = opendir(paths[i]);
diff --git a/shell/ash.c b/shell/ash.c
index abefba074..7967872f7 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -8784,15 +8784,19 @@ static void shellexec(char *prog, char **argv, const char *path, int idx)
8784 */ 8784 */
8785 goto try_PATH; 8785 goto try_PATH;
8786 } 8786 }
8787 e = errno;
8787#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE 8788#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE
8788 if (strcmp(oldprog, prog) != 0 && unix_path(oldprog)) { 8789 if (unix_path(oldprog)) {
8789 if ((applet_no = find_applet_by_name(bb_basename(oldprog))) >= 0) 8790 const char *name = bb_basename(oldprog);
8790 tryexec(applet_no, bb_basename(oldprog), argv, envp); 8791 if ((applet_no = find_applet_by_name(name)) >= 0) {
8791 else 8792 tryexec(applet_no, name, argv, envp);
8792 errno = ENOENT; 8793 e = errno;
8794 }
8795 else {
8796 e = ENOENT;
8797 }
8793 } 8798 }
8794#endif 8799#endif
8795 e = errno;
8796 } else { 8800 } else {
8797 try_PATH: 8801 try_PATH:
8798 e = ENOENT; 8802 e = ENOENT;
@@ -14318,16 +14322,13 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
14318#else /* ENABLE_PLATFORM_MINGW32 */ 14322#else /* ENABLE_PLATFORM_MINGW32 */
14319 /* If name contains a slash or drive prefix, don't use PATH or hash table */ 14323 /* If name contains a slash or drive prefix, don't use PATH or hash table */
14320 if (has_path(name)) { 14324 if (has_path(name)) {
14321# if ENABLE_FEATURE_SH_STANDALONE 14325 fullname = stack_add_system_drive(name);
14322 char *oldname = name;
14323# endif
14324 name = stack_add_system_drive(name);
14325 entry->u.index = -1; 14326 entry->u.index = -1;
14326 if (act & DO_ABS) { 14327 if (act & DO_ABS) {
14327 if (!add_win32_extension(name) && stat(name, &statb) < 0) { 14328 if (!add_win32_extension(fullname) && stat(fullname, &statb) < 0) {
14328# if ENABLE_FEATURE_SH_STANDALONE 14329# if ENABLE_FEATURE_SH_STANDALONE
14329 if (unix_path(oldname) && 14330 if (unix_path(name) &&
14330 find_applet_by_name(bb_basename(oldname)) >= 0) { 14331 find_applet_by_name(bb_basename(name)) >= 0) {
14331 entry->cmdtype = CMDNORMAL; 14332 entry->cmdtype = CMDNORMAL;
14332 entry->u.index = INT_MIN; 14333 entry->u.index = INT_MIN;
14333 return; 14334 return;
diff --git a/win32/mingw.c b/win32/mingw.c
index 4ffc49e9a..051dc3c0d 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -1496,25 +1496,6 @@ int add_win32_extension(char *p)
1496 return FALSE; 1496 return FALSE;
1497} 1497}
1498 1498
1499/* Check if path can be made into an executable by adding a suffix.
1500 * Return an allocated string containing the path if it can;
1501 * return NULL if not.
1502 *
1503 * If path already has a suffix don't even bother trying.
1504 */
1505char *alloc_win32_extension(const char *p)
1506{
1507 if (!has_exe_suffix_or_dot(p)) {
1508 int len = strlen(p);
1509 char *path = strcpy(xmalloc(len+5), p);
1510
1511 if (add_win32_extension(path))
1512 return path;
1513 free(path);
1514 }
1515 return NULL;
1516}
1517
1518char * FAST_FUNC bs_to_slash(char *str) 1499char * FAST_FUNC bs_to_slash(char *str)
1519{ 1500{
1520 char *p; 1501 char *p;
@@ -1706,11 +1687,14 @@ const char *need_system_drive(const char *path)
1706 return NULL; 1687 return NULL;
1707} 1688}
1708 1689
1709/* Add a system drive prefix to 'path' if necessary, else return 'path' */ 1690/* Allocate a string long enough to allow a system drive prefix and
1710char *auto_add_system_drive(const char *path) 1691 * file extension to be added to path. Add the prefix if necessary. */
1692char *alloc_system_drive(const char *path)
1711{ 1693{
1712 const char *sd = need_system_drive(path); 1694 const char *sd = need_system_drive(path);
1713 return sd ? auto_string(concat_path_file(sd, path)) : (char *)path; 1695 char *s = xmalloc(strlen(path) + 5 + (sd ? strlen(sd) : 0));
1696 sprintf(s, "%s%s", sd ?: "", path);
1697 return s;
1714} 1698}
1715 1699
1716int chdir_system_drive(void) 1700int chdir_system_drive(void)
@@ -1822,7 +1806,7 @@ void *get_proc_addr(const char *dll, const char *function,
1822int unix_path(const char *path) 1806int unix_path(const char *path)
1823{ 1807{
1824 int i; 1808 int i;
1825 char *p = strdup(path); 1809 char *p = xstrdup(path);
1826 1810
1827#define UNIX_PATHS "/bin\0/usr/bin\0/sbin\0/usr/sbin\0" 1811#define UNIX_PATHS "/bin\0/usr/bin\0/sbin\0/usr/sbin\0"
1828 i = index_in_strings(UNIX_PATHS, dirname(p)); 1812 i = index_in_strings(UNIX_PATHS, dirname(p));
diff --git a/win32/process.c b/win32/process.c
index a050ec11d..6e758e601 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -63,7 +63,6 @@ static int
63parse_interpreter(const char *cmd, interp_t *interp) 63parse_interpreter(const char *cmd, interp_t *interp)
64{ 64{
65 char *path, *t; 65 char *path, *t;
66 const char *sd;
67 int n; 66 int n;
68 67
69 while (TRUE) { 68 while (TRUE) {
@@ -89,12 +88,6 @@ parse_interpreter(const char *cmd, interp_t *interp)
89 if (*t == '\0') 88 if (*t == '\0')
90 break; 89 break;
91 90
92 sd = need_system_drive(path);
93 if (sd && strlen(sd) == 2) {
94 path -= 2;
95 memcpy(path, sd, 2);
96 }
97
98 interp->path = path; 91 interp->path = path;
99 interp->name = t; 92 interp->name = t;
100 interp->opts = strtok(NULL, "\r\n"); 93 interp->opts = strtok(NULL, "\r\n");
@@ -323,9 +316,9 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv,
323 new_argv[nopts+1] = (char *)prog; /* pass absolute path */ 316 new_argv[nopts+1] = (char *)prog; /* pass absolute path */
324 memcpy(new_argv+nopts+2, argv+1, sizeof(*argv)*argc); 317 memcpy(new_argv+nopts+2, argv+1, sizeof(*argv)*argc);
325 318
326 if ((fullpath=alloc_win32_extension(interp.path)) != NULL || 319 fullpath = alloc_system_drive(interp.path);
327 file_is_executable(interp.path)) { 320 if (add_win32_extension(fullpath) || file_is_executable(fullpath)) {
328 new_argv[0] = fullpath ? fullpath : interp.path; 321 new_argv[0] = fullpath;
329 ret = mingw_spawn_interpreter(mode, new_argv[0], new_argv, envp, level); 322 ret = mingw_spawn_interpreter(mode, new_argv[0], new_argv, envp, level);
330 } else 323 } else
331#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE 324#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE
@@ -349,7 +342,6 @@ static intptr_t
349mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp) 342mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp)
350{ 343{
351 char *prog; 344 char *prog;
352 const char *path;
353 intptr_t ret; 345 intptr_t ret;
354 346
355#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE 347#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE
@@ -358,15 +350,13 @@ mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp)
358 else 350 else
359#endif 351#endif
360 if (has_path(cmd)) { 352 if (has_path(cmd)) {
353 char *path = alloc_system_drive(cmd);
354 add_win32_extension(path);
355 ret = mingw_spawn_interpreter(mode, path, argv, envp, 0);
356 free(path);
361#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE 357#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE
362 const char *oldcmd = cmd; 358 if (ret == -1 && unix_path(cmd) &&
363#endif 359 find_applet_by_name(bb_basename(cmd)) >= 0) {
364 cmd = auto_add_system_drive(cmd);
365 path = auto_win32_extension(cmd);
366 ret = mingw_spawn_interpreter(mode, path ? path : cmd, argv, envp, 0);
367#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE
368 if (ret == -1 && cmd != oldcmd && unix_path(oldcmd) &&
369 find_applet_by_name(bb_basename(oldcmd))) {
370 return mingw_spawn_applet(mode, argv, envp); 360 return mingw_spawn_applet(mode, argv, envp);
371 } 361 }
372#endif 362#endif