[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-windows
Subject:    [patch] (msvc) use more modern facilities for setenv() unsetenv()
From:       Jarosław Staniek <js () iidea ! pl>
Date:       2008-03-06 16:05:00
Message-ID: 47D0162C.3060103 () iidea ! pl
[Download RAW message or body]

For review:

- setenv(): use secure _putenv_s() declared in msvc >= 2005 (recommended and 
apparently also used by Qt4/win); return -1 when name==0;

- unsetenv(): clear the variable using setenv(name, 0, 1) as environ is 
deprecated.

-- 
regards / pozdrawiam, Jaroslaw Staniek
  Sponsored by OpenOffice Polska (http://www.openoffice.com.pl/en) to work on
  Kexi & KOffice (http://www.kexi.pl/en, http://www.koffice.org/kexi)
  KDE Libraries for MS Windows (http://windows.kde.org)

["setenv_unsetenv.patch" (text/plain)]

Index: src/stdlib.c
===================================================================
--- src/stdlib.c	(revision 782933)
+++ src/stdlib.c	(working copy)
@@ -25,14 +25,27 @@
 #include <stdio.h>
 #include <string.h>
 
+#if defined(_MSC_VER) && _MSC_VER >= 1400
+// use secure functions declared in msvc >= 2005
+#define KDEWIN32_USE_ENV_S
+#endif
+
 // from kdecore/fakes.c
 KDEWIN32_EXPORT int setenv(const char *name, const char *value, int overwrite)
 {
+#ifndef KDEWIN32_USE_ENV_S
     int i, iRet;
     char * a;
+#endif
+    char dummy[1];
 
-    if (!overwrite && getenv(name)) return 0;
+    if (!overwrite && GetEnvironmentVariableA(name, dummy, 0) > 0) return 0;
 
+#ifdef KDEWIN32_USE_ENV_S
+    return _putenv_s(name, value);
+#else 
+    if (!name) return -1;
+
     i = strlen(name) + strlen(value) + 2;
     a = (char*)malloc(i);
     if (!a) return 1;
@@ -44,38 +57,20 @@
     iRet = putenv(a);
     free(a);
     return iRet;
+#endif
 }
 
 
 // from kdecore/fakes.c
 KDEWIN32_EXPORT void unsetenv (const char *name)
 {
-  size_t len;
-  char **ep;
-
   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
     {
       errno = EINVAL;
       return;
     }
 
-  len = strlen (name);
-
-  ep = environ;
-  while (*ep != NULL)
-    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
-      {
-	/* Found it.  Remove this pointer by moving later ones back.  */
-	char **dp = ep;
-
-	do
-	  dp[0] = dp[1];
-	while (*dp++);
-	/* Continue the loop in case NAME appears again.  */
-      }
-    else
-      ++ep;
-
+  setenv(name, 0, 1);
 }
 
 KDEWIN32_EXPORT long int random()


_______________________________________________
Kde-windows mailing list
Kde-windows@kde.org
https://mail.kde.org/mailman/listinfo/kde-windows


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic