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