First public contribution.
1 /************************************************************************
3 * environ.cpp - definitions of testsuite helpers
5 * $Id: environ.cpp 290013 2005-09-18 23:47:55Z sebor $
7 ************************************************************************
9 * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave
10 * Software division. Licensed under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance with the
12 * License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0. Unless required by
14 * applicable law or agreed to in writing, software distributed under
15 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
16 * CONDITIONS OF ANY KIND, either express or implied. See the License
17 * for the specific language governing permissions and limitations under
20 **************************************************************************/
22 // expand _TEST_EXPORT macros
23 #define _RWSTD_TEST_SRC
27 #include <assert.h> // for assert
28 #include <stdlib.h> // for getenv, malloc, putenv
29 #include <string.h> // for strchr, strlen, ...
34 #ifndef _RWSTD_NO_PUTENV_CONST_CHAR
36 IMPORT_C int putenv (const char*) _LIBC_THROWS ();
38 #else // if defined (_RWSTD_NO_PUTENV_CONST_CHAR)
40 _RWSTD_DLLIMPORT int putenv (char*) _LIBC_THROWS ();
42 #endif // _RWSTD_NO_PUTENV_CONST_CHAR
47 // sets one or more sep-separated environment variables
49 rw_putenv (const char* str, int sep /* = -1 */)
55 str = getenv ("RW_PUTENV");
62 for (const char *pvar = str; pvar && *pvar; ++nset) {
64 const char *pend = strchr (pvar, sep);
66 pend = pvar + strlen (pvar);
68 const size_t varlen = pend - pvar;
70 char* const envvar = (char*)malloc (pend - pvar + 1);
71 memcpy (envvar, pvar, varlen);
72 envvar [varlen] = '\0';
74 // Note: calling Solaris 7 putenv() during program startup
75 // (i.e., from ctors of namespace-scope objects) prevents
76 // getenv() from finding that variable at program runtime
77 ret = putenv (envvar);
79 // determine wheteher putenv() made copy of the variable
80 // or if it simply used the pointer passed to it; if the
81 // former, deallocate the buffer dynamically allocated
85 char* const equals = strchr (envvar, '=');
87 assert (size_t (equals - envvar) < sizeof namebuf);
89 memcpy (namebuf, envvar, equals - envvar);
90 namebuf [equals - envvar] = '\0';
92 const char* const var = getenv (namebuf);
94 if (equals + 1 != var)
100 pvar = pend + !!*pend;