From 6295322661ac6c2ed90af69cba0003c6e37f9700 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 29 Jan 2023 08:28:59 +0000 Subject: win32: provide a default value for HOME The busybox-w32 shell initialises HOME when it starts. However, if applets are run outside the environment provided by the shell they may find HOME is unset. This caused a problem for 'vi' as it was unable to locate its .exrc. If HOME isn't available in the environment make getenv(3) provide a sensible default value. The shell must use the *real* getenv(3) when determining if HOME is already set. Also, unrelated to the above, the shell shouldn't treat failure of getpwuid(3) as a fatal error. Costs 72-80 bytes. --- shell/ash.c | 12 ++++++++---- win32/env.c | 16 +++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/shell/ash.c b/shell/ash.c index 6a4b8e273..ebed9b50e 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -15437,6 +15437,8 @@ exitshell(void) } #if ENABLE_PLATFORM_MINGW32 +/* We need to see if HOME is *really* unset */ +# undef getenv static void xsetenv_if_unset(const char *key, const char *value) { if (!getenv(key)) @@ -15531,10 +15533,12 @@ init(void) /* Initialise some variables normally set at login, but * only if someone hasn't already set them. */ - pw = xgetpwuid(getuid()); - xsetenv_if_unset("USER", pw->pw_name); - xsetenv_if_unset("LOGNAME", pw->pw_name); - xsetenv_if_unset("HOME", pw->pw_dir); + pw = getpwuid(getuid()); + if (pw) { + xsetenv_if_unset("USER", pw->pw_name); + xsetenv_if_unset("LOGNAME", pw->pw_name); + xsetenv_if_unset("HOME", pw->pw_dir); + } xsetenv_if_unset("SHELL", DEFAULT_SHELL); } #endif diff --git a/win32/env.c b/win32/env.c index 8e54c4c5e..f30ee62f6 100644 --- a/win32/env.c +++ b/win32/env.c @@ -6,11 +6,17 @@ char *mingw_getenv(const char *name) { char *result = getenv(name); - if (!result && !strcmp(name, "TMPDIR")) { - /* on Windows it is TMP and TEMP */ - result = getenv("TMP"); - if (!result) - result = getenv("TEMP"); + if (!result) { + if (!strcmp(name, "TMPDIR")) { + /* on Windows it is TMP and TEMP */ + result = getenv("TMP"); + if (!result) + result = getenv("TEMP"); + } else if (!strcmp(name, "HOME")) { + struct passwd *p = getpwuid(getuid()); + if (p) + result = p->pw_dir; + } } return result; } -- cgit v1.2.3-55-g6feb