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