From 4c6c8d61bc95f5f56d91c83390f3e2631c55c37f Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 14 Aug 2021 13:06:36 +0100 Subject: 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. --- win32/mingw.c | 31 +++++++++++++++++-------------- 1 file 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) return buf; } +static char *getsysdir(void) +{ + static char *buf = NULL; + char dir[PATH_MAX]; + + if (!buf) { + buf = xzalloc(PATH_MAX); + GetSystemDirectory(dir, PATH_MAX); + realpath(dir, buf); + } + return buf; +} + #define NAME_LEN 100 static char *get_user_name(void) { @@ -1072,17 +1085,8 @@ struct passwd *getpwuid(uid_t uid) static struct passwd p; if (uid == 0) { - static char *buf = NULL; - char dir[PATH_MAX]; - - if (!buf) { - buf = xzalloc(PATH_MAX); - GetSystemDirectory(dir, PATH_MAX); - realpath(dir, buf); - } - p.pw_name = (char *)"root"; - p.pw_dir = buf; + p.pw_dir = getsysdir(); } else if (uid == DEFAULT_UID && (p.pw_name=get_user_name()) != NULL) { p.pw_dir = gethomedir(); @@ -1917,14 +1921,13 @@ int root_len(const char *path) const char *get_system_drive(void) { - struct passwd *pwd; static char *drive = NULL; int len; if (drive == NULL) { - pwd = getpwuid(0); - if (pwd != NULL && (len=root_len(pwd->pw_dir))) { - drive = xstrndup(pwd->pw_dir, len); + const char *sysdir = getsysdir(); + if ((len=root_len(sysdir))) { + drive = xstrndup(sysdir, len); } } -- cgit v1.2.3-55-g6feb