os/ossrv/stdcpp/tsrc/Stdcpp_test/stdcxx/testengine/src/environ.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/************************************************************************
sl@0
     2
 *
sl@0
     3
 * environ.cpp - definitions of testsuite helpers
sl@0
     4
 *
sl@0
     5
 * $Id: environ.cpp 290013 2005-09-18 23:47:55Z sebor $
sl@0
     6
 *
sl@0
     7
 ************************************************************************
sl@0
     8
 *
sl@0
     9
 * Copyright (c) 1994-2005 Quovadx,  Inc., acting through its  Rogue Wave
sl@0
    10
 * Software division. Licensed under the Apache License, Version 2.0 (the
sl@0
    11
 * "License");  you may  not use this file except  in compliance with the
sl@0
    12
 * License.    You    may   obtain   a   copy   of    the   License    at
sl@0
    13
 * http://www.apache.org/licenses/LICENSE-2.0.    Unless   required    by
sl@0
    14
 * applicable law  or agreed to  in writing,  software  distributed under
sl@0
    15
 * the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR
sl@0
    16
 * CONDITIONS OF  ANY KIND, either  express or implied.  See  the License
sl@0
    17
 * for the specific language governing permissions  and limitations under
sl@0
    18
 * the License.
sl@0
    19
 * 
sl@0
    20
 **************************************************************************/
sl@0
    21
sl@0
    22
// expand _TEST_EXPORT macros
sl@0
    23
#define _RWSTD_TEST_SRC
sl@0
    24
sl@0
    25
#include <environ.h>
sl@0
    26
sl@0
    27
#include <assert.h>   // for assert
sl@0
    28
#include <stdlib.h>   // for getenv, malloc, putenv
sl@0
    29
#include <string.h>   // for strchr, strlen, ...
sl@0
    30
sl@0
    31
sl@0
    32
extern "C" {
sl@0
    33
sl@0
    34
#ifndef _RWSTD_NO_PUTENV_CONST_CHAR
sl@0
    35
sl@0
    36
IMPORT_C int putenv (const char*) _LIBC_THROWS ();
sl@0
    37
sl@0
    38
#else   // if defined (_RWSTD_NO_PUTENV_CONST_CHAR)
sl@0
    39
sl@0
    40
_RWSTD_DLLIMPORT int putenv (char*) _LIBC_THROWS ();
sl@0
    41
sl@0
    42
#endif   // _RWSTD_NO_PUTENV_CONST_CHAR
sl@0
    43
sl@0
    44
}   // extern "C"
sl@0
    45
sl@0
    46
sl@0
    47
// sets one or more sep-separated environment variables
sl@0
    48
_TEST_EXPORT int
sl@0
    49
rw_putenv (const char* str, int sep /* = -1 */)
sl@0
    50
{
sl@0
    51
    int nset = 0;
sl@0
    52
    int ret  = 0;
sl@0
    53
sl@0
    54
    if (!str) {
sl@0
    55
        str = getenv ("RW_PUTENV");
sl@0
    56
        if (str)
sl@0
    57
            sep = *str++;
sl@0
    58
        else
sl@0
    59
            return 0;
sl@0
    60
    }
sl@0
    61
sl@0
    62
    for (const char *pvar = str; pvar && *pvar; ++nset) {
sl@0
    63
sl@0
    64
        const char *pend = strchr (pvar, sep);
sl@0
    65
        if (!pend)
sl@0
    66
            pend = pvar + strlen (pvar);
sl@0
    67
sl@0
    68
        const size_t varlen = pend - pvar;
sl@0
    69
sl@0
    70
        char* const envvar = (char*)malloc (pend - pvar + 1);
sl@0
    71
        memcpy (envvar, pvar, varlen);
sl@0
    72
        envvar [varlen] = '\0';
sl@0
    73
sl@0
    74
        // Note: calling Solaris 7 putenv() during program startup
sl@0
    75
        // (i.e., from ctors of namespace-scope objects) prevents
sl@0
    76
        // getenv() from finding that variable at program runtime
sl@0
    77
        ret = putenv (envvar);
sl@0
    78
sl@0
    79
        // determine wheteher putenv() made copy of the variable
sl@0
    80
        // or if it simply used the pointer passed to it; if the
sl@0
    81
        // former, deallocate the buffer dynamically allocated
sl@0
    82
        // above
sl@0
    83
sl@0
    84
        char namebuf [256];
sl@0
    85
        char* const equals = strchr (envvar, '=');
sl@0
    86
        if (equals) {
sl@0
    87
            assert (size_t (equals - envvar) < sizeof namebuf);
sl@0
    88
sl@0
    89
            memcpy (namebuf, envvar, equals - envvar);
sl@0
    90
            namebuf [equals - envvar] = '\0';
sl@0
    91
sl@0
    92
            const char* const var = getenv (namebuf);
sl@0
    93
sl@0
    94
            if (equals + 1 != var)
sl@0
    95
                free (envvar);
sl@0
    96
        }
sl@0
    97
        else
sl@0
    98
            free (envvar);
sl@0
    99
sl@0
   100
        pvar = pend + !!*pend;
sl@0
   101
    }
sl@0
   102
sl@0
   103
    if (1 == nset)
sl@0
   104
        return ret;
sl@0
   105
sl@0
   106
    return nset;
sl@0
   107
}