From 9e535b09a70a1ef7a2260e6d6955f7bbf291a08f Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 4 May 2022 14:26:39 +0100 Subject: win32: revert changes related to environment variables Revert the change to mingw_putenv() in the previous commit. When compiling for MSVCRT (i.e. not for UCRT) revert some of the changes from commit 5b48ca53b (win32: pass NULL to spawnve, not environ). (GitHub issue #250) --- win32/env.c | 20 ++++++++++++-------- win32/process.c | 13 +++++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/win32/env.c b/win32/env.c index 5de581e3d..4d4e9c8fd 100644 --- a/win32/env.c +++ b/win32/env.c @@ -78,7 +78,7 @@ int clearenv(void) int mingw_putenv(const char *env) { - char *s; + char *s, **envp; int ret = 0; if ( (s=strchr(env, '=')) == NULL ) { @@ -90,13 +90,17 @@ int mingw_putenv(const char *env) return _putenv(env); } else { - /* 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"); + /* 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); } return ret; diff --git a/win32/process.c b/win32/process.c index d4ab07ad8..5978226f0 100644 --- a/win32/process.c +++ b/win32/process.c @@ -345,27 +345,32 @@ mingw_spawnvp(int mode, const char *cmd, char *const *argv) { char *prog; intptr_t ret; +#if !defined(_UCRT) + char *const *envp = environ; +#else + char *const *envp = NULL; +#endif #if ENABLE_FEATURE_PREFER_APPLETS && NUM_APPLETS > 1 if (find_applet_by_name(cmd) >= 0) - return mingw_spawn_applet(mode, argv, NULL); + return mingw_spawn_applet(mode, argv, envp); else #endif if (has_path(cmd)) { char *path = alloc_system_drive(cmd); add_win32_extension(path); - ret = mingw_spawn_interpreter(mode, path, argv, NULL, 0); + ret = mingw_spawn_interpreter(mode, path, argv, envp, 0); free(path); #if ENABLE_FEATURE_PREFER_APPLETS && NUM_APPLETS > 1 if (ret == -1 && unix_path(cmd) && find_applet_by_name(bb_basename(cmd)) >= 0) { - return mingw_spawn_applet(mode, argv, NULL); + return mingw_spawn_applet(mode, argv, envp); } #endif return ret; } else if ((prog=find_first_executable(cmd)) != NULL) { - ret = mingw_spawn_interpreter(mode, prog, argv, NULL, 0); + ret = mingw_spawn_interpreter(mode, prog, argv, envp, 0); free(prog); return ret; } -- cgit v1.2.3-55-g6feb