1 | /* $Id: env-generic.cpp 21337 2009-07-07 14:58:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Environment, Generic.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/env.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/alloc.h>
|
---|
40 | #include <iprt/alloca.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/err.h>
|
---|
43 | #include "internal/magics.h"
|
---|
44 |
|
---|
45 | #include <stdlib.h>
|
---|
46 | #if !defined(RT_OS_WINDOWS)
|
---|
47 | # include <unistd.h>
|
---|
48 | #endif
|
---|
49 | #ifdef RT_OS_DARWIN
|
---|
50 | # include <crt_externs.h>
|
---|
51 | #endif
|
---|
52 | #if defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD)
|
---|
53 | RT_C_DECLS_BEGIN
|
---|
54 | extern char **environ;
|
---|
55 | RT_C_DECLS_END
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 | /*******************************************************************************
|
---|
60 | * Defined Constants And Macros *
|
---|
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 |
|
---|
68 | /*******************************************************************************
|
---|
69 | * Structures and Typedefs *
|
---|
70 | *******************************************************************************/
|
---|
71 | /**
|
---|
72 | * The internal representation of a (non-default) environment.
|
---|
73 | */
|
---|
74 | typedef struct RTENVINTERNAL
|
---|
75 | {
|
---|
76 | /** Magic value . */
|
---|
77 | uint32_t u32Magic;
|
---|
78 | /** Number of variables in the array.
|
---|
79 | * This does not include the terminating NULL entry. */
|
---|
80 | size_t cVars;
|
---|
81 | /** Capacity (allocated size) of the array.
|
---|
82 | * This includes space for the terminating NULL element (for compatibility
|
---|
83 | * with the C library), so that c <= cCapacity - 1. */
|
---|
84 | size_t cAllocated;
|
---|
85 | /** Array of environment variables. */
|
---|
86 | char **papszEnv;
|
---|
87 | /** Array of environment variables in the process CP.
|
---|
88 | * This get (re-)constructed when RTEnvGetExecEnvP method is called. */
|
---|
89 | char **papszEnvOtherCP;
|
---|
90 | } RTENVINTERNAL, *PRTENVINTERNAL;
|
---|
91 |
|
---|
92 | /** The allocation granularity of the RTENVINTERNAL::papszEnv memory. */
|
---|
93 | #define RTENV_GROW_SIZE 16
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Internal worker that resolves the pointer to the default
|
---|
98 | * process environment. (environ)
|
---|
99 | *
|
---|
100 | * @returns Pointer to the default environment.
|
---|
101 | * This may be NULL.
|
---|
102 | */
|
---|
103 | static const char * const *rtEnvDefault(void)
|
---|
104 | {
|
---|
105 | #ifdef RT_OS_DARWIN
|
---|
106 | return *(_NSGetEnviron());
|
---|
107 | #elif defined(RT_OS_L4)
|
---|
108 | /* So far, our L4 libraries do not include environment support. */
|
---|
109 | return NULL;
|
---|
110 | #else
|
---|
111 | return environ;
|
---|
112 | #endif
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Internal worker that creates an environment handle with a specified capacity.
|
---|
118 | *
|
---|
119 | * @returns IPRT status code.
|
---|
120 | * @param ppIntEnv Where to store the result.
|
---|
121 | * @param cAllocated The initial array size.
|
---|
122 | */
|
---|
123 | static int rtEnvCreate(PRTENVINTERNAL *ppIntEnv, size_t cAllocated)
|
---|
124 | {
|
---|
125 | /*
|
---|
126 | * Allocate environment handle.
|
---|
127 | */
|
---|
128 | PRTENVINTERNAL pIntEnv = (PRTENVINTERNAL)RTMemAlloc(sizeof(*pIntEnv));
|
---|
129 | if (pIntEnv)
|
---|
130 | {
|
---|
131 | /*
|
---|
132 | * Pre-allocate the variable array.
|
---|
133 | */
|
---|
134 | pIntEnv->u32Magic = RTENV_MAGIC;
|
---|
135 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
136 | pIntEnv->cVars = 0;
|
---|
137 | pIntEnv->cAllocated = RT_ALIGN_Z(RT_MAX(cAllocated, RTENV_GROW_SIZE), RTENV_GROW_SIZE);
|
---|
138 | pIntEnv->papszEnv = (char **)RTMemAllocZ(sizeof(pIntEnv->papszEnv[0]) * pIntEnv->cAllocated);
|
---|
139 | if (pIntEnv->papszEnv)
|
---|
140 | {
|
---|
141 | *ppIntEnv = pIntEnv;
|
---|
142 | return VINF_SUCCESS;
|
---|
143 | }
|
---|
144 |
|
---|
145 | RTMemFree(pIntEnv);
|
---|
146 | }
|
---|
147 |
|
---|
148 | return VERR_NO_MEMORY;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | RTDECL(int) RTEnvCreate(PRTENV pEnv)
|
---|
153 | {
|
---|
154 | AssertPtrReturn(pEnv, VERR_INVALID_POINTER);
|
---|
155 | return rtEnvCreate(pEnv, RTENV_GROW_SIZE);
|
---|
156 | }
|
---|
157 | RT_EXPORT_SYMBOL(RTEnvCreate);
|
---|
158 |
|
---|
159 |
|
---|
160 | RTDECL(int) RTEnvDestroy(RTENV Env)
|
---|
161 | {
|
---|
162 | /*
|
---|
163 | * Ignore NIL_RTENV and validate input.
|
---|
164 | */
|
---|
165 | if ( Env == NIL_RTENV
|
---|
166 | || Env == RTENV_DEFAULT)
|
---|
167 | return VINF_SUCCESS;
|
---|
168 |
|
---|
169 | PRTENVINTERNAL pIntEnv = Env;
|
---|
170 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
171 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Do the cleanup.
|
---|
175 | */
|
---|
176 | RTENV_LOCK(pIntEnv);
|
---|
177 | pIntEnv->u32Magic++;
|
---|
178 | size_t iVar = pIntEnv->cVars;
|
---|
179 | while (iVar-- > 0)
|
---|
180 | RTStrFree(pIntEnv->papszEnv[iVar]);
|
---|
181 | RTMemFree(pIntEnv->papszEnv);
|
---|
182 | pIntEnv->papszEnv = NULL;
|
---|
183 |
|
---|
184 | if (pIntEnv->papszEnvOtherCP)
|
---|
185 | {
|
---|
186 | for (iVar = 0; pIntEnv->papszEnvOtherCP[iVar]; iVar++)
|
---|
187 | {
|
---|
188 | RTStrFree(pIntEnv->papszEnvOtherCP[iVar]);
|
---|
189 | pIntEnv->papszEnvOtherCP[iVar] = NULL;
|
---|
190 | }
|
---|
191 | RTMemFree(pIntEnv->papszEnvOtherCP);
|
---|
192 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
193 | }
|
---|
194 |
|
---|
195 | RTENV_UNLOCK(pIntEnv);
|
---|
196 | /*RTCritSectDelete(&pIntEnv->CritSect) */
|
---|
197 | RTMemFree(pIntEnv);
|
---|
198 |
|
---|
199 | return VINF_SUCCESS;
|
---|
200 | }
|
---|
201 | RT_EXPORT_SYMBOL(RTEnvDestroy);
|
---|
202 |
|
---|
203 |
|
---|
204 | RTDECL(int) RTEnvClone(PRTENV pEnv, RTENV EnvToClone)
|
---|
205 | {
|
---|
206 | /*
|
---|
207 | * Validate input and figure out how many variable to clone and where to get them.
|
---|
208 | */
|
---|
209 | size_t cVars;
|
---|
210 | const char * const *papszEnv;
|
---|
211 | PRTENVINTERNAL pIntEnvToClone;
|
---|
212 | AssertPtrReturn(pEnv, VERR_INVALID_POINTER);
|
---|
213 | if (EnvToClone == RTENV_DEFAULT)
|
---|
214 | {
|
---|
215 | pIntEnvToClone = NULL;
|
---|
216 | papszEnv = rtEnvDefault();
|
---|
217 | cVars = 0;
|
---|
218 | if (papszEnv)
|
---|
219 | while (papszEnv[cVars])
|
---|
220 | cVars++;
|
---|
221 | }
|
---|
222 | else
|
---|
223 | {
|
---|
224 | pIntEnvToClone = EnvToClone;
|
---|
225 | AssertPtrReturn(pIntEnvToClone, VERR_INVALID_HANDLE);
|
---|
226 | AssertReturn(pIntEnvToClone->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
227 | RTENV_LOCK(pIntEnvToClone);
|
---|
228 |
|
---|
229 | papszEnv = pIntEnvToClone->papszEnv;
|
---|
230 | cVars = pIntEnvToClone->cVars;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Create the duplicate.
|
---|
235 | */
|
---|
236 | PRTENVINTERNAL pIntEnv;
|
---|
237 | int rc = rtEnvCreate(&pIntEnv, cVars + 1 /* NULL */);
|
---|
238 | if (RT_SUCCESS(rc))
|
---|
239 | {
|
---|
240 | pIntEnv->cVars = cVars;
|
---|
241 | pIntEnv->papszEnv[pIntEnv->cVars] = NULL;
|
---|
242 | if (EnvToClone == RTENV_DEFAULT)
|
---|
243 | {
|
---|
244 | /* ASSUMES the default environment is in the current codepage. */
|
---|
245 | for (size_t iVar = 0; iVar < cVars; iVar++)
|
---|
246 | {
|
---|
247 | int rc = RTStrCurrentCPToUtf8(&pIntEnv->papszEnv[iVar], papszEnv[iVar]);
|
---|
248 | if (RT_FAILURE(rc))
|
---|
249 | {
|
---|
250 | pIntEnv->cVars = iVar;
|
---|
251 | RTEnvDestroy(pIntEnv);
|
---|
252 | return rc;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | }
|
---|
256 | else
|
---|
257 | {
|
---|
258 | for (size_t iVar = 0; iVar < cVars; iVar++)
|
---|
259 | {
|
---|
260 | char *pszVar = RTStrDup(papszEnv[iVar]);
|
---|
261 | if (RT_UNLIKELY(!pszVar))
|
---|
262 | {
|
---|
263 | RTENV_UNLOCK(pIntEnvToClone);
|
---|
264 |
|
---|
265 | pIntEnv->cVars = iVar;
|
---|
266 | RTEnvDestroy(pIntEnv);
|
---|
267 | return rc;
|
---|
268 | }
|
---|
269 | pIntEnv->papszEnv[iVar] = pszVar;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* done */
|
---|
274 | *pEnv = pIntEnv;
|
---|
275 | }
|
---|
276 |
|
---|
277 | if (pIntEnvToClone)
|
---|
278 | RTENV_UNLOCK(pIntEnvToClone);
|
---|
279 | return rc;
|
---|
280 | }
|
---|
281 | RT_EXPORT_SYMBOL(RTEnvClone);
|
---|
282 |
|
---|
283 |
|
---|
284 | RTDECL(int) RTEnvPutEx(RTENV Env, const char *pszVarEqualValue)
|
---|
285 | {
|
---|
286 | int rc;
|
---|
287 | AssertPtrReturn(pszVarEqualValue, VERR_INVALID_POINTER);
|
---|
288 | const char *pszEq = strchr(pszVarEqualValue, '=');
|
---|
289 | if (!pszEq)
|
---|
290 | rc = RTEnvUnsetEx(Env, pszVarEqualValue);
|
---|
291 | else
|
---|
292 | {
|
---|
293 | /*
|
---|
294 | * Make a copy of the variable name so we can terminate it
|
---|
295 | * properly and then pass the request on to RTEnvSetEx.
|
---|
296 | */
|
---|
297 | const char *pszValue = pszEq + 1;
|
---|
298 |
|
---|
299 | size_t cchVar = pszEq - pszVarEqualValue;
|
---|
300 | Assert(cchVar < 1024);
|
---|
301 | char *pszVar = (char *)alloca(cchVar + 1);
|
---|
302 | memcpy(pszVar, pszVarEqualValue, cchVar);
|
---|
303 | pszVar[cchVar] = '\0';
|
---|
304 |
|
---|
305 | rc = RTEnvSetEx(Env, pszVar, pszValue);
|
---|
306 | }
|
---|
307 | return rc;
|
---|
308 | }
|
---|
309 | RT_EXPORT_SYMBOL(RTEnvPutEx);
|
---|
310 |
|
---|
311 |
|
---|
312 | RTDECL(int) RTEnvSetEx(RTENV Env, const char *pszVar, const char *pszValue)
|
---|
313 | {
|
---|
314 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
315 | AssertReturn(*pszVar, VERR_INVALID_PARAMETER);
|
---|
316 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
317 |
|
---|
318 | int rc;
|
---|
319 | if (Env == RTENV_DEFAULT)
|
---|
320 | {
|
---|
321 | /*
|
---|
322 | * Since RTEnvPut isn't UTF-8 clean and actually expects the strings
|
---|
323 | * to be in the current code page (codeset), we'll do the necessary
|
---|
324 | * conversions here.
|
---|
325 | */
|
---|
326 | char *pszVarOtherCP;
|
---|
327 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
328 | if (RT_SUCCESS(rc))
|
---|
329 | {
|
---|
330 | char *pszValueOtherCP;
|
---|
331 | rc = RTStrUtf8ToCurrentCP(&pszValueOtherCP, pszValue);
|
---|
332 | if (RT_SUCCESS(rc))
|
---|
333 | {
|
---|
334 | rc = RTEnvSet(pszVarOtherCP, pszValueOtherCP);
|
---|
335 | RTStrFree(pszValueOtherCP);
|
---|
336 | }
|
---|
337 | RTStrFree(pszVarOtherCP);
|
---|
338 | }
|
---|
339 | }
|
---|
340 | else
|
---|
341 | {
|
---|
342 | PRTENVINTERNAL pIntEnv = Env;
|
---|
343 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
344 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Create the variable string.
|
---|
348 | */
|
---|
349 | const size_t cchVar = strlen(pszVar);
|
---|
350 | const size_t cchValue = strlen(pszValue);
|
---|
351 | char *pszEntry = (char *)RTMemAlloc(cchVar + cchValue + 2);
|
---|
352 | if (pszEntry)
|
---|
353 | {
|
---|
354 | memcpy(pszEntry, pszVar, cchVar);
|
---|
355 | pszEntry[cchVar] = '=';
|
---|
356 | memcpy(&pszEntry[cchVar + 1], pszValue, cchValue + 1);
|
---|
357 |
|
---|
358 | RTENV_LOCK(pIntEnv);
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Find the location of the variable. (iVar = cVars if new)
|
---|
362 | */
|
---|
363 | rc = VINF_SUCCESS;
|
---|
364 | size_t iVar;
|
---|
365 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
366 | if ( !strncmp(pIntEnv->papszEnv[iVar], pszVar, cchVar)
|
---|
367 | && pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
368 | break;
|
---|
369 | if (iVar < pIntEnv->cVars)
|
---|
370 | {
|
---|
371 | /*
|
---|
372 | * Replace the current entry. Simple.
|
---|
373 | */
|
---|
374 | RTMemFree(pIntEnv->papszEnv[iVar]);
|
---|
375 | pIntEnv->papszEnv[iVar] = pszEntry;
|
---|
376 | }
|
---|
377 | else
|
---|
378 | {
|
---|
379 | /*
|
---|
380 | * Adding a new variable. Resize the array if required
|
---|
381 | * and then insert the new value at the end.
|
---|
382 | */
|
---|
383 | if (pIntEnv->cVars + 2 > pIntEnv->cAllocated)
|
---|
384 | {
|
---|
385 | void *pvNew = RTMemRealloc(pIntEnv->papszEnv, sizeof(char *) * (pIntEnv->cAllocated + RTENV_GROW_SIZE));
|
---|
386 | if (!pvNew)
|
---|
387 | rc = VERR_NO_MEMORY;
|
---|
388 | else
|
---|
389 | {
|
---|
390 | pIntEnv->papszEnv = (char **)pvNew;
|
---|
391 | pIntEnv->cAllocated += RTENV_GROW_SIZE;
|
---|
392 | for (size_t iNewVar = pIntEnv->cVars; iNewVar < pIntEnv->cAllocated; iNewVar++)
|
---|
393 | pIntEnv->papszEnv[iNewVar] = NULL;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | if (RT_SUCCESS(rc))
|
---|
397 | {
|
---|
398 | pIntEnv->papszEnv[iVar] = pszEntry;
|
---|
399 | pIntEnv->papszEnv[iVar + 1] = NULL; /* this isn't really necessary, but doesn't hurt. */
|
---|
400 | pIntEnv->cVars++;
|
---|
401 | Assert(pIntEnv->cVars == iVar + 1);
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | RTENV_UNLOCK(pIntEnv);
|
---|
406 |
|
---|
407 | if (RT_FAILURE(rc))
|
---|
408 | RTMemFree(pszEntry);
|
---|
409 | }
|
---|
410 | else
|
---|
411 | rc = VERR_NO_MEMORY;
|
---|
412 | }
|
---|
413 | return rc;
|
---|
414 | }
|
---|
415 | RT_EXPORT_SYMBOL(RTEnvSetEx);
|
---|
416 |
|
---|
417 |
|
---|
418 | RTDECL(int) RTEnvUnsetEx(RTENV Env, const char *pszVar)
|
---|
419 | {
|
---|
420 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
421 | AssertReturn(*pszVar, VERR_INVALID_PARAMETER);
|
---|
422 |
|
---|
423 | int rc;
|
---|
424 | if (Env == RTENV_DEFAULT)
|
---|
425 | {
|
---|
426 | /*
|
---|
427 | * Since RTEnvUnset isn't UTF-8 clean and actually expects the strings
|
---|
428 | * to be in the current code page (codeset), we'll do the necessary
|
---|
429 | * conversions here.
|
---|
430 | */
|
---|
431 | char *pszVarOtherCP;
|
---|
432 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
433 | if (RT_SUCCESS(rc))
|
---|
434 | {
|
---|
435 | rc = RTEnvUnset(pszVarOtherCP);
|
---|
436 | RTStrFree(pszVarOtherCP);
|
---|
437 | }
|
---|
438 | }
|
---|
439 | else
|
---|
440 | {
|
---|
441 | PRTENVINTERNAL pIntEnv = Env;
|
---|
442 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
443 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
444 |
|
---|
445 | RTENV_LOCK(pIntEnv);
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Remove all variable by the given name.
|
---|
449 | */
|
---|
450 | rc = VINF_ENV_VAR_NOT_FOUND;
|
---|
451 | const size_t cchVar = strlen(pszVar);
|
---|
452 | size_t iVar;
|
---|
453 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
454 | if ( !strncmp(pIntEnv->papszEnv[iVar], pszVar, cchVar)
|
---|
455 | && pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
456 | {
|
---|
457 | RTMemFree(pIntEnv->papszEnv[iVar]);
|
---|
458 | pIntEnv->cVars--;
|
---|
459 | if (pIntEnv->cVars > 0)
|
---|
460 | pIntEnv->papszEnv[iVar] = pIntEnv->papszEnv[pIntEnv->cVars];
|
---|
461 | pIntEnv->papszEnv[pIntEnv->cVars] = NULL;
|
---|
462 | rc = VINF_SUCCESS;
|
---|
463 | /* no break, there could be more. */
|
---|
464 | }
|
---|
465 |
|
---|
466 | RTENV_UNLOCK(pIntEnv);
|
---|
467 | }
|
---|
468 | return rc;
|
---|
469 |
|
---|
470 | }
|
---|
471 | RT_EXPORT_SYMBOL(RTEnvUnsetEx);
|
---|
472 |
|
---|
473 |
|
---|
474 | RTDECL(int) RTEnvGetEx(RTENV Env, const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
|
---|
475 | {
|
---|
476 | AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
|
---|
477 | AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER);
|
---|
478 | AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER);
|
---|
479 | AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER);
|
---|
480 |
|
---|
481 | if (pcchActual)
|
---|
482 | *pcchActual = 0;
|
---|
483 | int rc;
|
---|
484 | if (Env == RTENV_DEFAULT)
|
---|
485 | {
|
---|
486 | /*
|
---|
487 | * Since RTEnvGet isn't UTF-8 clean and actually expects the strings
|
---|
488 | * to be in the current code page (codeset), we'll do the necessary
|
---|
489 | * conversions here.
|
---|
490 | */
|
---|
491 | char *pszVarOtherCP;
|
---|
492 | rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
493 | if (RT_SUCCESS(rc))
|
---|
494 | {
|
---|
495 | const char *pszValueOtherCP = RTEnvGet(pszVarOtherCP);
|
---|
496 | RTStrFree(pszVarOtherCP);
|
---|
497 | if (pszValueOtherCP)
|
---|
498 | {
|
---|
499 | char *pszValueUtf8;
|
---|
500 | rc = RTStrCurrentCPToUtf8(&pszValueUtf8, pszValueOtherCP);
|
---|
501 | if (RT_SUCCESS(rc))
|
---|
502 | {
|
---|
503 | rc = VINF_SUCCESS;
|
---|
504 | size_t cch = strlen(pszValueUtf8);
|
---|
505 | if (pcchActual)
|
---|
506 | *pcchActual = cch;
|
---|
507 | if (pszValue && cbValue)
|
---|
508 | {
|
---|
509 | if (cch < cbValue)
|
---|
510 | memcpy(pszValue, pszValueUtf8, cch + 1);
|
---|
511 | else
|
---|
512 | rc = VERR_BUFFER_OVERFLOW;
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 | else
|
---|
517 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
518 | }
|
---|
519 | }
|
---|
520 | else
|
---|
521 | {
|
---|
522 | PRTENVINTERNAL pIntEnv = Env;
|
---|
523 | AssertPtrReturn(pIntEnv, VERR_INVALID_HANDLE);
|
---|
524 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, VERR_INVALID_HANDLE);
|
---|
525 |
|
---|
526 | RTENV_LOCK(pIntEnv);
|
---|
527 |
|
---|
528 | /*
|
---|
529 | * Locate the first variable and return it to the caller.
|
---|
530 | */
|
---|
531 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
532 | const size_t cchVar = strlen(pszVar);
|
---|
533 | size_t iVar;
|
---|
534 | for (iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
535 | if ( !strncmp(pIntEnv->papszEnv[iVar], pszVar, cchVar)
|
---|
536 | && pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
537 | {
|
---|
538 | rc = VINF_SUCCESS;
|
---|
539 | const char *pszValueOrg = pIntEnv->papszEnv[iVar] + cchVar + 1;
|
---|
540 | size_t cch = strlen(pszValueOrg);
|
---|
541 | if (pcchActual)
|
---|
542 | *pcchActual = cch;
|
---|
543 | if (pszValue && cbValue)
|
---|
544 | {
|
---|
545 | if (cch < cbValue)
|
---|
546 | memcpy(pszValue, pszValueOrg, cch + 1);
|
---|
547 | else
|
---|
548 | rc = VERR_BUFFER_OVERFLOW;
|
---|
549 | }
|
---|
550 | break;
|
---|
551 | }
|
---|
552 |
|
---|
553 | RTENV_UNLOCK(pIntEnv);
|
---|
554 | }
|
---|
555 | return rc;
|
---|
556 |
|
---|
557 | }
|
---|
558 | RT_EXPORT_SYMBOL(RTEnvGetEx);
|
---|
559 |
|
---|
560 |
|
---|
561 | RTDECL(bool) RTEnvExistEx(RTENV Env, const char *pszVar)
|
---|
562 | {
|
---|
563 | AssertPtrReturn(pszVar, false);
|
---|
564 |
|
---|
565 | bool fExist = false;
|
---|
566 | if (Env == RTENV_DEFAULT)
|
---|
567 | {
|
---|
568 | /*
|
---|
569 | * Since RTEnvExist isn't UTF-8 clean and actually expects the strings
|
---|
570 | * to be in the current code page (codeset), we'll do the necessary
|
---|
571 | * conversions here.
|
---|
572 | */
|
---|
573 | char *pszVarOtherCP;
|
---|
574 | int rc = RTStrUtf8ToCurrentCP(&pszVarOtherCP, pszVar);
|
---|
575 | if (RT_SUCCESS(rc))
|
---|
576 | {
|
---|
577 | fExist = RTEnvExist(pszVarOtherCP);
|
---|
578 | RTStrFree(pszVarOtherCP);
|
---|
579 | }
|
---|
580 | }
|
---|
581 | else
|
---|
582 | {
|
---|
583 | PRTENVINTERNAL pIntEnv = Env;
|
---|
584 | AssertPtrReturn(pIntEnv, false);
|
---|
585 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, false);
|
---|
586 |
|
---|
587 | RTENV_LOCK(pIntEnv);
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Simple search.
|
---|
591 | */
|
---|
592 | const size_t cchVar = strlen(pszVar);
|
---|
593 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
594 | if ( !strncmp(pIntEnv->papszEnv[iVar], pszVar, cchVar)
|
---|
595 | && pIntEnv->papszEnv[iVar][cchVar] == '=')
|
---|
596 | {
|
---|
597 | fExist = true;
|
---|
598 | break;
|
---|
599 | }
|
---|
600 |
|
---|
601 | RTENV_UNLOCK(pIntEnv);
|
---|
602 | }
|
---|
603 | return fExist;
|
---|
604 | }
|
---|
605 | RT_EXPORT_SYMBOL(RTEnvExistEx);
|
---|
606 |
|
---|
607 |
|
---|
608 | RTDECL(char const * const *) RTEnvGetExecEnvP(RTENV Env)
|
---|
609 | {
|
---|
610 | const char * const *papszRet;
|
---|
611 | if (Env == RTENV_DEFAULT)
|
---|
612 | {
|
---|
613 | papszRet = rtEnvDefault();
|
---|
614 | if (!papszRet)
|
---|
615 | {
|
---|
616 | static const char * const s_papszDummy[2] = { NULL, NULL };
|
---|
617 | papszRet = &s_papszDummy[0];
|
---|
618 | }
|
---|
619 | }
|
---|
620 | else
|
---|
621 | {
|
---|
622 | PRTENVINTERNAL pIntEnv = Env;
|
---|
623 | AssertPtrReturn(pIntEnv, NULL);
|
---|
624 | AssertReturn(pIntEnv->u32Magic == RTENV_MAGIC, NULL);
|
---|
625 |
|
---|
626 | RTENV_LOCK(pIntEnv);
|
---|
627 |
|
---|
628 | /*
|
---|
629 | * Free any old envp.
|
---|
630 | */
|
---|
631 | if (pIntEnv->papszEnvOtherCP)
|
---|
632 | {
|
---|
633 | for (size_t iVar = 0; pIntEnv->papszEnvOtherCP[iVar]; iVar++)
|
---|
634 | {
|
---|
635 | RTStrFree(pIntEnv->papszEnvOtherCP[iVar]);
|
---|
636 | pIntEnv->papszEnvOtherCP[iVar] = NULL;
|
---|
637 | }
|
---|
638 | RTMemFree(pIntEnv->papszEnvOtherCP);
|
---|
639 | pIntEnv->papszEnvOtherCP = NULL;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /*
|
---|
643 | * Construct a new envp with the strings in the process code set.
|
---|
644 | */
|
---|
645 | char **papsz;
|
---|
646 | papszRet = pIntEnv->papszEnvOtherCP = papsz = (char **)RTMemAlloc(sizeof(char *) * (pIntEnv->cVars + 1));
|
---|
647 | if (papsz)
|
---|
648 | {
|
---|
649 | papsz[pIntEnv->cVars] = NULL;
|
---|
650 | for (size_t iVar = 0; iVar < pIntEnv->cVars; iVar++)
|
---|
651 | {
|
---|
652 | int rc = RTStrUtf8ToCurrentCP(&papsz[iVar], pIntEnv->papszEnv[iVar]);
|
---|
653 | if (RT_FAILURE(rc))
|
---|
654 | {
|
---|
655 | /* RTEnvDestroy / we cleans up later. */
|
---|
656 | papsz[iVar] = NULL;
|
---|
657 | AssertRC(rc);
|
---|
658 | papszRet = NULL;
|
---|
659 | break;
|
---|
660 | }
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | RTENV_UNLOCK(pIntEnv);
|
---|
665 | }
|
---|
666 | return papszRet;
|
---|
667 | }
|
---|
668 | RT_EXPORT_SYMBOL(RTEnvGetExecEnvP);
|
---|