aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2022-05-08 11:05:30 +0100
committerRon Yorston <rmy@pobox.com>2022-05-08 11:05:30 +0100
commit3194a475deb5e3e9e7aef680d9bf5fb0a63d551a (patch)
tree3f67439af8aca8b754d0a1c8146d5118b6bd83ae
parentdc6dbbd9601aeaa9f715b2ea8ec128e6b7846eb9 (diff)
downloadbusybox-w32-3194a475deb5e3e9e7aef680d9bf5fb0a63d551a.tar.gz
busybox-w32-3194a475deb5e3e9e7aef680d9bf5fb0a63d551a.tar.bz2
busybox-w32-3194a475deb5e3e9e7aef680d9bf5fb0a63d551a.zip
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.
-rw-r--r--include/libbb.h6
-rw-r--r--libbb/appletlib.c2
-rw-r--r--libbb/messages.c12
-rw-r--r--shell/ash.c42
-rw-r--r--win32/mingw.c2
-rw-r--r--win32/winansi.c2
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;
2360#define BB_PATH_ROOT_PATH "PATH=/sbin;/usr/sbin;/bin;/usr/bin" BB_ADDITIONAL_PATH 2360#define BB_PATH_ROOT_PATH "PATH=/sbin;/usr/sbin;/bin;/usr/bin" BB_ADDITIONAL_PATH
2361#define PATH_SEP ';' 2361#define PATH_SEP ';'
2362#define PATH_SEP_STR ";" 2362#define PATH_SEP_STR ";"
2363extern const char bb_skip_ansi_emulation[] ALIGN1; 2363extern const char bbvar[] ALIGN1;
2364#define bbafter(p) (p + sizeof(#p))
2365#define BB_OVERRIDE_APPLETS bbvar
2366#define BB_SKIP_ANSI_EMULATION bbafter(BB_OVERRIDE_APPLETS)
2367#define BB_SYSTEMROOT bbafter(BB_SKIP_ANSI_EMULATION)
2364#endif 2368#endif
2365extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */ 2369extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */
2366#define bb_default_root_path (bb_PATH_root_path + sizeof("PATH")) 2370#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)
268 const char *var, *s; 268 const char *var, *s;
269 size_t len; 269 size_t len;
270 270
271 var = getenv("BB_OVERRIDE_APPLETS"); 271 var = getenv(BB_OVERRIDE_APPLETS);
272 if (var && *var) { 272 if (var && *var) {
273 /* '-' overrides all applets */ 273 /* '-' overrides all applets */
274 if (var[0] == '-' && var[1] == '\0') 274 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";
30#if !ENABLE_PLATFORM_MINGW32 30#if !ENABLE_PLATFORM_MINGW32
31const char bb_busybox_exec_path[] ALIGN1 = CONFIG_BUSYBOX_EXEC_PATH; 31const char bb_busybox_exec_path[] ALIGN1 = CONFIG_BUSYBOX_EXEC_PATH;
32#else 32#else
33const char bb_skip_ansi_emulation[] ALIGN1 = "BB_SKIP_ANSI_EMULATION"; 33/* Some special shell variables are placed in the environment immediately
34 * when they're exported.
35 *
36 * BB_GLOBBING and BB_UMASK are excluded because users shouln't be
37 * messing with them; BB_ALT_BUFFER and BB_FIX_BACKSLASH are excluded
38 * because they only affect particular applets, not the shell itself.
39 */
40const char bbvar[] ALIGN1 =
41 "BB_OVERRIDE_APPLETS\0" \
42 "BB_SKIP_ANSI_EMULATION\0" \
43 "BB_SYSTEMROOT\0";
34#endif 44#endif
35const char bb_default_login_shell[] ALIGN1 = LIBBB_DEFAULT_LOGIN_SHELL; 45const char bb_default_login_shell[] ALIGN1 = LIBBB_DEFAULT_LOGIN_SHELL;
36/* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin, 46/* 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 {
2371 { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, 2371 { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL },
2372#endif 2372#endif
2373#if ENABLE_PLATFORM_MINGW32 2373#if ENABLE_PLATFORM_MINGW32
2374 { VSTRFIXED|VTEXTFIXED|VUNSET, bb_skip_ansi_emulation, change_skip_ansi }, 2374 { VSTRFIXED|VTEXTFIXED|VUNSET, BB_SKIP_ANSI_EMULATION, change_skip_ansi },
2375#endif 2375#endif
2376}; 2376};
2377 2377
@@ -2652,6 +2652,29 @@ fix_pathvar(const char *path, int len)
2652 } 2652 }
2653 return newpath; 2653 return newpath;
2654} 2654}
2655
2656#define BB_VAR_EXACT 1 /* exact match for name */
2657#define BB_VAR_ASSIGN 2 /* matches name followed by '=' */
2658
2659/* Match variables that should be placed in the environment immediately
2660 * they're exported and removed immediately they're no longer exported */
2661static int
2662is_bb_var(const char *s)
2663{
2664 const char *p;
2665 int len;
2666
2667 for (p = bbvar; *p; p += len + 1) {
2668 len = strlen(p);
2669 if (strncmp(s, p, len) == 0) {
2670 if (s[len] == '\0')
2671 return BB_VAR_EXACT;
2672 else if (s[len] == '=')
2673 return BB_VAR_ASSIGN;
2674 }
2675 }
2676 return FALSE;
2677}
2655#endif 2678#endif
2656 2679
2657/* 2680/*
@@ -2711,6 +2734,11 @@ setvareq(char *s, int flags)
2711 if (!(vp->flags & (VTEXTFIXED|VSTACK))) 2734 if (!(vp->flags & (VTEXTFIXED|VSTACK)))
2712 free((char*)vp->var_text); 2735 free((char*)vp->var_text);
2713 2736
2737#if ENABLE_PLATFORM_MINGW32
2738 if ((flags & VUNSET) && (vp->flags & VEXPORT) &&
2739 is_bb_var(s) == BB_VAR_EXACT)
2740 unsetenv(s);
2741#endif
2714 if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) { 2742 if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) {
2715 *vpp = vp->next; 2743 *vpp = vp->next;
2716 free(vp); 2744 free(vp);
@@ -2740,6 +2768,10 @@ setvareq(char *s, int flags)
2740 s = ckstrdup(s); 2768 s = ckstrdup(s);
2741 vp->var_text = s; 2769 vp->var_text = s;
2742 vp->flags = flags; 2770 vp->flags = flags;
2771#if ENABLE_PLATFORM_MINGW32
2772 if ((flags & VEXPORT) && is_bb_var(s) == BB_VAR_ASSIGN)
2773 putenv(s);
2774#endif
2743 2775
2744 out: 2776 out:
2745 return vp; 2777 return vp;
@@ -15028,6 +15060,14 @@ exportcmd(int argc UNUSED_PARAM, char **argv)
15028 } else { 15060 } else {
15029 vp = *findvar(hashvar(name), name); 15061 vp = *findvar(hashvar(name), name);
15030 if (vp) { 15062 if (vp) {
15063#if ENABLE_PLATFORM_MINGW32
15064 if (is_bb_var(name) == BB_VAR_EXACT) {
15065 if (flag_off == ~VEXPORT)
15066 unsetenv(name);
15067 else if (flag == VEXPORT && !(vp->flags & VUNSET))
15068 putenv(vp->var_text);
15069 }
15070#endif
15031 vp->flags = ((vp->flags | flag) & flag_off); 15071 vp->flags = ((vp->flags | flag) & flag_off);
15032 continue; 15072 continue;
15033 } 15073 }
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)
1956 } 1956 }
1957 } 1957 }
1958 1958
1959 return getenv("BB_SYSTEMROOT") ?: drive; 1959 return getenv(BB_SYSTEMROOT) ?: drive;
1960} 1960}
1961 1961
1962/* Return pointer to system drive if path is of form '/file', else NULL */ 1962/* 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)
76 static int skip = -1; 76 static int skip = -1;
77 77
78 if (skip < 0 || reset) { 78 if (skip < 0 || reset) {
79 const char *var = getenv(bb_skip_ansi_emulation); 79 const char *var = getenv(BB_SKIP_ANSI_EMULATION);
80 int dflt = is_wine() ? 0 : CONFIG_SKIP_ANSI_EMULATION_DEFAULT; 80 int dflt = is_wine() ? 0 : CONFIG_SKIP_ANSI_EMULATION_DEFAULT;
81 skip = var == NULL ? dflt : atoi(var); 81 skip = var == NULL ? dflt : atoi(var);
82 if (skip < 0 || skip > 2) 82 if (skip < 0 || skip > 2)