stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r390297 - /incubator/stdcxx/trunk/tests/src/environ.cpp
Date Fri, 31 Mar 2006 01:55:12 GMT
Author: sebor
Date: Thu Mar 30 17:55:11 2006
New Revision: 390297

URL: http://svn.apache.org/viewcvs?rev=390297&view=rev
Log:
2006-03-30  Martin Sebor  <sebor@roguewave.com>

	* environ.cpp (rw_putenv): Removed the variable from the environment
	when it doesn't contain the equals sign.

Modified:
    incubator/stdcxx/trunk/tests/src/environ.cpp

Modified: incubator/stdcxx/trunk/tests/src/environ.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/environ.cpp?rev=390297&r1=390296&r2=390297&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/environ.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/environ.cpp Thu Mar 30 17:55:11 2006
@@ -6,16 +6,22 @@
  *
  ************************************************************************
  *
- * Copyright (c) 1994-2005 Quovadx,  Inc., acting through its  Rogue Wave
- * Software division. Licensed under the Apache License, Version 2.0 (the
- * "License");  you may  not use this file except  in compliance with the
- * License.    You    may   obtain   a   copy   of    the   License    at
- * http://www.apache.org/licenses/LICENSE-2.0.    Unless   required    by
- * applicable law  or agreed to  in writing,  software  distributed under
- * the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR
- * CONDITIONS OF  ANY KIND, either  express or implied.  See  the License
- * for the specific language governing permissions  and limitations under
- * the License.
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Copyright 2001-2006 Rogue Wave Software.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  * 
  **************************************************************************/
 
@@ -31,6 +37,8 @@
 
 extern "C" {
 
+// char** environ;
+
 #ifndef _RWSTD_NO_PUTENV_CONST_CHAR
 
 _RWSTD_DLLIMPORT int putenv (const char*) _LIBC_THROWS ();
@@ -44,7 +52,7 @@
 }   // extern "C"
 
 
-// sets one or more sep-separated environment variables
+// sets (or unsets) one or more sep-separated environment variables
 _TEST_EXPORT int
 rw_putenv (const char* str, int sep /* = -1 */)
 {
@@ -61,8 +69,10 @@
 
     for (const char *pvar = str; pvar && *pvar; ++nset) {
 
-        const char *pend = strchr (pvar, sep);
-        if (!pend)
+        const char *pend =
+            sep < int (_RWSTD_UCHAR_MAX) ? strchr (pvar, sep) : 0;
+
+        if (0 == pend)
             pend = pvar + strlen (pvar);
 
         const size_t varlen = pend - pvar;
@@ -71,31 +81,68 @@
         memcpy (envvar, pvar, varlen);
         envvar [varlen] = '\0';
 
-        // Note: calling Solaris 7 putenv() during program startup
-        // (i.e., from ctors of namespace-scope objects) prevents
-        // getenv() from finding that variable at program runtime
-        ret = putenv (envvar);
-
-        // determine wheteher putenv() made copy of the variable
-        // or if it simply used the pointer passed to it; if the
-        // former, deallocate the buffer dynamically allocated
-        // above
+        // look for the first equals sign
+        const char* const equals = strchr (envvar, '=');
+
+        char *var = 0;
 
-        char namebuf [256];
-        char* const equals = strchr (envvar, '=');
         if (equals) {
+            // add the variable to the environment or modify it if
+            // it's already defined
+
+            // Note: calling Solaris 7 putenv() during program startup
+            // (i.e., from ctors of namespace-scope objects) prevents
+            // getenv() from finding that variable at program runtime
+            ret = putenv (envvar);
+
+            // determine wheteher putenv() made copy of the variable
+            // or if it simply used the pointer passed to it; if the
+            // former, deallocate the buffer dynamically allocated
+            // above
+
+            char namebuf [256];
             assert (size_t (equals - envvar) < sizeof namebuf);
 
             memcpy (namebuf, envvar, equals - envvar);
             namebuf [equals - envvar] = '\0';
 
-            const char* const var = getenv (namebuf);
+            var = getenv (namebuf);
 
             if (equals + 1 != var)
                 free (envvar);
         }
-        else
+        else if ((var = getenv (envvar))) {
+            // try to remove variable from the environment
+            ret = putenv (envvar);
+
+            if (0 == ret) {
+                // see if the variable has been removed
+                var = getenv (envvar);
+                if (var) {
+                    // if not, zero-out the first byte of its name
+                    // FIXME: make this more robust, e.g., by calling
+                    // unsetenv() when provided or by manipulating
+                    // the environment directly
+                    *(var - 1 - varlen) = '\0';
+
+#if 0   // disabled
+
+                    char **penv = environ;
+                    if (penv) {
+                        while (*penv && *penv != (var - 1 - varlen))
+                            ++penv;
+
+                        while (*penv)
+                            *penv = penv [1];
+                    }
+
+#endif   // 0/1
+
+                }
+            }
+
             free (envvar);
+        }
 
         pvar = pend + !!*pend;
     }



Mime
View raw message