[1] | 1 | /* $Id: env-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Environment, Posix.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[936] | 41 | #include <iprt/env.h>
|
---|
[50408] | 42 |
|
---|
[95792] | 43 | #ifdef IPRT_NO_CRT
|
---|
| 44 | # include <iprt/asm.h>
|
---|
| 45 | #endif
|
---|
[1494] | 46 | #include <iprt/alloca.h>
|
---|
[4475] | 47 | #include <iprt/assert.h>
|
---|
[76359] | 48 | #include <iprt/err.h>
|
---|
[50408] | 49 | #include <iprt/string.h>
|
---|
| 50 | #include <iprt/mem.h>
|
---|
[76409] | 51 | #include <iprt/utf16.h>
|
---|
[1] | 52 |
|
---|
[95792] | 53 | #ifndef IPRT_NO_CRT
|
---|
| 54 | # include <stdlib.h>
|
---|
| 55 | # include <errno.h>
|
---|
| 56 | #endif
|
---|
| 57 | #include <iprt/win/windows.h>
|
---|
[1] | 58 |
|
---|
[936] | 59 |
|
---|
[95792] | 60 | /*********************************************************************************************************************************
|
---|
| 61 | * Global Variables *
|
---|
| 62 | *********************************************************************************************************************************/
|
---|
| 63 | #ifdef IPRT_NO_CRT
|
---|
| 64 | static uint32_t volatile g_idxGetEnvBufs = 0;
|
---|
| 65 | static char *g_apszGetEnvBufs[64]; /* leak */
|
---|
| 66 | #endif
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | /*********************************************************************************************************************************
|
---|
| 70 | * Internal Functions *
|
---|
| 71 | *********************************************************************************************************************************/
|
---|
| 72 | int rtEnvSetUtf8Worker(const char *pchVar, size_t cchVar, const char *pszValue);
|
---|
| 73 |
|
---|
| 74 |
|
---|
[96587] | 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 | */
|
---|
| 79 | static DWORD rtEnvNt31CheckEmpty(PCRTUTF16 pwszVar)
|
---|
| 80 | {
|
---|
| 81 | /* Check the version first: */
|
---|
| 82 | DWORD dwVersion = GetVersion();
|
---|
| 83 | if (RT_BYTE1(dwVersion) != 3)
|
---|
| 84 | return 0;
|
---|
[95792] | 85 |
|
---|
[96587] | 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 |
|
---|
[50408] | 94 | RTDECL(bool) RTEnvExistsBad(const char *pszVar)
|
---|
| 95 | {
|
---|
[95792] | 96 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 97 | return RTEnvGetBad(pszVar) != NULL;
|
---|
[95792] | 98 | #else
|
---|
| 99 | return RTEnvExistsUtf8(pszVar);
|
---|
| 100 | #endif
|
---|
[50408] | 101 | }
|
---|
[20821] | 102 |
|
---|
[50408] | 103 |
|
---|
[1494] | 104 | RTDECL(bool) RTEnvExist(const char *pszVar)
|
---|
| 105 | {
|
---|
[95792] | 106 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 107 | return RTEnvExistsBad(pszVar);
|
---|
[95792] | 108 | #else
|
---|
| 109 | return RTEnvExistsUtf8(pszVar);
|
---|
| 110 | #endif
|
---|
[1494] | 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
[50408] | 114 | RTDECL(bool) RTEnvExistsUtf8(const char *pszVar)
|
---|
| 115 | {
|
---|
[55562] | 116 | AssertReturn(strchr(pszVar, '=') == NULL, false);
|
---|
| 117 |
|
---|
[50408] | 118 | PRTUTF16 pwszVar;
|
---|
| 119 | int rc = RTStrToUtf16(pszVar, &pwszVar);
|
---|
| 120 | AssertRCReturn(rc, false);
|
---|
[95792] | 121 |
|
---|
| 122 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 123 | bool fRet = _wgetenv(pwszVar) != NULL;
|
---|
[95792] | 124 | #else
|
---|
| 125 | DWORD dwRet = GetEnvironmentVariableW(pwszVar, NULL, 0);
|
---|
| 126 | bool fRet = dwRet != 0;
|
---|
| 127 | #endif
|
---|
| 128 |
|
---|
[50408] | 129 | RTUtf16Free(pwszVar);
|
---|
| 130 | return fRet;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | RTDECL(const char *) RTEnvGetBad(const char *pszVar)
|
---|
| 135 | {
|
---|
[55562] | 136 | AssertReturn(strchr(pszVar, '=') == NULL, NULL);
|
---|
[95792] | 137 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 138 | return getenv(pszVar);
|
---|
[95792] | 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
|
---|
[50408] | 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
[936] | 158 | RTDECL(const char *) RTEnvGet(const char *pszVar)
|
---|
[1] | 159 | {
|
---|
[50408] | 160 | return RTEnvGetBad(pszVar);
|
---|
[1] | 161 | }
|
---|
| 162 |
|
---|
[50408] | 163 | RTDECL(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);
|
---|
[55562] | 170 | AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
[936] | 171 |
|
---|
[50408] | 172 | if (pcchActual)
|
---|
| 173 | *pcchActual = 0;
|
---|
| 174 |
|
---|
[95792] | 175 | /*
|
---|
| 176 | * Convert the name to UTF-16.
|
---|
| 177 | */
|
---|
[50408] | 178 | PRTUTF16 pwszVar;
|
---|
| 179 | int rc = RTStrToUtf16(pszVar, &pwszVar);
|
---|
[55562] | 180 | AssertRCReturn(rc, rc);
|
---|
[50408] | 181 |
|
---|
[95792] | 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++)
|
---|
[50408] | 192 | {
|
---|
[96587] | 193 | /* This API is weird, it didn't always set ERROR_BUFFER_OVERFLOW nor ERROR_ENVVAR_NOT_FOUND.
|
---|
[95792] | 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);
|
---|
[96587] | 198 | DWORD dwErr = GetLastError();
|
---|
[95792] | 199 |
|
---|
| 200 | if (cwcValueRet < cwcValueBuf)
|
---|
| 201 | {
|
---|
[96587] | 202 | #ifdef RT_ARCH_X86
|
---|
| 203 | if (cwcValueRet == 0 && dwErr == NO_ERROR)
|
---|
| 204 | dwErr = rtEnvNt31CheckEmpty(pwszVar);
|
---|
| 205 | #endif
|
---|
[95792] | 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);
|
---|
[50408] | 234 | }
|
---|
[95792] | 235 |
|
---|
| 236 | RTMemTmpFree(pwszValueFree);
|
---|
| 237 | RTUtf16Free(pwszVar);
|
---|
[50408] | 238 | return rc;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|
[95792] | 242 | RTDECL(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 | {
|
---|
[96587] | 265 | /* This API is weird, it didn't always set ERROR_BUFFER_OVERFLOW nor ERROR_ENVVAR_NOT_FOUND.
|
---|
[95792] | 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);
|
---|
[96587] | 270 | DWORD dwErr = GetLastError();
|
---|
[95792] | 271 |
|
---|
| 272 | if (cwcValueRet < cwcValueBuf)
|
---|
| 273 | {
|
---|
[96587] | 274 | #ifdef RT_ARCH_X86
|
---|
| 275 | if (cwcValueRet == 0 && dwErr == NO_ERROR)
|
---|
| 276 | dwErr = rtEnvNt31CheckEmpty(pwszVar);
|
---|
| 277 | #endif
|
---|
[95792] | 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 |
|
---|
[50408] | 312 | RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
|
---|
[936] | 313 | {
|
---|
[95792] | 314 | #ifndef IPRT_NO_CRT
|
---|
[1494] | 315 | /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
|
---|
[936] | 316 | if (!putenv((char *)pszVarEqualValue))
|
---|
| 317 | return 0;
|
---|
| 318 | return RTErrConvertFromErrno(errno);
|
---|
[95792] | 319 | #else
|
---|
| 320 | return RTEnvPutUtf8(pszVarEqualValue);
|
---|
| 321 | #endif
|
---|
[936] | 322 | }
|
---|
| 323 |
|
---|
[50408] | 324 |
|
---|
| 325 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
|
---|
[1494] | 326 | {
|
---|
[95792] | 327 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 328 | return RTEnvPutBad(pszVarEqualValue);
|
---|
[95792] | 329 | #else
|
---|
| 330 | return RTEnvPutUtf8(pszVarEqualValue);
|
---|
| 331 | #endif
|
---|
[50408] | 332 | }
|
---|
| 333 |
|
---|
| 334 |
|
---|
| 335 | RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue)
|
---|
| 336 | {
|
---|
| 337 | PRTUTF16 pwszVarEqualValue;
|
---|
| 338 | int rc = RTStrToUtf16(pszVarEqualValue, &pwszVarEqualValue);
|
---|
| 339 | if (RT_SUCCESS(rc))
|
---|
| 340 | {
|
---|
[95792] | 341 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 342 | if (!_wputenv(pwszVarEqualValue))
|
---|
| 343 | rc = VINF_SUCCESS;
|
---|
| 344 | else
|
---|
| 345 | rc = RTErrConvertFromErrno(errno);
|
---|
[95792] | 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
|
---|
[50408] | 373 | RTUtf16Free(pwszVarEqualValue);
|
---|
| 374 | }
|
---|
| 375 | return rc;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
|
---|
| 381 | {
|
---|
[95792] | 382 | #ifndef IPRT_NO_CRT
|
---|
[55562] | 383 | AssertMsgReturn(strchr(pszVar, '=') == NULL, ("'%s'\n", pszVar), VERR_ENV_INVALID_VAR_NAME);
|
---|
[95792] | 384 | int rc;
|
---|
| 385 | if (!RTEnvExist(pszVar))
|
---|
| 386 | rc = VINF_ENV_VAR_NOT_FOUND;
|
---|
[1494] | 387 | else
|
---|
| 388 | {
|
---|
[95792] | 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);
|
---|
[1494] | 394 | }
|
---|
[95792] | 395 | return rc;
|
---|
| 396 | #else
|
---|
| 397 | return RTEnvSetUtf8(pszVar, pszValue);
|
---|
| 398 | #endif
|
---|
[50408] | 399 | }
|
---|
[20821] | 400 |
|
---|
[50408] | 401 |
|
---|
| 402 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
|
---|
| 403 | {
|
---|
[95792] | 404 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 405 | return RTEnvSetBad(pszVar, pszValue);
|
---|
[95792] | 406 | #else
|
---|
| 407 | return RTEnvSetUtf8(pszVar, pszValue);
|
---|
| 408 | #endif
|
---|
[1494] | 409 | }
|
---|
| 410 |
|
---|
[80764] | 411 |
|
---|
| 412 | /**
|
---|
| 413 | * Worker common to RTEnvSetUtf8() and rtEnvSetExWorker().
|
---|
| 414 | */
|
---|
| 415 | int rtEnvSetUtf8Worker(const char *pchVar, size_t cchVar, const char *pszValue)
|
---|
[50408] | 416 | {
|
---|
[95792] | 417 | PRTUTF16 pwszVar;
|
---|
| 418 | int rc = RTStrToUtf16Ex(pchVar, cchVar, &pwszVar, 0, NULL);
|
---|
[50408] | 419 | if (RT_SUCCESS(rc))
|
---|
| 420 | {
|
---|
[95792] | 421 | PRTUTF16 pwszValue;
|
---|
| 422 | rc = RTStrToUtf16(pszValue, &pwszValue);
|
---|
[50408] | 423 | if (RT_SUCCESS(rc))
|
---|
| 424 | {
|
---|
[95792] | 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;
|
---|
[50408] | 430 | else
|
---|
[95792] | 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);
|
---|
[50408] | 440 | }
|
---|
[95792] | 441 | RTUtf16Free(pwszVar);
|
---|
[50408] | 442 | }
|
---|
| 443 | return rc;
|
---|
| 444 | }
|
---|
[4475] | 445 |
|
---|
[50408] | 446 |
|
---|
[80764] | 447 | RTDECL(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 |
|
---|
[50408] | 455 | RTDECL(int) RTEnvUnsetBad(const char *pszVar)
|
---|
[4475] | 456 | {
|
---|
[95792] | 457 | #ifndef IPRT_NO_CRT
|
---|
[55562] | 458 | AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
[95792] | 459 | int rc;
|
---|
[4475] | 460 | if (!RTEnvExist(pszVar))
|
---|
[95792] | 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;
|
---|
[16502] | 471 | #else
|
---|
[95792] | 472 | return RTEnvUnsetUtf8(pszVar);
|
---|
[16502] | 473 | #endif
|
---|
[4475] | 474 | }
|
---|
| 475 |
|
---|
[50408] | 476 |
|
---|
| 477 | RTDECL(int) RTEnvUnset(const char *pszVar)
|
---|
| 478 | {
|
---|
[95792] | 479 | #ifndef IPRT_NO_CRT
|
---|
[50408] | 480 | return RTEnvUnsetBad(pszVar);
|
---|
[95792] | 481 | #else
|
---|
| 482 | return RTEnvUnsetUtf8(pszVar);
|
---|
| 483 | #endif
|
---|
[50408] | 484 | }
|
---|
| 485 |
|
---|
| 486 |
|
---|
| 487 | RTDECL(int) RTEnvUnsetUtf8(const char *pszVar)
|
---|
| 488 | {
|
---|
[55562] | 489 | AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
| 490 |
|
---|
[95792] | 491 | PRTUTF16 pwszVar;
|
---|
| 492 | int rc = RTStrToUtf16(pszVar, &pwszVar);
|
---|
[50408] | 493 | if (RT_SUCCESS(rc))
|
---|
| 494 | {
|
---|
[95792] | 495 | #ifndef IPRT_NO_CRT
|
---|
| 496 | if (_wgetenv(pwszVar))
|
---|
[50408] | 497 | {
|
---|
[95792] | 498 | errno_t rcErrno = _wputenv_s(pwszVar, NULL);
|
---|
| 499 | if (rcErrno == 0)
|
---|
| 500 | rc = VINF_SUCCESS;
|
---|
| 501 | else
|
---|
| 502 | rc = RTErrConvertFromErrno(rcErrno);
|
---|
[50408] | 503 | }
|
---|
[95792] | 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);
|
---|
[50408] | 517 | }
|
---|
| 518 | return rc;
|
---|
| 519 | }
|
---|
| 520 |
|
---|