VirtualBox

Changeset 1494

Show
Ignore:
Timestamp:
03/15/07 02:18:15 (2 years ago)
Author:
vboxsync
Message:

two new RTEnv APIs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/include/iprt/env.h

    r936 r1494  
    3434#ifdef IN_RING3 
    3535 
     36 
     37/** 
     38 * Checks if an environment variable exists. 
     39 *  
     40 * @returns IPRT status code. Typical error is VERR_NO_MEMORY. 
     41 *  
     42 * @param   pszVar      The environment variable name. 
     43 */ 
     44RTDECL(bool) RTEnvExist(const char *pszVar); 
     45 
    3646/** 
    3747 * Gets an environment variable (getenv). 
     
    5666RTDECL(int) RTEnvPut(const char *pszVarEqualValue); 
    5767 
     68/** 
     69 * Sets an environment variable (setenv(,,1)). 
     70 *  
     71 * @returns IPRT status code. Typical error is VERR_NO_MEMORY. 
     72 *  
     73 * @param   pszVar      The environment variable name. 
     74 * @param   pszValue    The environment variable value. 
     75 */ 
     76RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue); 
     77 
    5878/** @todo Add the missing environment APIs: safe, printf like, and various modifications. */ 
    5979 
  • 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 

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy