sl@0: /************************************************************************ sl@0: * sl@0: * environ.cpp - definitions of testsuite helpers sl@0: * sl@0: * $Id: environ.cpp 290013 2005-09-18 23:47:55Z sebor $ sl@0: * sl@0: ************************************************************************ sl@0: * sl@0: * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave sl@0: * Software division. Licensed under the Apache License, Version 2.0 (the sl@0: * "License"); you may not use this file except in compliance with the sl@0: * License. You may obtain a copy of the License at sl@0: * http://www.apache.org/licenses/LICENSE-2.0. Unless required by sl@0: * applicable law or agreed to in writing, software distributed under sl@0: * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR sl@0: * CONDITIONS OF ANY KIND, either express or implied. See the License sl@0: * for the specific language governing permissions and limitations under sl@0: * the License. sl@0: * sl@0: **************************************************************************/ sl@0: sl@0: // expand _TEST_EXPORT macros sl@0: #define _RWSTD_TEST_SRC sl@0: sl@0: #include sl@0: sl@0: #include // for assert sl@0: #include // for getenv, malloc, putenv sl@0: #include // for strchr, strlen, ... sl@0: sl@0: sl@0: extern "C" { sl@0: sl@0: #ifndef _RWSTD_NO_PUTENV_CONST_CHAR sl@0: sl@0: IMPORT_C int putenv (const char*) _LIBC_THROWS (); sl@0: sl@0: #else // if defined (_RWSTD_NO_PUTENV_CONST_CHAR) sl@0: sl@0: _RWSTD_DLLIMPORT int putenv (char*) _LIBC_THROWS (); sl@0: sl@0: #endif // _RWSTD_NO_PUTENV_CONST_CHAR sl@0: sl@0: } // extern "C" sl@0: sl@0: sl@0: // sets one or more sep-separated environment variables sl@0: _TEST_EXPORT int sl@0: rw_putenv (const char* str, int sep /* = -1 */) sl@0: { sl@0: int nset = 0; sl@0: int ret = 0; sl@0: sl@0: if (!str) { sl@0: str = getenv ("RW_PUTENV"); sl@0: if (str) sl@0: sep = *str++; sl@0: else sl@0: return 0; sl@0: } sl@0: sl@0: for (const char *pvar = str; pvar && *pvar; ++nset) { sl@0: sl@0: const char *pend = strchr (pvar, sep); sl@0: if (!pend) sl@0: pend = pvar + strlen (pvar); sl@0: sl@0: const size_t varlen = pend - pvar; sl@0: sl@0: char* const envvar = (char*)malloc (pend - pvar + 1); sl@0: memcpy (envvar, pvar, varlen); sl@0: envvar [varlen] = '\0'; sl@0: sl@0: // Note: calling Solaris 7 putenv() during program startup sl@0: // (i.e., from ctors of namespace-scope objects) prevents sl@0: // getenv() from finding that variable at program runtime sl@0: ret = putenv (envvar); sl@0: sl@0: // determine wheteher putenv() made copy of the variable sl@0: // or if it simply used the pointer passed to it; if the sl@0: // former, deallocate the buffer dynamically allocated sl@0: // above sl@0: sl@0: char namebuf [256]; sl@0: char* const equals = strchr (envvar, '='); sl@0: if (equals) { sl@0: assert (size_t (equals - envvar) < sizeof namebuf); sl@0: sl@0: memcpy (namebuf, envvar, equals - envvar); sl@0: namebuf [equals - envvar] = '\0'; sl@0: sl@0: const char* const var = getenv (namebuf); sl@0: sl@0: if (equals + 1 != var) sl@0: free (envvar); sl@0: } sl@0: else sl@0: free (envvar); sl@0: sl@0: pvar = pend + !!*pend; sl@0: } sl@0: sl@0: if (1 == nset) sl@0: return ret; sl@0: sl@0: return nset; sl@0: }