aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2020-08-13 11:43:26 +0100
committerRon Yorston <rmy@pobox.com>2020-08-13 14:58:01 +0100
commit7d639339e0c46311f8873d560e6f168e71473cd9 (patch)
treee483966b030472f5c0ae85c0a11f090acbaa57ff
parent258ad6a1d52f1811f9de1d6b976f3797f5b31a2b (diff)
downloadbusybox-w32-7d639339e0c46311f8873d560e6f168e71473cd9.tar.gz
busybox-w32-7d639339e0c46311f8873d560e6f168e71473cd9.tar.bz2
busybox-w32-7d639339e0c46311f8873d560e6f168e71473cd9.zip
win32: use a static buffer in get_system_drive()
Allocate static storage for the system drive string instead of making a new allocation on every call. This is easier to manage. Adds 16 bytes.
-rw-r--r--include/mingw.h2
-rw-r--r--networking/httpd.c2
-rw-r--r--shell/ash.c3
-rw-r--r--win32/mingw.c27
4 files changed, 16 insertions, 18 deletions
diff --git a/include/mingw.h b/include/mingw.h
index 5c0b0a7f4..91e4dc1a5 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -546,7 +546,7 @@ void hide_console(void);
546 546
547int unc_root_len(const char *dir); 547int unc_root_len(const char *dir);
548int root_len(const char *path); 548int root_len(const char *path);
549char *get_system_drive(void); 549const char *get_system_drive(void);
550int chdir_system_drive(void); 550int chdir_system_drive(void);
551char *xabsolute_path(char *path); 551char *xabsolute_path(char *path);
552char *get_drive_cwd(const char *path, char *buffer, int size); 552char *get_drive_cwd(const char *path, char *buffer, int size);
diff --git a/networking/httpd.c b/networking/httpd.c
index 5105eedac..cfb635df3 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -665,7 +665,7 @@ static void parse_conf(const char *path, int flag)
665 filename = alloca(strlen(path) + sizeof(HTTPD_CONF) + 2); 665 filename = alloca(strlen(path) + sizeof(HTTPD_CONF) + 2);
666 sprintf((char *)filename, "%s/%s", path, HTTPD_CONF); 666 sprintf((char *)filename, "%s/%s", path, HTTPD_CONF);
667#else 667#else
668 char *sd = get_system_drive(); 668 const char *sd = get_system_drive();
669 669
670 filename = auto_string(xasprintf("%s%s/%s", 670 filename = auto_string(xasprintf("%s%s/%s",
671 (sd && path[0] == '/' && root_len(path) == 0) ? sd : "", 671 (sd && path[0] == '/' && root_len(path) == 0) ? sd : "",
diff --git a/shell/ash.c b/shell/ash.c
index db7b18957..7a2d0ab68 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -15245,7 +15245,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
15245 struct stackmark smark; 15245 struct stackmark smark;
15246 int login_sh; 15246 int login_sh;
15247#if ENABLE_PLATFORM_MINGW32 15247#if ENABLE_PLATFORM_MINGW32
15248 char *sd; 15248 const char *sd;
15249 15249
15250 INIT_G_memstack(); 15250 INIT_G_memstack();
15251 15251
@@ -15366,7 +15366,6 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
15366 if (sd) { 15366 if (sd) {
15367 char *path = xasprintf("%s/etc/profile", sd); 15367 char *path = xasprintf("%s/etc/profile", sd);
15368 read_profile(path); 15368 read_profile(path);
15369 free(sd);
15370 free(path); 15369 free(path);
15371 } 15370 }
15372 else 15371 else
diff --git a/win32/mingw.c b/win32/mingw.c
index d9bb6e973..8501ecdd4 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -1682,16 +1682,17 @@ int root_len(const char *path)
1682 return unc_root_len(path); 1682 return unc_root_len(path);
1683} 1683}
1684 1684
1685char *get_system_drive(void) 1685const char *get_system_drive(void)
1686{ 1686{
1687 struct passwd *pwd; 1687 struct passwd *pwd;
1688 char *drive = NULL; 1688 static char *drive = NULL;
1689 int len; 1689 int len;
1690 1690
1691 pwd = getpwuid(0); 1691 if (drive == NULL) {
1692 if (pwd != NULL && (len=root_len(pwd->pw_dir))) { 1692 pwd = getpwuid(0);
1693 drive = xstrdup(pwd->pw_dir); 1693 if (pwd != NULL && (len=root_len(pwd->pw_dir))) {
1694 drive[len] = '\0'; 1694 drive = xstrndup(pwd->pw_dir, len);
1695 }
1695 } 1696 }
1696 1697
1697 return drive; 1698 return drive;
@@ -1699,14 +1700,11 @@ char *get_system_drive(void)
1699 1700
1700int chdir_system_drive(void) 1701int chdir_system_drive(void)
1701{ 1702{
1702 char *sd = get_system_drive(); 1703 const char *sd = get_system_drive();
1703 int ret = -1; 1704 int ret = -1;
1704 1705
1705 if (sd) { 1706 if (sd)
1706 strcat(sd, "/"); 1707 ret = chdir(auto_string(concat_path_file(sd, "")));
1707 ret = chdir(sd);
1708 }
1709 free(sd);
1710 return ret; 1708 return ret;
1711} 1709}
1712 1710
@@ -1722,13 +1720,14 @@ int chdir_system_drive(void)
1722 */ 1720 */
1723char *xabsolute_path(char *path) 1721char *xabsolute_path(char *path)
1724{ 1722{
1725 char *rpath, *sd; 1723 char *rpath;
1724 const char *sd;
1726 1725
1727 if (root_len(path) != 0) 1726 if (root_len(path) != 0)
1728 return path; // absolute path 1727 return path; // absolute path
1729 rpath = xmalloc_realpath(path); 1728 rpath = xmalloc_realpath(path);
1730 if (rpath) { 1729 if (rpath) {
1731 sd = auto_string(get_system_drive()); 1730 sd = get_system_drive();
1732 if (sd && is_prefixed_with_case(rpath, sd)) { 1731 if (sd && is_prefixed_with_case(rpath, sd)) {
1733 free(rpath); 1732 free(rpath);
1734 return path; // resolved path is on system drive 1733 return path; // resolved path is on system drive