[4063] | 1 | /* $Id: env-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Environment, Generic.
|
---|
[4063] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[4063] | 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
|
---|
[4063] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[4063] | 41 | #include <iprt/env.h>
|
---|
[21337] | 42 | #include "internal/iprt.h"
|
---|
| 43 |
|
---|
[4063] | 44 | #include <iprt/assert.h>
|
---|
| 45 | #include <iprt/alloc.h>
|
---|
[4475] | 46 | #include <iprt/alloca.h>
|
---|
[76408] | 47 | #include <iprt/err.h>
|
---|
| 48 | #include <iprt/sort.h>
|
---|
[4063] | 49 | #include <iprt/string.h>
|
---|
[76408] | 50 | #include <iprt/utf16.h>
|
---|
[4475] | 51 | #include "internal/magics.h"
|
---|
[4063] | 52 |
|
---|
[95842] | 53 | #ifdef RT_OS_WINDOWS
|
---|
| 54 | # include <iprt/nt/nt.h>
|
---|
| 55 | #else
|
---|
| 56 | # include <stdlib.h>
|
---|
| 57 | # if !defined(RT_OS_WINDOWS)
|
---|
| 58 | # include <unistd.h>
|
---|
| 59 | # endif
|
---|
| 60 | # ifdef RT_OS_DARWIN
|
---|
| 61 | # include <crt_externs.h>
|
---|
| 62 | # endif
|
---|
| 63 | # if defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD)
|
---|
[20374] | 64 | RT_C_DECLS_BEGIN
|
---|
[4474] | 65 | extern char **environ;
|
---|
[20374] | 66 | RT_C_DECLS_END
|
---|
[95842] | 67 | # endif
|
---|
[4287] | 68 | #endif
|
---|
| 69 |
|
---|
[21337] | 70 |
|
---|
[57358] | 71 | /*********************************************************************************************************************************
|
---|
| 72 | * Defined Constants And Macros *
|
---|
| 73 | *********************************************************************************************************************************/
|
---|
[50408] | 74 | /** The allocation granularity of the RTENVINTERNAL::papszEnv memory. */
|
---|
| 75 | #define RTENV_GROW_SIZE 16
|
---|
| 76 |
|
---|
[4475] | 77 | /** Macro that unlocks the specified environment block. */
|
---|
| 78 | #define RTENV_LOCK(pEnvInt) do { } while (0)
|
---|
| 79 | /** Macro that unlocks the specified environment block. */
|
---|
| 80 | #define RTENV_UNLOCK(pEnvInt) do { } while (0)
|
---|
| 81 |
|
---|
[50408] | 82 | /** @def RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
| 83 | * Indicates the RTEnv*Utf8 APIs are implemented. */
|
---|
| 84 | #if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
|
---|
| 85 | # define RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API 1
|
---|
| 86 | #endif
|
---|
| 87 |
|
---|
[80827] | 88 | /** @def RTENV_ALLOW_EQUAL_FIRST_IN_VAR
|
---|
[80878] | 89 | * Allows a variable to start with an '=' sign by default. This is used by
|
---|
| 90 | * windows to maintain CWDs of non-current drives.
|
---|
[80827] | 91 | * @note Not supported by _wputenv AFAIK. */
|
---|
| 92 | #if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
|
---|
| 93 | # define RTENV_ALLOW_EQUAL_FIRST_IN_VAR 1
|
---|
| 94 | #endif
|
---|
| 95 |
|
---|
| 96 |
|
---|
[57358] | 97 | /*********************************************************************************************************************************
|
---|
| 98 | * Structures and Typedefs *
|
---|
| 99 | *********************************************************************************************************************************/
|
---|
[4475] | 100 | /**
|
---|
| 101 | * The internal representation of a (non-default) environment.
|
---|
| 102 | */
|
---|
| 103 | typedef struct RTENVINTERNAL
|
---|
[4063] | 104 | {
|
---|
[4475] | 105 | /** Magic value . */
|
---|
| 106 | uint32_t u32Magic;
|
---|
[55562] | 107 | /** Set if this is a record of environment changes, putenv style. */
|
---|
| 108 | bool fPutEnvBlock;
|
---|
[80827] | 109 | /** Set if starting a variable with an equal sign is okay, clear if not okay
|
---|
| 110 | * (RTENV_CREATE_F_ALLOW_EQUAL_FIRST_IN_VAR). */
|
---|
| 111 | bool fFirstEqual;
|
---|
[20364] | 112 | /** Number of variables in the array.
|
---|
[4475] | 113 | * This does not include the terminating NULL entry. */
|
---|
| 114 | size_t cVars;
|
---|
[20364] | 115 | /** Capacity (allocated size) of the array.
|
---|
| 116 | * This includes space for the terminating NULL element (for compatibility
|
---|
[4475] | 117 | * with the C library), so that c <= cCapacity - 1. */
|
---|
| 118 | size_t cAllocated;
|
---|
[55562] | 119 | /** Array of environment variables.
|
---|
| 120 | * These are always in "NAME=VALUE" form, where the value can be empty. If
|
---|
| 121 | * fPutEnvBlock is set though, there will be "NAME" entries too for variables
|
---|
| 122 | * that need to be removed when merged with another environment block. */
|
---|
[4475] | 123 | char **papszEnv;
|
---|
[20364] | 124 | /** Array of environment variables in the process CP.
|
---|
[4475] | 125 | * This get (re-)constructed when RTEnvGetExecEnvP method is called. */
|
---|
| 126 | char **papszEnvOtherCP;
|
---|
[50408] | 127 |
|
---|
| 128 | /** The compare function we're using. */
|
---|
[85121] | 129 | DECLCALLBACKMEMBER(int, pfnCompare,(const char *psz1, const char *psz2, size_t cchMax));
|
---|
[4475] | 130 | } RTENVINTERNAL, *PRTENVINTERNAL;
|
---|
[4063] | 131 |
|
---|
| 132 |
|
---|
[95842] | 133 | #ifndef RT_OS_WINDOWS
|
---|
[4475] | 134 | /**
|
---|
[20364] | 135 | * Internal worker that resolves the pointer to the default
|
---|
[4475] | 136 | * process environment. (environ)
|
---|
[20364] | 137 | *
|
---|
[4475] | 138 | * @returns Pointer to the default environment.
|
---|
| 139 | * This may be NULL.
|
---|
| 140 | */
|
---|
| 141 | static const char * const *rtEnvDefault(void)
|
---|
[4063] | 142 | {
|
---|
[95842] | 143 | # ifdef RT_OS_DARWIN
|
---|
[4475] | 144 | return *(_NSGetEnviron());
|
---|
[95842] | 145 | # else
|
---|
[4475] | 146 | return environ;
|
---|
[95842] | 147 | # endif
|
---|
| 148 | }
|
---|
[20364] | 149 | #endif
|
---|
[4063] | 150 |
|
---|
[4475] | 151 |
|
---|
| 152 | /**
|
---|
| 153 | * Internal worker that creates an environment handle with a specified capacity.
|
---|
[20364] | 154 | *
|
---|
[4475] | 155 | * @returns IPRT status code.
|
---|
[50408] | 156 | * @param ppIntEnv Where to store the result.
|
---|
| 157 | * @param cAllocated The initial array size.
|
---|
| 158 | * @param fCaseSensitive Whether the environment block is case sensitive or
|
---|
| 159 | * not.
|
---|
[55562] | 160 | * @param fPutEnvBlock Indicates whether this is a special environment
|
---|
| 161 | * block that will be used to record change another
|
---|
| 162 | * block. We will keep unsets in putenv format, i.e.
|
---|
| 163 | * just the variable name without any equal sign.
|
---|
[80827] | 164 | * @param fFirstEqual The RTENV_CREATE_F_ALLOW_EQUAL_FIRST_IN_VAR value.
|
---|
| 165 | */
|
---|
| 166 | static int rtEnvCreate(PRTENVINTERNAL *ppIntEnv, size_t cAllocated, bool fCaseSensitive, bool fPutEnvBlock, bool fFirstEqual)
|
---|
[4475] | 167 | {
|
---|
[4063] | 168 | /*
|
---|
| 169 | * Allocate environment handle.
|
---|
| 170 | */
|
---|
[4475] | 171 | PRTENVINTERNAL pIntEnv = (PRTENVINTERNAL)RTMemAlloc(sizeof(*pIntEnv));
|
---|
[4063] | 172 | if (pIntEnv)
|
---|
| 173 | {
|
---|
| 174 | /*
|
---|
| 175 | * Pre-allocate the variable array.
|
---|
| 176 | */
|
---|
[4475] | 177 | pIntEnv->u32Magic = RTENV_MAGIC;
|
---|
[55562] | 178 | pIntEnv->fPutEnvBlock = fPutEnvBlock;
|
---|
[80827] | 179 | pIntEnv->fFirstEqual = fFirstEqual;
|
---|
[50408] | 180 | pIntEnv->pfnCompare = fCaseSensitive ? RTStrNCmp : RTStrNICmp;
|
---|
[4475] | 181 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
| 182 | pIntEnv->cVars = 0;
|
---|
| 183 | pIntEnv->cAllocated = RT_ALIGN_Z(RT_MAX(cAllocated, RTENV_GROW_SIZE), RTENV_GROW_SIZE);
|
---|
| 184 | pIntEnv->papszEnv = (char **)RTMemAllocZ(sizeof(pIntEnv->papszEnv[0]) * pIntEnv->cAllocated);
|
---|
| 185 | if (pIntEnv->papszEnv)
|
---|
[4063] | 186 | {
|
---|
| 187 | *ppIntEnv = pIntEnv;
|
---|
| 188 | return VINF_SUCCESS;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | RTMemFree(pIntEnv);
|
---|
[20364] | 192 | }
|
---|
[4063] | 193 |
|
---|
[4475] | 194 | return VERR_NO_MEMORY;
|
---|
[4063] | 195 | }
|
---|
| 196 |
|
---|
[4475] | 197 |
|
---|
[4063] | 198 | RTDECL(int) RTEnvCreate(PRTENV pEnv)
|
---|
| 199 | {
|
---|
[4475] | 200 | AssertPtrReturn(pEnv, VERR_INVALID_POINTER);
|
---|
[80827] | 201 | #ifdef RTENV_ALLOW_EQUAL_FIRST_IN_VAR
|
---|
| 202 | return rtEnvCreate(pEnv, RTENV_GROW_SIZE, true /*fCaseSensitive*/, false /*fPutEnvBlock*/, true /*fFirstEqual*/);
|
---|
| 203 | #else
|
---|
| 204 | return rtEnvCreate(pEnv, RTENV_GROW_SIZE, true /*fCaseSensitive*/, false /*fPutEnvBlock*/, false/*fFirstEqual*/);
|
---|
| 205 | #endif
|
---|
[4063] | 206 | }
|
---|
[21337] | 207 | RT_EXPORT_SYMBOL(RTEnvCreate);
|
---|
[4063] | 208 |
|
---|
[4475] | 209 |
|
---|
[80827] | 210 | RTDECL(int) RTEnvCreateEx(PRTENV phEnv, uint32_t fFlags)
|
---|
| 211 | {
|
---|
| 212 | AssertPtrReturn(phEnv, VERR_INVALID_POINTER);
|
---|
| 213 | AssertReturn(!(fFlags & ~RTENV_CREATE_F_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
| 214 | return rtEnvCreate(phEnv, RTENV_GROW_SIZE, true /*fCaseSensitive*/, false /*fPutEnvBlock*/,
|
---|
| 215 | RT_BOOL(fFlags & RTENV_CREATE_F_ALLOW_EQUAL_FIRST_IN_VAR));
|
---|
| 216 | }
|
---|
| 217 | RT_EXPORT_SYMBOL(RTEnvCreateEx);
|
---|
| 218 |
|
---|
| 219 |
|
---|
[4063] | 220 | RTDECL(int) RTEnvDestroy(RTENV Env)
|
---|
| 221 | {
|
---|
[20364] | 222 | /*
|
---|
[4475] | 223 | * Ignore NIL_RTENV and validate input.
|
---|
| 224 | */
|
---|
| 225 | if ( Env == NIL_RTENV
|
---|
| 226 | || Env == RTENV_DEFAULT)
|
---|
| 227 | return VINF_SUCCESS;
|
---|
[4063] | 228 |
|
---|
[4475] | 229 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 230 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 231 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
[4063] | 232 |
|
---|
[20364] | 233 | /*
|
---|
[4475] | 234 | * Do the cleanup.
|
---|
| 235 | */
|
---|
| 236 | RTENV_LOCK(pIntEnv);
|
---|
| 237 | pIntEnv->u32Magic++;
|
---|
| 238 | size_t iVar = pIntEnv->cVars;
|
---|
| 239 | while (iVar-- > 0)
|
---|
| 240 | RTStrFree(pIntEnv->papszEnv[iVar]);
|
---|
| 241 | RTMemFree(pIntEnv->papszEnv);
|
---|
| 242 | pIntEnv->papszEnv = NULL;
|
---|
| 243 |
|
---|
| 244 | if (pIntEnv->papszEnvOtherCP)
|
---|
[4063] | 245 | {
|
---|
[4475] | 246 | for (iVar = 0; pIntEnv->papszEnvOtherCP[iVar]; iVar++)
|
---|
| 247 | {
|
---|
| 248 | RTStrFree(pIntEnv->papszEnvOtherCP[iVar]);
|
---|
| 249 | pIntEnv->papszEnvOtherCP[iVar] = NULL;
|
---|
| 250 | }
|
---|
| 251 | RTMemFree(pIntEnv->papszEnvOtherCP);
|
---|
| 252 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
[4063] | 253 | }
|
---|
| 254 |
|
---|
[4475] | 255 | RTENV_UNLOCK(pIntEnv);
|
---|
| 256 | /*RTCritSectDelete(&pIntEnv->CritSect) */
|
---|
[4063] | 257 | RTMemFree(pIntEnv);
|
---|
[20364] | 258 |
|
---|
[4063] | 259 | return VINF_SUCCESS;
|
---|
| 260 | }
|
---|
[21337] | 261 | RT_EXPORT_SYMBOL(RTEnvDestroy);
|
---|
[4063] | 262 |
|
---|
[4475] | 263 |
|
---|
[95842] | 264 | static int rtEnvCloneDefault(PRTENV phEnv)
|
---|
[4063] | 265 | {
|
---|
[95842] | 266 | #ifdef RTENV_ALLOW_EQUAL_FIRST_IN_VAR
|
---|
| 267 | bool const fFirstEqual = true;
|
---|
| 268 | #else
|
---|
| 269 | bool const fFirstEqual = false;
|
---|
| 270 | #endif
|
---|
| 271 |
|
---|
| 272 | #ifdef RT_OS_WINDOWS
|
---|
[4475] | 273 | /*
|
---|
[95842] | 274 | * Lock the PEB, get the process environment.
|
---|
| 275 | *
|
---|
| 276 | * On older windows version GetEnviornmentStringsW will not copy the
|
---|
| 277 | * environment block, but return the pointer stored in the PEB. This
|
---|
| 278 | * should be safer wrt to concurrent changes.
|
---|
[4475] | 279 | */
|
---|
[95842] | 280 | PPEB pPeb = RTNtCurrentPeb();
|
---|
| 281 |
|
---|
| 282 | RtlAcquirePebLock();
|
---|
| 283 |
|
---|
| 284 | /* Count variables in the block: */
|
---|
| 285 | size_t cVars = 0;
|
---|
| 286 | PCRTUTF16 pwszzEnv = pPeb->ProcessParameters ? pPeb->ProcessParameters->Environment : NULL;
|
---|
| 287 | if (pwszzEnv)
|
---|
[4475] | 288 | {
|
---|
[95842] | 289 | PCRTUTF16 pwsz = pwszzEnv;
|
---|
| 290 | while (*pwsz)
|
---|
[50644] | 291 | {
|
---|
[95842] | 292 | cVars++;
|
---|
| 293 | pwsz += RTUtf16Len(pwsz) + 1;
|
---|
[50644] | 294 | }
|
---|
[95842] | 295 | }
|
---|
[50408] | 296 |
|
---|
[95842] | 297 | PRTENVINTERNAL pIntEnv;
|
---|
| 298 | int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */, false /*fCaseSensitive*/, false /*fPutEnvBlock*/, fFirstEqual);
|
---|
| 299 | if (RT_SUCCESS(rc))
|
---|
[4475] | 300 | {
|
---|
[95842] | 301 | size_t iDst;
|
---|
| 302 | for (iDst = 0; iDst < cVars && *pwszzEnv; iDst++, pwszzEnv += RTUtf16Len(pwszzEnv) + 1)
|
---|
| 303 | {
|
---|
| 304 | int rc2 = RTUtf16ToUtf8(pwszzEnv, &pIntEnv->papszEnv[iDst]);
|
---|
| 305 | if (RT_SUCCESS(rc2))
|
---|
| 306 | {
|
---|
| 307 | /* Make sure it contains an '='. */
|
---|
| 308 | if (strchr(pIntEnv->papszEnv[iDst], '='))
|
---|
| 309 | continue;
|
---|
| 310 | rc2 = RTStrAAppend(&pIntEnv->papszEnv[iDst], "=");
|
---|
| 311 | if (RT_SUCCESS(rc2))
|
---|
| 312 | continue;
|
---|
| 313 | }
|
---|
[4063] | 314 |
|
---|
[95842] | 315 | /* failed fatally. */
|
---|
| 316 | pIntEnv->cVars = iDst + 1;
|
---|
| 317 | RtlReleasePebLock();
|
---|
| 318 | RTEnvDestroy(pIntEnv);
|
---|
| 319 | return rc2;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | Assert(!*pwszzEnv); Assert(iDst == cVars);
|
---|
| 323 | pIntEnv->cVars = iDst;
|
---|
| 324 | pIntEnv->papszEnv[iDst] = NULL;
|
---|
| 325 |
|
---|
| 326 | /* done */
|
---|
| 327 | *phEnv = pIntEnv;
|
---|
[4475] | 328 | }
|
---|
[4063] | 329 |
|
---|
[95842] | 330 | RtlReleasePebLock();
|
---|
| 331 | return rc;
|
---|
| 332 |
|
---|
| 333 | #else /* !RT_OS_WINDOWS */
|
---|
| 334 |
|
---|
[4475] | 335 | /*
|
---|
[95842] | 336 | * Figure out how many variable to clone.
|
---|
| 337 | */
|
---|
| 338 | const char * const *papszEnv = rtEnvDefault();
|
---|
| 339 | size_t cVars = 0;
|
---|
| 340 | if (papszEnv)
|
---|
| 341 | while (papszEnv[cVars])
|
---|
| 342 | cVars++;
|
---|
| 343 |
|
---|
| 344 | bool fCaseSensitive = true;
|
---|
| 345 | # if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
| 346 | /* DOS systems was case insensitive. A prime example is the 'Path'
|
---|
| 347 | variable on windows which turns into the 'PATH' variable. */
|
---|
| 348 | fCaseSensitive = false;
|
---|
| 349 | # endif
|
---|
| 350 |
|
---|
| 351 | /*
|
---|
[4475] | 352 | * Create the duplicate.
|
---|
| 353 | */
|
---|
| 354 | PRTENVINTERNAL pIntEnv;
|
---|
[95842] | 355 | int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */, fCaseSensitive, false /*fPutEnvBlock*/, fFirstEqual);
|
---|
[4475] | 356 | if (RT_SUCCESS(rc))
|
---|
[4063] | 357 | {
|
---|
[4475] | 358 | pIntEnv->cVars = cVars;
|
---|
| 359 | pIntEnv->papszEnv[pIntEnv->cVars] = NULL;
|
---|
[95842] | 360 |
|
---|
| 361 | /* ASSUMES the default environment is in the current codepage. */
|
---|
| 362 | size_t iDst = 0;
|
---|
| 363 | for (size_t iSrc = 0; iSrc < cVars; iSrc++)
|
---|
[4063] | 364 | {
|
---|
[95842] | 365 | int rc2 = RTStrCurrentCPToUtf8(&pIntEnv->papszEnv[iDst], papszEnv[iSrc]);
|
---|
| 366 | if (RT_SUCCESS(rc2))
|
---|
[4475] | 367 | {
|
---|
[95842] | 368 | /* Make sure it contains an '='. */
|
---|
| 369 | iDst++;
|
---|
| 370 | if (strchr(pIntEnv->papszEnv[iDst - 1], '='))
|
---|
| 371 | continue;
|
---|
| 372 | rc2 = RTStrAAppend(&pIntEnv->papszEnv[iDst - 1], "=");
|
---|
[41723] | 373 | if (RT_SUCCESS(rc2))
|
---|
[55562] | 374 | continue;
|
---|
[95842] | 375 | }
|
---|
| 376 | else if (rc2 == VERR_NO_TRANSLATION)
|
---|
| 377 | {
|
---|
| 378 | rc = VWRN_ENV_NOT_FULLY_TRANSLATED;
|
---|
| 379 | continue;
|
---|
| 380 | }
|
---|
[55562] | 381 |
|
---|
[95842] | 382 | /* failed fatally. */
|
---|
[41670] | 383 | pIntEnv->cVars = iDst;
|
---|
[95842] | 384 | RTEnvDestroy(pIntEnv);
|
---|
| 385 | return rc2;
|
---|
[4063] | 386 | }
|
---|
[95842] | 387 | pIntEnv->cVars = iDst;
|
---|
| 388 |
|
---|
| 389 | /* done */
|
---|
| 390 | *phEnv = pIntEnv;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | return rc;
|
---|
| 394 | #endif /* !RT_OS_WINDOWS */
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 |
|
---|
| 398 | /**
|
---|
| 399 | * Clones a non-default environment instance.
|
---|
| 400 | *
|
---|
| 401 | * @param phEnv Where to return the handle to the cloned environment.
|
---|
| 402 | * @param pIntEnvToClone The source environment. Caller takes care of
|
---|
| 403 | * locking.
|
---|
| 404 | */
|
---|
| 405 | static int rtEnvCloneNonDefault(PRTENV phEnv, PRTENVINTERNAL pIntEnvToClone)
|
---|
| 406 | {
|
---|
| 407 | PRTENVINTERNAL pIntEnv;
|
---|
| 408 | size_t const cVars = pIntEnvToClone->cVars;
|
---|
| 409 | int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */,
|
---|
| 410 | pIntEnvToClone->pfnCompare != RTStrNICmp,
|
---|
| 411 | pIntEnvToClone->fPutEnvBlock,
|
---|
| 412 | pIntEnvToClone->fFirstEqual);
|
---|
| 413 | if (RT_SUCCESS(rc))
|
---|
| 414 | {
|
---|
| 415 | pIntEnv->cVars = cVars;
|
---|
| 416 | pIntEnv->papszEnv[cVars] = NULL;
|
---|
| 417 |
|
---|
| 418 | const char * const * const papszEnv = pIntEnvToClone->papszEnv;
|
---|
| 419 | for (size_t iVar = 0; iVar < cVars; iVar++)
|
---|
[4475] | 420 | {
|
---|
[95842] | 421 | char *pszVar = RTStrDup(papszEnv[iVar]);
|
---|
| 422 | if (RT_UNLIKELY(!pszVar))
|
---|
[4475] | 423 | {
|
---|
[95842] | 424 | pIntEnv->cVars = iVar;
|
---|
| 425 | RTEnvDestroy(pIntEnv);
|
---|
| 426 | return VERR_NO_STR_MEMORY;
|
---|
[4475] | 427 | }
|
---|
[95842] | 428 | pIntEnv->papszEnv[iVar] = pszVar;
|
---|
[4475] | 429 | }
|
---|
[4063] | 430 |
|
---|
[4475] | 431 | /* done */
|
---|
[95842] | 432 | *phEnv = pIntEnv;
|
---|
[4063] | 433 | }
|
---|
[95842] | 434 | return rc;
|
---|
| 435 | }
|
---|
[4063] | 436 |
|
---|
[95842] | 437 |
|
---|
| 438 | RTDECL(int) RTEnvClone(PRTENV phEnv, RTENV hEnvToClone)
|
---|
| 439 | {
|
---|
| 440 | /*
|
---|
| 441 | * Validate input and what kind of source block we're working with.
|
---|
| 442 | */
|
---|
| 443 | int rc;
|
---|
| 444 | AssertPtrReturn(phEnv, VERR_INVALID_POINTER);
|
---|
| 445 | if (hEnvToClone == RTENV_DEFAULT)
|
---|
| 446 | rc = rtEnvCloneDefault(phEnv);
|
---|
| 447 | else
|
---|
| 448 | {
|
---|
| 449 | PRTENVINTERNAL pIntEnvToClone = hEnvToClone;
|
---|
| 450 | AssertPtrReturn(pIntEnvToClone, VERR_INVALID_HANDLE);
|
---|
| 451 | AssertReturn(pIntEnvToClone->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
| 452 |
|
---|
| 453 | RTENV_LOCK(pIntEnvToClone);
|
---|
| 454 | rc = rtEnvCloneNonDefault(phEnv, pIntEnvToClone);
|
---|
[4475] | 455 | RTENV_UNLOCK(pIntEnvToClone);
|
---|
[95842] | 456 | }
|
---|
[4063] | 457 | return rc;
|
---|
| 458 | }
|
---|
[21337] | 459 | RT_EXPORT_SYMBOL(RTEnvClone);
|
---|
[4063] | 460 |
|
---|
[4475] | 461 |
|
---|
[57835] | 462 | RTDECL(int) RTEnvCloneUtf16Block(PRTENV phEnv, PCRTUTF16 pwszzBlock, uint32_t fFlags)
|
---|
| 463 | {
|
---|
| 464 | AssertPtrReturn(pwszzBlock, VERR_INVALID_POINTER);
|
---|
| 465 | AssertReturn(!fFlags, VERR_INVALID_FLAGS);
|
---|
| 466 |
|
---|
| 467 | /*
|
---|
| 468 | * Count the number of variables in the block.
|
---|
| 469 | */
|
---|
| 470 | uint32_t cVars = 0;
|
---|
| 471 | PCRTUTF16 pwsz = pwszzBlock;
|
---|
| 472 | while (*pwsz != '\0')
|
---|
| 473 | {
|
---|
| 474 | cVars++;
|
---|
| 475 | pwsz += RTUtf16Len(pwsz) + 1;
|
---|
| 476 | AssertReturn(cVars < _256K, VERR_OUT_OF_RANGE);
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | /*
|
---|
| 480 | * Create the duplicate.
|
---|
| 481 | */
|
---|
| 482 | PRTENVINTERNAL pIntEnv;
|
---|
[80827] | 483 | #ifdef RTENV_ALLOW_EQUAL_FIRST_IN_VAR
|
---|
| 484 | int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */, false /*fCaseSensitive*/, false /*fPutEnvBlock*/, true /*fFirstEqual*/);
|
---|
| 485 | #else
|
---|
| 486 | int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */, false /*fCaseSensitive*/, false /*fPutEnvBlock*/, false /*fFirstEqual*/);
|
---|
| 487 | #endif
|
---|
[57835] | 488 | if (RT_SUCCESS(rc))
|
---|
| 489 | {
|
---|
| 490 | pIntEnv->cVars = cVars;
|
---|
| 491 | pIntEnv->papszEnv[pIntEnv->cVars] = NULL;
|
---|
| 492 |
|
---|
| 493 | size_t iDst = 0;
|
---|
| 494 | for (pwsz = pwszzBlock; *pwsz != '\0'; pwsz += RTUtf16Len(pwsz) + 1)
|
---|
| 495 | {
|
---|
| 496 | int rc2 = RTUtf16ToUtf8(pwsz, &pIntEnv->papszEnv[iDst]);
|
---|
| 497 | if (RT_SUCCESS(rc2))
|
---|
| 498 | {
|
---|
| 499 | /* Make sure it contains an '='. */
|
---|
| 500 | const char *pszEqual = strchr(pIntEnv->papszEnv[iDst], '=');
|
---|
| 501 | if (!pszEqual)
|
---|
| 502 | {
|
---|
| 503 | rc2 = RTStrAAppend(&pIntEnv->papszEnv[iDst], "=");
|
---|
| 504 | if (RT_SUCCESS(rc2))
|
---|
| 505 | pszEqual = strchr(pIntEnv->papszEnv[iDst], '=');
|
---|
| 506 |
|
---|
| 507 | }
|
---|
| 508 | if (pszEqual)
|
---|
| 509 | {
|
---|
| 510 | /* Check for duplicates, keep the last version. */
|
---|
| 511 | const char *pchVar = pIntEnv->papszEnv[iDst];
|
---|
| 512 | size_t cchVarNmAndEq = pszEqual - pchVar;
|
---|
| 513 | for (size_t iDst2 = 0; iDst2 < iDst; iDst2++)
|
---|
| 514 | if (pIntEnv->pfnCompare(pIntEnv->papszEnv[iDst2], pchVar, cchVarNmAndEq) == 0)
|
---|
| 515 | {
|
---|
| 516 | RTStrFree(pIntEnv->papszEnv[iDst2]);
|
---|
| 517 | pIntEnv->papszEnv[iDst2] = pIntEnv->papszEnv[iDst];
|
---|
| 518 | pIntEnv->papszEnv[iDst] = NULL;
|
---|
| 519 | iDst--;
|
---|
| 520 | break;
|
---|
| 521 | }
|
---|
| 522 | iDst++;
|
---|
| 523 | continue;
|
---|
| 524 | }
|
---|
| 525 | iDst++;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | /* failed fatally. */
|
---|
| 529 | pIntEnv->cVars = iDst;
|
---|
| 530 | RTEnvDestroy(pIntEnv);
|
---|
| 531 | return rc2;
|
---|
| 532 | }
|
---|
| 533 | Assert(iDst <= pIntEnv->cVars);
|
---|
| 534 | pIntEnv->cVars = iDst;
|
---|
| 535 |
|
---|
| 536 | /* done */
|
---|
| 537 | *phEnv = pIntEnv;
|
---|
| 538 | }
|
---|
| 539 | return rc;
|
---|
| 540 | }
|
---|
| 541 | RT_EXPORT_SYMBOL(RTEnvCloneUtf16Block);
|
---|
| 542 |
|
---|
| 543 |
|
---|
| 544 |
|
---|
[55584] | 545 | RTDECL(int) RTEnvReset(RTENV hEnv)
|
---|
| 546 | {
|
---|
| 547 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
| 548 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 549 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
| 550 |
|
---|
| 551 | RTENV_LOCK(pIntEnv);
|
---|
| 552 |
|
---|
| 553 | size_t iVar = pIntEnv->cVars;
|
---|
| 554 | pIntEnv->cVars = 0;
|
---|
| 555 | while (iVar-- > 0)
|
---|
| 556 | {
|
---|
| 557 | RTMemFree(pIntEnv->papszEnv[iVar]);
|
---|
| 558 | pIntEnv->papszEnv[iVar] = NULL;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | RTENV_UNLOCK(pIntEnv);
|
---|
| 562 | return VINF_SUCCESS;
|
---|
| 563 | }
|
---|
| 564 | RT_EXPORT_SYMBOL(RTEnvReset);
|
---|
| 565 |
|
---|
| 566 |
|
---|
[55597] | 567 | /**
|
---|
| 568 | * Appends an already allocated string to papszEnv.
|
---|
| 569 | *
|
---|
| 570 | * @returns IPRT status code
|
---|
| 571 | * @param pIntEnv The environment block to append it to.
|
---|
| 572 | * @param pszEntry The string to add. Already duplicated, caller
|
---|
| 573 | * does error cleanup.
|
---|
| 574 | */
|
---|
| 575 | static int rtEnvIntAppend(PRTENVINTERNAL pIntEnv, char *pszEntry)
|
---|
| 576 | {
|
---|
| 577 | /*
|
---|
| 578 | * Do we need to resize the array?
|
---|
| 579 | */
|
---|
| 580 | int rc = VINF_SUCCESS;
|
---|
| 581 | size_t iVar = pIntEnv->cVars;
|
---|
| 582 | if (iVar + 2 > pIntEnv->cAllocated)
|
---|
| 583 | {
|
---|
| 584 | void *pvNew = RTMemRealloc(pIntEnv->papszEnv, sizeof(char *) * (pIntEnv->cAllocated + RTENV_GROW_SIZE));
|
---|
| 585 | if (!pvNew)
|
---|
| 586 | rc = VERR_NO_MEMORY;
|
---|
| 587 | else
|
---|
| 588 | {
|
---|
| 589 | pIntEnv->papszEnv = (char **)pvNew;
|
---|
| 590 | pIntEnv->cAllocated += RTENV_GROW_SIZE;
|
---|
| 591 | for (size_t iNewVar = pIntEnv->cVars; iNewVar < pIntEnv->cAllocated; iNewVar++)
|
---|
| 592 | pIntEnv->papszEnv[iNewVar] = NULL;
|
---|
| 593 | }
|
---|
| 594 | }
|
---|
| 595 | if (RT_SUCCESS(rc))
|
---|
| 596 | {
|
---|
| 597 | /*
|
---|
| 598 | * Append it.
|
---|
| 599 | */
|
---|
| 600 | pIntEnv->papszEnv[iVar] = pszEntry;
|
---|
| 601 | pIntEnv->papszEnv[iVar + 1] = NULL; /* this isn't really necessary, but doesn't hurt. */
|
---|
| 602 | pIntEnv->cVars = iVar + 1;
|
---|
| 603 | }
|
---|
| 604 | return rc;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 |
|
---|
[80764] | 608 | /**
|
---|
| 609 | * Worker for RTEnvSetEx and RTEnvPutEx.
|
---|
| 610 | */
|
---|
| 611 | static int rtEnvSetExWorker(RTENV Env, const char *pchVar, size_t cchVar, const char *pszValue)
|
---|
[4063] | 612 | {
|
---|
| 613 | int rc;
|
---|
[4475] | 614 | if (Env == RTENV_DEFAULT)
|
---|
[4063] | 615 | {
|
---|
[50408] | 616 | #ifdef RT_OS_WINDOWS
|
---|
[80764] | 617 | extern int rtEnvSetUtf8Worker(const char *pchVar, size_t cchVar, const char *pszValue);
|
---|
| 618 | rc = rtEnvSetUtf8Worker(pchVar, cchVar, pszValue);
|
---|
[50408] | 619 | #else
|
---|
[20364] | 620 | /*
|
---|
[4475] | 621 | * Since RTEnvPut isn't UTF-8 clean and actually expects the strings
|
---|
[20364] | 622 | * to be in the current code page (codeset), we'll do the necessary
|
---|
[4475] | 623 | * conversions here.
|
---|
| 624 | */
|
---|
| 625 | char *pszVarOtherCP;
|
---|
[80764] | 626 | rc = RTStrUtf8ToCurrentCPEx(&pszVarOtherCP, pchVar, cchVar);
|
---|
[4475] | 627 | if (RT_SUCCESS(rc))
|
---|
[4063] | 628 | {
|
---|
[4475] | 629 | char *pszValueOtherCP;
|
---|
| 630 | rc = RTStrUtf8ToCurrentCP(&pszValueOtherCP, pszValue);
|
---|
| 631 | if (RT_SUCCESS(rc))
|
---|
| 632 | {
|
---|
| 633 | rc = RTEnvSet(pszVarOtherCP, pszValueOtherCP);
|
---|
| 634 | RTStrFree(pszValueOtherCP);
|
---|
| 635 | }
|
---|
| 636 | RTStrFree(pszVarOtherCP);
|
---|
[4063] | 637 | }
|
---|
[50408] | 638 | #endif
|
---|
[4063] | 639 | }
|
---|
| 640 | else
|
---|
[4475] | 641 | {
|
---|
| 642 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 643 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 644 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
[4063] | 645 |
|
---|
[4475] | 646 | /*
|
---|
| 647 | * Create the variable string.
|
---|
| 648 | */
|
---|
| 649 | const size_t cchValue = strlen(pszValue);
|
---|
| 650 | char *pszEntry = (char *)RTMemAlloc(cchVar + cchValue + 2);
|
---|
| 651 | if (pszEntry)
|
---|
| 652 | {
|
---|
[80764] | 653 | memcpy(pszEntry, pchVar, cchVar);
|
---|
[4475] | 654 | pszEntry[cchVar] = '=';
|
---|
| 655 | memcpy(&pszEntry[cchVar + 1], pszValue, cchValue + 1);
|
---|
| 656 |
|
---|
| 657 | RTENV_LOCK(pIntEnv);
|
---|
| 658 |
|
---|
| 659 | /*
|
---|
| 660 | * Find the location of the variable. (iVar = cVars if new)
|
---|
| 661 | */
|
---|
| 662 | rc = VINF_SUCCESS;
|
---|
| 663 | size_t iVar;
|
---|
| 664 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
[80764] | 665 | if ( !pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pchVar, cchVar)
|
---|
[55562] | 666 | && ( pIntEnv->papszEnv[iVar][cchVar] == '='
|
---|
| 667 | || pIntEnv->papszEnv[iVar][cchVar] == '\0') )
|
---|
[4475] | 668 | break;
|
---|
| 669 | if (iVar < pIntEnv->cVars)
|
---|
| 670 | {
|
---|
[20364] | 671 | /*
|
---|
[4475] | 672 | * Replace the current entry. Simple.
|
---|
| 673 | */
|
---|
| 674 | RTMemFree(pIntEnv->papszEnv[iVar]);
|
---|
| 675 | pIntEnv->papszEnv[iVar] = pszEntry;
|
---|
| 676 | }
|
---|
| 677 | else
|
---|
| 678 | {
|
---|
| 679 | /*
|
---|
[55597] | 680 | * New variable, append it.
|
---|
[4475] | 681 | */
|
---|
[55597] | 682 | Assert(pIntEnv->cVars == iVar);
|
---|
| 683 | rc = rtEnvIntAppend(pIntEnv, pszEntry);
|
---|
[4475] | 684 | }
|
---|
[20364] | 685 |
|
---|
[4475] | 686 | RTENV_UNLOCK(pIntEnv);
|
---|
| 687 |
|
---|
| 688 | if (RT_FAILURE(rc))
|
---|
| 689 | RTMemFree(pszEntry);
|
---|
| 690 | }
|
---|
| 691 | else
|
---|
| 692 | rc = VERR_NO_MEMORY;
|
---|
| 693 | }
|
---|
[4063] | 694 | return rc;
|
---|
| 695 | }
|
---|
[80764] | 696 |
|
---|
| 697 |
|
---|
| 698 | RTDECL(int) RTEnvSetEx(RTENV Env, const char *pszVar, const char *pszValue)
|
---|
| 699 | {
|
---|
| 700 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
| 701 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
| 702 | size_t const cchVar = strlen(pszVar);
|
---|
[80827] | 703 | AssertReturn(cchVar > 0, VERR_ENV_INVALID_VAR_NAME);
|
---|
| 704 | char const *pszEq = (char const *)memchr(pszVar, '=', cchVar);
|
---|
| 705 | if (!pszEq)
|
---|
| 706 | { /* likely */ }
|
---|
| 707 | else
|
---|
| 708 | {
|
---|
| 709 | AssertReturn(Env != RTENV_DEFAULT, VERR_ENV_INVALID_VAR_NAME);
|
---|
| 710 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 711 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 712 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
| 713 | AssertReturn(pIntEnv->fFirstEqual, VERR_ENV_INVALID_VAR_NAME);
|
---|
| 714 | AssertReturn(memchr(pszVar + 1, '=', cchVar - 1) == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
| 715 | }
|
---|
[80764] | 716 |
|
---|
| 717 | return rtEnvSetExWorker(Env, pszVar, cchVar, pszValue);
|
---|
| 718 | }
|
---|
[21337] | 719 | RT_EXPORT_SYMBOL(RTEnvSetEx);
|
---|
[4063] | 720 |
|
---|
[4475] | 721 |
|
---|
| 722 | RTDECL(int) RTEnvUnsetEx(RTENV Env, const char *pszVar)
|
---|
[4063] | 723 | {
|
---|
[4475] | 724 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
[80827] | 725 | AssertReturn(*pszVar, VERR_ENV_INVALID_VAR_NAME);
|
---|
[4063] | 726 |
|
---|
[4475] | 727 | int rc;
|
---|
| 728 | if (Env == RTENV_DEFAULT)
|
---|
[4063] | 729 | {
|
---|
[50408] | 730 | #ifdef RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
| 731 | rc = RTEnvUnsetUtf8(pszVar);
|
---|
| 732 | #else
|
---|
[20364] | 733 | /*
|
---|
[4475] | 734 | * Since RTEnvUnset isn't UTF-8 clean and actually expects the strings
|
---|
[20364] | 735 | * to be in the current code page (codeset), we'll do the necessary
|
---|
[4475] | 736 | * conversions here.
|
---|
| 737 | */
|
---|
| 738 | char *pszVarOtherCP;
|
---|
| 739 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
| 740 | if (RT_SUCCESS(rc))
|
---|
[4063] | 741 | {
|
---|
[4475] | 742 | rc = RTEnvUnset(pszVarOtherCP);
|
---|
| 743 | RTStrFree(pszVarOtherCP);
|
---|
[4063] | 744 | }
|
---|
[50408] | 745 | #endif
|
---|
[4063] | 746 | }
|
---|
[4475] | 747 | else
|
---|
| 748 | {
|
---|
| 749 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 750 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 751 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
[80827] | 752 | const size_t cchVar = strlen(pszVar);
|
---|
| 753 | AssertReturn(cchVar > 0, VERR_ENV_INVALID_VAR_NAME);
|
---|
| 754 | AssertReturn(strchr(pIntEnv->fFirstEqual ? pszVar + 1 : pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
[4063] | 755 |
|
---|
[4475] | 756 | RTENV_LOCK(pIntEnv);
|
---|
[4063] | 757 |
|
---|
[4475] | 758 | /*
|
---|
| 759 | * Remove all variable by the given name.
|
---|
| 760 | */
|
---|
| 761 | rc = VINF_ENV_VAR_NOT_FOUND;
|
---|
| 762 | size_t iVar;
|
---|
| 763 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
[50408] | 764 | if ( !pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pszVar, cchVar)
|
---|
[55562] | 765 | && ( pIntEnv->papszEnv[iVar][cchVar] == '='
|
---|
| 766 | || pIntEnv->papszEnv[iVar][cchVar] == '\0') )
|
---|
[4475] | 767 | {
|
---|
[55562] | 768 | if (!pIntEnv->fPutEnvBlock)
|
---|
| 769 | {
|
---|
| 770 | RTMemFree(pIntEnv->papszEnv[iVar]);
|
---|
| 771 | pIntEnv->cVars--;
|
---|
| 772 | if (pIntEnv->cVars > 0)
|
---|
| 773 | pIntEnv->papszEnv[iVar] = pIntEnv->papszEnv[pIntEnv->cVars];
|
---|
| 774 | pIntEnv->papszEnv[pIntEnv->cVars] = NULL;
|
---|
| 775 | }
|
---|
| 776 | else
|
---|
| 777 | {
|
---|
| 778 | /* Record this unset by keeping the variable without any equal sign. */
|
---|
| 779 | pIntEnv->papszEnv[iVar][cchVar] = '\0';
|
---|
| 780 | }
|
---|
[4475] | 781 | rc = VINF_SUCCESS;
|
---|
| 782 | /* no break, there could be more. */
|
---|
| 783 | }
|
---|
| 784 |
|
---|
[55597] | 785 | /*
|
---|
| 786 | * If this is a change record, we may need to add it.
|
---|
| 787 | */
|
---|
| 788 | if (rc == VINF_ENV_VAR_NOT_FOUND && pIntEnv->fPutEnvBlock)
|
---|
| 789 | {
|
---|
| 790 | char *pszEntry = (char *)RTMemDup(pszVar, cchVar + 1);
|
---|
| 791 | if (pszEntry)
|
---|
| 792 | {
|
---|
| 793 | rc = rtEnvIntAppend(pIntEnv, pszEntry);
|
---|
| 794 | if (RT_SUCCESS(rc))
|
---|
| 795 | rc = VINF_ENV_VAR_NOT_FOUND;
|
---|
| 796 | else
|
---|
| 797 | RTMemFree(pszEntry);
|
---|
| 798 | }
|
---|
| 799 | else
|
---|
| 800 | rc = VERR_NO_MEMORY;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[4475] | 803 | RTENV_UNLOCK(pIntEnv);
|
---|
[4063] | 804 | }
|
---|
[4475] | 805 | return rc;
|
---|
[20364] | 806 |
|
---|
[4475] | 807 | }
|
---|
[21337] | 808 | RT_EXPORT_SYMBOL(RTEnvUnsetEx);
|
---|
[4063] | 809 |
|
---|
| 810 |
|
---|
[80764] | 811 | RTDECL(int) RTEnvPutEx(RTENV Env, const char *pszVarEqualValue)
|
---|
| 812 | {
|
---|
| 813 | int rc;
|
---|
| 814 | AssertPtrReturn(pszVarEqualValue, VERR_INVALID_POINTER);
|
---|
| 815 | const char *pszEq = strchr(pszVarEqualValue, '=');
|
---|
[80827] | 816 | if ( pszEq == pszVarEqualValue
|
---|
| 817 | && Env != RTENV_DEFAULT)
|
---|
| 818 | {
|
---|
| 819 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 820 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 821 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
| 822 | if (pIntEnv->fFirstEqual)
|
---|
| 823 | pszEq = strchr(pszVarEqualValue + 1, '=');
|
---|
| 824 | }
|
---|
[80764] | 825 | if (!pszEq)
|
---|
| 826 | rc = RTEnvUnsetEx(Env, pszVarEqualValue);
|
---|
| 827 | else
|
---|
[80827] | 828 | {
|
---|
| 829 | AssertReturn(pszEq != pszVarEqualValue, VERR_ENV_INVALID_VAR_NAME);
|
---|
[80764] | 830 | rc = rtEnvSetExWorker(Env, pszVarEqualValue, pszEq - pszVarEqualValue, pszEq + 1);
|
---|
[80827] | 831 | }
|
---|
[80764] | 832 | return rc;
|
---|
| 833 | }
|
---|
| 834 | RT_EXPORT_SYMBOL(RTEnvPutEx);
|
---|
| 835 |
|
---|
| 836 |
|
---|
[4475] | 837 | RTDECL(int) RTEnvGetEx(RTENV Env, const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
|
---|
| 838 | {
|
---|
| 839 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
| 840 | AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER);
|
---|
| 841 | AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER);
|
---|
| 842 | AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER);
|
---|
| 843 |
|
---|
| 844 | if (pcchActual)
|
---|
| 845 | *pcchActual = 0;
|
---|
| 846 | int rc;
|
---|
| 847 | if (Env == RTENV_DEFAULT)
|
---|
[4063] | 848 | {
|
---|
[50408] | 849 | #ifdef RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
| 850 | rc = RTEnvGetUtf8(pszVar, pszValue, cbValue, pcchActual);
|
---|
| 851 | #else
|
---|
[20364] | 852 | /*
|
---|
[4475] | 853 | * Since RTEnvGet isn't UTF-8 clean and actually expects the strings
|
---|
[20364] | 854 | * to be in the current code page (codeset), we'll do the necessary
|
---|
[4475] | 855 | * conversions here.
|
---|
| 856 | */
|
---|
| 857 | char *pszVarOtherCP;
|
---|
| 858 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
| 859 | if (RT_SUCCESS(rc))
|
---|
| 860 | {
|
---|
| 861 | const char *pszValueOtherCP = RTEnvGet(pszVarOtherCP);
|
---|
| 862 | RTStrFree(pszVarOtherCP);
|
---|
| 863 | if (pszValueOtherCP)
|
---|
| 864 | {
|
---|
| 865 | char *pszValueUtf8;
|
---|
| 866 | rc = RTStrCurrentCPToUtf8(&pszValueUtf8, pszValueOtherCP);
|
---|
| 867 | if (RT_SUCCESS(rc))
|
---|
| 868 | {
|
---|
| 869 | rc = VINF_SUCCESS;
|
---|
| 870 | size_t cch = strlen(pszValueUtf8);
|
---|
| 871 | if (pcchActual)
|
---|
| 872 | *pcchActual = cch;
|
---|
| 873 | if (pszValue && cbValue)
|
---|
| 874 | {
|
---|
| 875 | if (cch < cbValue)
|
---|
| 876 | memcpy(pszValue, pszValueUtf8, cch + 1);
|
---|
| 877 | else
|
---|
| 878 | rc = VERR_BUFFER_OVERFLOW;
|
---|
| 879 | }
|
---|
[36576] | 880 | RTStrFree(pszValueUtf8);
|
---|
[4475] | 881 | }
|
---|
| 882 | }
|
---|
| 883 | else
|
---|
| 884 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
| 885 | }
|
---|
[50408] | 886 | #endif
|
---|
[4063] | 887 | }
|
---|
[4475] | 888 | else
|
---|
| 889 | {
|
---|
| 890 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 891 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 892 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
[80827] | 893 | const size_t cchVar = strlen(pszVar);
|
---|
| 894 | AssertReturn(cchVar > 0, VERR_ENV_INVALID_VAR_NAME);
|
---|
| 895 | AssertReturn(strchr(pIntEnv->fFirstEqual ? pszVar + 1 : pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
[4063] | 896 |
|
---|
[4475] | 897 | RTENV_LOCK(pIntEnv);
|
---|
| 898 |
|
---|
| 899 | /*
|
---|
| 900 | * Locate the first variable and return it to the caller.
|
---|
| 901 | */
|
---|
| 902 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
| 903 | size_t iVar;
|
---|
| 904 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
[55562] | 905 | if (!pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pszVar, cchVar))
|
---|
[4475] | 906 | {
|
---|
[55562] | 907 | if (pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
[4475] | 908 | {
|
---|
[55562] | 909 | rc = VINF_SUCCESS;
|
---|
| 910 | const char *pszValueOrg = pIntEnv->papszEnv[iVar] + cchVar + 1;
|
---|
| 911 | size_t cch = strlen(pszValueOrg);
|
---|
| 912 | if (pcchActual)
|
---|
| 913 | *pcchActual = cch;
|
---|
| 914 | if (pszValue && cbValue)
|
---|
| 915 | {
|
---|
| 916 | if (cch < cbValue)
|
---|
| 917 | memcpy(pszValue, pszValueOrg, cch + 1);
|
---|
| 918 | else
|
---|
| 919 | rc = VERR_BUFFER_OVERFLOW;
|
---|
| 920 | }
|
---|
| 921 | break;
|
---|
[4475] | 922 | }
|
---|
[55562] | 923 | if (pIntEnv->papszEnv[iVar][cchVar] == '\0')
|
---|
| 924 | {
|
---|
| 925 | Assert(pIntEnv->fPutEnvBlock);
|
---|
| 926 | rc = VERR_ENV_VAR_UNSET;
|
---|
| 927 | break;
|
---|
| 928 | }
|
---|
[4475] | 929 | }
|
---|
| 930 |
|
---|
| 931 | RTENV_UNLOCK(pIntEnv);
|
---|
| 932 | }
|
---|
[4063] | 933 | return rc;
|
---|
| 934 | }
|
---|
[21337] | 935 | RT_EXPORT_SYMBOL(RTEnvGetEx);
|
---|
[4063] | 936 |
|
---|
[4475] | 937 |
|
---|
| 938 | RTDECL(bool) RTEnvExistEx(RTENV Env, const char *pszVar)
|
---|
[4063] | 939 | {
|
---|
[4475] | 940 | AssertPtrReturn(pszVar, false);
|
---|
[4063] | 941 |
|
---|
[50408] | 942 | bool fExists = false;
|
---|
[4475] | 943 | if (Env == RTENV_DEFAULT)
|
---|
| 944 | {
|
---|
[50408] | 945 | #ifdef RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
| 946 | fExists = RTEnvExistsUtf8(pszVar);
|
---|
| 947 | #else
|
---|
[20364] | 948 | /*
|
---|
[4475] | 949 | * Since RTEnvExist isn't UTF-8 clean and actually expects the strings
|
---|
[20364] | 950 | * to be in the current code page (codeset), we'll do the necessary
|
---|
[4475] | 951 | * conversions here.
|
---|
| 952 | */
|
---|
| 953 | char *pszVarOtherCP;
|
---|
| 954 | int rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
| 955 | if (RT_SUCCESS(rc))
|
---|
| 956 | {
|
---|
[50408] | 957 | fExists = RTEnvExist(pszVarOtherCP);
|
---|
[4475] | 958 | RTStrFree(pszVarOtherCP);
|
---|
| 959 | }
|
---|
[50408] | 960 | #endif
|
---|
[4475] | 961 | }
|
---|
| 962 | else
|
---|
| 963 | {
|
---|
| 964 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 965 | AssertPtrReturn(pIntEnv, false);
|
---|
| 966 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, false);
|
---|
[80827] | 967 | const size_t cchVar = strlen(pszVar);
|
---|
| 968 | AssertReturn(cchVar > 0, false);
|
---|
| 969 | AssertReturn(strchr(pIntEnv->fFirstEqual ? pszVar + 1 : pszVar, '=') == NULL, false);
|
---|
[4063] | 970 |
|
---|
[4475] | 971 | RTENV_LOCK(pIntEnv);
|
---|
[4063] | 972 |
|
---|
[4475] | 973 | /*
|
---|
| 974 | * Simple search.
|
---|
| 975 | */
|
---|
| 976 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
[55562] | 977 | if (!pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pszVar, cchVar))
|
---|
[4475] | 978 | {
|
---|
[55562] | 979 | if (pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
| 980 | {
|
---|
| 981 | fExists = true;
|
---|
| 982 | break;
|
---|
| 983 | }
|
---|
| 984 | if (pIntEnv->papszEnv[iVar][cchVar] == '\0')
|
---|
| 985 | break;
|
---|
[4475] | 986 | }
|
---|
| 987 |
|
---|
| 988 | RTENV_UNLOCK(pIntEnv);
|
---|
| 989 | }
|
---|
[50408] | 990 | return fExists;
|
---|
[4063] | 991 | }
|
---|
[21337] | 992 | RT_EXPORT_SYMBOL(RTEnvExistEx);
|
---|
[4063] | 993 |
|
---|
[4475] | 994 |
|
---|
[95842] | 995 | #ifndef RT_OS_WINDOWS
|
---|
[4475] | 996 | RTDECL(char const * const *) RTEnvGetExecEnvP(RTENV Env)
|
---|
[4063] | 997 | {
|
---|
[4475] | 998 | const char * const *papszRet;
|
---|
| 999 | if (Env == RTENV_DEFAULT)
|
---|
| 1000 | {
|
---|
[50408] | 1001 | /** @todo fix this API it's fundamentally wrong! */
|
---|
[4475] | 1002 | papszRet = rtEnvDefault();
|
---|
| 1003 | if (!papszRet)
|
---|
| 1004 | {
|
---|
| 1005 | static const char * const s_papszDummy[2] = { NULL, NULL };
|
---|
| 1006 | papszRet = &s_papszDummy[0];
|
---|
| 1007 | }
|
---|
| 1008 | }
|
---|
| 1009 | else
|
---|
| 1010 | {
|
---|
| 1011 | PRTENVINTERNAL pIntEnv = Env;
|
---|
| 1012 | AssertPtrReturn(pIntEnv, NULL);
|
---|
| 1013 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, NULL);
|
---|
[4063] | 1014 |
|
---|
[4475] | 1015 | RTENV_LOCK(pIntEnv);
|
---|
[4063] | 1016 |
|
---|
[20364] | 1017 | /*
|
---|
| 1018 | * Free any old envp.
|
---|
[4475] | 1019 | */
|
---|
| 1020 | if (pIntEnv->papszEnvOtherCP)
|
---|
| 1021 | {
|
---|
| 1022 | for (size_t iVar = 0; pIntEnv->papszEnvOtherCP[iVar]; iVar++)
|
---|
| 1023 | {
|
---|
| 1024 | RTStrFree(pIntEnv->papszEnvOtherCP[iVar]);
|
---|
| 1025 | pIntEnv->papszEnvOtherCP[iVar] = NULL;
|
---|
| 1026 | }
|
---|
| 1027 | RTMemFree(pIntEnv->papszEnvOtherCP);
|
---|
| 1028 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 | /*
|
---|
| 1032 | * Construct a new envp with the strings in the process code set.
|
---|
| 1033 | */
|
---|
| 1034 | char **papsz;
|
---|
| 1035 | papszRet = pIntEnv->papszEnvOtherCP = papsz = (char **)RTMemAlloc(sizeof(char *) * (pIntEnv->cVars + 1));
|
---|
| 1036 | if (papsz)
|
---|
| 1037 | {
|
---|
| 1038 | papsz[pIntEnv->cVars] = NULL;
|
---|
| 1039 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
| 1040 | {
|
---|
| 1041 | int rc = RTStrUtf8ToCurrentCP(&papsz[iVar], pIntEnv->papszEnv[iVar]);
|
---|
| 1042 | if (RT_FAILURE(rc))
|
---|
| 1043 | {
|
---|
| 1044 | /* RTEnvDestroy / we cleans up later. */
|
---|
| 1045 | papsz[iVar] = NULL;
|
---|
| 1046 | AssertRC(rc);
|
---|
| 1047 | papszRet = NULL;
|
---|
| 1048 | break;
|
---|
| 1049 | }
|
---|
| 1050 | }
|
---|
| 1051 | }
|
---|
| 1052 |
|
---|
| 1053 | RTENV_UNLOCK(pIntEnv);
|
---|
| 1054 | }
|
---|
| 1055 | return papszRet;
|
---|
[4063] | 1056 | }
|
---|
[21337] | 1057 | RT_EXPORT_SYMBOL(RTEnvGetExecEnvP);
|
---|
[95842] | 1058 | #endif /* !RT_OS_WINDOWS */
|
---|
[27346] | 1059 |
|
---|
| 1060 |
|
---|
| 1061 | /**
|
---|
[33540] | 1062 | * RTSort callback for comparing two environment variables.
|
---|
[27346] | 1063 | *
|
---|
| 1064 | * @returns -1, 0, 1. See PFNRTSORTCMP.
|
---|
| 1065 | * @param pvElement1 Variable 1.
|
---|
| 1066 | * @param pvElement2 Variable 2.
|
---|
| 1067 | * @param pvUser Ignored.
|
---|
| 1068 | */
|
---|
[55562] | 1069 | static DECLCALLBACK(int) rtEnvSortCompare(const void *pvElement1, const void *pvElement2, void *pvUser)
|
---|
[27346] | 1070 | {
|
---|
| 1071 | NOREF(pvUser);
|
---|
| 1072 | int iDiff = strcmp((const char *)pvElement1, (const char *)pvElement2);
|
---|
| 1073 | if (iDiff < 0)
|
---|
| 1074 | iDiff = -1;
|
---|
| 1075 | else if (iDiff > 0)
|
---|
| 1076 | iDiff = 1;
|
---|
| 1077 | return iDiff;
|
---|
| 1078 | }
|
---|
| 1079 |
|
---|
| 1080 |
|
---|
| 1081 | RTDECL(int) RTEnvQueryUtf16Block(RTENV hEnv, PRTUTF16 *ppwszzBlock)
|
---|
| 1082 | {
|
---|
| 1083 | RTENV hClone = NIL_RTENV;
|
---|
| 1084 | PRTENVINTERNAL pIntEnv;
|
---|
| 1085 | int rc;
|
---|
| 1086 |
|
---|
| 1087 | /*
|
---|
| 1088 | * Validate / simplify input.
|
---|
| 1089 | */
|
---|
| 1090 | if (hEnv == RTENV_DEFAULT)
|
---|
| 1091 | {
|
---|
| 1092 | rc = RTEnvClone(&hClone, RTENV_DEFAULT);
|
---|
| 1093 | if (RT_FAILURE(rc))
|
---|
| 1094 | return rc;
|
---|
| 1095 | pIntEnv = hClone;
|
---|
| 1096 | }
|
---|
| 1097 | else
|
---|
| 1098 | {
|
---|
| 1099 | pIntEnv = hEnv;
|
---|
| 1100 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 1101 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
[27350] | 1102 | rc = VINF_SUCCESS;
|
---|
[27346] | 1103 | }
|
---|
| 1104 |
|
---|
| 1105 | RTENV_LOCK(pIntEnv);
|
---|
| 1106 |
|
---|
| 1107 | /*
|
---|
| 1108 | * Sort it first.
|
---|
| 1109 | */
|
---|
| 1110 | RTSortApvShell((void **)pIntEnv->papszEnv, pIntEnv->cVars, rtEnvSortCompare, pIntEnv);
|
---|
| 1111 |
|
---|
| 1112 | /*
|
---|
| 1113 | * Calculate the size.
|
---|
| 1114 | */
|
---|
| 1115 | size_t cwc;
|
---|
| 1116 | size_t cwcTotal = 2;
|
---|
| 1117 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
| 1118 | {
|
---|
| 1119 | rc = RTStrCalcUtf16LenEx(pIntEnv->papszEnv[iVar], RTSTR_MAX, &cwc);
|
---|
| 1120 | AssertRCBreak(rc);
|
---|
| 1121 | cwcTotal += cwc + 1;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | PRTUTF16 pwszzBlock = NULL;
|
---|
| 1125 | if (RT_SUCCESS(rc))
|
---|
| 1126 | {
|
---|
| 1127 | /*
|
---|
| 1128 | * Perform the conversion.
|
---|
| 1129 | */
|
---|
| 1130 | PRTUTF16 pwszz = pwszzBlock = (PRTUTF16)RTMemAlloc(cwcTotal * sizeof(RTUTF16));
|
---|
| 1131 | if (pwszz)
|
---|
| 1132 | {
|
---|
| 1133 | size_t cwcLeft = cwcTotal;
|
---|
| 1134 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
| 1135 | {
|
---|
| 1136 | rc = RTStrToUtf16Ex(pIntEnv->papszEnv[iVar], RTSTR_MAX,
|
---|
| 1137 | &pwszz, cwcTotal - (pwszz - pwszzBlock), &cwc);
|
---|
| 1138 | AssertRCBreak(rc);
|
---|
| 1139 | pwszz += cwc + 1;
|
---|
| 1140 | cwcLeft -= cwc + 1;
|
---|
| 1141 | AssertBreakStmt(cwcLeft >= 2, rc = VERR_INTERNAL_ERROR_3);
|
---|
| 1142 | }
|
---|
| 1143 | AssertStmt(cwcLeft == 2 || RT_FAILURE(rc), rc = VERR_INTERNAL_ERROR_2);
|
---|
| 1144 | if (RT_SUCCESS(rc))
|
---|
| 1145 | {
|
---|
| 1146 | pwszz[0] = '\0';
|
---|
| 1147 | pwszz[1] = '\0';
|
---|
| 1148 | }
|
---|
| 1149 | else
|
---|
| 1150 | {
|
---|
| 1151 | RTMemFree(pwszzBlock);
|
---|
| 1152 | pwszzBlock = NULL;
|
---|
| 1153 | }
|
---|
| 1154 | }
|
---|
| 1155 | else
|
---|
| 1156 | rc = VERR_NO_MEMORY;
|
---|
| 1157 | }
|
---|
| 1158 |
|
---|
| 1159 | RTENV_UNLOCK(pIntEnv);
|
---|
| 1160 |
|
---|
| 1161 | if (hClone != NIL_RTENV)
|
---|
| 1162 | RTEnvDestroy(hClone);
|
---|
| 1163 | if (RT_SUCCESS(rc))
|
---|
| 1164 | *ppwszzBlock = pwszzBlock;
|
---|
| 1165 | return rc;
|
---|
| 1166 | }
|
---|
[55584] | 1167 | RT_EXPORT_SYMBOL(RTEnvQueryUtf16Block);
|
---|
[27346] | 1168 |
|
---|
| 1169 |
|
---|
| 1170 | RTDECL(void) RTEnvFreeUtf16Block(PRTUTF16 pwszzBlock)
|
---|
| 1171 | {
|
---|
| 1172 | RTMemFree(pwszzBlock);
|
---|
| 1173 | }
|
---|
| 1174 | RT_EXPORT_SYMBOL(RTEnvFreeUtf16Block);
|
---|
| 1175 |
|
---|
[50642] | 1176 |
|
---|
[55584] | 1177 | RTDECL(int) RTEnvQueryUtf8Block(RTENV hEnv, bool fSorted, char **ppszzBlock, size_t *pcbBlock)
|
---|
| 1178 | {
|
---|
| 1179 | RTENV hClone = NIL_RTENV;
|
---|
| 1180 | PRTENVINTERNAL pIntEnv;
|
---|
| 1181 | int rc;
|
---|
| 1182 |
|
---|
| 1183 | /*
|
---|
| 1184 | * Validate / simplify input.
|
---|
| 1185 | */
|
---|
| 1186 | if (hEnv == RTENV_DEFAULT)
|
---|
| 1187 | {
|
---|
| 1188 | rc = RTEnvClone(&hClone, RTENV_DEFAULT);
|
---|
| 1189 | if (RT_FAILURE(rc))
|
---|
| 1190 | return rc;
|
---|
| 1191 | pIntEnv = hClone;
|
---|
| 1192 | }
|
---|
| 1193 | else
|
---|
| 1194 | {
|
---|
| 1195 | pIntEnv = hEnv;
|
---|
| 1196 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
| 1197 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
| 1198 | rc = VINF_SUCCESS;
|
---|
| 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | RTENV_LOCK(pIntEnv);
|
---|
| 1202 |
|
---|
| 1203 | /*
|
---|
| 1204 | * Sort it, if requested.
|
---|
| 1205 | */
|
---|
| 1206 | if (fSorted)
|
---|
| 1207 | RTSortApvShell((void **)pIntEnv->papszEnv, pIntEnv->cVars, rtEnvSortCompare, pIntEnv);
|
---|
| 1208 |
|
---|
| 1209 | /*
|
---|
| 1210 | * Calculate the size. We add one extra terminator just to be on the safe side.
|
---|
| 1211 | */
|
---|
| 1212 | size_t cbBlock = 2;
|
---|
| 1213 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
| 1214 | cbBlock += strlen(pIntEnv->papszEnv[iVar]) + 1;
|
---|
| 1215 |
|
---|
| 1216 | if (pcbBlock)
|
---|
| 1217 | *pcbBlock = cbBlock - 1;
|
---|
| 1218 |
|
---|
| 1219 | /*
|
---|
| 1220 | * Allocate memory and copy out the variables.
|
---|
| 1221 | */
|
---|
| 1222 | char *pszzBlock;
|
---|
| 1223 | char *pszz = pszzBlock = (char *)RTMemAlloc(cbBlock);
|
---|
| 1224 | if (pszz)
|
---|
| 1225 | {
|
---|
| 1226 | size_t cbLeft = cbBlock;
|
---|
| 1227 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
| 1228 | {
|
---|
| 1229 | size_t cb = strlen(pIntEnv->papszEnv[iVar]) + 1;
|
---|
| 1230 | AssertBreakStmt(cb + 2 <= cbLeft, rc = VERR_INTERNAL_ERROR_3);
|
---|
| 1231 | memcpy(pszz, pIntEnv->papszEnv[iVar], cb);
|
---|
| 1232 | pszz += cb;
|
---|
| 1233 | cbLeft -= cb;
|
---|
| 1234 | }
|
---|
| 1235 | if (RT_SUCCESS(rc))
|
---|
| 1236 | {
|
---|
| 1237 | pszz[0] = '\0';
|
---|
| 1238 | pszz[1] = '\0'; /* The extra one. */
|
---|
| 1239 | }
|
---|
| 1240 | else
|
---|
| 1241 | {
|
---|
| 1242 | RTMemFree(pszzBlock);
|
---|
| 1243 | pszzBlock = NULL;
|
---|
| 1244 | }
|
---|
| 1245 | }
|
---|
| 1246 | else
|
---|
| 1247 | rc = VERR_NO_MEMORY;
|
---|
| 1248 |
|
---|
| 1249 | RTENV_UNLOCK(pIntEnv);
|
---|
| 1250 |
|
---|
| 1251 | if (hClone != NIL_RTENV)
|
---|
| 1252 | RTEnvDestroy(hClone);
|
---|
| 1253 | if (RT_SUCCESS(rc))
|
---|
| 1254 | *ppszzBlock = pszzBlock;
|
---|
| 1255 | return rc;
|
---|
| 1256 | }
|
---|
| 1257 | RT_EXPORT_SYMBOL(RTEnvQueryUtf8Block);
|
---|
| 1258 |
|
---|
| 1259 |
|
---|
| 1260 | RTDECL(void) RTEnvFreeUtf8Block(char *pszzBlock)
|
---|
| 1261 | {
|
---|
[55585] | 1262 | RTMemFree(pszzBlock);
|
---|
[55584] | 1263 | }
|
---|
| 1264 | RT_EXPORT_SYMBOL(RTEnvFreeUtf8Block);
|
---|
| 1265 |
|
---|
| 1266 |
|
---|
[50642] | 1267 | RTDECL(uint32_t) RTEnvCountEx(RTENV hEnv)
|
---|
| 1268 | {
|
---|
| 1269 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
| 1270 | AssertPtrReturn(pIntEnv, UINT32_MAX);
|
---|
| 1271 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, UINT32_MAX);
|
---|
| 1272 |
|
---|
| 1273 | RTENV_LOCK(pIntEnv);
|
---|
| 1274 | uint32_t cVars = (uint32_t)pIntEnv->cVars;
|
---|
| 1275 | RTENV_UNLOCK(pIntEnv);
|
---|
| 1276 |
|
---|
| 1277 | return cVars;
|
---|
| 1278 | }
|
---|
| 1279 | RT_EXPORT_SYMBOL(RTEnvCountEx);
|
---|
| 1280 |
|
---|
| 1281 |
|
---|
[55562] | 1282 | RTDECL(int) RTEnvGetByIndexEx(RTENV hEnv, uint32_t iVar, char *pszVar, size_t cbVar, char *pszValue, size_t cbValue)
|
---|
[50642] | 1283 | {
|
---|
| 1284 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
| 1285 | AssertPtrReturn(pIntEnv, UINT32_MAX);
|
---|
| 1286 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, UINT32_MAX);
|
---|
| 1287 | if (cbVar)
|
---|
| 1288 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
| 1289 | if (cbValue)
|
---|
| 1290 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
| 1291 |
|
---|
| 1292 | RTENV_LOCK(pIntEnv);
|
---|
| 1293 |
|
---|
| 1294 | int rc;
|
---|
| 1295 | if (iVar < pIntEnv->cVars)
|
---|
| 1296 | {
|
---|
| 1297 | const char *pszSrcVar = pIntEnv->papszEnv[iVar];
|
---|
| 1298 | const char *pszSrcValue = strchr(pszSrcVar, '=');
|
---|
[80878] | 1299 | if (pszSrcValue == pszSrcVar && pIntEnv->fFirstEqual)
|
---|
| 1300 | pszSrcValue = strchr(pszSrcVar + 1, '=');
|
---|
[50642] | 1301 | bool fHasEqual = pszSrcValue != NULL;
|
---|
| 1302 | if (pszSrcValue)
|
---|
[55562] | 1303 | {
|
---|
[50642] | 1304 | pszSrcValue++;
|
---|
[55562] | 1305 | rc = VINF_SUCCESS;
|
---|
| 1306 | }
|
---|
[50642] | 1307 | else
|
---|
[55562] | 1308 | {
|
---|
[50642] | 1309 | pszSrcValue = strchr(pszSrcVar, '\0');
|
---|
[55562] | 1310 | rc = VINF_ENV_VAR_UNSET;
|
---|
| 1311 | }
|
---|
[50642] | 1312 | if (cbVar)
|
---|
[55562] | 1313 | {
|
---|
| 1314 | int rc2 = RTStrCopyEx(pszVar, cbVar, pszSrcVar, pszSrcValue - pszSrcVar - fHasEqual);
|
---|
| 1315 | if (RT_FAILURE(rc2))
|
---|
| 1316 | rc = rc2;
|
---|
| 1317 | }
|
---|
[50642] | 1318 | if (cbValue)
|
---|
| 1319 | {
|
---|
| 1320 | int rc2 = RTStrCopy(pszValue, cbValue, pszSrcValue);
|
---|
| 1321 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
| 1322 | rc = rc2;
|
---|
| 1323 | }
|
---|
| 1324 | }
|
---|
| 1325 | else
|
---|
| 1326 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
| 1327 |
|
---|
| 1328 | RTENV_UNLOCK(pIntEnv);
|
---|
| 1329 |
|
---|
| 1330 | return rc;
|
---|
| 1331 | }
|
---|
| 1332 | RT_EXPORT_SYMBOL(RTEnvGetByIndexEx);
|
---|
| 1333 |
|
---|
[55562] | 1334 |
|
---|
| 1335 | RTDECL(const char *) RTEnvGetByIndexRawEx(RTENV hEnv, uint32_t iVar)
|
---|
| 1336 | {
|
---|
| 1337 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
| 1338 | AssertPtrReturn(pIntEnv, NULL);
|
---|
| 1339 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, NULL);
|
---|
| 1340 |
|
---|
| 1341 | RTENV_LOCK(pIntEnv);
|
---|
| 1342 |
|
---|
| 1343 | const char *pszRet;
|
---|
| 1344 | if (iVar < pIntEnv->cVars)
|
---|
| 1345 | pszRet = pIntEnv->papszEnv[iVar];
|
---|
| 1346 | else
|
---|
| 1347 | pszRet = NULL;
|
---|
| 1348 |
|
---|
| 1349 | RTENV_UNLOCK(pIntEnv);
|
---|
| 1350 |
|
---|
| 1351 | return pszRet;
|
---|
| 1352 | }
|
---|
| 1353 | RT_EXPORT_SYMBOL(RTEnvGetByIndexRawEx);
|
---|
| 1354 |
|
---|
| 1355 |
|
---|
| 1356 | RTDECL(int) RTEnvCreateChangeRecord(PRTENV phEnv)
|
---|
| 1357 | {
|
---|
| 1358 | AssertPtrReturn(phEnv, VERR_INVALID_POINTER);
|
---|
[80827] | 1359 | #ifdef RTENV_ALLOW_EQUAL_FIRST_IN_VAR
|
---|
| 1360 | return rtEnvCreate(phEnv, RTENV_GROW_SIZE, true /*fCaseSensitive*/, true /*fPutEnvBlock*/, true /*fFirstEqual*/);
|
---|
| 1361 | #else
|
---|
| 1362 | return rtEnvCreate(phEnv, RTENV_GROW_SIZE, true /*fCaseSensitive*/, true /*fPutEnvBlock*/, false /*fFirstEqual*/);
|
---|
| 1363 | #endif
|
---|
[55562] | 1364 | }
|
---|
| 1365 | RT_EXPORT_SYMBOL(RTEnvCreateChangeRecord);
|
---|
| 1366 |
|
---|
| 1367 |
|
---|
[80827] | 1368 | RTDECL(int) RTEnvCreateChangeRecordEx(PRTENV phEnv, uint32_t fFlags)
|
---|
| 1369 | {
|
---|
| 1370 | AssertPtrReturn(phEnv, VERR_INVALID_POINTER);
|
---|
| 1371 | AssertReturn(!(fFlags & ~RTENV_CREATE_F_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
| 1372 | return rtEnvCreate(phEnv, RTENV_GROW_SIZE, true /*fCaseSensitive*/, true /*fPutEnvBlock*/,
|
---|
| 1373 | RT_BOOL(fFlags & RTENV_CREATE_F_ALLOW_EQUAL_FIRST_IN_VAR));
|
---|
| 1374 | }
|
---|
| 1375 | RT_EXPORT_SYMBOL(RTEnvCreateChangeRecord);
|
---|
| 1376 |
|
---|
| 1377 |
|
---|
[55562] | 1378 | RTDECL(bool) RTEnvIsChangeRecord(RTENV hEnv)
|
---|
| 1379 | {
|
---|
| 1380 | if (hEnv == RTENV_DEFAULT)
|
---|
| 1381 | return false;
|
---|
| 1382 |
|
---|
| 1383 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
| 1384 | AssertPtrReturn(pIntEnv, false);
|
---|
| 1385 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, false);
|
---|
| 1386 | return pIntEnv->fPutEnvBlock;
|
---|
| 1387 | }
|
---|
| 1388 | RT_EXPORT_SYMBOL(RTEnvIsChangeRecord);
|
---|
| 1389 |
|
---|
| 1390 |
|
---|
| 1391 | RTDECL(int) RTEnvApplyChanges(RTENV hEnvDst, RTENV hEnvChanges)
|
---|
| 1392 | {
|
---|
| 1393 | PRTENVINTERNAL pIntEnvChanges = hEnvChanges;
|
---|
| 1394 | AssertPtrReturn(pIntEnvChanges, VERR_INVALID_HANDLE);
|
---|
| 1395 | AssertReturn(pIntEnvChanges->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
| 1396 |
|
---|
| 1397 | /** @todo lock validator trouble ahead here! */
|
---|
| 1398 | RTENV_LOCK(pIntEnvChanges);
|
---|
| 1399 |
|
---|
| 1400 | int rc = VINF_SUCCESS;
|
---|
| 1401 | for (uint32_t iChange = 0; iChange < pIntEnvChanges->cVars && RT_SUCCESS(rc); iChange++)
|
---|
| 1402 | rc = RTEnvPutEx(hEnvDst, pIntEnvChanges->papszEnv[iChange]);
|
---|
| 1403 |
|
---|
| 1404 | RTENV_UNLOCK(pIntEnvChanges);
|
---|
| 1405 |
|
---|
| 1406 | return rc;
|
---|
| 1407 | }
|
---|
| 1408 | RT_EXPORT_SYMBOL(RTEnvApplyChanges);
|
---|
| 1409 |
|
---|