aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-01-29 08:28:59 +0000
committerRon Yorston <rmy@pobox.com>2023-01-29 08:28:59 +0000
commit6295322661ac6c2ed90af69cba0003c6e37f9700 (patch)
tree44b70fdf880c37e5168ea16f2fa684b5debbadbc
parent82f0d19b101271889b0b2ebe8cdb2f5eb4f64e6c (diff)
downloadbusybox-w32-6295322661ac6c2ed90af69cba0003c6e37f9700.tar.gz
busybox-w32-6295322661ac6c2ed90af69cba0003c6e37f9700.tar.bz2
busybox-w32-6295322661ac6c2ed90af69cba0003c6e37f9700.zip
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.
-rw-r--r--shell/ash.c12
-rw-r--r--win32/env.c16
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)
15437} 15437}
15438 15438
15439#if ENABLE_PLATFORM_MINGW32 15439#if ENABLE_PLATFORM_MINGW32
15440/* We need to see if HOME is *really* unset */
15441# undef getenv
15440static void xsetenv_if_unset(const char *key, const char *value) 15442static void xsetenv_if_unset(const char *key, const char *value)
15441{ 15443{
15442 if (!getenv(key)) 15444 if (!getenv(key))
@@ -15531,10 +15533,12 @@ init(void)
15531 15533
15532 /* Initialise some variables normally set at login, but 15534 /* Initialise some variables normally set at login, but
15533 * only if someone hasn't already set them. */ 15535 * only if someone hasn't already set them. */
15534 pw = xgetpwuid(getuid()); 15536 pw = getpwuid(getuid());
15535 xsetenv_if_unset("USER", pw->pw_name); 15537 if (pw) {
15536 xsetenv_if_unset("LOGNAME", pw->pw_name); 15538 xsetenv_if_unset("USER", pw->pw_name);
15537 xsetenv_if_unset("HOME", pw->pw_dir); 15539 xsetenv_if_unset("LOGNAME", pw->pw_name);
15540 xsetenv_if_unset("HOME", pw->pw_dir);
15541 }
15538 xsetenv_if_unset("SHELL", DEFAULT_SHELL); 15542 xsetenv_if_unset("SHELL", DEFAULT_SHELL);
15539 } 15543 }
15540#endif 15544#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 @@
6char *mingw_getenv(const char *name) 6char *mingw_getenv(const char *name)
7{ 7{
8 char *result = getenv(name); 8 char *result = getenv(name);
9 if (!result && !strcmp(name, "TMPDIR")) { 9 if (!result) {
10 /* on Windows it is TMP and TEMP */ 10 if (!strcmp(name, "TMPDIR")) {
11 result = getenv("TMP"); 11 /* on Windows it is TMP and TEMP */
12 if (!result) 12 result = getenv("TMP");
13 result = getenv("TEMP"); 13 if (!result)
14 result = getenv("TEMP");
15 } else if (!strcmp(name, "HOME")) {
16 struct passwd *p = getpwuid(getuid());
17 if (p)
18 result = p->pw_dir;
19 }
14 } 20 }
15 return result; 21 return result;
16} 22}