1 | /* $Id: env-posix.cpp 5999 2007-12-07 15:05:06Z 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 (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 <iprt/string.h>
|
---|
33 | #include <iprt/alloca.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 |
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <errno.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | RTDECL(bool) RTEnvExist(const char *pszVar)
|
---|
41 | {
|
---|
42 | return getenv(pszVar) != NULL;
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | RTDECL(const char *) RTEnvGet(const char *pszVar)
|
---|
47 | {
|
---|
48 | return getenv(pszVar);
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
|
---|
53 | {
|
---|
54 | /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
|
---|
55 | if (!putenv((char *)pszVarEqualValue))
|
---|
56 | return 0;
|
---|
57 | return RTErrConvertFromErrno(errno);
|
---|
58 | }
|
---|
59 |
|
---|
60 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
|
---|
61 | {
|
---|
62 | #if defined(_MSC_VER)
|
---|
63 | /* make a local copy and feed it to putenv. */
|
---|
64 | const size_t cchVar = strlen(pszVar);
|
---|
65 | const size_t cchValue = strlen(pszValue);
|
---|
66 | char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
|
---|
67 | memcpy(pszTmp, pszVar, cchVar);
|
---|
68 | pszTmp[cchVar] = '=';
|
---|
69 | if (*pszValue)
|
---|
70 | memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
|
---|
71 | else
|
---|
72 | {
|
---|
73 | pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
|
---|
74 | pszTmp[cchVar + 2] = '\0';
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (!putenv(pszTmp))
|
---|
78 | return 0;
|
---|
79 | return RTErrConvertFromErrno(errno);
|
---|
80 |
|
---|
81 | #else
|
---|
82 | if (!setenv(pszVar, pszValue, 1))
|
---|
83 | return VINF_SUCCESS;
|
---|
84 | return RTErrConvertFromErrno(errno);
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | RTDECL(int) RTEnvUnset(const char *pszVar)
|
---|
90 | {
|
---|
91 | AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
|
---|
92 |
|
---|
93 | /* Check that it exists first. */
|
---|
94 | if (!RTEnvExist(pszVar))
|
---|
95 | return VINF_ENV_VAR_NOT_FOUND;
|
---|
96 |
|
---|
97 | /* Ok, try remove it. */
|
---|
98 | if (!putenv((char *)pszVar))
|
---|
99 | return VINF_SUCCESS;
|
---|
100 | return RTErrConvertFromErrno(errno);
|
---|
101 | }
|
---|
102 |
|
---|