aboutsummaryrefslogtreecommitdiff
path: root/win32
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 /win32
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.
Diffstat (limited to 'win32')
-rw-r--r--win32/mingw.c30
-rw-r--r--win32/process.c28
2 files changed, 16 insertions, 42 deletions
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