env-posix.cpp revision dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/* $Id$ */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Environment, Posix.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/*
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Copyright (C) 2006-2014 Oracle Corporation
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.
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync */
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync/*******************************************************************************
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync* Header Files *
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync*******************************************************************************/
e593c0cb11d4eccdc28dcf82c9b8aac46c631371vboxsync#ifdef RT_OS_DARWIN
37c0e44bde09575588c7c31f768e9a85293423efvboxsync/* pick the correct prototype for unsetenv. */
37c0e44bde09575588c7c31f768e9a85293423efvboxsync# define _POSIX_C_SOURCE 1
e593c0cb11d4eccdc28dcf82c9b8aac46c631371vboxsync#endif
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <iprt/env.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <iprt/string.h>
739c77783387df1b22501b27bd870869a865d20cvboxsync#include <iprt/alloca.h>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#include <iprt/assert.h>
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync#if defined(DEBUG) && defined(RT_OS_LINUX)
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync# include <iprt/asm.h>
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync#endif
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <stdlib.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync#include <errno.h>
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync#include "internal/alignmentchecks.h"
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(bool) RTEnvExistsBad(const char *pszVar)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync{
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTEnvGetBad(pszVar) != NULL;
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync}
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsyncRTDECL(bool) RTEnvExist(const char *pszVar)
739c77783387df1b22501b27bd870869a865d20cvboxsync{
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTEnvExistsBad(pszVar);
739c77783387df1b22501b27bd870869a865d20cvboxsync}
739c77783387df1b22501b27bd870869a865d20cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(const char *) RTEnvGetBad(const char *pszVar)
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync{
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc causes trouble */
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync const char *pszValue = getenv(pszVar);
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync IPRT_ALIGNMENT_CHECKS_ENABLE();
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync return pszValue;
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync}
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
c39e02bbf326184d8f70d4d6f4fbceb8ea5b6b97vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(const char *) RTEnvGet(const char *pszVar)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync{
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTEnvGetBad(pszVar);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync}
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvPutBad(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
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvPut(const char *pszVarEqualValue)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync{
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTEnvPutBad(pszVarEqualValue);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync}
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvSetBad(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);
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync
739c77783387df1b22501b27bd870869a865d20cvboxsync#else
739c77783387df1b22501b27bd870869a865d20cvboxsync if (!setenv(pszVar, pszValue, 1))
739c77783387df1b22501b27bd870869a865d20cvboxsync return VINF_SUCCESS;
739c77783387df1b22501b27bd870869a865d20cvboxsync return RTErrConvertFromErrno(errno);
223b7220b8ccc3642433776c4c9d07e01448254cvboxsync#endif
739c77783387df1b22501b27bd870869a865d20cvboxsync}
739c77783387df1b22501b27bd870869a865d20cvboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync{
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTEnvSetBad(pszVar, pszValue);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync}
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvUnsetBad(const char *pszVar)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync{
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync /*
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync * Check that it exists first.
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync */
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync if (!RTEnvExist(pszVar))
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return VINF_ENV_VAR_NOT_FOUND;
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync /*
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync * Ok, try remove it.
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync */
9815e49bff82d86e25ec26dba9eb26a92f8c72f6vboxsync#ifdef RT_OS_WINDOWS
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync /* Use putenv(var=) since Windows does not have unsetenv(). */
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync size_t cchVar = strlen(pszVar);
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync char *pszBuf = (char *)alloca(cchVar + 2);
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync memcpy(pszBuf, pszVar, cchVar);
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync pszBuf[cchVar] = '=';
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync pszBuf[cchVar + 1] = '\0';
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync if (!putenv(pszBuf))
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return VINF_SUCCESS;
4a4a02cc2a09b5e3c55908c6995182c6b038e398vboxsync
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
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTEnvUnset(const char *pszVar)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync{
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTEnvUnsetBad(pszVar);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync}
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync