VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/env-win.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 22 months ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 14.9 KB
Line 
1/* $Id: env-win.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#include <iprt/env.h>
42
43#ifdef IPRT_NO_CRT
44# include <iprt/asm.h>
45#endif
46#include <iprt/alloca.h>
47#include <iprt/assert.h>
48#include <iprt/err.h>
49#include <iprt/string.h>
50#include <iprt/mem.h>
51#include <iprt/utf16.h>
52
53#ifndef IPRT_NO_CRT
54# include <stdlib.h>
55# include <errno.h>
56#endif
57#include <iprt/win/windows.h>
58
59
60/*********************************************************************************************************************************
61* Global Variables *
62*********************************************************************************************************************************/
63#ifdef IPRT_NO_CRT
64static uint32_t volatile g_idxGetEnvBufs = 0;
65static char *g_apszGetEnvBufs[64]; /* leak */
66#endif
67
68
69/*********************************************************************************************************************************
70* Internal Functions *
71*********************************************************************************************************************************/
72int rtEnvSetUtf8Worker(const char *pchVar, size_t cchVar, const char *pszValue);
73
74
75
76RTDECL(bool) RTEnvExistsBad(const char *pszVar)
77{
78#ifndef IPRT_NO_CRT
79 return RTEnvGetBad(pszVar) != NULL;
80#else
81 return RTEnvExistsUtf8(pszVar);
82#endif
83}
84
85
86RTDECL(bool) RTEnvExist(const char *pszVar)
87{
88#ifndef IPRT_NO_CRT
89 return RTEnvExistsBad(pszVar);
90#else
91 return RTEnvExistsUtf8(pszVar);
92#endif
93}
94
95
96RTDECL(bool) RTEnvExistsUtf8(const char *pszVar)
97{
98 AssertReturn(strchr(pszVar, '=') == NULL, false);
99
100 PRTUTF16 pwszVar;
101 int rc = RTStrToUtf16(pszVar, &pwszVar);
102 AssertRCReturn(rc, false);
103
104#ifndef IPRT_NO_CRT
105 bool fRet = _wgetenv(pwszVar) != NULL;
106#else
107 DWORD dwRet = GetEnvironmentVariableW(pwszVar, NULL, 0);
108 bool fRet = dwRet != 0;
109#endif
110
111 RTUtf16Free(pwszVar);
112 return fRet;
113}
114
115
116RTDECL(const char *) RTEnvGetBad(const char *pszVar)
117{
118 AssertReturn(strchr(pszVar, '=') == NULL, NULL);
119#ifndef IPRT_NO_CRT
120 return getenv(pszVar);
121#else
122 /*
123 * Query the value into heap buffer which we give a lifetime of 64
124 * RTEnvGetBad calls.
125 */
126 char *pszValue = RTEnvDup(pszVar);
127 if (pszValue)
128 {
129 RTMEM_MAY_LEAK(pszValue); /* Quite possible we'll leak this, but the leak is limited to 64 values. */
130
131 uint32_t idx = ASMAtomicIncU32(&g_idxGetEnvBufs) % RT_ELEMENTS(g_apszGetEnvBufs);
132 char *pszOld = (char *)ASMAtomicXchgPtr((void * volatile *)&g_apszGetEnvBufs[idx], pszValue);
133 RTStrFree(pszOld);
134 }
135 return pszValue;
136#endif
137}
138
139
140RTDECL(const char *) RTEnvGet(const char *pszVar)
141{
142 return RTEnvGetBad(pszVar);
143}
144
145RTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
146{
147 AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
148 AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER);
149 AssertReturn(pszValue || !cbValue, VERR_INVALID_PARAMETER);
150 AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER);
151 AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER);
152 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
153
154 if (pcchActual)
155 *pcchActual = 0;
156
157 /*
158 * Convert the name to UTF-16.
159 */
160 PRTUTF16 pwszVar;
161 int rc = RTStrToUtf16(pszVar, &pwszVar);
162 AssertRCReturn(rc, rc);
163
164 /*
165 * Query the variable. First try with a medium sized stack buffer (too
166 * small for your typical PATH, but large enough for most other things).
167 */
168 RTUTF16 wszValue[512];
169 uint32_t cwcValueBuf = RT_ELEMENTS(wszValue);
170 PRTUTF16 pwszValue = wszValue;
171 PRTUTF16 pwszValueFree = NULL;
172
173 for (unsigned iTry = 0;; iTry++)
174 {
175 /* This API is weird, it didn't always set ERROR_BUFFER_OVERFLOW.
176 Note! Assume that the CRT transparently updates the process
177 environment and that we don't need to use _wgetenv_s here. */
178 SetLastError(NO_ERROR);
179 DWORD const cwcValueRet = GetEnvironmentVariableW(pwszVar, pwszValue, cwcValueBuf);
180 DWORD const dwErr = GetLastError();
181
182 if (cwcValueRet < cwcValueBuf)
183 {
184 if (cwcValueRet > 0 || dwErr == NO_ERROR) /* In case of empty values we have to see if last error was set or not. */
185 {
186 if (cbValue)
187 rc = RTUtf16ToUtf8Ex(pwszValue, cwcValueRet, &pszValue, cbValue, pcchActual);
188 else
189 rc = RTUtf16CalcUtf8LenEx(pwszValue, cwcValueRet, pcchActual);
190 }
191 else
192 {
193 Assert(cwcValueRet == 0);
194 Assert(dwErr != NO_ERROR);
195 rc = RTErrConvertFromWin32(dwErr);
196 }
197 break;
198 }
199
200 /*
201 * Insufficient buffer, so increase it. The first re-try will use the
202 * returned size, further re-tries will max out with a multiple of the
203 * stack buffer till we reaches 32KB chars (128 loops).
204 */
205 Assert(dwErr == NO_ERROR || dwErr == ERROR_BUFFER_OVERFLOW);
206 RTMemTmpFree(pwszValueFree);
207 AssertBreakStmt(cwcValueBuf < _32K, rc = VERR_INTERNAL_ERROR_3 /* not a good one */);
208
209 cwcValueBuf = RT_MAX(cwcValueRet + iTry, RT_ELEMENTS(wszValue) * iTry);
210 pwszValueFree = pwszValue = (PRTUTF16)RTMemTmpAlloc(cwcValueBuf * sizeof(RTUTF16));
211 AssertBreakStmt(pwszValue, rc = VERR_NO_TMP_MEMORY);
212 }
213
214 RTMemTmpFree(pwszValueFree);
215 RTUtf16Free(pwszVar);
216 return rc;
217}
218
219
220RTDECL(char *) RTEnvDup(const char *pszVar)
221{
222 AssertPtrReturn(pszVar, NULL);
223
224 /*
225 * Convert the name to UTF-16.
226 */
227 PRTUTF16 pwszVar;
228 int rc = RTStrToUtf16(pszVar, &pwszVar);
229 AssertRCReturn(rc, NULL);
230
231 /*
232 * Query the variable. First try with a medium sized stack buffer (too
233 * small for your typical PATH, but large enough for most other things).
234 */
235 char *pszRet = NULL;
236 RTUTF16 wszValue[512];
237 uint32_t cwcValueBuf = RT_ELEMENTS(wszValue);
238 PRTUTF16 pwszValue = wszValue;
239 PRTUTF16 pwszValueFree = NULL;
240
241 for (unsigned iTry = 0;; iTry++)
242 {
243 /* This API is weird, it didn't always set ERROR_BUFFER_OVERFLOW.
244 Note! Assume that the CRT transparently updates the process
245 environment and that we don't need to use _wgetenv_s here. */
246 SetLastError(NO_ERROR);
247 DWORD const cwcValueRet = GetEnvironmentVariableW(pwszVar, pwszValue, cwcValueBuf);
248 DWORD const dwErr = GetLastError();
249
250 if (cwcValueRet < cwcValueBuf)
251 {
252 if (cwcValueRet > 0 || dwErr == NO_ERROR) /* In case of empty values we have to see if last error was set or not. */
253 {
254 rc = RTUtf16ToUtf8Ex(pwszValue, cwcValueRet, &pszRet, 0, NULL);
255 if (RT_FAILURE(rc))
256 pszRet = NULL;
257 }
258 else
259 {
260 Assert(cwcValueRet == 0);
261 Assert(dwErr != NO_ERROR);
262 }
263 break;
264 }
265
266 /*
267 * Insufficient buffer, so increase it. The first re-try will use the
268 * returned size, further re-tries will max out with a multiple of the
269 * stack buffer till we reaches 32KB chars (128 loops).
270 */
271 Assert(dwErr == NO_ERROR || dwErr == ERROR_BUFFER_OVERFLOW);
272 RTMemTmpFree(pwszValueFree);
273 AssertBreakStmt(cwcValueBuf < _32K, rc = VERR_INTERNAL_ERROR_3 /* not a good one */);
274
275 cwcValueBuf = RT_MAX(cwcValueRet + iTry, RT_ELEMENTS(wszValue) * iTry);
276 pwszValueFree = pwszValue = (PRTUTF16)RTMemTmpAlloc(cwcValueBuf * sizeof(RTUTF16));
277 AssertBreakStmt(pwszValue, rc = VERR_NO_TMP_MEMORY);
278 }
279
280 RTMemTmpFree(pwszValueFree);
281 RTUtf16Free(pwszVar);
282 return pszRet;
283}
284
285
286RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
287{
288#ifndef IPRT_NO_CRT
289 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
290 if (!putenv((char *)pszVarEqualValue))
291 return 0;
292 return RTErrConvertFromErrno(errno);
293#else
294 return RTEnvPutUtf8(pszVarEqualValue);
295#endif
296}
297
298
299RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
300{
301#ifndef IPRT_NO_CRT
302 return RTEnvPutBad(pszVarEqualValue);
303#else
304 return RTEnvPutUtf8(pszVarEqualValue);
305#endif
306}
307
308
309RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue)
310{
311 PRTUTF16 pwszVarEqualValue;
312 int rc = RTStrToUtf16(pszVarEqualValue, &pwszVarEqualValue);
313 if (RT_SUCCESS(rc))
314 {
315#ifndef IPRT_NO_CRT
316 if (!_wputenv(pwszVarEqualValue))
317 rc = VINF_SUCCESS;
318 else
319 rc = RTErrConvertFromErrno(errno);
320#else
321 PRTUTF16 pwszValue = RTUtf16Chr(pwszVarEqualValue, '=');
322 if (pwszValue)
323 {
324 *pwszValue++ = '\0';
325
326 SetLastError(*pwszValue ? ERROR_OUTOFMEMORY : ERROR_ENVVAR_NOT_FOUND); /* The API did not always set the last error. */
327 if (SetEnvironmentVariableW(pwszVarEqualValue, *pwszValue ? pwszValue : NULL))
328 rc = VINF_SUCCESS;
329 else
330 {
331 DWORD dwErr = GetLastError();
332 if (dwErr == ERROR_ENVVAR_NOT_FOUND)
333 {
334 Assert(!*pwszValue);
335 rc = VINF_SUCCESS;
336 }
337 else
338 {
339 Assert(*pwszValue);
340 rc = RTErrConvertFromWin32(GetLastError());
341 }
342 }
343 }
344 else
345 rc = VERR_INVALID_PARAMETER;
346#endif
347 RTUtf16Free(pwszVarEqualValue);
348 }
349 return rc;
350}
351
352
353
354RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
355{
356#ifndef IPRT_NO_CRT
357 AssertMsgReturn(strchr(pszVar, '=') == NULL, ("'%s'\n", pszVar), VERR_ENV_INVALID_VAR_NAME);
358 int rc;
359 if (!RTEnvExist(pszVar))
360 rc = VINF_ENV_VAR_NOT_FOUND;
361 else
362 {
363 errno_t rcErrno = _putenv_s(pszVar, *pszValue ? pszValue : " " /* wrong, but will be treated as unset otherwise */);
364 if (rcErrno == 0)
365 rc = VINF_SUCCESS;
366 else
367 rc = RTErrConvertFromErrno(rcErrno);
368 }
369 return rc;
370#else
371 return RTEnvSetUtf8(pszVar, pszValue);
372#endif
373}
374
375
376RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
377{
378#ifndef IPRT_NO_CRT
379 return RTEnvSetBad(pszVar, pszValue);
380#else
381 return RTEnvSetUtf8(pszVar, pszValue);
382#endif
383}
384
385
386/**
387 * Worker common to RTEnvSetUtf8() and rtEnvSetExWorker().
388 */
389int rtEnvSetUtf8Worker(const char *pchVar, size_t cchVar, const char *pszValue)
390{
391 PRTUTF16 pwszVar;
392 int rc = RTStrToUtf16Ex(pchVar, cchVar, &pwszVar, 0, NULL);
393 if (RT_SUCCESS(rc))
394 {
395 PRTUTF16 pwszValue;
396 rc = RTStrToUtf16(pszValue, &pwszValue);
397 if (RT_SUCCESS(rc))
398 {
399#ifndef IPRT_NO_CRT
400 errno_t rcErrno = _wputenv_s(pwszVar,
401 *pwszValue ? pwszValue : L" " /* wrong, but will be treated as unset otherwise */);
402 if (rcErrno == 0)
403 rc = VINF_SUCCESS;
404 else
405 rc = RTErrConvertFromErrno(rcErrno);
406#else
407 SetLastError(ERROR_OUTOFMEMORY); /* The API did not always set the last error. */
408 if (SetEnvironmentVariableW(pwszVar, pwszValue))
409 rc = VINF_SUCCESS;
410 else
411 rc = RTErrConvertFromWin32(GetLastError());
412#endif
413 RTUtf16Free(pwszValue);
414 }
415 RTUtf16Free(pwszVar);
416 }
417 return rc;
418}
419
420
421RTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue)
422{
423 size_t cchVar = strlen(pszVar);
424 AssertReturn(memchr(pszVar, '=', cchVar) == NULL, VERR_ENV_INVALID_VAR_NAME);
425 return rtEnvSetUtf8Worker(pszVar, cchVar, pszValue);
426}
427
428
429RTDECL(int) RTEnvUnsetBad(const char *pszVar)
430{
431#ifndef IPRT_NO_CRT
432 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
433 int rc;
434 if (!RTEnvExist(pszVar))
435 rc = VINF_ENV_VAR_NOT_FOUND;
436 else
437 {
438 errno_t rcErrno = _putenv_s(pszVar, NULL);
439 if (rcErrno == 0)
440 rc = VINF_SUCCESS;
441 else
442 rc = RTErrConvertFromErrno(rcErrno);
443 }
444 return rc;
445#else
446 return RTEnvUnsetUtf8(pszVar);
447#endif
448}
449
450
451RTDECL(int) RTEnvUnset(const char *pszVar)
452{
453#ifndef IPRT_NO_CRT
454 return RTEnvUnsetBad(pszVar);
455#else
456 return RTEnvUnsetUtf8(pszVar);
457#endif
458}
459
460
461RTDECL(int) RTEnvUnsetUtf8(const char *pszVar)
462{
463 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
464
465 PRTUTF16 pwszVar;
466 int rc = RTStrToUtf16(pszVar, &pwszVar);
467 if (RT_SUCCESS(rc))
468 {
469#ifndef IPRT_NO_CRT
470 if (_wgetenv(pwszVar))
471 {
472 errno_t rcErrno = _wputenv_s(pwszVar, NULL);
473 if (rcErrno == 0)
474 rc = VINF_SUCCESS;
475 else
476 rc = RTErrConvertFromErrno(rcErrno);
477 }
478 else
479 rc = VINF_ENV_VAR_NOT_FOUND;
480#else
481 SetLastError(ERROR_ENVVAR_NOT_FOUND); /* The API did not always set the last error. */
482 if (SetEnvironmentVariableW(pwszVar, NULL))
483 rc = VINF_SUCCESS;
484 else
485 {
486 DWORD dwErr = GetLastError();
487 rc = dwErr == ERROR_ENVVAR_NOT_FOUND ? VINF_ENV_VAR_NOT_FOUND : RTErrConvertFromWin32(dwErr);
488 }
489#endif
490 RTUtf16Free(pwszVar);
491 }
492 return rc;
493}
494
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use