From 3194a475deb5e3e9e7aef680d9bf5fb0a63d551a Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 8 May 2022 11:05:30 +0100 Subject: ash: export certain variables to the environment immediately The environment variables BB_OVERRIDE_APPLETS, BB_SKIP_ANSI_EMULATION and BB_SYSTEMROOT affect of the behaviour of the shell itself. Setting them as shell variables is insufficient for them to affect the current shell. When these three variables are exported from the shell they are now placed in the environment immediately. Conversely, when they're unset or unexported they're removed from the environment. --- include/libbb.h | 6 +++++- libbb/appletlib.c | 2 +- libbb/messages.c | 12 +++++++++++- shell/ash.c | 42 +++++++++++++++++++++++++++++++++++++++++- win32/mingw.c | 2 +- win32/winansi.c | 2 +- 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 482fcce0c..6d6fc28f0 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -2360,7 +2360,11 @@ extern const char bb_busybox_exec_path[] ALIGN1; #define BB_PATH_ROOT_PATH "PATH=/sbin;/usr/sbin;/bin;/usr/bin" BB_ADDITIONAL_PATH #define PATH_SEP ';' #define PATH_SEP_STR ";" -extern const char bb_skip_ansi_emulation[] ALIGN1; +extern const char bbvar[] ALIGN1; +#define bbafter(p) (p + sizeof(#p)) +#define BB_OVERRIDE_APPLETS bbvar +#define BB_SKIP_ANSI_EMULATION bbafter(BB_OVERRIDE_APPLETS) +#define BB_SYSTEMROOT bbafter(BB_SKIP_ANSI_EMULATION) #endif extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */ #define bb_default_root_path (bb_PATH_root_path + sizeof("PATH")) diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 8d58ce2ea..9e415610d 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -268,7 +268,7 @@ int FAST_FUNC is_applet_preferred(const char *name) const char *var, *s; size_t len; - var = getenv("BB_OVERRIDE_APPLETS"); + var = getenv(BB_OVERRIDE_APPLETS); if (var && *var) { /* '-' overrides all applets */ if (var[0] == '-' && var[1] == '\0') diff --git a/libbb/messages.c b/libbb/messages.c index 04863855a..e35bf8c6b 100644 --- a/libbb/messages.c +++ b/libbb/messages.c @@ -30,7 +30,17 @@ const char bb_hexdigits_upcase[] ALIGN1 = "0123456789ABCDEF"; #if !ENABLE_PLATFORM_MINGW32 const char bb_busybox_exec_path[] ALIGN1 = CONFIG_BUSYBOX_EXEC_PATH; #else -const char bb_skip_ansi_emulation[] ALIGN1 = "BB_SKIP_ANSI_EMULATION"; +/* Some special shell variables are placed in the environment immediately + * when they're exported. + * + * BB_GLOBBING and BB_UMASK are excluded because users shouln't be + * messing with them; BB_ALT_BUFFER and BB_FIX_BACKSLASH are excluded + * because they only affect particular applets, not the shell itself. + */ +const char bbvar[] ALIGN1 = + "BB_OVERRIDE_APPLETS\0" \ + "BB_SKIP_ANSI_EMULATION\0" \ + "BB_SYSTEMROOT\0"; #endif const char bb_default_login_shell[] ALIGN1 = LIBBB_DEFAULT_LOGIN_SHELL; /* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin, diff --git a/shell/ash.c b/shell/ash.c index c2c1f2098..ddac42895 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2371,7 +2371,7 @@ static const struct { { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, #endif #if ENABLE_PLATFORM_MINGW32 - { VSTRFIXED|VTEXTFIXED|VUNSET, bb_skip_ansi_emulation, change_skip_ansi }, + { VSTRFIXED|VTEXTFIXED|VUNSET, BB_SKIP_ANSI_EMULATION, change_skip_ansi }, #endif }; @@ -2652,6 +2652,29 @@ fix_pathvar(const char *path, int len) } return newpath; } + +#define BB_VAR_EXACT 1 /* exact match for name */ +#define BB_VAR_ASSIGN 2 /* matches name followed by '=' */ + +/* Match variables that should be placed in the environment immediately + * they're exported and removed immediately they're no longer exported */ +static int +is_bb_var(const char *s) +{ + const char *p; + int len; + + for (p = bbvar; *p; p += len + 1) { + len = strlen(p); + if (strncmp(s, p, len) == 0) { + if (s[len] == '\0') + return BB_VAR_EXACT; + else if (s[len] == '=') + return BB_VAR_ASSIGN; + } + } + return FALSE; +} #endif /* @@ -2711,6 +2734,11 @@ setvareq(char *s, int flags) if (!(vp->flags & (VTEXTFIXED|VSTACK))) free((char*)vp->var_text); +#if ENABLE_PLATFORM_MINGW32 + if ((flags & VUNSET) && (vp->flags & VEXPORT) && + is_bb_var(s) == BB_VAR_EXACT) + unsetenv(s); +#endif if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) { *vpp = vp->next; free(vp); @@ -2740,6 +2768,10 @@ setvareq(char *s, int flags) s = ckstrdup(s); vp->var_text = s; vp->flags = flags; +#if ENABLE_PLATFORM_MINGW32 + if ((flags & VEXPORT) && is_bb_var(s) == BB_VAR_ASSIGN) + putenv(s); +#endif out: return vp; @@ -15028,6 +15060,14 @@ exportcmd(int argc UNUSED_PARAM, char **argv) } else { vp = *findvar(hashvar(name), name); if (vp) { +#if ENABLE_PLATFORM_MINGW32 + if (is_bb_var(name) == BB_VAR_EXACT) { + if (flag_off == ~VEXPORT) + unsetenv(name); + else if (flag == VEXPORT && !(vp->flags & VUNSET)) + putenv(vp->var_text); + } +#endif vp->flags = ((vp->flags | flag) & flag_off); continue; } diff --git a/win32/mingw.c b/win32/mingw.c index 5601d8428..6954ee9b5 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1956,7 +1956,7 @@ const char *get_system_drive(void) } } - return getenv("BB_SYSTEMROOT") ?: drive; + return getenv(BB_SYSTEMROOT) ?: drive; } /* Return pointer to system drive if path is of form '/file', else NULL */ diff --git a/win32/winansi.c b/win32/winansi.c index 622ba1c77..a78d5f7e9 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -76,7 +76,7 @@ int skip_ansi_emulation(int reset) static int skip = -1; if (skip < 0 || reset) { - const char *var = getenv(bb_skip_ansi_emulation); + const char *var = getenv(BB_SKIP_ANSI_EMULATION); int dflt = is_wine() ? 0 : CONFIG_SKIP_ANSI_EMULATION_DEFAULT; skip = var == NULL ? dflt : atoi(var); if (skip < 0 || skip > 2) -- cgit v1.2.3-55-g6feb