diff options
author | Ron Yorston <rmy@pobox.com> | 2021-08-14 13:06:36 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2021-08-14 13:12:29 +0100 |
commit | 4c6c8d61bc95f5f56d91c83390f3e2631c55c37f (patch) | |
tree | 0108e9bbc2971cd631693260e389917b0fe5ff88 /win32 | |
parent | f1453b79a582d50e66fe68744abac64f384f91b1 (diff) | |
download | busybox-w32-4c6c8d61bc95f5f56d91c83390f3e2631c55c37f.tar.gz busybox-w32-4c6c8d61bc95f5f56d91c83390f3e2631c55c37f.tar.bz2 busybox-w32-4c6c8d61bc95f5f56d91c83390f3e2631c55c37f.zip |
win32: tidy up fetching of system directory
Add a new function, getsysdir(), to fetch and cache the system
directory. This avoids the non-intuitive use of getpwuid() in
get_system_drive().
The call to GetSystemDirectory() in get_proc_addr() can't be
replaced because getsysdir() calls realpath() which requires a
call to get_proc_addr().
No change in the size of the binary.
Diffstat (limited to 'win32')
-rw-r--r-- | win32/mingw.c | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index 387220bef..604e378c8 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -1005,6 +1005,19 @@ static char *gethomedir(void) | |||
1005 | return buf; | 1005 | return buf; |
1006 | } | 1006 | } |
1007 | 1007 | ||
1008 | static char *getsysdir(void) | ||
1009 | { | ||
1010 | static char *buf = NULL; | ||
1011 | char dir[PATH_MAX]; | ||
1012 | |||
1013 | if (!buf) { | ||
1014 | buf = xzalloc(PATH_MAX); | ||
1015 | GetSystemDirectory(dir, PATH_MAX); | ||
1016 | realpath(dir, buf); | ||
1017 | } | ||
1018 | return buf; | ||
1019 | } | ||
1020 | |||
1008 | #define NAME_LEN 100 | 1021 | #define NAME_LEN 100 |
1009 | static char *get_user_name(void) | 1022 | static char *get_user_name(void) |
1010 | { | 1023 | { |
@@ -1072,17 +1085,8 @@ struct passwd *getpwuid(uid_t uid) | |||
1072 | static struct passwd p; | 1085 | static struct passwd p; |
1073 | 1086 | ||
1074 | if (uid == 0) { | 1087 | if (uid == 0) { |
1075 | static char *buf = NULL; | ||
1076 | char dir[PATH_MAX]; | ||
1077 | |||
1078 | if (!buf) { | ||
1079 | buf = xzalloc(PATH_MAX); | ||
1080 | GetSystemDirectory(dir, PATH_MAX); | ||
1081 | realpath(dir, buf); | ||
1082 | } | ||
1083 | |||
1084 | p.pw_name = (char *)"root"; | 1088 | p.pw_name = (char *)"root"; |
1085 | p.pw_dir = buf; | 1089 | p.pw_dir = getsysdir(); |
1086 | } | 1090 | } |
1087 | else if (uid == DEFAULT_UID && (p.pw_name=get_user_name()) != NULL) { | 1091 | else if (uid == DEFAULT_UID && (p.pw_name=get_user_name()) != NULL) { |
1088 | p.pw_dir = gethomedir(); | 1092 | p.pw_dir = gethomedir(); |
@@ -1917,14 +1921,13 @@ int root_len(const char *path) | |||
1917 | 1921 | ||
1918 | const char *get_system_drive(void) | 1922 | const char *get_system_drive(void) |
1919 | { | 1923 | { |
1920 | struct passwd *pwd; | ||
1921 | static char *drive = NULL; | 1924 | static char *drive = NULL; |
1922 | int len; | 1925 | int len; |
1923 | 1926 | ||
1924 | if (drive == NULL) { | 1927 | if (drive == NULL) { |
1925 | pwd = getpwuid(0); | 1928 | const char *sysdir = getsysdir(); |
1926 | if (pwd != NULL && (len=root_len(pwd->pw_dir))) { | 1929 | if ((len=root_len(sysdir))) { |
1927 | drive = xstrndup(pwd->pw_dir, len); | 1930 | drive = xstrndup(sysdir, len); |
1928 | } | 1931 | } |
1929 | } | 1932 | } |
1930 | 1933 | ||