1 | /* $Id: env-win.cpp 50408 2014-02-11 02:21:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Environment, Posix.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | #include <iprt/env.h>
|
---|
32 |
|
---|
33 | #include <iprt/alloca.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 |
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <errno.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | RTDECL(bool) RTEnvExistsBad(const char *pszVar)
|
---|
43 | {
|
---|
44 | return RTEnvGetBad(pszVar) != NULL;
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | RTDECL(bool) RTEnvExist(const char *pszVar)
|
---|
49 | {
|
---|
50 | return RTEnvExistsBad(pszVar);
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | RTDECL(bool) RTEnvExistsUtf8(const char *pszVar)
|
---|
55 | {
|
---|
56 | PRTUTF16 pwszVar;
|
---|
57 | int rc = RTStrToUtf16(pszVar, &pwszVar);
|
---|
58 | AssertRCReturn(rc, false);
|
---|
59 | bool fRet = _wgetenv(pwszVar) != NULL;
|
---|
60 | RTUtf16Free(pwszVar);
|
---|
61 | return fRet;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | RTDECL(const char *) RTEnvGetBad(const char *pszVar)
|
---|
66 | {
|
---|
67 | return getenv(pszVar);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(const char *) RTEnvGet(const char *pszVar)
|
---|
72 | {
|
---|
73 | return RTEnvGetBad(pszVar);
|
---|
74 | }
|
---|
75 |
|
---|
76 | RTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
|
---|
77 | {
|
---|
78 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
79 | AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER);
|
---|
80 | AssertReturn(pszValue || !cbValue, VERR_INVALID_PARAMETER);
|
---|
81 | AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER);
|
---|
82 | AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER);
|
---|
83 |
|
---|
84 | if (pcchActual)
|
---|
85 | *pcchActual = 0;
|
---|
86 |
|
---|
87 | PRTUTF16 pwszVar;
|
---|
88 | int rc = RTStrToUtf16(pszVar, &pwszVar);
|
---|
89 | AssertRCReturn(rc, false);
|
---|
90 |
|
---|
91 | /** @todo Consider _wgetenv_s or GetEnvironmentVariableW here to avoid the
|
---|
92 | * potential race with a concurrent _wputenv/_putenv. */
|
---|
93 | PCRTUTF16 pwszValue = _wgetenv(pwszVar);
|
---|
94 | RTUtf16Free(pwszVar);
|
---|
95 | if (pwszValue)
|
---|
96 | {
|
---|
97 | if (cbValue)
|
---|
98 | rc = RTUtf16ToUtf8Ex(pwszValue, RTSTR_MAX, &pszValue, cbValue, pcchActual);
|
---|
99 | else
|
---|
100 | rc = RTUtf16CalcUtf8LenEx(pwszValue, RTSTR_MAX, pcchActual);
|
---|
101 | }
|
---|
102 | else
|
---|
103 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
|
---|
109 | {
|
---|
110 | /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
|
---|
111 | if (!putenv((char *)pszVarEqualValue))
|
---|
112 | return 0;
|
---|
113 | return RTErrConvertFromErrno(errno);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
|
---|
118 | {
|
---|
119 | return RTEnvPutBad(pszVarEqualValue);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue)
|
---|
124 | {
|
---|
125 | PRTUTF16 pwszVarEqualValue;
|
---|
126 | int rc = RTStrToUtf16(pszVarEqualValue, &pwszVarEqualValue);
|
---|
127 | if (RT_SUCCESS(rc))
|
---|
128 | {
|
---|
129 | if (!_wputenv(pwszVarEqualValue))
|
---|
130 | rc = VINF_SUCCESS;
|
---|
131 | else
|
---|
132 | rc = RTErrConvertFromErrno(errno);
|
---|
133 | RTUtf16Free(pwszVarEqualValue);
|
---|
134 | }
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 | RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
|
---|
141 | {
|
---|
142 | /* make a local copy and feed it to putenv. */
|
---|
143 | const size_t cchVar = strlen(pszVar);
|
---|
144 | const size_t cchValue = strlen(pszValue);
|
---|
145 | char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
|
---|
146 | memcpy(pszTmp, pszVar, cchVar);
|
---|
147 | pszTmp[cchVar] = '=';
|
---|
148 | if (*pszValue)
|
---|
149 | memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
|
---|
150 | else
|
---|
151 | {
|
---|
152 | pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
|
---|
153 | pszTmp[cchVar + 2] = '\0';
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (!putenv(pszTmp))
|
---|
157 | return 0;
|
---|
158 | return RTErrConvertFromErrno(errno);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
|
---|
163 | {
|
---|
164 | return RTEnvSetBad(pszVar, pszValue);
|
---|
165 | }
|
---|
166 |
|
---|
167 | RTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue)
|
---|
168 | {
|
---|
169 | size_t cwcVar;
|
---|
170 | int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar);
|
---|
171 | if (RT_SUCCESS(rc))
|
---|
172 | {
|
---|
173 | size_t cwcValue;
|
---|
174 | rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcValue);
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + cwcValue + 1) * sizeof(RTUTF16));
|
---|
178 | if (pwszTmp)
|
---|
179 | {
|
---|
180 | rc = RTStrToUtf16Ex(pszVar, RTSTR_MAX, &pwszTmp, cwcVar + 1, NULL);
|
---|
181 | if (RT_SUCCESS(rc))
|
---|
182 | {
|
---|
183 | PRTUTF16 pwszTmpValue = &pwszTmp[cwcVar];
|
---|
184 | *pwszTmpValue++ = '=';
|
---|
185 | rc = RTStrToUtf16Ex(pszValue, RTSTR_MAX, &pwszTmpValue, cwcValue + 1, NULL);
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | {
|
---|
188 | if (!_wputenv(pwszTmp))
|
---|
189 | rc = VINF_SUCCESS;
|
---|
190 | else
|
---|
191 | rc = RTErrConvertFromErrno(errno);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | RTMemTmpFree(pwszTmp);
|
---|
195 | }
|
---|
196 | else
|
---|
197 | rc = VERR_NO_TMP_MEMORY;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | RTDECL(int) RTEnvUnsetBad(const char *pszVar)
|
---|
205 | {
|
---|
206 | AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Check that it exists first.
|
---|
210 | */
|
---|
211 | if (!RTEnvExist(pszVar))
|
---|
212 | return VINF_ENV_VAR_NOT_FOUND;
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Ok, try remove it.
|
---|
216 | */
|
---|
217 | #ifdef RT_OS_WINDOWS
|
---|
218 | /* Use putenv(var=) since Windows does not have unsetenv(). */
|
---|
219 | size_t cchVar = strlen(pszVar);
|
---|
220 | char *pszBuf = (char *)alloca(cchVar + 2);
|
---|
221 | memcpy(pszBuf, pszVar, cchVar);
|
---|
222 | pszBuf[cchVar] = '=';
|
---|
223 | pszBuf[cchVar + 1] = '\0';
|
---|
224 |
|
---|
225 | if (!putenv(pszBuf))
|
---|
226 | return VINF_SUCCESS;
|
---|
227 |
|
---|
228 | #else
|
---|
229 | /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
|
---|
230 | if (!unsetenv((char*)pszVar))
|
---|
231 | return VINF_SUCCESS;
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | return RTErrConvertFromErrno(errno);
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | RTDECL(int) RTEnvUnset(const char *pszVar)
|
---|
239 | {
|
---|
240 | return RTEnvUnsetBad(pszVar);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | RTDECL(int) RTEnvUnsetUtf8(const char *pszVar)
|
---|
245 | {
|
---|
246 | size_t cwcVar;
|
---|
247 | int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar);
|
---|
248 | if (RT_SUCCESS(rc))
|
---|
249 | {
|
---|
250 | PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + 1) * sizeof(RTUTF16));
|
---|
251 | if (pwszTmp)
|
---|
252 | {
|
---|
253 | rc = RTStrToUtf16Ex(pszVar, RTSTR_MAX, &pwszTmp, cwcVar + 1, NULL);
|
---|
254 | if (RT_SUCCESS(rc))
|
---|
255 | {
|
---|
256 | pwszTmp[cwcVar] = '=';
|
---|
257 | pwszTmp[cwcVar + 1] = '\0';
|
---|
258 | if (!_wputenv(pwszTmp))
|
---|
259 | rc = VINF_SUCCESS;
|
---|
260 | else
|
---|
261 | rc = RTErrConvertFromErrno(errno);
|
---|
262 | }
|
---|
263 | RTMemTmpFree(pwszTmp);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | return rc;
|
---|
267 | }
|
---|
268 |
|
---|