From 386fd4d71956d16b1e649c3b3e14f75e84e40310 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 3 May 2022 21:34:01 +0100 Subject: win32: new code to set empty environment variable Windows environment variables: a never-ending source of fun. It seems the recent hack to work around problems in UCRT breaks the hack to set empty environment variables. The change to the empty variable isn't reflected in the environment seen by the C runtime. Use the WIN32 API to set the empty variable then set a dummy variable to make the C runtime take notice. (GitHub issue #250) --- win32/env.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/win32/env.c b/win32/env.c index 4d4e9c8fd..5de581e3d 100644 --- a/win32/env.c +++ b/win32/env.c @@ -78,7 +78,7 @@ int clearenv(void) int mingw_putenv(const char *env) { - char *s, **envp; + char *s; int ret = 0; if ( (s=strchr(env, '=')) == NULL ) { @@ -90,17 +90,13 @@ int mingw_putenv(const char *env) return _putenv(env); } else { - /* set empty value by setting a non-empty one then truncating */ - char *envstr = xasprintf("%s0", env); - ret = _putenv(envstr); - - for (envp = environ; *envp; ++envp) { - if (strcmp(*envp, envstr) == 0) { - (*envp)[s - env + 1] = '\0'; - break; - } - } - free(envstr); + /* set empty value using WIN32 API*/ + char *name = xstrdup(env); + name[s - env] = '\0'; + SetEnvironmentVariable(name, ""); + free(name); + /* set a dummy variable to force CRT housekeeping */ + _putenv("BB_DUMMY=0"); } return ret; -- cgit v1.2.3-55-g6feb