From fa147bd7ecb086f4fb9a4afea16b946693a822ce Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 23 Nov 2014 17:35:35 +0000 Subject: Use putenv to implement unsetenv/clearenv noexec applets failed on ReactOS 0.3.17. This was because the environment was being manipulated directly using the environ pointer. Implementing unsetenv and clearenv using putenv fixes the problem. WIN32 putenv doesn't allow environment variables to have empty values. This was the case before and it's still the case after this change. Shell variables are fine. --- include/mingw.h | 4 +++- win32/env.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 7caedd063..654ca3ee6 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -180,13 +180,15 @@ int mingw_system(const char *cmd); int clearenv(void); char *mingw_getenv(const char *name); +int mingw_putenv(const char *env); char *mingw_mktemp(char *template); int mkstemp(char *template); char *realpath(const char *path, char *resolved_path); int setenv(const char *name, const char *value, int replace); -void unsetenv(const char *env); +int unsetenv(const char *env); #define getenv mingw_getenv +#define putenv mingw_putenv #define mktemp mingw_mktemp /* diff --git a/win32/env.c b/win32/env.c index f8d231a8f..eb2e1f004 100644 --- a/win32/env.c +++ b/win32/env.c @@ -104,27 +104,62 @@ char **env_setenv(char **env, const char *name) else { for (; env[i]; i++) env[i] = env[i+1]; - SetEnvironmentVariable(name, NULL); } } return env; } -void unsetenv(const char *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) { - env_setenv(environ, 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 **env = environ; - if (!env) - return 0; - while (*env) { - free(*env); - env++; + char *name, *s; + + while ( environ && *environ ) { + if ( (s=strchr(*environ, '=')) != NULL ) { + name = xstrndup(*environ, s-*environ+1); + putenv(name); + free(name); + } + else { + return -1; + } } - free(env); - environ = NULL; + 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; } -- cgit v1.2.3-55-g6feb