VirtualBox

Changeset 50642 in vbox


Ignore:
Timestamp:
Feb 27, 2014 8:22:26 PM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
92536
Message:

iprt/env.h: Added RTEnvCountEx and RTEnvGetByIndexEx for enumerating the environment.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/env.h

    r50408 r50642  
    257257RTDECL(char *) RTEnvDupEx(RTENV Env, const char *pszVar);
    258258
     259/**
     260 * Counts the variables in the environment.
     261 *
     262 * @returns Number of variables in the environment. UINT32_MAX on error.
     263 * @param   hEnv        The environment handle.
     264 *                      RTENV_DEFAULT is currently not accepted.
     265 */
     266RTDECL(uint32_t) RTEnvCountEx(RTENV hEnv);
     267
     268/**
     269 * Queries an environment variable by it's index.
     270 *
     271 * This can be used together with RTEnvCount to enumerate the environment block.
     272 *
     273 * @returns IPRT status code.
     274 * @retval  VERR_ENV_VAR_NOT_FOUND if the index is out of bounds, output buffers
     275 *          untouched.
     276 * @retval  VERR_BUFFER_OVERFLOW if one of the buffers are too small.  We'll
     277 *          fill it with as much we can in RTStrCopy fashion.
     278 *
     279 * @param   hEnv        The environment handle.
     280 *                      RTENV_DEFAULT is currently not accepted.
     281 * @param   iVar        The variable index.
     282 * @param   pszVar      Variable name buffer.
     283 * @param   cbVar       The size of the variable name buffer.
     284 * @param   pszValue    Value buffer.
     285 * @param   cbValue     The size of the value buffer.
     286 */
     287RTDECL(uint32_t) RTEnvGetByIndexEx(RTENV hEnv, uint32_t iVar, char *pszVar, size_t cbVar, char *pszValue, size_t cbValue);
     288
    259289#endif /* IN_RING3 */
    260290
  • trunk/include/iprt/mangling.h

    r50526 r50642  
    495495# define RTDvmVolumeCreateVfsFile                       RT_MANGLER(RTDvmVolumeCreateVfsFile)
    496496# define RTEnvClone                                     RT_MANGLER(RTEnvClone)
     497# define RTEnvCountEx                                   RT_MANGLER(RTEnvCountEx)
    497498# define RTEnvCreate                                    RT_MANGLER(RTEnvCreate)
    498499# define RTEnvDestroy                                   RT_MANGLER(RTEnvDestroy)
     
    505506# define RTEnvGet                                       RT_MANGLER(RTEnvGet)
    506507# define RTEnvGetBad                                    RT_MANGLER(RTEnvGetBad)
     508# define RTEnvGetByIndexEx                              RT_MANGLER(RTEnvGetByIndexEx)
    507509# define RTEnvGetUtf8                                   RT_MANGLER(RTEnvGetUtf8)
    508510# define RTEnvGetEx                                     RT_MANGLER(RTEnvGetEx)
  • trunk/src/VBox/Runtime/generic/env-generic.cpp

    r50408 r50642  
    234234#ifdef RTENV_HAVE_WENVIRON
    235235        papszEnv  = NULL;
    236         papwszEnv = (PCRTUTF16 * const )_wenviron;
     236        papwszEnv = (PCRTUTF16 * const)_wenviron;
    237237        if (papwszEnv)
    238238            while (papwszEnv[cVars])
     
    842842RT_EXPORT_SYMBOL(RTEnvFreeUtf16Block);
    843843
     844
     845RTDECL(uint32_t) RTEnvCountEx(RTENV hEnv)
     846{
     847    PRTENVINTERNAL pIntEnv = hEnv;
     848    AssertPtrReturn(pIntEnv, UINT32_MAX);
     849    AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, UINT32_MAX);
     850
     851    RTENV_LOCK(pIntEnv);
     852    uint32_t cVars = (uint32_t)pIntEnv->cVars;
     853    RTENV_UNLOCK(pIntEnv);
     854
     855    return cVars;
     856}
     857RT_EXPORT_SYMBOL(RTEnvCountEx);
     858
     859
     860RTDECL(uint32_t) RTEnvGetByIndexEx(RTENV hEnv, uint32_t iVar, char *pszVar, size_t cbVar, char *pszValue, size_t cbValue)
     861{
     862    PRTENVINTERNAL pIntEnv = hEnv;
     863    AssertPtrReturn(pIntEnv, UINT32_MAX);
     864    AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, UINT32_MAX);
     865    if (cbVar)
     866        AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
     867    if (cbValue)
     868        AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
     869
     870    RTENV_LOCK(pIntEnv);
     871
     872    int rc;
     873    if (iVar < pIntEnv->cVars)
     874    {
     875        const char *pszSrcVar   = pIntEnv->papszEnv[iVar];
     876        const char *pszSrcValue = strchr(pszSrcVar, '=');
     877        bool        fHasEqual   = pszSrcValue != NULL;
     878        if (pszSrcValue)
     879            pszSrcValue++;
     880        else
     881            pszSrcValue = strchr(pszSrcVar, '\0');
     882        rc = VINF_SUCCESS;
     883        if (cbVar)
     884            rc = RTStrCopyEx(pszVar, cbVar, pszSrcVar, pszSrcValue - pszSrcVar - fHasEqual);
     885        if (cbValue)
     886        {
     887            int rc2 = RTStrCopy(pszValue, cbValue, pszSrcValue);
     888            if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
     889                rc = rc2;
     890        }
     891    }
     892    else
     893        rc = VERR_ENV_VAR_NOT_FOUND;
     894
     895    RTENV_UNLOCK(pIntEnv);
     896
     897    return rc;
     898}
     899RT_EXPORT_SYMBOL(RTEnvGetByIndexEx);
     900
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