1 | /* $Id: env-posix.cpp 4475 2007-09-01 01:21:19Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Environment, Posix.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/env.h>
|
---|
23 | #include <iprt/string.h>
|
---|
24 | #include <iprt/alloca.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 |
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <errno.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | RTDECL(bool) RTEnvExist(const char *pszVar)
|
---|
32 | {
|
---|
33 | return getenv(pszVar) != NULL;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | RTDECL(const char *) RTEnvGet(const char *pszVar)
|
---|
38 | {
|
---|
39 | return getenv(pszVar);
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
|
---|
44 | {
|
---|
45 | /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
|
---|
46 | if (!putenv((char *)pszVarEqualValue))
|
---|
47 | return 0;
|
---|
48 | return RTErrConvertFromErrno(errno);
|
---|
49 | }
|
---|
50 |
|
---|
51 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
|
---|
52 | {
|
---|
53 | #if defined(_MSC_VER)
|
---|
54 | /* make a local copy and feed it to putenv. */
|
---|
55 | const size_t cchVar = strlen(pszVar);
|
---|
56 | const size_t cchValue = strlen(pszValue);
|
---|
57 | char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
|
---|
58 | memcpy(pszTmp, pszVar, cchVar);
|
---|
59 | pszTmp[cchVar] = '=';
|
---|
60 | if (*pszValue)
|
---|
61 | memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
|
---|
62 | else
|
---|
63 | {
|
---|
64 | pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
|
---|
65 | pszTmp[cchVar + 2] = '\0';
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (!putenv(pszTmp))
|
---|
69 | return 0;
|
---|
70 | return RTErrConvertFromErrno(errno);
|
---|
71 |
|
---|
72 | #else
|
---|
73 | if (!setenv(pszVar, pszValue, 1))
|
---|
74 | return VINF_SUCCESS;
|
---|
75 | return RTErrConvertFromErrno(errno);
|
---|
76 | #endif
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | RTDECL(int) RTEnvUnset(const char *pszVar)
|
---|
81 | {
|
---|
82 | AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
|
---|
83 |
|
---|
84 | /* Check that it exists first. */
|
---|
85 | if (!RTEnvExist(pszVar))
|
---|
86 | return VINF_ENV_VAR_NOT_FOUND;
|
---|
87 |
|
---|
88 | /* Ok, try remove it. */
|
---|
89 | if (!putenv((char *)pszVar))
|
---|
90 | return VINF_SUCCESS;
|
---|
91 | return RTErrConvertFromErrno(errno);
|
---|
92 | }
|
---|
93 |
|
---|