stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r290013 - in /incubator/stdcxx/trunk/tests: include/environ.h src/environ.cpp
Date Sun, 18 Sep 2005 23:47:59 GMT
Author: sebor
Date: Sun Sep 18 16:47:55 2005
New Revision: 290013

URL: http://svn.apache.org/viewcvs?rev=290013&view=rev
Log:
2005-09-18  Martin Sebor  <sebor@roguewave.com>

	STDCXX-3
	* environ.h: New. Declaration of the rw_putenv() helper function
	for seeting one or more environment variables in a portable way.
	* environ.cpp: New. Implementation of the same.


Added:
    incubator/stdcxx/trunk/tests/include/environ.h   (with props)
    incubator/stdcxx/trunk/tests/src/environ.cpp   (with props)

Added: incubator/stdcxx/trunk/tests/include/environ.h
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/include/environ.h?rev=290013&view=auto
==============================================================================
--- incubator/stdcxx/trunk/tests/include/environ.h (added)
+++ incubator/stdcxx/trunk/tests/include/environ.h Sun Sep 18 16:47:55 2005
@@ -0,0 +1,38 @@
+/************************************************************************
+ *
+ * environ.h - declarations of testsuite helpers
+ *
+ * $Id:$
+ *
+ ************************************************************************
+ *
+ * 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.
+ *
+ **************************************************************************/
+
+#ifndef RW_ENVIRON_H_INCLUDED
+#define RW_ENVIRON_H_INCLUDED
+
+
+#include <testdefs.h>
+
+
+// rw_putenv is a wrapper for the C standard library putenv function
+// when called with the first argument of 0, rw_putenv defines any
+// environment variables specified in the RW_PUTENV environment
+// variable (e.g., RW_PUTENV=:foo=1:bar=2 will cause the variables
+// foo=1 and bar=2 to be defined in the environment)
+_TEST_EXPORT int
+rw_putenv (const char*, int = -1);
+
+
+#endif   // RW_ENVIRON_H_INCLUDED

Propchange: incubator/stdcxx/trunk/tests/include/environ.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/stdcxx/trunk/tests/include/environ.h
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/stdcxx/trunk/tests/src/environ.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/environ.cpp?rev=290013&view=auto
==============================================================================
--- incubator/stdcxx/trunk/tests/src/environ.cpp (added)
+++ incubator/stdcxx/trunk/tests/src/environ.cpp Sun Sep 18 16:47:55 2005
@@ -0,0 +1,107 @@
+/************************************************************************
+ *
+ * environ.cpp - definitions of testsuite helpers
+ *
+ * $Id$
+ *
+ ************************************************************************
+ *
+ * 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.
+ * 
+ **************************************************************************/
+
+// expand _TEST_EXPORT macros
+#define _RWSTD_TEST_SRC
+
+#include <environ.h>
+
+#include <assert.h>   // for assert
+#include <stdlib.h>   // for getenv, malloc, putenv
+#include <string.h>   // for strchr, strlen, ...
+
+
+extern "C" {
+
+#ifndef _RWSTD_NO_PUTENV_CONST_CHAR
+
+_RWSTD_DLLIMPORT int putenv (const char*) _LIBC_THROWS ();
+
+#else   // if defined (_RWSTD_NO_PUTENV_CONST_CHAR)
+
+_RWSTD_DLLIMPORT int putenv (char*) _LIBC_THROWS ();
+
+#endif   // _RWSTD_NO_PUTENV_CONST_CHAR
+
+}   // extern "C"
+
+
+// sets one or more sep-separated environment variables
+_TEST_EXPORT int
+rw_putenv (const char* str, int sep /* = -1 */)
+{
+    int nset = 0;
+    int ret  = 0;
+
+    if (!str) {
+        str = getenv ("RW_PUTENV");
+        if (str)
+            sep = *str++;
+        else
+            return 0;
+    }
+
+    for (const char *pvar = str; pvar && *pvar; ++nset) {
+
+        const char *pend = strchr (pvar, sep);
+        if (!pend)
+            pend = pvar + strlen (pvar);
+
+        const size_t varlen = pend - pvar;
+
+        char* const envvar = (char*)malloc (pend - pvar + 1);
+        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
+
+        char namebuf [256];
+        char* const equals = strchr (envvar, '=');
+        if (equals) {
+            assert (size_t (equals - envvar) < sizeof namebuf);
+
+            memcpy (namebuf, envvar, equals - envvar);
+            namebuf [equals - envvar] = '\0';
+
+            const char* const var = getenv (namebuf);
+
+            if (equals + 1 != var)
+                free (envvar);
+        }
+        else
+            free (envvar);
+
+        pvar = pend + !!*pend;
+    }
+
+    if (1 == nset)
+        return ret;
+
+    return nset;
+}

Propchange: incubator/stdcxx/trunk/tests/src/environ.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/stdcxx/trunk/tests/src/environ.cpp
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message