aboutsummaryrefslogtreecommitdiff
path: root/win32/mingw.c
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 /win32/mingw.c
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.
Diffstat (limited to 'win32/mingw.c')
-rw-r--r--win32/mingw.c27
1 files changed, 13 insertions, 14 deletions
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