diff options
author | Ron Yorston <rmy@pobox.com> | 2023-06-21 08:48:57 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-06-21 08:48:57 +0100 |
commit | 29f13acfb2d3818592d93590aa06de3c4f169b42 (patch) | |
tree | fcf5692d0acf57a30c3dab26ba2a703f67d7a4a2 /shell | |
parent | 830e2cfae63a20beddd81d9db00dc906265a0e34 (diff) | |
download | busybox-w32-29f13acfb2d3818592d93590aa06de3c4f169b42.tar.gz busybox-w32-29f13acfb2d3818592d93590aa06de3c4f169b42.tar.bz2 busybox-w32-29f13acfb2d3818592d93590aa06de3c4f169b42.zip |
ash: code shrink
- There's no need to set USER, LOGNAME, HOME and SHELL as environment
variables: making them shell variables is enough.
- Use endofname() to detect invalid characters in variable names and
take the copy of the invalid variable before it's modified.
Saves 48-64 bytes.
Diffstat (limited to 'shell')
-rw-r--r-- | shell/ash.c | 48 |
1 files changed, 20 insertions, 28 deletions
diff --git a/shell/ash.c b/shell/ash.c index f7e00ee0f..19026fb55 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -15757,10 +15757,10 @@ exitshell(void) | |||
15757 | #if ENABLE_PLATFORM_MINGW32 | 15757 | #if ENABLE_PLATFORM_MINGW32 |
15758 | /* We need to see if HOME is *really* unset */ | 15758 | /* We need to see if HOME is *really* unset */ |
15759 | # undef getenv | 15759 | # undef getenv |
15760 | static void xsetenv_if_unset(const char *key, const char *value) | 15760 | static void setvar_if_unset(const char *key, const char *value) |
15761 | { | 15761 | { |
15762 | if (!getenv(key) || getuid() == 0) | 15762 | if (!getenv(key) || getuid() == 0) |
15763 | xsetenv(key, value); | 15763 | setvar(key, value, VEXPORT); |
15764 | } | 15764 | } |
15765 | #endif | 15765 | #endif |
15766 | 15766 | ||
@@ -15814,6 +15814,20 @@ init(void) | |||
15814 | if (!(end=strchr(*envp, '='))) | 15814 | if (!(end=strchr(*envp, '='))) |
15815 | continue; | 15815 | continue; |
15816 | 15816 | ||
15817 | /* check for invalid characters in name */ | ||
15818 | start = (char *)endofname(*envp); | ||
15819 | if (*start != '=') { | ||
15820 | /* Make a copy of the original variable */ | ||
15821 | setvareq(xstrdup(*envp), VEXPORT|VNOSAVE); | ||
15822 | |||
15823 | /* Replace invalid characters with underscores */ | ||
15824 | for (; start < end; start++) { | ||
15825 | if (!isalnum(*start)) { | ||
15826 | *start = '_'; | ||
15827 | } | ||
15828 | } | ||
15829 | } | ||
15830 | |||
15817 | /* make all variable names uppercase */ | 15831 | /* make all variable names uppercase */ |
15818 | for (start = *envp;start < end;start++) | 15832 | for (start = *envp;start < end;start++) |
15819 | *start = toupper(*start); | 15833 | *start = toupper(*start); |
@@ -15825,39 +15839,17 @@ init(void) | |||
15825 | !is_prefixed_with(*envp, "COMSPEC=")) { | 15839 | !is_prefixed_with(*envp, "COMSPEC=")) { |
15826 | bs_to_slash(end+1); | 15840 | bs_to_slash(end+1); |
15827 | } | 15841 | } |
15828 | |||
15829 | /* check for invalid characters in name */ | ||
15830 | for (start = *envp;start < end;start++) { | ||
15831 | if (!isdigit(*start) && !isalpha(*start) && *start != '_') { | ||
15832 | break; | ||
15833 | } | ||
15834 | } | ||
15835 | |||
15836 | if (start != end) { | ||
15837 | /* | ||
15838 | * Make a copy of the variable, replacing invalid | ||
15839 | * characters in the name with underscores. | ||
15840 | */ | ||
15841 | char *var = xstrdup(*envp); | ||
15842 | |||
15843 | for (start = var;*start != '=';start++) { | ||
15844 | if (!isdigit(*start) && !isalpha(*start)) { | ||
15845 | *start = '_'; | ||
15846 | } | ||
15847 | } | ||
15848 | setvareq(var, VEXPORT|VNOSAVE); | ||
15849 | } | ||
15850 | } | 15842 | } |
15851 | 15843 | ||
15852 | /* Initialise some variables normally set at login, but | 15844 | /* Initialise some variables normally set at login, but |
15853 | * only if someone hasn't already set them or we're root. */ | 15845 | * only if someone hasn't already set them or we're root. */ |
15854 | pw = getpwuid(getuid()); | 15846 | pw = getpwuid(getuid()); |
15855 | if (pw) { | 15847 | if (pw) { |
15856 | xsetenv_if_unset("USER", pw->pw_name); | 15848 | setvar_if_unset("USER", pw->pw_name); |
15857 | xsetenv_if_unset("LOGNAME", pw->pw_name); | 15849 | setvar_if_unset("LOGNAME", pw->pw_name); |
15858 | xsetenv_if_unset("HOME", pw->pw_dir); | 15850 | setvar_if_unset("HOME", pw->pw_dir); |
15859 | } | 15851 | } |
15860 | xsetenv_if_unset("SHELL", DEFAULT_SHELL); | 15852 | setvar_if_unset("SHELL", DEFAULT_SHELL); |
15861 | } | 15853 | } |
15862 | #endif | 15854 | #endif |
15863 | for (envp = environ; envp && *envp; envp++) { | 15855 | for (envp = environ; envp && *envp; envp++) { |