env-posix.cpp revision e593c0cb11d4eccdc28dcf82c9b8aac46c631371
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/* $Id$ */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Environment, Posix.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * available from http://www.virtualbox.org. This file is free software;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/*******************************************************************************
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync* Header Files *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync*******************************************************************************/
e593c0cb11d4eccdc28dcf82c9b8aac46c631371vboxsync#ifdef RT_OS_DARWIN
e593c0cb11d4eccdc28dcf82c9b8aac46c631371vboxsync/*XXX: see sys/cdefs.h */
e593c0cb11d4eccdc28dcf82c9b8aac46c631371vboxsync#define __DARWIN_UNIX03 1
e593c0cb11d4eccdc28dcf82c9b8aac46c631371vboxsync#endif
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <iprt/env.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <iprt/string.h>
739c77783387df1b22501b27bd870869a865d20cvboxsync#include <iprt/alloca.h>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#include <iprt/assert.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <stdlib.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <errno.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsyncRTDECL(bool) RTEnvExist(const char *pszVar)
739c77783387df1b22501b27bd870869a865d20cvboxsync{
739c77783387df1b22501b27bd870869a865d20cvboxsync return getenv(pszVar) != NULL;
739c77783387df1b22501b27bd870869a865d20cvboxsync}
739c77783387df1b22501b27bd870869a865d20cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsyncRTDECL(const char *) RTEnvGet(const char *pszVar)
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync{
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync return getenv(pszVar);
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync}
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsyncRTDECL(int) RTEnvPut(const char *pszVarEqualValue)
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync{
739c77783387df1b22501b27bd870869a865d20cvboxsync /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync if (!putenv((char *)pszVarEqualValue))
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync return 0;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync return RTErrConvertFromErrno(errno);
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync}
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsyncRTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
739c77783387df1b22501b27bd870869a865d20cvboxsync{
739c77783387df1b22501b27bd870869a865d20cvboxsync#if defined(_MSC_VER)
739c77783387df1b22501b27bd870869a865d20cvboxsync /* make a local copy and feed it to putenv. */
739c77783387df1b22501b27bd870869a865d20cvboxsync const size_t cchVar = strlen(pszVar);
739c77783387df1b22501b27bd870869a865d20cvboxsync const size_t cchValue = strlen(pszValue);
739c77783387df1b22501b27bd870869a865d20cvboxsync char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
739c77783387df1b22501b27bd870869a865d20cvboxsync memcpy(pszTmp, pszVar, cchVar);
739c77783387df1b22501b27bd870869a865d20cvboxsync pszTmp[cchVar] = '=';
739c77783387df1b22501b27bd870869a865d20cvboxsync if (*pszValue)
739c77783387df1b22501b27bd870869a865d20cvboxsync memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
739c77783387df1b22501b27bd870869a865d20cvboxsync else
739c77783387df1b22501b27bd870869a865d20cvboxsync {
739c77783387df1b22501b27bd870869a865d20cvboxsync pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
739c77783387df1b22501b27bd870869a865d20cvboxsync pszTmp[cchVar + 2] = '\0';
739c77783387df1b22501b27bd870869a865d20cvboxsync }
739c77783387df1b22501b27bd870869a865d20cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync if (!putenv(pszTmp))
739c77783387df1b22501b27bd870869a865d20cvboxsync return 0;
739c77783387df1b22501b27bd870869a865d20cvboxsync return RTErrConvertFromErrno(errno);
739c77783387df1b22501b27bd870869a865d20cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync#else
739c77783387df1b22501b27bd870869a865d20cvboxsync if (!setenv(pszVar, pszValue, 1))
739c77783387df1b22501b27bd870869a865d20cvboxsync return VINF_SUCCESS;
739c77783387df1b22501b27bd870869a865d20cvboxsync return RTErrConvertFromErrno(errno);
739c77783387df1b22501b27bd870869a865d20cvboxsync#endif
739c77783387df1b22501b27bd870869a865d20cvboxsync}
739c77783387df1b22501b27bd870869a865d20cvboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTDECL(int) RTEnvUnset(const char *pszVar)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync{
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync /* Check that it exists first. */
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync if (!RTEnvExist(pszVar))
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return VINF_ENV_VAR_NOT_FOUND;
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync /* Ok, try remove it. */
9815e49bff82d86e25ec26dba9eb26a92f8c72f6vboxsync#ifdef RT_OS_WINDOWS
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync /* Windows does not have unsetenv(). Clear the environment variable according to the MSN docs. */
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync char *pszBuf;
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync int rc = RTStrAPrintf(&pszBuf, "%s=", pszVar);
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync if (RT_FAILURE(rc))
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync return rc;
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync rc = putenv(pszBuf);
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync RTStrFree(pszBuf);
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync if (!rc)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return VINF_SUCCESS;
9815e49bff82d86e25ec26dba9eb26a92f8c72f6vboxsync#else
8556cbc7d613d44c2d31260928bd71d17feb492fvboxsync /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
a1f113e90f9ac5b466cc7fef22943a3fb3068c12vboxsync if (!unsetenv((char*)pszVar))
9815e49bff82d86e25ec26dba9eb26a92f8c72f6vboxsync return VINF_SUCCESS;
9815e49bff82d86e25ec26dba9eb26a92f8c72f6vboxsync#endif
9815e49bff82d86e25ec26dba9eb26a92f8c72f6vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return RTErrConvertFromErrno(errno);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync}
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync