dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * IPRT - Environment, Posix.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Copyright (C) 2006-2014 Oracle Corporation
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * available from http://www.virtualbox.org. This file is free software;
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * you can redistribute it and/or modify it under the terms of the GNU
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * General Public License (GPL) as published by the Free Software
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * The contents of this file may alternatively be used under the terms
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * of the Common Development and Distribution License Version 1.0
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * VirtualBox OSE distribution, in which case the provisions of the
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * CDDL are applicable instead of those of the GPL.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * You may elect to license modified versions of this file under the
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * terms and conditions of either the GPL or the CDDL or both.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync/*******************************************************************************
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync* Header Files *
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync*******************************************************************************/
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(const char *) RTEnvGetBad(const char *pszVar)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync AssertReturn(pszValue || !cbValue, VERR_INVALID_PARAMETER);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync /** @todo Consider _wgetenv_s or GetEnvironmentVariableW here to avoid the
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * potential race with a concurrent _wputenv/_putenv. */
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync rc = RTUtf16ToUtf8Ex(pwszValue, RTSTR_MAX, &pszValue, cbValue, pcchActual);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync rc = RTUtf16CalcUtf8LenEx(pwszValue, RTSTR_MAX, pcchActual);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync int rc = RTStrToUtf16(pszVarEqualValue, &pwszVarEqualValue);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync /* make a local copy and feed it to putenv. */
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcValue);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + cwcValue + 1) * sizeof(RTUTF16));
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync rc = RTStrToUtf16Ex(pszVar, RTSTR_MAX, &pwszTmp, cwcVar + 1, NULL);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync rc = RTStrToUtf16Ex(pszValue, RTSTR_MAX, &pwszTmpValue, cwcValue + 1, NULL);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Check that it exists first.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Ok, try remove it.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync /* Use putenv(var=) since Windows does not have unsetenv(). */
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + 1) * sizeof(RTUTF16));