VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use