]> code.delx.au - gnu-emacs/commitdiff
Provide unsetenv for MS-Windows and make putenv Posix-compatible.
authorEli Zaretskii <eliz@gnu.org>
Sat, 8 Dec 2012 11:32:10 +0000 (13:32 +0200)
committerEli Zaretskii <eliz@gnu.org>
Sat, 8 Dec 2012 11:32:10 +0000 (13:32 +0200)
 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
nt/ChangeLog
nt/config.nt
nt/inc/ms-w32.h
src/ChangeLog
src/w32.c

index 3cbe45fbd0e3083bc42613bdb0515c9242861776..df6a42595ada72fcbb747614185066327fcf35d5 100644 (file)
@@ -1,3 +1,9 @@
+2012-12-08  Eli Zaretskii  <eliz@gnu.org>
+
+       * inc/ms-w32.h (putenv): Redirect to sys_putenv.
+
+       * config.nt (HAVE_UNSETENV): Define to 1.
+
 2012-12-01  Juanma Barranquero  <lekktu@gmail.com>
 
        * config.nt: Sync with autogen/config.in.
index baa10198a8c1fabf1e6c76fadc4c37e997a047f5..e83c9ff655fda2df4f24161d42a89de907a487ef 100644 (file)
@@ -993,6 +993,9 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 /* 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 <util.h> header file. */
 #undef HAVE_UTIL_H
 
index 7b16ccab069401791b7ec67f9d862c18e61804cb..1cbcb7f88f3da9ae767932bd579033bfb2c1d526 100644 (file)
@@ -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);
 
index 8e1e422d15469689bf714b43f344ec13f73d39cb..3e8b8519ba3eeda8b8cbb01edbbdb73e40f8b37c 100644 (file)
@@ -1,3 +1,7 @@
+2012-12-08  Eli Zaretskii  <eliz@gnu.org>
+
+       * w32.c (unsetenv, sys_putenv): New functions.
+
 2012-12-08  Chong Yidong  <cyd@gnu.org>
 
        * editfns.c (Finsert_char): Make the error message more
index e81fc7b4f3e805cf26d06c7c36ebbbe00104ad18..203c5cd40ffeddfb91872a8985efd67011c501a4 100644 (file)
--- 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