From 9fc98788e4fb1ea1f330d44c8b2019b3fe6444f7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 3 Jul 2015 14:12:47 +0100 Subject: Allow different ways to manipulate environment for XP vs 64-bit Commit fa147bd 'Use putenv to implement unsetenv/clearenv' allowed BusyBox to run on ReactOS but broke it on Windows XP so it was reverted. It turns out that the same change is required on 64-bit Windows. Reinstate the 'safe' environment manipulation code but make it a configuration option. Add a config file for 64-bit Windows that does the right thing. --- win32/env.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'win32') diff --git a/win32/env.c b/win32/env.c index f8d231a8f..48b86c555 100644 --- a/win32/env.c +++ b/win32/env.c @@ -104,12 +104,70 @@ char **env_setenv(char **env, const char *name) else { for (; env[i]; i++) env[i] = env[i+1]; +#if !ENABLE_SAFE_ENV SetEnvironmentVariable(name, NULL); +#endif } } return env; } +#if ENABLE_SAFE_ENV +/* + * Removing an environment variable with WIN32 putenv requires an argument + * like "NAME="; glibc omits the '='. The implementations of unsetenv and + * clearenv allow for this. + * + * It isn't possible to create an environment variable with an empty value + * using WIN32 putenv. + */ +#undef putenv +int unsetenv(const char *env) +{ + char *name; + int ret; + + name = xmalloc(strlen(env)+2); + strcat(strcpy(name, env), "="); + ret = putenv(name); + free(name); + + return ret; +} + +int clearenv(void) +{ + char *name, *s; + + while ( environ && *environ ) { + if ( (s=strchr(*environ, '=')) != NULL ) { + name = xstrndup(*environ, s-*environ+1); + putenv(name); + free(name); + } + else { + return -1; + } + } + return 0; +} + +int mingw_putenv(const char *env) +{ + char *s; + + if ( (s=strchr(env, '=')) == NULL ) { + return unsetenv(env); + } + + if ( s[1] != '\0' ) { + return putenv(env); + } + + /* can't set empty value */ + return 0; +} +#else void unsetenv(const char *env) { env_setenv(environ, env); @@ -128,3 +186,4 @@ int clearenv(void) environ = NULL; return 0; } +#endif -- cgit v1.2.3-55-g6feb