env-generic.cpp revision 4157c84dffb94fe976739ad709e4fd95d22c6667
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * innotek Portable Runtime - Environment, Generic.
98427c0ab08697e468c26dc33ee9571308577867vboxsync * Copyright (C) 2006-2007 innotek GmbH
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * available from http://www.virtualbox.org. This file is free software;
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * you can redistribute it and/or modify it under the terms of the GNU
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * General Public License as published by the Free Software Foundation,
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/*******************************************************************************
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync* Header Files *
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync*******************************************************************************/
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#if defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncextern char **environ;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/*******************************************************************************
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync* Defined Constants And Macros *
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync*******************************************************************************/
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/** Macro that unlocks the specified environment block. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/** Macro that unlocks the specified environment block. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/*******************************************************************************
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync* Structures and Typedefs *
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync*******************************************************************************/
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * The internal representation of a (non-default) environment.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /** Magic value . */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /** Number of variables in the array.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * This does not include the terminating NULL entry. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /** Capacity (allocated size) of the array.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * This includes space for the terminating NULL element (for compatibility
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * with the C library), so that c <= cCapacity - 1. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /** Array of environment variables. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /** Array of environment variables in the process CP.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * This get (re-)constructed when RTEnvGetExecEnvP method is called. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/** The allocation granularity of the RTENVINTERNAL::papszEnv memory. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Internal worker that resolves the pointer to the default
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * process environment. (environ)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * @returns Pointer to the default environment.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * This may be NULL.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncstatic const char * const *rtEnvDefault(void)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /* So far, our L4 libraries do not include environment support. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Internal worker that creates an environment handle with a specified capacity.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * @returns IPRT status code.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * @param ppIntEnv Where to store the result.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * @param cAllocated The initial array size.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncstatic int rtEnvCreate(PRTENVINTERNAL *ppIntEnv, size_t cAllocated)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Allocate environment handle.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync PRTENVINTERNAL pIntEnv = (PRTENVINTERNAL)RTMemAlloc(sizeof(*pIntEnv));
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Pre-allocate the variable array.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync pIntEnv->cAllocated = RT_ALIGN_Z(RT_MAX(cAllocated, RTENV_GROW_SIZE), RTENV_GROW_SIZE);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync pIntEnv->papszEnv = (char **)RTMemAllocZ(sizeof(pIntEnv->papszEnv[0]) * pIntEnv->cAllocated);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Ignore NIL_RTENV and validate input.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Do the cleanup.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync while (iVar-- > 0)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync for (iVar = 0; pIntEnv->papszEnvOtherCP[iVar]; iVar++)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /*RTCritSectDelete(&pIntEnv->CritSect) */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncRTDECL(int) RTEnvClone(PRTENV pEnv, RTENV EnvToClone)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Validate input and figure out how many variable to clone and where to get them.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync const char * const *papszEnv;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertPtrReturn(pIntEnvToClone, VERR_INVALID_HANDLE);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertReturn(pIntEnvToClone->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Create the duplicate.
98427c0ab08697e468c26dc33ee9571308577867vboxsync int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /* ASSUMES the default environment is in the current codepage. */
98427c0ab08697e468c26dc33ee9571308577867vboxsync int rc = RTStrCurrentCPToUtf8(&pIntEnv->papszEnv[iVar], papszEnv[iVar]);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncRTDECL(int) RTEnvPutEx(RTENV Env, const char *pszVarEqualValue)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertPtrReturn(pszVarEqualValue, VERR_INVALID_POINTER);
98427c0ab08697e468c26dc33ee9571308577867vboxsync * Make a copy of the variable name so we can terminate it
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * properly and then pass the request on to RTEnvSetEx.
int rc;
char *pszVarOtherCP;
char *pszValueOtherCP;
if (pszEntry)
void *pvNew = RTMemRealloc(pIntEnv->papszEnv, sizeof(char *) * (pIntEnv->cAllocated + RTENV_GROW_SIZE));
if (!pvNew)
return rc;
int rc;
char *pszVarOtherCP;
return rc;
RTDECL(int) RTEnvGetEx(RTENV Env, const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
if (pcchActual)
*pcchActual = 0;
int rc;
char *pszVarOtherCP;
if (pszValueOtherCP)
char *pszValueUtf8;
if (pcchActual)
if (pcchActual)
return rc;
bool fExist = false;
char *pszVarOtherCP;
fExist = true;
return fExist;
const char * const *papszRet;
if (!papszRet)
char **papsz;
papszRet = pIntEnv->papszEnvOtherCP = papsz = (char **)RTMemAlloc(sizeof(char *) * (pIntEnv->cVars + 1));
if (papsz)
return papszRet;