VirtualBox

Changeset 55584 in vbox


Ignore:
Timestamp:
May 1, 2015 7:11:32 PM (10 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
99957
Message:

iprt/env.h: Added RTEnvQueryUtf8Block, RTEnvFreeUtf8Block and RTEnvReset.

Location:
trunk
Files:
3 edited

Legend:

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

    r55562 r55584  
    8181
    8282/**
     83 * Resets the environment block to contain zero variables.
     84 *
     85 * @returns IPRT status code.
     86 *
     87 * @param   hEnv    Environment block handle.  RTENV_DEFAULT is not supported.
     88 */
     89RTDECL(int) RTEnvReset(RTENV hEnv);
     90
     91/**
    8392 * Get the execve/spawnve/main envp.
    8493 *
     
    112121 */
    113122RTDECL(void) RTEnvFreeUtf16Block(PRTUTF16 pwszzBlock);
     123
     124/**
     125 * Get a sorted, UTF-8 environment block.
     126 *
     127 * The environment block is a sequence of putenv formatted ("NAME=VALUE" or
     128 * "NAME") zero terminated strings ending with an empty string (i.e. last string
     129 * has two zeros).
     130 *
     131 * @returns IPRT status code.
     132 *
     133 * @param   hEnv            Environment block handle.
     134 * @param   fSorted         Whether to sort it, this will affect @a hEnv.
     135 * @param   ppszzBlock      Where to return the environment block.  This must be
     136 *                          freed by calling RTEnvFreeUtf8Block.
     137 * @param   pcbBlock        Where to return the size of the block. Optional.
     138 */
     139RTDECL(int) RTEnvQueryUtf8Block(RTENV hEnv, bool fSorted, char **ppszzBlock, size_t *pcbBlock);
     140
     141/**
     142 * Frees an environment block returned by RTEnvGetUtf8Block().
     143 *
     144 * @param   pszzBlock       What RTEnvGetUtf8Block returned.  NULL is ignored.
     145 */
     146RTDECL(void) RTEnvFreeUtf8Block(char *pszzBlock);
    114147
    115148/**
  • trunk/include/iprt/mangling.h

    r55562 r55584  
    519519# define RTEnvExistsUtf8                                RT_MANGLER(RTEnvExistsUtf8)
    520520# define RTEnvExistEx                                   RT_MANGLER(RTEnvExistEx)
     521# define RTEnvFreeUtf8Block                             RT_MANGLER(RTEnvFreeUtf8Block)
    521522# define RTEnvFreeUtf16Block                            RT_MANGLER(RTEnvFreeUtf16Block)
    522523# define RTEnvGet                                       RT_MANGLER(RTEnvGet)
     
    533534# define RTEnvPutEx                                     RT_MANGLER(RTEnvPutEx)
    534535# define RTEnvQueryUtf16Block                           RT_MANGLER(RTEnvQueryUtf16Block)
     536# define RTEnvQueryUtf8Block                            RT_MANGLER(RTEnvQueryUtf8Block)
     537# define RTEnvReset                                     RT_MANGLER(RTEnvReset)
    535538# define RTEnvSet                                       RT_MANGLER(RTEnvSet)
    536539# define RTEnvSetBad                                    RT_MANGLER(RTEnvSetBad)
  • trunk/src/VBox/Runtime/generic/env-generic.cpp

    r55562 r55584  
    348348}
    349349RT_EXPORT_SYMBOL(RTEnvClone);
     350
     351
     352RTDECL(int) RTEnvReset(RTENV hEnv)
     353{
     354    PRTENVINTERNAL pIntEnv = hEnv;
     355    AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
     356    AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
     357
     358    RTENV_LOCK(pIntEnv);
     359
     360    size_t iVar = pIntEnv->cVars;
     361    pIntEnv->cVars = 0;
     362    while (iVar-- > 0)
     363    {
     364        RTMemFree(pIntEnv->papszEnv[iVar]);
     365        pIntEnv->papszEnv[iVar] = NULL;
     366    }
     367
     368    RTENV_UNLOCK(pIntEnv);
     369    return VINF_SUCCESS;
     370}
     371RT_EXPORT_SYMBOL(RTEnvReset);
    350372
    351373
     
    885907    return rc;
    886908}
    887 RT_EXPORT_SYMBOL(RTEnvGetExecEnvP);
     909RT_EXPORT_SYMBOL(RTEnvQueryUtf16Block);
    888910
    889911
     
    893915}
    894916RT_EXPORT_SYMBOL(RTEnvFreeUtf16Block);
     917
     918
     919RTDECL(int) RTEnvQueryUtf8Block(RTENV hEnv, bool fSorted, char **ppszzBlock, size_t *pcbBlock)
     920{
     921    RTENV           hClone  = NIL_RTENV;
     922    PRTENVINTERNAL  pIntEnv;
     923    int             rc;
     924
     925    /*
     926     * Validate / simplify input.
     927     */
     928    if (hEnv == RTENV_DEFAULT)
     929    {
     930        rc = RTEnvClone(&hClone, RTENV_DEFAULT);
     931        if (RT_FAILURE(rc))
     932            return rc;
     933        pIntEnv = hClone;
     934    }
     935    else
     936    {
     937        pIntEnv = hEnv;
     938        AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
     939        AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
     940        rc = VINF_SUCCESS;
     941    }
     942
     943    RTENV_LOCK(pIntEnv);
     944
     945    /*
     946     * Sort it, if requested.
     947     */
     948    if (fSorted)
     949        RTSortApvShell((void **)pIntEnv->papszEnv, pIntEnv->cVars, rtEnvSortCompare, pIntEnv);
     950
     951    /*
     952     * Calculate the size. We add one extra terminator just to be on the safe side.
     953     */
     954    size_t cbBlock = 2;
     955    for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
     956        cbBlock += strlen(pIntEnv->papszEnv[iVar]) + 1;
     957
     958    if (pcbBlock)
     959        *pcbBlock = cbBlock - 1;
     960
     961    /*
     962     * Allocate memory and copy out the variables.
     963     */
     964    char *pszzBlock;
     965    char *pszz = pszzBlock = (char *)RTMemAlloc(cbBlock);
     966    if (pszz)
     967    {
     968        size_t cbLeft = cbBlock;
     969        for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
     970        {
     971            size_t cb = strlen(pIntEnv->papszEnv[iVar]) + 1;
     972            AssertBreakStmt(cb + 2 <= cbLeft, rc = VERR_INTERNAL_ERROR_3);
     973            memcpy(pszz, pIntEnv->papszEnv[iVar], cb);
     974            pszz   += cb;
     975            cbLeft -= cb;
     976        }
     977        if (RT_SUCCESS(rc))
     978        {
     979            pszz[0] = '\0';
     980            pszz[1] = '\0'; /* The extra one. */
     981        }
     982        else
     983        {
     984            RTMemFree(pszzBlock);
     985            pszzBlock = NULL;
     986        }
     987    }
     988    else
     989        rc = VERR_NO_MEMORY;
     990
     991    RTENV_UNLOCK(pIntEnv);
     992
     993    if (hClone != NIL_RTENV)
     994        RTEnvDestroy(hClone);
     995    if (RT_SUCCESS(rc))
     996        *ppszzBlock = pszzBlock;
     997    return rc;
     998}
     999RT_EXPORT_SYMBOL(RTEnvQueryUtf8Block);
     1000
     1001
     1002RTDECL(void) RTEnvFreeUtf8Block(char *pszzBlock)
     1003{
     1004    RTMemFree(RTEnvQueryUtf8Block);
     1005}
     1006RT_EXPORT_SYMBOL(RTEnvFreeUtf8Block);
    8951007
    8961008
Note: See TracChangeset for help on using the changeset viewer.

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