Add some test cases for _putenv, fix non-existant delete

Mike Hearn mh at codeweavers.com
Sat Jul 17 12:03:56 CDT 2004


Confusingly _putenv has a different behaviour to
[Rtl]SetEnvironmentVariable in the case of deleting non-existant
environment variables. Maybe they are implemented in different ways, or
maybe it's backwards compatibility goo. Whatever the cause, here is a fix
and some simple tests for _putenv/getenv.


Mike Hearn <mh at codeweavers.com>
Add some test cases for _wputenv, fix deletion of non-existant environment
variables, return -1 on error

Index: dlls/msvcrt/environ.c
===================================================================
RCS file: /home/wine/wine/dlls/msvcrt/environ.c,v
retrieving revision 1.12
diff -u -p -r1.12 environ.c
--- dlls/msvcrt/environ.c       25 Jun 2004 01:19:15 -0000      1.12
+++ dlls/msvcrt/environ.c       17 Jul 2004 16:56:01 -0000
@@ -91,7 +91,11 @@ int _putenv(const char *str)
   *dst++ = *str++;
  *dst = '\0';
  
- ret = !SetEnvironmentVariableA(name, value[0] ? value : NULL);
+ ret = SetEnvironmentVariableA(name, value[0] ? value : NULL) ? 0 : -1;
+
+ /* _putenv returns success on deletion of non-existant variable, unlike [Rtl]SetEnvironmentVariable */
+ if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
+
  /* Update the __p__environ array only when already initialized */
  if (_environ)
    _environ = msvcrt_SnapshotOfEnvironmentA(_environ);
@@ -123,7 +127,11 @@ int _wputenv(const MSVCRT_wchar_t *str)
   *dst++ = *str++;
  *dst = 0;
  
- ret = !SetEnvironmentVariableW(name, value[0] ? value : NULL);
+ ret = SetEnvironmentVariableW(name, value[0] ? value : NULL) ? 0 : -1;
+
+ /* _putenv returns success on deletion of non-existant variable, unlike [Rtl]SetEnvironmentVariable */
+ if ((ret == -1) && (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) ret = 0;
+
  /* Update the __p__environ array only when already initialized */
  if (_environ)
    _environ = msvcrt_SnapshotOfEnvironmentA(_environ);
Index: dlls/msvcrt/tests/Makefile.in
===================================================================
RCS file: /home/wine/wine/dlls/msvcrt/tests/Makefile.in,v
retrieving revision 1.7
diff -u -p -r1.7 Makefile.in
--- dlls/msvcrt/tests/Makefile.in       25 Jun 2004 01:19:15 -0000      1.7
+++ dlls/msvcrt/tests/Makefile.in       17 Jul 2004 16:56:01 -0000
@@ -8,6 +8,7 @@ EXTRAINCL = -I$(TOPSRCDIR)/include/msvcr
  
 CTESTS = \
        cpp.c \
+       env.c \
        file.c \
        headers.c \
        heap.c \
--- /dev/null   2003-09-15 14:40:47.000000000 +0100
+++ dlls/msvcrt/tests/env.c     2004-07-17 17:57:48.000000000 +0100
@@ -0,0 +1,35 @@
+/*
+ * Unit tests for C library environment routines
+ *
+ * Copyright 2004 Mike Hearn <mh at codeweavers.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "wine/test.h"
+#include <stdlib.h>
+
+START_TEST(env)
+{
+    ok( _putenv("cat=") == 0, "_putenv failed on deletion of non-existant environment variable\n" );
+    ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
+    ok( strcmp(getenv("cat"), "dog") == 0, "getenv did not return 'dog'\n" );
+    ok( _putenv("cat=") == 0, "failed deleting cat\n" );
+
+    ok( _putenv("=") == -1, "should not accept '=' as input\n" );
+    ok( _putenv("=dog") == -1, "should not accept '=dog' as input\n" );
+
+    ok( getenv("nonexistant") == NULL, "getenv should fail with nonexistant var name\n" );
+}





More information about the wine-patches mailing list