1 | /* $Id: env-generic.cpp 55562 2015-04-30 15:15:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Environment, Generic.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/env.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/alloca.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/sort.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include "internal/magics.h"
|
---|
41 |
|
---|
42 | #include <stdlib.h>
|
---|
43 | #if !defined(RT_OS_WINDOWS)
|
---|
44 | # include <unistd.h>
|
---|
45 | #endif
|
---|
46 | #ifdef RT_OS_DARWIN
|
---|
47 | # include <crt_externs.h>
|
---|
48 | #endif
|
---|
49 | #if defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD)
|
---|
50 | RT_C_DECLS_BEGIN
|
---|
51 | extern char **environ;
|
---|
52 | RT_C_DECLS_END
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | /*******************************************************************************
|
---|
57 | * Defined Constants And Macros *
|
---|
58 | *******************************************************************************/
|
---|
59 | /** The allocation granularity of the RTENVINTERNAL::papszEnv memory. */
|
---|
60 | #define RTENV_GROW_SIZE 16
|
---|
61 |
|
---|
62 | /** Macro that unlocks the specified environment block. */
|
---|
63 | #define RTENV_LOCK(pEnvInt) do { } while (0)
|
---|
64 | /** Macro that unlocks the specified environment block. */
|
---|
65 | #define RTENV_UNLOCK(pEnvInt) do { } while (0)
|
---|
66 |
|
---|
67 | /** @def RTENV_HAVE_WENVIRON
|
---|
68 | * Indicates that we have a _wenviron variable with UTF-16 strings that we
|
---|
69 | * better use instead of the current-cp strings in environ. */
|
---|
70 | #if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
|
---|
71 | # define RTENV_HAVE_WENVIRON 1
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | /** @def RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
75 | * Indicates the RTEnv*Utf8 APIs are implemented. */
|
---|
76 | #if defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
|
---|
77 | # define RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API 1
|
---|
78 | #endif
|
---|
79 |
|
---|
80 |
|
---|
81 | /*******************************************************************************
|
---|
82 | * Structures and Typedefs *
|
---|
83 | *******************************************************************************/
|
---|
84 | /**
|
---|
85 | * The internal representation of a (non-default) environment.
|
---|
86 | */
|
---|
87 | typedef struct RTENVINTERNAL
|
---|
88 | {
|
---|
89 | /** Magic value . */
|
---|
90 | uint32_t u32Magic;
|
---|
91 | /** Set if this is a record of environment changes, putenv style. */
|
---|
92 | bool fPutEnvBlock;
|
---|
93 | /** Number of variables in the array.
|
---|
94 | * This does not include the terminating NULL entry. */
|
---|
95 | size_t cVars;
|
---|
96 | /** Capacity (allocated size) of the array.
|
---|
97 | * This includes space for the terminating NULL element (for compatibility
|
---|
98 | * with the C library), so that c <= cCapacity - 1. */
|
---|
99 | size_t cAllocated;
|
---|
100 | /** Array of environment variables.
|
---|
101 | * These are always in "NAME=VALUE" form, where the value can be empty. If
|
---|
102 | * fPutEnvBlock is set though, there will be "NAME" entries too for variables
|
---|
103 | * that need to be removed when merged with another environment block. */
|
---|
104 | char **papszEnv;
|
---|
105 | /** Array of environment variables in the process CP.
|
---|
106 | * This get (re-)constructed when RTEnvGetExecEnvP method is called. */
|
---|
107 | char **papszEnvOtherCP;
|
---|
108 |
|
---|
109 | /** The compare function we're using. */
|
---|
110 | DECLCALLBACKMEMBER(int, pfnCompare)(const char *psz1, const char *psz2, size_t cchMax);
|
---|
111 | } RTENVINTERNAL, *PRTENVINTERNAL;
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Internal worker that resolves the pointer to the default
|
---|
116 | * process environment. (environ)
|
---|
117 | *
|
---|
118 | * @returns Pointer to the default environment.
|
---|
119 | * This may be NULL.
|
---|
120 | */
|
---|
121 | static const char * const *rtEnvDefault(void)
|
---|
122 | {
|
---|
123 | #ifdef RT_OS_DARWIN
|
---|
124 | return *(_NSGetEnviron());
|
---|
125 | #else
|
---|
126 | return environ;
|
---|
127 | #endif
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Internal worker that creates an environment handle with a specified capacity.
|
---|
133 | *
|
---|
134 | * @returns IPRT status code.
|
---|
135 | * @param ppIntEnv Where to store the result.
|
---|
136 | * @param cAllocated The initial array size.
|
---|
137 | * @param fCaseSensitive Whether the environment block is case sensitive or
|
---|
138 | * not.
|
---|
139 | * @param fPutEnvBlock Indicates whether this is a special environment
|
---|
140 | * block that will be used to record change another
|
---|
141 | * block. We will keep unsets in putenv format, i.e.
|
---|
142 | * just the variable name without any equal sign.
|
---|
143 | */
|
---|
144 | static int rtEnvCreate(PRTENVINTERNAL *ppIntEnv, size_t cAllocated, bool fCaseSensitive, bool fPutEnvBlock)
|
---|
145 | {
|
---|
146 | /*
|
---|
147 | * Allocate environment handle.
|
---|
148 | */
|
---|
149 | PRTENVINTERNAL pIntEnv = (PRTENVINTERNAL)RTMemAlloc(sizeof(*pIntEnv));
|
---|
150 | if (pIntEnv)
|
---|
151 | {
|
---|
152 | /*
|
---|
153 | * Pre-allocate the variable array.
|
---|
154 | */
|
---|
155 | pIntEnv->u32Magic = RTENV_MAGIC;
|
---|
156 | pIntEnv->fPutEnvBlock = fPutEnvBlock;
|
---|
157 | pIntEnv->pfnCompare = fCaseSensitive ? RTStrNCmp : RTStrNICmp;
|
---|
158 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
159 | pIntEnv->cVars = 0;
|
---|
160 | pIntEnv->cAllocated = RT_ALIGN_Z(RT_MAX(cAllocated, RTENV_GROW_SIZE), RTENV_GROW_SIZE);
|
---|
161 | pIntEnv->papszEnv = (char **)RTMemAllocZ(sizeof(pIntEnv->papszEnv[0]) * pIntEnv->cAllocated);
|
---|
162 | if (pIntEnv->papszEnv)
|
---|
163 | {
|
---|
164 | *ppIntEnv = pIntEnv;
|
---|
165 | return VINF_SUCCESS;
|
---|
166 | }
|
---|
167 |
|
---|
168 | RTMemFree(pIntEnv);
|
---|
169 | }
|
---|
170 |
|
---|
171 | return VERR_NO_MEMORY;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | RTDECL(int) RTEnvCreate(PRTENV pEnv)
|
---|
176 | {
|
---|
177 | AssertPtrReturn(pEnv, VERR_INVALID_POINTER);
|
---|
178 | return rtEnvCreate(pEnv, RTENV_GROW_SIZE, false /*fCaseSensitive*/, false /*fPutEnvBlock*/);
|
---|
179 | }
|
---|
180 | RT_EXPORT_SYMBOL(RTEnvCreate);
|
---|
181 |
|
---|
182 |
|
---|
183 | RTDECL(int) RTEnvDestroy(RTENV Env)
|
---|
184 | {
|
---|
185 | /*
|
---|
186 | * Ignore NIL_RTENV and validate input.
|
---|
187 | */
|
---|
188 | if ( Env == NIL_RTENV
|
---|
189 | || Env == RTENV_DEFAULT)
|
---|
190 | return VINF_SUCCESS;
|
---|
191 |
|
---|
192 | PRTENVINTERNAL pIntEnv = Env;
|
---|
193 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
194 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Do the cleanup.
|
---|
198 | */
|
---|
199 | RTENV_LOCK(pIntEnv);
|
---|
200 | pIntEnv->u32Magic++;
|
---|
201 | size_t iVar = pIntEnv->cVars;
|
---|
202 | while (iVar-- > 0)
|
---|
203 | RTStrFree(pIntEnv->papszEnv[iVar]);
|
---|
204 | RTMemFree(pIntEnv->papszEnv);
|
---|
205 | pIntEnv->papszEnv = NULL;
|
---|
206 |
|
---|
207 | if (pIntEnv->papszEnvOtherCP)
|
---|
208 | {
|
---|
209 | for (iVar = 0; pIntEnv->papszEnvOtherCP[iVar]; iVar++)
|
---|
210 | {
|
---|
211 | RTStrFree(pIntEnv->papszEnvOtherCP[iVar]);
|
---|
212 | pIntEnv->papszEnvOtherCP[iVar] = NULL;
|
---|
213 | }
|
---|
214 | RTMemFree(pIntEnv->papszEnvOtherCP);
|
---|
215 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
216 | }
|
---|
217 |
|
---|
218 | RTENV_UNLOCK(pIntEnv);
|
---|
219 | /*RTCritSectDelete(&pIntEnv->CritSect) */
|
---|
220 | RTMemFree(pIntEnv);
|
---|
221 |
|
---|
222 | return VINF_SUCCESS;
|
---|
223 | }
|
---|
224 | RT_EXPORT_SYMBOL(RTEnvDestroy);
|
---|
225 |
|
---|
226 |
|
---|
227 | RTDECL(int) RTEnvClone(PRTENV pEnv, RTENV EnvToClone)
|
---|
228 | {
|
---|
229 | /*
|
---|
230 | * Validate input and figure out how many variable to clone and where to get them.
|
---|
231 | */
|
---|
232 | bool fCaseSensitive = true;
|
---|
233 | bool fPutEnvBlock = false;
|
---|
234 | size_t cVars;
|
---|
235 | const char * const *papszEnv;
|
---|
236 | #ifdef RTENV_HAVE_WENVIRON
|
---|
237 | PCRTUTF16 const * papwszEnv;
|
---|
238 | #endif
|
---|
239 | PRTENVINTERNAL pIntEnvToClone;
|
---|
240 | AssertPtrReturn(pEnv, VERR_INVALID_POINTER);
|
---|
241 | if (EnvToClone == RTENV_DEFAULT)
|
---|
242 | {
|
---|
243 | cVars = 0;
|
---|
244 | pIntEnvToClone = NULL;
|
---|
245 | #ifdef RTENV_HAVE_WENVIRON
|
---|
246 | papszEnv = NULL;
|
---|
247 | papwszEnv = (PCRTUTF16 * const)_wenviron;
|
---|
248 | if (!papwszEnv)
|
---|
249 | {
|
---|
250 | _wgetenv(L"Path"); /* Force the CRT to initalize it. */
|
---|
251 | papwszEnv = (PCRTUTF16 * const)_wenviron;
|
---|
252 | }
|
---|
253 | if (papwszEnv)
|
---|
254 | while (papwszEnv[cVars])
|
---|
255 | cVars++;
|
---|
256 | #else
|
---|
257 | papszEnv = rtEnvDefault();
|
---|
258 | if (papszEnv)
|
---|
259 | while (papszEnv[cVars])
|
---|
260 | cVars++;
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
264 | /* DOS systems was case insensitive. A prime example is the 'Path'
|
---|
265 | variable on windows which turns into the 'PATH' variable. */
|
---|
266 | fCaseSensitive = false;
|
---|
267 | #endif
|
---|
268 | }
|
---|
269 | else
|
---|
270 | {
|
---|
271 | pIntEnvToClone = EnvToClone;
|
---|
272 | AssertPtrReturn(pIntEnvToClone, VERR_INVALID_HANDLE);
|
---|
273 | AssertReturn(pIntEnvToClone->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
274 | RTENV_LOCK(pIntEnvToClone);
|
---|
275 |
|
---|
276 | fPutEnvBlock = pIntEnvToClone->fPutEnvBlock;
|
---|
277 | papszEnv = pIntEnvToClone->papszEnv;
|
---|
278 | cVars = pIntEnvToClone->cVars;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Create the duplicate.
|
---|
283 | */
|
---|
284 | PRTENVINTERNAL pIntEnv;
|
---|
285 | int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */, fCaseSensitive, fPutEnvBlock);
|
---|
286 | if (RT_SUCCESS(rc))
|
---|
287 | {
|
---|
288 | pIntEnv->cVars = cVars;
|
---|
289 | pIntEnv->papszEnv[pIntEnv->cVars] = NULL;
|
---|
290 | if (EnvToClone == RTENV_DEFAULT)
|
---|
291 | {
|
---|
292 | /* ASSUMES the default environment is in the current codepage. */
|
---|
293 | size_t iDst = 0;
|
---|
294 | for (size_t iSrc = 0; iSrc < cVars; iSrc++)
|
---|
295 | {
|
---|
296 | #ifdef RTENV_HAVE_WENVIRON
|
---|
297 | int rc2 = RTUtf16ToUtf8(papwszEnv[iSrc], &pIntEnv->papszEnv[iDst]);
|
---|
298 | #else
|
---|
299 | int rc2 = RTStrCurrentCPToUtf8(&pIntEnv->papszEnv[iDst], papszEnv[iSrc]);
|
---|
300 | #endif
|
---|
301 | if (RT_SUCCESS(rc2))
|
---|
302 | {
|
---|
303 | /* Make sure it contains an '='. */
|
---|
304 | iDst++;
|
---|
305 | if (strchr(pIntEnv->papszEnv[iDst - 1], '='))
|
---|
306 | continue;
|
---|
307 | rc2 = RTStrAAppend(&pIntEnv->papszEnv[iDst - 1], "=");
|
---|
308 | if (RT_SUCCESS(rc2))
|
---|
309 | continue;
|
---|
310 | }
|
---|
311 | else if (rc2 == VERR_NO_TRANSLATION)
|
---|
312 | {
|
---|
313 | rc = VWRN_ENV_NOT_FULLY_TRANSLATED;
|
---|
314 | continue;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /* failed fatally. */
|
---|
318 | pIntEnv->cVars = iDst;
|
---|
319 | RTEnvDestroy(pIntEnv);
|
---|
320 | return rc2;
|
---|
321 | }
|
---|
322 | pIntEnv->cVars = iDst;
|
---|
323 | }
|
---|
324 | else
|
---|
325 | {
|
---|
326 | for (size_t iVar = 0; iVar < cVars; iVar++)
|
---|
327 | {
|
---|
328 | char *pszVar = RTStrDup(papszEnv[iVar]);
|
---|
329 | if (RT_UNLIKELY(!pszVar))
|
---|
330 | {
|
---|
331 | RTENV_UNLOCK(pIntEnvToClone);
|
---|
332 |
|
---|
333 | pIntEnv->cVars = iVar;
|
---|
334 | RTEnvDestroy(pIntEnv);
|
---|
335 | return VERR_NO_STR_MEMORY;
|
---|
336 | }
|
---|
337 | pIntEnv->papszEnv[iVar] = pszVar;
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* done */
|
---|
342 | *pEnv = pIntEnv;
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (pIntEnvToClone)
|
---|
346 | RTENV_UNLOCK(pIntEnvToClone);
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 | RT_EXPORT_SYMBOL(RTEnvClone);
|
---|
350 |
|
---|
351 |
|
---|
352 | RTDECL(int) RTEnvPutEx(RTENV Env, const char *pszVarEqualValue)
|
---|
353 | {
|
---|
354 | int rc;
|
---|
355 | AssertPtrReturn(pszVarEqualValue, VERR_INVALID_POINTER);
|
---|
356 | const char *pszEq = strchr(pszVarEqualValue, '=');
|
---|
357 | if (!pszEq)
|
---|
358 | rc = RTEnvUnsetEx(Env, pszVarEqualValue);
|
---|
359 | else
|
---|
360 | {
|
---|
361 | /*
|
---|
362 | * Make a copy of the variable name so we can terminate it
|
---|
363 | * properly and then pass the request on to RTEnvSetEx.
|
---|
364 | */
|
---|
365 | const char *pszValue = pszEq + 1;
|
---|
366 |
|
---|
367 | size_t cchVar = pszEq - pszVarEqualValue;
|
---|
368 | Assert(cchVar < 1024);
|
---|
369 | char *pszVar = (char *)alloca(cchVar + 1);
|
---|
370 | memcpy(pszVar, pszVarEqualValue, cchVar);
|
---|
371 | pszVar[cchVar] = '\0';
|
---|
372 |
|
---|
373 | rc = RTEnvSetEx(Env, pszVar, pszValue);
|
---|
374 | }
|
---|
375 | return rc;
|
---|
376 | }
|
---|
377 | RT_EXPORT_SYMBOL(RTEnvPutEx);
|
---|
378 |
|
---|
379 |
|
---|
380 | RTDECL(int) RTEnvSetEx(RTENV Env, const char *pszVar, const char *pszValue)
|
---|
381 | {
|
---|
382 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
383 | AssertReturn(*pszVar, VERR_INVALID_PARAMETER);
|
---|
384 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
385 | AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
386 |
|
---|
387 | int rc;
|
---|
388 | if (Env == RTENV_DEFAULT)
|
---|
389 | {
|
---|
390 | #ifdef RT_OS_WINDOWS
|
---|
391 | rc = RTEnvSetUtf8(pszVar, pszValue);
|
---|
392 | #else
|
---|
393 | /*
|
---|
394 | * Since RTEnvPut isn't UTF-8 clean and actually expects the strings
|
---|
395 | * to be in the current code page (codeset), we'll do the necessary
|
---|
396 | * conversions here.
|
---|
397 | */
|
---|
398 | char *pszVarOtherCP;
|
---|
399 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
400 | if (RT_SUCCESS(rc))
|
---|
401 | {
|
---|
402 | char *pszValueOtherCP;
|
---|
403 | rc = RTStrUtf8ToCurrentCP(&pszValueOtherCP, pszValue);
|
---|
404 | if (RT_SUCCESS(rc))
|
---|
405 | {
|
---|
406 | rc = RTEnvSet(pszVarOtherCP, pszValueOtherCP);
|
---|
407 | RTStrFree(pszValueOtherCP);
|
---|
408 | }
|
---|
409 | RTStrFree(pszVarOtherCP);
|
---|
410 | }
|
---|
411 | #endif
|
---|
412 | }
|
---|
413 | else
|
---|
414 | {
|
---|
415 | PRTENVINTERNAL pIntEnv = Env;
|
---|
416 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
417 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
418 |
|
---|
419 | /*
|
---|
420 | * Create the variable string.
|
---|
421 | */
|
---|
422 | const size_t cchVar = strlen(pszVar);
|
---|
423 | const size_t cchValue = strlen(pszValue);
|
---|
424 | char *pszEntry = (char *)RTMemAlloc(cchVar + cchValue + 2);
|
---|
425 | if (pszEntry)
|
---|
426 | {
|
---|
427 | memcpy(pszEntry, pszVar, cchVar);
|
---|
428 | pszEntry[cchVar] = '=';
|
---|
429 | memcpy(&pszEntry[cchVar + 1], pszValue, cchValue + 1);
|
---|
430 |
|
---|
431 | RTENV_LOCK(pIntEnv);
|
---|
432 |
|
---|
433 | /*
|
---|
434 | * Find the location of the variable. (iVar = cVars if new)
|
---|
435 | */
|
---|
436 | rc = VINF_SUCCESS;
|
---|
437 | size_t iVar;
|
---|
438 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
439 | if ( !pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pszVar, cchVar)
|
---|
440 | && ( pIntEnv->papszEnv[iVar][cchVar] == '='
|
---|
441 | || pIntEnv->papszEnv[iVar][cchVar] == '\0') )
|
---|
442 | break;
|
---|
443 | if (iVar < pIntEnv->cVars)
|
---|
444 | {
|
---|
445 | /*
|
---|
446 | * Replace the current entry. Simple.
|
---|
447 | */
|
---|
448 | RTMemFree(pIntEnv->papszEnv[iVar]);
|
---|
449 | pIntEnv->papszEnv[iVar] = pszEntry;
|
---|
450 | }
|
---|
451 | else
|
---|
452 | {
|
---|
453 | /*
|
---|
454 | * Adding a new variable. Resize the array if required
|
---|
455 | * and then insert the new value at the end.
|
---|
456 | */
|
---|
457 | if (pIntEnv->cVars + 2 > pIntEnv->cAllocated)
|
---|
458 | {
|
---|
459 | void *pvNew = RTMemRealloc(pIntEnv->papszEnv, sizeof(char *) * (pIntEnv->cAllocated + RTENV_GROW_SIZE));
|
---|
460 | if (!pvNew)
|
---|
461 | rc = VERR_NO_MEMORY;
|
---|
462 | else
|
---|
463 | {
|
---|
464 | pIntEnv->papszEnv = (char **)pvNew;
|
---|
465 | pIntEnv->cAllocated += RTENV_GROW_SIZE;
|
---|
466 | for (size_t iNewVar = pIntEnv->cVars; iNewVar < pIntEnv->cAllocated; iNewVar++)
|
---|
467 | pIntEnv->papszEnv[iNewVar] = NULL;
|
---|
468 | }
|
---|
469 | }
|
---|
470 | if (RT_SUCCESS(rc))
|
---|
471 | {
|
---|
472 | pIntEnv->papszEnv[iVar] = pszEntry;
|
---|
473 | pIntEnv->papszEnv[iVar + 1] = NULL; /* this isn't really necessary, but doesn't hurt. */
|
---|
474 | pIntEnv->cVars++;
|
---|
475 | Assert(pIntEnv->cVars == iVar + 1);
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | RTENV_UNLOCK(pIntEnv);
|
---|
480 |
|
---|
481 | if (RT_FAILURE(rc))
|
---|
482 | RTMemFree(pszEntry);
|
---|
483 | }
|
---|
484 | else
|
---|
485 | rc = VERR_NO_MEMORY;
|
---|
486 | }
|
---|
487 | return rc;
|
---|
488 | }
|
---|
489 | RT_EXPORT_SYMBOL(RTEnvSetEx);
|
---|
490 |
|
---|
491 |
|
---|
492 | RTDECL(int) RTEnvUnsetEx(RTENV Env, const char *pszVar)
|
---|
493 | {
|
---|
494 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
495 | AssertReturn(*pszVar, VERR_INVALID_PARAMETER);
|
---|
496 | AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
497 |
|
---|
498 | int rc;
|
---|
499 | if (Env == RTENV_DEFAULT)
|
---|
500 | {
|
---|
501 | #ifdef RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
502 | rc = RTEnvUnsetUtf8(pszVar);
|
---|
503 | #else
|
---|
504 | /*
|
---|
505 | * Since RTEnvUnset isn't UTF-8 clean and actually expects the strings
|
---|
506 | * to be in the current code page (codeset), we'll do the necessary
|
---|
507 | * conversions here.
|
---|
508 | */
|
---|
509 | char *pszVarOtherCP;
|
---|
510 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
511 | if (RT_SUCCESS(rc))
|
---|
512 | {
|
---|
513 | rc = RTEnvUnset(pszVarOtherCP);
|
---|
514 | RTStrFree(pszVarOtherCP);
|
---|
515 | }
|
---|
516 | #endif
|
---|
517 | }
|
---|
518 | else
|
---|
519 | {
|
---|
520 | PRTENVINTERNAL pIntEnv = Env;
|
---|
521 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
522 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
523 |
|
---|
524 | RTENV_LOCK(pIntEnv);
|
---|
525 |
|
---|
526 | /*
|
---|
527 | * Remove all variable by the given name.
|
---|
528 | */
|
---|
529 | rc = VINF_ENV_VAR_NOT_FOUND;
|
---|
530 | const size_t cchVar = strlen(pszVar);
|
---|
531 | size_t iVar;
|
---|
532 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
533 | if ( !pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pszVar, cchVar)
|
---|
534 | && ( pIntEnv->papszEnv[iVar][cchVar] == '='
|
---|
535 | || pIntEnv->papszEnv[iVar][cchVar] == '\0') )
|
---|
536 | {
|
---|
537 | if (!pIntEnv->fPutEnvBlock)
|
---|
538 | {
|
---|
539 | RTMemFree(pIntEnv->papszEnv[iVar]);
|
---|
540 | pIntEnv->cVars--;
|
---|
541 | if (pIntEnv->cVars > 0)
|
---|
542 | pIntEnv->papszEnv[iVar] = pIntEnv->papszEnv[pIntEnv->cVars];
|
---|
543 | pIntEnv->papszEnv[pIntEnv->cVars] = NULL;
|
---|
544 | }
|
---|
545 | else
|
---|
546 | {
|
---|
547 | /* Record this unset by keeping the variable without any equal sign. */
|
---|
548 | pIntEnv->papszEnv[iVar][cchVar] = '\0';
|
---|
549 | }
|
---|
550 | rc = VINF_SUCCESS;
|
---|
551 | /* no break, there could be more. */
|
---|
552 | }
|
---|
553 |
|
---|
554 | RTENV_UNLOCK(pIntEnv);
|
---|
555 | }
|
---|
556 | return rc;
|
---|
557 |
|
---|
558 | }
|
---|
559 | RT_EXPORT_SYMBOL(RTEnvUnsetEx);
|
---|
560 |
|
---|
561 |
|
---|
562 | RTDECL(int) RTEnvGetEx(RTENV Env, const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
|
---|
563 | {
|
---|
564 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
565 | AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER);
|
---|
566 | AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER);
|
---|
567 | AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER);
|
---|
568 | AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
|
---|
569 |
|
---|
570 | if (pcchActual)
|
---|
571 | *pcchActual = 0;
|
---|
572 | int rc;
|
---|
573 | if (Env == RTENV_DEFAULT)
|
---|
574 | {
|
---|
575 | #ifdef RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
576 | rc = RTEnvGetUtf8(pszVar, pszValue, cbValue, pcchActual);
|
---|
577 | #else
|
---|
578 | /*
|
---|
579 | * Since RTEnvGet isn't UTF-8 clean and actually expects the strings
|
---|
580 | * to be in the current code page (codeset), we'll do the necessary
|
---|
581 | * conversions here.
|
---|
582 | */
|
---|
583 | char *pszVarOtherCP;
|
---|
584 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
585 | if (RT_SUCCESS(rc))
|
---|
586 | {
|
---|
587 | const char *pszValueOtherCP = RTEnvGet(pszVarOtherCP);
|
---|
588 | RTStrFree(pszVarOtherCP);
|
---|
589 | if (pszValueOtherCP)
|
---|
590 | {
|
---|
591 | char *pszValueUtf8;
|
---|
592 | rc = RTStrCurrentCPToUtf8(&pszValueUtf8, pszValueOtherCP);
|
---|
593 | if (RT_SUCCESS(rc))
|
---|
594 | {
|
---|
595 | rc = VINF_SUCCESS;
|
---|
596 | size_t cch = strlen(pszValueUtf8);
|
---|
597 | if (pcchActual)
|
---|
598 | *pcchActual = cch;
|
---|
599 | if (pszValue && cbValue)
|
---|
600 | {
|
---|
601 | if (cch < cbValue)
|
---|
602 | memcpy(pszValue, pszValueUtf8, cch + 1);
|
---|
603 | else
|
---|
604 | rc = VERR_BUFFER_OVERFLOW;
|
---|
605 | }
|
---|
606 | RTStrFree(pszValueUtf8);
|
---|
607 | }
|
---|
608 | }
|
---|
609 | else
|
---|
610 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
611 | }
|
---|
612 | #endif
|
---|
613 | }
|
---|
614 | else
|
---|
615 | {
|
---|
616 | PRTENVINTERNAL pIntEnv = Env;
|
---|
617 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
618 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
619 |
|
---|
620 | RTENV_LOCK(pIntEnv);
|
---|
621 |
|
---|
622 | /*
|
---|
623 | * Locate the first variable and return it to the caller.
|
---|
624 | */
|
---|
625 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
626 | const size_t cchVar = strlen(pszVar);
|
---|
627 | size_t iVar;
|
---|
628 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
629 | if (!pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pszVar, cchVar))
|
---|
630 | {
|
---|
631 | if (pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
632 | {
|
---|
633 | rc = VINF_SUCCESS;
|
---|
634 | const char *pszValueOrg = pIntEnv->papszEnv[iVar] + cchVar + 1;
|
---|
635 | size_t cch = strlen(pszValueOrg);
|
---|
636 | if (pcchActual)
|
---|
637 | *pcchActual = cch;
|
---|
638 | if (pszValue && cbValue)
|
---|
639 | {
|
---|
640 | if (cch < cbValue)
|
---|
641 | memcpy(pszValue, pszValueOrg, cch + 1);
|
---|
642 | else
|
---|
643 | rc = VERR_BUFFER_OVERFLOW;
|
---|
644 | }
|
---|
645 | break;
|
---|
646 | }
|
---|
647 | if (pIntEnv->papszEnv[iVar][cchVar] == '\0')
|
---|
648 | {
|
---|
649 | Assert(pIntEnv->fPutEnvBlock);
|
---|
650 | rc = VERR_ENV_VAR_UNSET;
|
---|
651 | break;
|
---|
652 | }
|
---|
653 | }
|
---|
654 |
|
---|
655 | RTENV_UNLOCK(pIntEnv);
|
---|
656 | }
|
---|
657 | return rc;
|
---|
658 | }
|
---|
659 | RT_EXPORT_SYMBOL(RTEnvGetEx);
|
---|
660 |
|
---|
661 |
|
---|
662 | RTDECL(bool) RTEnvExistEx(RTENV Env, const char *pszVar)
|
---|
663 | {
|
---|
664 | AssertPtrReturn(pszVar, false);
|
---|
665 |
|
---|
666 | bool fExists = false;
|
---|
667 | if (Env == RTENV_DEFAULT)
|
---|
668 | {
|
---|
669 | #ifdef RTENV_IMPLEMENTS_UTF8_DEFAULT_ENV_API
|
---|
670 | fExists = RTEnvExistsUtf8(pszVar);
|
---|
671 | #else
|
---|
672 | /*
|
---|
673 | * Since RTEnvExist isn't UTF-8 clean and actually expects the strings
|
---|
674 | * to be in the current code page (codeset), we'll do the necessary
|
---|
675 | * conversions here.
|
---|
676 | */
|
---|
677 | char *pszVarOtherCP;
|
---|
678 | int rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
679 | if (RT_SUCCESS(rc))
|
---|
680 | {
|
---|
681 | fExists = RTEnvExist(pszVarOtherCP);
|
---|
682 | RTStrFree(pszVarOtherCP);
|
---|
683 | }
|
---|
684 | #endif
|
---|
685 | }
|
---|
686 | else
|
---|
687 | {
|
---|
688 | PRTENVINTERNAL pIntEnv = Env;
|
---|
689 | AssertPtrReturn(pIntEnv, false);
|
---|
690 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, false);
|
---|
691 |
|
---|
692 | RTENV_LOCK(pIntEnv);
|
---|
693 |
|
---|
694 | /*
|
---|
695 | * Simple search.
|
---|
696 | */
|
---|
697 | const size_t cchVar = strlen(pszVar);
|
---|
698 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
699 | if (!pIntEnv->pfnCompare(pIntEnv->papszEnv[iVar], pszVar, cchVar))
|
---|
700 | {
|
---|
701 | if (pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
702 | {
|
---|
703 | fExists = true;
|
---|
704 | break;
|
---|
705 | }
|
---|
706 | if (pIntEnv->papszEnv[iVar][cchVar] == '\0')
|
---|
707 | break;
|
---|
708 | }
|
---|
709 |
|
---|
710 | RTENV_UNLOCK(pIntEnv);
|
---|
711 | }
|
---|
712 | return fExists;
|
---|
713 | }
|
---|
714 | RT_EXPORT_SYMBOL(RTEnvExistEx);
|
---|
715 |
|
---|
716 |
|
---|
717 | RTDECL(char const * const *) RTEnvGetExecEnvP(RTENV Env)
|
---|
718 | {
|
---|
719 | const char * const *papszRet;
|
---|
720 | if (Env == RTENV_DEFAULT)
|
---|
721 | {
|
---|
722 | /** @todo fix this API it's fundamentally wrong! */
|
---|
723 | papszRet = rtEnvDefault();
|
---|
724 | if (!papszRet)
|
---|
725 | {
|
---|
726 | static const char * const s_papszDummy[2] = { NULL, NULL };
|
---|
727 | papszRet = &s_papszDummy[0];
|
---|
728 | }
|
---|
729 | }
|
---|
730 | else
|
---|
731 | {
|
---|
732 | PRTENVINTERNAL pIntEnv = Env;
|
---|
733 | AssertPtrReturn(pIntEnv, NULL);
|
---|
734 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, NULL);
|
---|
735 |
|
---|
736 | RTENV_LOCK(pIntEnv);
|
---|
737 |
|
---|
738 | /*
|
---|
739 | * Free any old envp.
|
---|
740 | */
|
---|
741 | if (pIntEnv->papszEnvOtherCP)
|
---|
742 | {
|
---|
743 | for (size_t iVar = 0; pIntEnv->papszEnvOtherCP[iVar]; iVar++)
|
---|
744 | {
|
---|
745 | RTStrFree(pIntEnv->papszEnvOtherCP[iVar]);
|
---|
746 | pIntEnv->papszEnvOtherCP[iVar] = NULL;
|
---|
747 | }
|
---|
748 | RTMemFree(pIntEnv->papszEnvOtherCP);
|
---|
749 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
750 | }
|
---|
751 |
|
---|
752 | /*
|
---|
753 | * Construct a new envp with the strings in the process code set.
|
---|
754 | */
|
---|
755 | char **papsz;
|
---|
756 | papszRet = pIntEnv->papszEnvOtherCP = papsz = (char **)RTMemAlloc(sizeof(char *) * (pIntEnv->cVars + 1));
|
---|
757 | if (papsz)
|
---|
758 | {
|
---|
759 | papsz[pIntEnv->cVars] = NULL;
|
---|
760 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
761 | {
|
---|
762 | int rc = RTStrUtf8ToCurrentCP(&papsz[iVar], pIntEnv->papszEnv[iVar]);
|
---|
763 | if (RT_FAILURE(rc))
|
---|
764 | {
|
---|
765 | /* RTEnvDestroy / we cleans up later. */
|
---|
766 | papsz[iVar] = NULL;
|
---|
767 | AssertRC(rc);
|
---|
768 | papszRet = NULL;
|
---|
769 | break;
|
---|
770 | }
|
---|
771 | }
|
---|
772 | }
|
---|
773 |
|
---|
774 | RTENV_UNLOCK(pIntEnv);
|
---|
775 | }
|
---|
776 | return papszRet;
|
---|
777 | }
|
---|
778 | RT_EXPORT_SYMBOL(RTEnvGetExecEnvP);
|
---|
779 |
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * RTSort callback for comparing two environment variables.
|
---|
783 | *
|
---|
784 | * @returns -1, 0, 1. See PFNRTSORTCMP.
|
---|
785 | * @param pvElement1 Variable 1.
|
---|
786 | * @param pvElement2 Variable 2.
|
---|
787 | * @param pvUser Ignored.
|
---|
788 | */
|
---|
789 | static DECLCALLBACK(int) rtEnvSortCompare(const void *pvElement1, const void *pvElement2, void *pvUser)
|
---|
790 | {
|
---|
791 | NOREF(pvUser);
|
---|
792 | int iDiff = strcmp((const char *)pvElement1, (const char *)pvElement2);
|
---|
793 | if (iDiff < 0)
|
---|
794 | iDiff = -1;
|
---|
795 | else if (iDiff > 0)
|
---|
796 | iDiff = 1;
|
---|
797 | return iDiff;
|
---|
798 | }
|
---|
799 |
|
---|
800 |
|
---|
801 | RTDECL(int) RTEnvQueryUtf16Block(RTENV hEnv, PRTUTF16 *ppwszzBlock)
|
---|
802 | {
|
---|
803 | RTENV hClone = NIL_RTENV;
|
---|
804 | PRTENVINTERNAL pIntEnv;
|
---|
805 | int rc;
|
---|
806 |
|
---|
807 | /*
|
---|
808 | * Validate / simplify input.
|
---|
809 | */
|
---|
810 | if (hEnv == RTENV_DEFAULT)
|
---|
811 | {
|
---|
812 | rc = RTEnvClone(&hClone, RTENV_DEFAULT);
|
---|
813 | if (RT_FAILURE(rc))
|
---|
814 | return rc;
|
---|
815 | pIntEnv = hClone;
|
---|
816 | }
|
---|
817 | else
|
---|
818 | {
|
---|
819 | pIntEnv = hEnv;
|
---|
820 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
821 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
822 | rc = VINF_SUCCESS;
|
---|
823 | }
|
---|
824 |
|
---|
825 | RTENV_LOCK(pIntEnv);
|
---|
826 |
|
---|
827 | /*
|
---|
828 | * Sort it first.
|
---|
829 | */
|
---|
830 | RTSortApvShell((void **)pIntEnv->papszEnv, pIntEnv->cVars, rtEnvSortCompare, pIntEnv);
|
---|
831 |
|
---|
832 | /*
|
---|
833 | * Calculate the size.
|
---|
834 | */
|
---|
835 | size_t cwc;
|
---|
836 | size_t cwcTotal = 2;
|
---|
837 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
838 | {
|
---|
839 | rc = RTStrCalcUtf16LenEx(pIntEnv->papszEnv[iVar], RTSTR_MAX, &cwc);
|
---|
840 | AssertRCBreak(rc);
|
---|
841 | cwcTotal += cwc + 1;
|
---|
842 | }
|
---|
843 |
|
---|
844 | PRTUTF16 pwszzBlock = NULL;
|
---|
845 | if (RT_SUCCESS(rc))
|
---|
846 | {
|
---|
847 | /*
|
---|
848 | * Perform the conversion.
|
---|
849 | */
|
---|
850 | PRTUTF16 pwszz = pwszzBlock = (PRTUTF16)RTMemAlloc(cwcTotal * sizeof(RTUTF16));
|
---|
851 | if (pwszz)
|
---|
852 | {
|
---|
853 | size_t cwcLeft = cwcTotal;
|
---|
854 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
855 | {
|
---|
856 | rc = RTStrToUtf16Ex(pIntEnv->papszEnv[iVar], RTSTR_MAX,
|
---|
857 | &pwszz, cwcTotal - (pwszz - pwszzBlock), &cwc);
|
---|
858 | AssertRCBreak(rc);
|
---|
859 | pwszz += cwc + 1;
|
---|
860 | cwcLeft -= cwc + 1;
|
---|
861 | AssertBreakStmt(cwcLeft >= 2, rc = VERR_INTERNAL_ERROR_3);
|
---|
862 | }
|
---|
863 | AssertStmt(cwcLeft == 2 || RT_FAILURE(rc), rc = VERR_INTERNAL_ERROR_2);
|
---|
864 | if (RT_SUCCESS(rc))
|
---|
865 | {
|
---|
866 | pwszz[0] = '\0';
|
---|
867 | pwszz[1] = '\0';
|
---|
868 | }
|
---|
869 | else
|
---|
870 | {
|
---|
871 | RTMemFree(pwszzBlock);
|
---|
872 | pwszzBlock = NULL;
|
---|
873 | }
|
---|
874 | }
|
---|
875 | else
|
---|
876 | rc = VERR_NO_MEMORY;
|
---|
877 | }
|
---|
878 |
|
---|
879 | RTENV_UNLOCK(pIntEnv);
|
---|
880 |
|
---|
881 | if (hClone != NIL_RTENV)
|
---|
882 | RTEnvDestroy(hClone);
|
---|
883 | if (RT_SUCCESS(rc))
|
---|
884 | *ppwszzBlock = pwszzBlock;
|
---|
885 | return rc;
|
---|
886 | }
|
---|
887 | RT_EXPORT_SYMBOL(RTEnvGetExecEnvP);
|
---|
888 |
|
---|
889 |
|
---|
890 | RTDECL(void) RTEnvFreeUtf16Block(PRTUTF16 pwszzBlock)
|
---|
891 | {
|
---|
892 | RTMemFree(pwszzBlock);
|
---|
893 | }
|
---|
894 | RT_EXPORT_SYMBOL(RTEnvFreeUtf16Block);
|
---|
895 |
|
---|
896 |
|
---|
897 | RTDECL(uint32_t) RTEnvCountEx(RTENV hEnv)
|
---|
898 | {
|
---|
899 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
900 | AssertPtrReturn(pIntEnv, UINT32_MAX);
|
---|
901 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, UINT32_MAX);
|
---|
902 |
|
---|
903 | RTENV_LOCK(pIntEnv);
|
---|
904 | uint32_t cVars = (uint32_t)pIntEnv->cVars;
|
---|
905 | RTENV_UNLOCK(pIntEnv);
|
---|
906 |
|
---|
907 | return cVars;
|
---|
908 | }
|
---|
909 | RT_EXPORT_SYMBOL(RTEnvCountEx);
|
---|
910 |
|
---|
911 |
|
---|
912 | RTDECL(int) RTEnvGetByIndexEx(RTENV hEnv, uint32_t iVar, char *pszVar, size_t cbVar, char *pszValue, size_t cbValue)
|
---|
913 | {
|
---|
914 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
915 | AssertPtrReturn(pIntEnv, UINT32_MAX);
|
---|
916 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, UINT32_MAX);
|
---|
917 | if (cbVar)
|
---|
918 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
919 | if (cbValue)
|
---|
920 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
921 |
|
---|
922 | RTENV_LOCK(pIntEnv);
|
---|
923 |
|
---|
924 | int rc;
|
---|
925 | if (iVar < pIntEnv->cVars)
|
---|
926 | {
|
---|
927 | const char *pszSrcVar = pIntEnv->papszEnv[iVar];
|
---|
928 | const char *pszSrcValue = strchr(pszSrcVar, '=');
|
---|
929 | bool fHasEqual = pszSrcValue != NULL;
|
---|
930 | if (pszSrcValue)
|
---|
931 | {
|
---|
932 | pszSrcValue++;
|
---|
933 | rc = VINF_SUCCESS;
|
---|
934 | }
|
---|
935 | else
|
---|
936 | {
|
---|
937 | pszSrcValue = strchr(pszSrcVar, '\0');
|
---|
938 | rc = VINF_ENV_VAR_UNSET;
|
---|
939 | }
|
---|
940 | if (cbVar)
|
---|
941 | {
|
---|
942 | int rc2 = RTStrCopyEx(pszVar, cbVar, pszSrcVar, pszSrcValue - pszSrcVar - fHasEqual);
|
---|
943 | if (RT_FAILURE(rc2))
|
---|
944 | rc = rc2;
|
---|
945 | }
|
---|
946 | if (cbValue)
|
---|
947 | {
|
---|
948 | int rc2 = RTStrCopy(pszValue, cbValue, pszSrcValue);
|
---|
949 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
950 | rc = rc2;
|
---|
951 | }
|
---|
952 | }
|
---|
953 | else
|
---|
954 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
955 |
|
---|
956 | RTENV_UNLOCK(pIntEnv);
|
---|
957 |
|
---|
958 | return rc;
|
---|
959 | }
|
---|
960 | RT_EXPORT_SYMBOL(RTEnvGetByIndexEx);
|
---|
961 |
|
---|
962 |
|
---|
963 | RTDECL(const char *) RTEnvGetByIndexRawEx(RTENV hEnv, uint32_t iVar)
|
---|
964 | {
|
---|
965 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
966 | AssertPtrReturn(pIntEnv, NULL);
|
---|
967 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, NULL);
|
---|
968 |
|
---|
969 | RTENV_LOCK(pIntEnv);
|
---|
970 |
|
---|
971 | const char *pszRet;
|
---|
972 | if (iVar < pIntEnv->cVars)
|
---|
973 | pszRet = pIntEnv->papszEnv[iVar];
|
---|
974 | else
|
---|
975 | pszRet = NULL;
|
---|
976 |
|
---|
977 | RTENV_UNLOCK(pIntEnv);
|
---|
978 |
|
---|
979 | return pszRet;
|
---|
980 | }
|
---|
981 | RT_EXPORT_SYMBOL(RTEnvGetByIndexRawEx);
|
---|
982 |
|
---|
983 |
|
---|
984 | RTDECL(int) RTEnvCreateChangeRecord(PRTENV phEnv)
|
---|
985 | {
|
---|
986 | AssertPtrReturn(phEnv, VERR_INVALID_POINTER);
|
---|
987 | return rtEnvCreate(phEnv, RTENV_GROW_SIZE, false /*fCaseSensitive*/, true /*fPutEnvBlock*/);
|
---|
988 | }
|
---|
989 | RT_EXPORT_SYMBOL(RTEnvCreateChangeRecord);
|
---|
990 |
|
---|
991 |
|
---|
992 | RTDECL(bool) RTEnvIsChangeRecord(RTENV hEnv)
|
---|
993 | {
|
---|
994 | if (hEnv == RTENV_DEFAULT)
|
---|
995 | return false;
|
---|
996 |
|
---|
997 | PRTENVINTERNAL pIntEnv = hEnv;
|
---|
998 | AssertPtrReturn(pIntEnv, false);
|
---|
999 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, false);
|
---|
1000 | return pIntEnv->fPutEnvBlock;
|
---|
1001 | }
|
---|
1002 | RT_EXPORT_SYMBOL(RTEnvIsChangeRecord);
|
---|
1003 |
|
---|
1004 |
|
---|
1005 | RTDECL(int) RTEnvApplyChanges(RTENV hEnvDst, RTENV hEnvChanges)
|
---|
1006 | {
|
---|
1007 | PRTENVINTERNAL pIntEnvChanges = hEnvChanges;
|
---|
1008 | AssertPtrReturn(pIntEnvChanges, VERR_INVALID_HANDLE);
|
---|
1009 | AssertReturn(pIntEnvChanges->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
1010 |
|
---|
1011 | /** @todo lock validator trouble ahead here! */
|
---|
1012 | RTENV_LOCK(pIntEnvChanges);
|
---|
1013 |
|
---|
1014 | int rc = VINF_SUCCESS;
|
---|
1015 | for (uint32_t iChange = 0; iChange < pIntEnvChanges->cVars && RT_SUCCESS(rc); iChange++)
|
---|
1016 | rc = RTEnvPutEx(hEnvDst, pIntEnvChanges->papszEnv[iChange]);
|
---|
1017 |
|
---|
1018 | RTENV_UNLOCK(pIntEnvChanges);
|
---|
1019 |
|
---|
1020 | return rc;
|
---|
1021 | }
|
---|
1022 | RT_EXPORT_SYMBOL(RTEnvApplyChanges);
|
---|
1023 |
|
---|