VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/env-posix.cpp@ 30552

Last change on this file since 30552 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/* $Id: env-posix.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Environment, Posix.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#ifdef RT_OS_DARWIN
32/* pick the correct prototype for unsetenv. */
33# define _POSIX_C_SOURCE 1
34#endif
35#include <iprt/env.h>
36#include <iprt/string.h>
37#include <iprt/alloca.h>
38#include <iprt/assert.h>
39#if defined(DEBUG) && defined(RT_OS_LINUX)
40# include <iprt/asm.h>
41#endif
42
43#include <stdlib.h>
44#include <errno.h>
45
46#include "internal/alignmentchecks.h"
47
48
49RTDECL(bool) RTEnvExist(const char *pszVar)
50{
51 return RTEnvGet(pszVar) != NULL;
52}
53
54
55RTDECL(const char *) RTEnvGet(const char *pszVar)
56{
57 IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc causes trouble */
58 const char *pszValue = getenv(pszVar);
59 IPRT_ALIGNMENT_CHECKS_ENABLE();
60 return pszValue;
61}
62
63
64RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
65{
66 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
67 if (!putenv((char *)pszVarEqualValue))
68 return 0;
69 return RTErrConvertFromErrno(errno);
70}
71
72RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
73{
74#if defined(_MSC_VER)
75 /* make a local copy and feed it to putenv. */
76 const size_t cchVar = strlen(pszVar);
77 const size_t cchValue = strlen(pszValue);
78 char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
79 memcpy(pszTmp, pszVar, cchVar);
80 pszTmp[cchVar] = '=';
81 if (*pszValue)
82 memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
83 else
84 {
85 pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
86 pszTmp[cchVar + 2] = '\0';
87 }
88
89 if (!putenv(pszTmp))
90 return 0;
91 return RTErrConvertFromErrno(errno);
92
93#else
94 if (!setenv(pszVar, pszValue, 1))
95 return VINF_SUCCESS;
96 return RTErrConvertFromErrno(errno);
97#endif
98}
99
100
101RTDECL(int) RTEnvUnset(const char *pszVar)
102{
103 AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
104
105 /* Check that it exists first. */
106 if (!RTEnvExist(pszVar))
107 return VINF_ENV_VAR_NOT_FOUND;
108
109 /* Ok, try remove it. */
110#ifdef RT_OS_WINDOWS
111 /* Windows does not have unsetenv(). Clear the environment variable according to the MSN docs. */
112 char *pszBuf;
113 int rc = RTStrAPrintf(&pszBuf, "%s=", pszVar);
114 if (RT_FAILURE(rc))
115 return rc;
116 rc = putenv(pszBuf);
117 RTStrFree(pszBuf);
118 if (!rc)
119 return VINF_SUCCESS;
120#else
121 /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
122 if (!unsetenv((char*)pszVar))
123 return VINF_SUCCESS;
124#endif
125
126 return RTErrConvertFromErrno(errno);
127}
128
Note: See TracBrowser for help on using the repository browser.

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