VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.6 KB
Line 
1/* $Id: env-posix.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Environment, Posix.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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
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#include <iprt/err.h>
43#include <iprt/string.h>
44
45#include <stdlib.h>
46#include <errno.h>
47
48#include "internal/alignmentchecks.h"
49
50
51RTDECL(bool) RTEnvExistsBad(const char *pszVar)
52{
53 AssertReturn(strchr(pszVar, '=') == NULL, false);
54 return RTEnvGetBad(pszVar) != NULL;
55}
56
57
58RTDECL(bool) RTEnvExist(const char *pszVar)
59{
60 return RTEnvExistsBad(pszVar);
61}
62
63
64RTDECL(const char *) RTEnvGetBad(const char *pszVar)
65{
66 AssertReturn(strchr(pszVar, '=') == NULL, NULL);
67
68 IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc causes trouble */
69 const char *pszValue = getenv(pszVar);
70 IPRT_ALIGNMENT_CHECKS_ENABLE();
71 return pszValue;
72}
73
74
75RTDECL(const char *) RTEnvGet(const char *pszVar)
76{
77 return RTEnvGetBad(pszVar);
78}
79
80
81RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
82{
83 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
84 if (!putenv((char *)pszVarEqualValue))
85 return 0;
86 return RTErrConvertFromErrno(errno);
87}
88
89
90RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
91{
92 return RTEnvPutBad(pszVarEqualValue);
93}
94
95
96RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
97{
98 AssertMsgReturn(strchr(pszVar, '=') == NULL, ("'%s'\n", pszVar), VERR_ENV_INVALID_VAR_NAME);
99
100#if defined(_MSC_VER)
101 /* make a local copy and feed it to putenv. */
102 const size_t cchVar = strlen(pszVar);
103 const size_t cchValue = strlen(pszValue);
104 char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
105 memcpy(pszTmp, pszVar, cchVar);
106 pszTmp[cchVar] = '=';
107 if (*pszValue)
108 memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
109 else
110 {
111 pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
112 pszTmp[cchVar + 2] = '\0';
113 }
114
115 if (!putenv(pszTmp))
116 return 0;
117 return RTErrConvertFromErrno(errno);
118
119#else
120 if (!setenv(pszVar, pszValue, 1))
121 return VINF_SUCCESS;
122 return RTErrConvertFromErrno(errno);
123#endif
124}
125
126
127RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
128{
129 return RTEnvSetBad(pszVar, pszValue);
130}
131
132RTDECL(int) RTEnvUnsetBad(const char *pszVar)
133{
134 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
135
136 /*
137 * Check that it exists first.
138 */
139 if (!RTEnvExist(pszVar))
140 return VINF_ENV_VAR_NOT_FOUND;
141
142 /*
143 * Ok, try remove it.
144 */
145#ifdef RT_OS_WINDOWS
146 /* Use putenv(var=) since Windows does not have unsetenv(). */
147 size_t cchVar = strlen(pszVar);
148 char *pszBuf = (char *)alloca(cchVar + 2);
149 memcpy(pszBuf, pszVar, cchVar);
150 pszBuf[cchVar] = '=';
151 pszBuf[cchVar + 1] = '\0';
152
153 if (!putenv(pszBuf))
154 return VINF_SUCCESS;
155
156#else
157 /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
158 if (!unsetenv((char*)pszVar))
159 return VINF_SUCCESS;
160#endif
161
162 return RTErrConvertFromErrno(errno);
163}
164
165RTDECL(int) RTEnvUnset(const char *pszVar)
166{
167 return RTEnvUnsetBad(pszVar);
168}
169
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use