From: Eli Zaretskii Date: Sat, 8 Dec 2012 11:32:10 +0000 (+0200) Subject: Provide unsetenv for MS-Windows and make putenv Posix-compatible. X-Git-Tag: emacs-24.3.90~173^2~9^2~5 X-Git-Url: https://code.delx.au/gnu-emacs/commitdiff_plain/75ceee05671c1698e27e5188487e7e74c490f201 Provide unsetenv for MS-Windows and make putenv Posix-compatible. src/w32.c (unsetenv, sys_putenv): New functions. nt/inc/ms-w32.h (putenv): Redirect to sys_putenv. nt/config.nt (HAVE_UNSETENV): Define to 1. Fixes: debbugs:13070 --- diff --git a/nt/ChangeLog b/nt/ChangeLog index 3cbe45fbd0..df6a42595a 100644 --- a/nt/ChangeLog +++ b/nt/ChangeLog @@ -1,3 +1,9 @@ +2012-12-08 Eli Zaretskii + + * inc/ms-w32.h (putenv): Redirect to sys_putenv. + + * config.nt (HAVE_UNSETENV): Define to 1. + 2012-12-01 Juanma Barranquero * config.nt: Sync with autogen/config.in. diff --git a/nt/config.nt b/nt/config.nt index baa10198a8..e83c9ff655 100644 --- a/nt/config.nt +++ b/nt/config.nt @@ -993,6 +993,9 @@ along with GNU Emacs. If not, see . */ /* Define to 1 if the system has the type 'unsigned long long int'. */ #undef HAVE_UNSIGNED_LONG_LONG_INT +/* Define to 1 if you have the `unsetenv' function. */ +#define HAVE_UNSETENV 1 + /* Define to 1 if you have the header file. */ #undef HAVE_UTIL_H diff --git a/nt/inc/ms-w32.h b/nt/inc/ms-w32.h index 7b16ccab06..1cbcb7f88f 100644 --- a/nt/inc/ms-w32.h +++ b/nt/inc/ms-w32.h @@ -376,6 +376,12 @@ extern char *get_emacs_configuration_options (void); #define sys_nerr _sys_nerr #endif +/* This must be after including stdlib.h, which defines putenv on MinGW. */ +#ifdef putenv +# undef putenv +#endif +#define putenv sys_putenv + extern int getloadavg (double *, int); extern int getpagesize (void); diff --git a/src/ChangeLog b/src/ChangeLog index 8e1e422d15..3e8b8519ba 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2012-12-08 Eli Zaretskii + + * w32.c (unsetenv, sys_putenv): New functions. + 2012-12-08 Chong Yidong * editfns.c (Finsert_char): Make the error message more diff --git a/src/w32.c b/src/w32.c index e81fc7b4f3..203c5cd40f 100644 --- a/src/w32.c +++ b/src/w32.c @@ -1544,6 +1544,50 @@ is_unc_volume (const char *filename) return 1; } +/* Emulate the Posix unsetenv. */ +int +unsetenv (const char *name) +{ + char *var; + size_t name_len; + int retval; + + if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) + { + errno = EINVAL; + return -1; + } + name_len = strlen (name); + /* MS docs says an environment variable cannot be longer than 32K. */ + if (name_len > 32767) + { + errno = ENOMEM; + return -1; + } + /* It is safe to use 'alloca' with 32K size, since the stack is at + least 2MB, and we set it to 8MB in the link command line. */ + var = alloca (name_len + 2); + var[name_len++] = '='; + var[name_len] = '\0'; + return _putenv (var); +} + +/* MS _putenv doesn't support removing a variable when the argument + does not include the '=' character, so we fix that here. */ +int +sys_putenv (char *str) +{ + const char *const name_end = strchr (str, '='); + + if (name_end == NULL) + { + /* Remove the variable from the environment. */ + return unsetenv (str); + } + + return _putenv (str); +} + #define REG_ROOT "SOFTWARE\\GNU\\Emacs" LPBYTE