os/ossrv/stdcpp/tsrc/Stdcpp_test/stdcxx/testengine/src/environ.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/stdcpp/tsrc/Stdcpp_test/stdcxx/testengine/src/environ.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,107 @@
     1.4 +/************************************************************************
     1.5 + *
     1.6 + * environ.cpp - definitions of testsuite helpers
     1.7 + *
     1.8 + * $Id: environ.cpp 290013 2005-09-18 23:47:55Z sebor $
     1.9 + *
    1.10 + ************************************************************************
    1.11 + *
    1.12 + * Copyright (c) 1994-2005 Quovadx,  Inc., acting through its  Rogue Wave
    1.13 + * Software division. Licensed under the Apache License, Version 2.0 (the
    1.14 + * "License");  you may  not use this file except  in compliance with the
    1.15 + * License.    You    may   obtain   a   copy   of    the   License    at
    1.16 + * http://www.apache.org/licenses/LICENSE-2.0.    Unless   required    by
    1.17 + * applicable law  or agreed to  in writing,  software  distributed under
    1.18 + * the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR
    1.19 + * CONDITIONS OF  ANY KIND, either  express or implied.  See  the License
    1.20 + * for the specific language governing permissions  and limitations under
    1.21 + * the License.
    1.22 + * 
    1.23 + **************************************************************************/
    1.24 +
    1.25 +// expand _TEST_EXPORT macros
    1.26 +#define _RWSTD_TEST_SRC
    1.27 +
    1.28 +#include <environ.h>
    1.29 +
    1.30 +#include <assert.h>   // for assert
    1.31 +#include <stdlib.h>   // for getenv, malloc, putenv
    1.32 +#include <string.h>   // for strchr, strlen, ...
    1.33 +
    1.34 +
    1.35 +extern "C" {
    1.36 +
    1.37 +#ifndef _RWSTD_NO_PUTENV_CONST_CHAR
    1.38 +
    1.39 +IMPORT_C int putenv (const char*) _LIBC_THROWS ();
    1.40 +
    1.41 +#else   // if defined (_RWSTD_NO_PUTENV_CONST_CHAR)
    1.42 +
    1.43 +_RWSTD_DLLIMPORT int putenv (char*) _LIBC_THROWS ();
    1.44 +
    1.45 +#endif   // _RWSTD_NO_PUTENV_CONST_CHAR
    1.46 +
    1.47 +}   // extern "C"
    1.48 +
    1.49 +
    1.50 +// sets one or more sep-separated environment variables
    1.51 +_TEST_EXPORT int
    1.52 +rw_putenv (const char* str, int sep /* = -1 */)
    1.53 +{
    1.54 +    int nset = 0;
    1.55 +    int ret  = 0;
    1.56 +
    1.57 +    if (!str) {
    1.58 +        str = getenv ("RW_PUTENV");
    1.59 +        if (str)
    1.60 +            sep = *str++;
    1.61 +        else
    1.62 +            return 0;
    1.63 +    }
    1.64 +
    1.65 +    for (const char *pvar = str; pvar && *pvar; ++nset) {
    1.66 +
    1.67 +        const char *pend = strchr (pvar, sep);
    1.68 +        if (!pend)
    1.69 +            pend = pvar + strlen (pvar);
    1.70 +
    1.71 +        const size_t varlen = pend - pvar;
    1.72 +
    1.73 +        char* const envvar = (char*)malloc (pend - pvar + 1);
    1.74 +        memcpy (envvar, pvar, varlen);
    1.75 +        envvar [varlen] = '\0';
    1.76 +
    1.77 +        // Note: calling Solaris 7 putenv() during program startup
    1.78 +        // (i.e., from ctors of namespace-scope objects) prevents
    1.79 +        // getenv() from finding that variable at program runtime
    1.80 +        ret = putenv (envvar);
    1.81 +
    1.82 +        // determine wheteher putenv() made copy of the variable
    1.83 +        // or if it simply used the pointer passed to it; if the
    1.84 +        // former, deallocate the buffer dynamically allocated
    1.85 +        // above
    1.86 +
    1.87 +        char namebuf [256];
    1.88 +        char* const equals = strchr (envvar, '=');
    1.89 +        if (equals) {
    1.90 +            assert (size_t (equals - envvar) < sizeof namebuf);
    1.91 +
    1.92 +            memcpy (namebuf, envvar, equals - envvar);
    1.93 +            namebuf [equals - envvar] = '\0';
    1.94 +
    1.95 +            const char* const var = getenv (namebuf);
    1.96 +
    1.97 +            if (equals + 1 != var)
    1.98 +                free (envvar);
    1.99 +        }
   1.100 +        else
   1.101 +            free (envvar);
   1.102 +
   1.103 +        pvar = pend + !!*pend;
   1.104 +    }
   1.105 +
   1.106 +    if (1 == nset)
   1.107 +        return ret;
   1.108 +
   1.109 +    return nset;
   1.110 +}