X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fputenv.c;h=b9f507926cb5d019f0b7622913e9238a47a0158c;hb=1276a2c5f24c0c932426aca9c899fa524d2443f2;hp=ed666afc3bbbf01725b02ccc863da107695acc8b;hpb=77a0af5285e21031e00629d3bda125b9493923d6;p=gnulib.git diff --git a/lib/putenv.c b/lib/putenv.c index ed666afc3..b9f507926 100644 --- a/lib/putenv.c +++ b/lib/putenv.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2013 Free Software +/* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2014 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with the GNU C @@ -62,7 +62,9 @@ static int _unsetenv (const char *name) { size_t len; +#if !HAVE_DECL__PUTENV char **ep; +#endif if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) { @@ -72,7 +74,7 @@ _unsetenv (const char *name) len = strlen (name); -#if HAVE__PUTENV +#if HAVE_DECL__PUTENV { int putenv_result, putenv_errno; char *name_ = malloc (len + 2); @@ -125,46 +127,46 @@ putenv (char *string) return _unsetenv (string); } -#if HAVE__PUTENV - /* Rely on _putenv to allocate the new environment. If other - parts of the application use _putenv, the !HAVE__PUTENV code - would fight over who owns the environ vector, causing a crash. */ - if (name_end[1]) - return _putenv (string); - else - { - /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ") - to allocate the environ vector and then replace the new - entry with "NAME=". */ - int putenv_result, putenv_errno; - char *name_x = malloc (name_end - string + sizeof "= "); - if (!name_x) - return -1; - memcpy (name_x, string, name_end - string + 1); - name_x[name_end - string + 1] = ' '; - name_x[name_end - string + 2] = 0; - putenv_result = _putenv (name_x); - putenv_errno = errno; - for (ep = environ; *ep; ep++) - if (strcmp (*ep, name_x) == 0) - { - *ep = string; - break; - } +#if HAVE_DECL__PUTENV + /* Rely on _putenv to allocate the new environment. If other + parts of the application use _putenv, the !HAVE_DECL__PUTENV code + would fight over who owns the environ vector, causing a crash. */ + if (name_end[1]) + return _putenv (string); + else + { + /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ") + to allocate the environ vector and then replace the new + entry with "NAME=". */ + int putenv_result, putenv_errno; + char *name_x = malloc (name_end - string + sizeof "= "); + if (!name_x) + return -1; + memcpy (name_x, string, name_end - string + 1); + name_x[name_end - string + 1] = ' '; + name_x[name_end - string + 2] = 0; + putenv_result = _putenv (name_x); + putenv_errno = errno; + for (ep = environ; *ep; ep++) + if (strcmp (*ep, name_x) == 0) + { + *ep = string; + break; + } # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ - if (putenv_result == 0) - { - /* _putenv propagated "NAME= " into the subprocess environment; - fix that by calling SetEnvironmentVariable directly. */ - name_x[name_end - string] = 0; - putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1; - putenv_errno = ENOMEM; /* ENOMEM is the only way to fail. */ - } -# endif - free (name_x); - __set_errno (putenv_errno); - return putenv_result; + if (putenv_result == 0) + { + /* _putenv propagated "NAME= " into the subprocess environment; + fix that by calling SetEnvironmentVariable directly. */ + name_x[name_end - string] = 0; + putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1; + putenv_errno = ENOMEM; /* ENOMEM is the only way to fail. */ } +# endif + free (name_x); + __set_errno (putenv_errno); + return putenv_result; + } #else for (ep = environ; *ep; ep++) if (strncmp (*ep, string, name_end - string) == 0 @@ -186,7 +188,7 @@ putenv (char *string) last_environ = new_environ; environ = new_environ; } -#endif return 0; +#endif }