diff --git a/lib/putenv.c b/lib/putenv.c --- a/lib/putenv.c +++ b/lib/putenv.c @@ -34,6 +34,10 @@ #include #include +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# include +#endif + #if _LIBC # if HAVE_GNU_LD # define environ __environ @@ -67,6 +71,15 @@ len = strlen (name); +#if HAVE__PUTENV + { + char *name_x = malloc (len+2); + strcpy (name_x, name); + strcat (name_x, "="); + _putenv (name_x); + free (name_x); + } +#else LOCK; ep = environ; @@ -85,6 +98,7 @@ ++ep; UNLOCK; +#endif return 0; } @@ -93,7 +107,7 @@ /* Put STRING, which is of the form "NAME=VALUE", in the environment. If STRING contains no '=', then remove STRING from the environment. */ int -putenv (char *string) +my_putenv (char *string) { const char *const name_end = strchr (string, '='); register size_t size; @@ -123,6 +137,18 @@ return _putenv (string); else { +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + int putenv_result; + char *val; + size_t len = strlen (string); + char *name_x = malloc (len + 1); + strcpy (name_x, string); + name_x[name_end - string] = '\0'; + val = &name_x[name_end - string + 1]; + putenv_result = !SetEnvironmentVariable (name_x, val); + free (name_x); + return putenv_result; +#else /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME=x") to allocate the environ vector and then replace the new entry with "NAME=". */ @@ -144,6 +170,7 @@ free (name_x); __set_errno (putenv_errno); return putenv_result; +#endif } #else static char **last_environ = NULL; @@ -160,7 +187,13 @@ #endif } else - *ep = string; + { +#if HAVE__PUTENV + _putenv (string); +#else + *ep = string; +#endif + } return 0; }