1 | /* $Id: env-posix.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Environment, Posix.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #ifdef RT_OS_DARWIN
|
---|
42 | /* pick the correct prototype for unsetenv. */
|
---|
43 | # define _POSIX_C_SOURCE 1
|
---|
44 | #endif
|
---|
45 | #include <iprt/env.h>
|
---|
46 |
|
---|
47 | #include <iprt/alloca.h>
|
---|
48 | #include <iprt/assert.h>
|
---|
49 | #if defined(DEBUG) && defined(RT_OS_LINUX)
|
---|
50 | # include <iprt/asm.h>
|
---|
51 | #endif
|
---|
52 | #include <iprt/err.h>
|
---|
53 | #include <iprt/string.h>
|
---|
54 |
|
---|
55 | #include <stdlib.h>
|
---|
56 | #include <errno.h>
|
---|
57 |
|
---|
58 | #include "internal/alignmentchecks.h"
|
---|
59 |
|
---|
60 |
|
---|
61 | RTDECL(bool) RTEnvExistsBad(const char *pszVar)
|
---|
62 | {
|
---|
63 | AssertReturn(strchr(pszVar, '=') == NULL, false);
|
---|
64 | return RTEnvGetBad(pszVar) != NULL;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | RTDECL(bool) RTEnvExist(const char *pszVar)
|
---|
69 | {
|
---|
70 | return RTEnvExistsBad(pszVar);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(const char *) RTEnvGetBad(const char *pszVar)
|
---|
75 | {
|
---|
76 | AssertReturn(strchr(pszVar, '=') == NULL, NULL);
|
---|
77 |
|
---|
78 | IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc causes trouble */
|
---|
79 | const char *pszValue = getenv(pszVar);
|
---|
80 | IPRT_ALIGNMENT_CHECKS_ENABLE();
|
---|
81 | return pszValue;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | RTDECL(const char *) RTEnvGet(const char *pszVar)
|
---|
86 | {
|
---|
87 | return RTEnvGetBad(pszVar);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
|
---|
92 | {
|
---|
93 | /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
|
---|
94 | if (!putenv((char *)pszVarEqualValue))
|
---|
95 | return 0;
|
---|
96 | return RTErrConvertFromErrno(errno);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
|
---|
101 | {
|
---|
102 | return RTEnvPutBad(pszVarEqualValue);
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
|
---|
107 | {
|
---|
108 | AssertMsgReturn(strchr(pszVar, '=') == NULL, ("'%s'\n", pszVar), VERR_ENV_INVALID_VAR_NAME);
|
---|
109 |
|
---|
110 | #if defined(_MSC_VER)
|
---|
111 | /* make a local copy and feed it to putenv. */
|
---|
112 | const size_t cchVar = strlen(pszVar);
|
---|
113 | const size_t cchValue = strlen(pszValue);
|
---|
114 | char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
|
---|
115 | memcpy(pszTmp, pszVar, cchVar);
|
---|
116 | pszTmp[cchVar] = '=';
|
---|
117 | if (*pszValue)
|
---|
118 | memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
|
---|
119 | else
|
---|
120 | {
|
---|
121 | pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
|
---|
122 | pszTmp[cchVar + 2] = '\0';
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (!putenv(pszTmp))
|
---|
126 | return 0;
|
---|
127 | return RTErrConvertFromErrno(errno);
|
---|
128 |
|
---|
129 | #else
|
---|
130 | if (!setenv(pszVar, pszValue, 1))
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | return RTErrConvertFromErrno(errno);
|
---|
133 | #endif
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
|
---|
138 | {
|
---|
139 | return RTEnvSetBad(pszVar, pszValue);
|
---|
140 | }
|
---|
141 |
|
---|
142 | RTDECL(int) RTEnvUnsetBad(const char *pszVar)
|
---|
143 | {
|
---|
144 | AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Check that it exists first.
|
---|
148 | */
|
---|
149 | if (!RTEnvExist(pszVar))
|
---|
150 | return VINF_ENV_VAR_NOT_FOUND;
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Ok, try remove it.
|
---|
154 | */
|
---|
155 | #ifdef RT_OS_WINDOWS
|
---|
156 | /* Use putenv(var=) since Windows does not have unsetenv(). */
|
---|
157 | size_t cchVar = strlen(pszVar);
|
---|
158 | char *pszBuf = (char *)alloca(cchVar + 2);
|
---|
159 | memcpy(pszBuf, pszVar, cchVar);
|
---|
160 | pszBuf[cchVar] = '=';
|
---|
161 | pszBuf[cchVar + 1] = '\0';
|
---|
162 |
|
---|
163 | if (!putenv(pszBuf))
|
---|
164 | return VINF_SUCCESS;
|
---|
165 |
|
---|
166 | #else
|
---|
167 | /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
|
---|
168 | if (!unsetenv((char*)pszVar))
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | return RTErrConvertFromErrno(errno);
|
---|
173 | }
|
---|
174 |
|
---|
175 | RTDECL(int) RTEnvUnset(const char *pszVar)
|
---|
176 | {
|
---|
177 | return RTEnvUnsetBad(pszVar);
|
---|
178 | }
|
---|
179 |
|
---|