VirtualBox

Changeset 1494 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Mar 15, 2007 1:18:15 AM (18 years ago)
Author:
vboxsync
Message:

two new RTEnv APIs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/posix/env-posix.cpp

    r936 r1494  
    2626#include <iprt/env.h>
    2727#include <iprt/string.h>
     28#include <iprt/alloca.h>
    2829
    2930#include <stdlib.h>
     
    3132
    3233
    33 /**
    34  * Gets an environment variable (getenv).
    35  *
    36  * The caller is responsible for ensuring that nobody changes the environment
    37  * while it's using the returned string pointer!
    38  *
    39  * @returns Pointer to read only string on success, NULL if the variable wasn't found.
    40  *
    41  * @param   pszVar      The environment variable name.
    42  */
     34RTDECL(bool) RTEnvExist(const char *pszVar)
     35{
     36    return getenv(pszVar) != NULL;
     37}
     38
     39
    4340RTDECL(const char *) RTEnvGet(const char *pszVar)
    4441{
     
    4744
    4845
    49 /**
    50  * Puts an variable=value string into the environment (putenv).
    51  *
    52  * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
    53  *
    54  * @param   pszVarEqualValue    The variable '=' value string. If the value and '=' is
    55  *                              omitted, the variable is removed from the environment.
    56  */
    5746RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
    5847{
    59     /** @todo putenv is a memory leak. deal with this on a per system basis. */
     48    /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
    6049    if (!putenv((char *)pszVarEqualValue))
    6150        return 0;
     
    6352}
    6453
     54RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
     55{
     56#if defined(_MSC_VER)
     57    /* make a local copy and feed it to putenv. */
     58    const size_t cchVar = strlen(pszVar);
     59    const size_t cchValue = strlen(pszValue);
     60    char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
     61    memcpy(pszTmp, pszVar, cchVar);
     62    pszTmp[cchVar] = '=';
     63    if (*pszValue)
     64        memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
     65    else
     66    {
     67        pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
     68        pszTmp[cchVar + 2] = '\0';
     69    }
     70
     71    if (!putenv(pszTmp))
     72        return 0;
     73    return RTErrConvertFromErrno(errno);
     74   
     75#else
     76    if (!setenv(pszVar, pszValue, 1))
     77        return VINF_SUCCESS;
     78    return RTErrConvertFromErrno(errno);
     79#endif
     80}
     81
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette