1 | /* $Id: env-posix.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Environment, Posix.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 | #ifdef RT_OS_DARWIN
|
---|
32 | /* pick the correct prototype for unsetenv. */
|
---|
33 | # define _POSIX_C_SOURCE 1
|
---|
34 | #endif
|
---|
35 | #include <iprt/env.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/alloca.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #if defined(DEBUG) && defined(RT_OS_LINUX)
|
---|
40 | # include <iprt/asm.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <errno.h>
|
---|
45 |
|
---|
46 | #include "internal/alignmentchecks.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | RTDECL(bool) RTEnvExist(const char *pszVar)
|
---|
50 | {
|
---|
51 | return RTEnvGet(pszVar) != NULL;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | RTDECL(const char *) RTEnvGet(const char *pszVar)
|
---|
56 | {
|
---|
57 | IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc causes trouble */
|
---|
58 | const char *pszValue = getenv(pszVar);
|
---|
59 | IPRT_ALIGNMENT_CHECKS_ENABLE();
|
---|
60 | return pszValue;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
|
---|
65 | {
|
---|
66 | /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
|
---|
67 | if (!putenv((char *)pszVarEqualValue))
|
---|
68 | return 0;
|
---|
69 | return RTErrConvertFromErrno(errno);
|
---|
70 | }
|
---|
71 |
|
---|
72 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
|
---|
73 | {
|
---|
74 | #if defined(_MSC_VER)
|
---|
75 | /* make a local copy and feed it to putenv. */
|
---|
76 | const size_t cchVar = strlen(pszVar);
|
---|
77 | const size_t cchValue = strlen(pszValue);
|
---|
78 | char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
|
---|
79 | memcpy(pszTmp, pszVar, cchVar);
|
---|
80 | pszTmp[cchVar] = '=';
|
---|
81 | if (*pszValue)
|
---|
82 | memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
|
---|
83 | else
|
---|
84 | {
|
---|
85 | pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
|
---|
86 | pszTmp[cchVar + 2] = '\0';
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (!putenv(pszTmp))
|
---|
90 | return 0;
|
---|
91 | return RTErrConvertFromErrno(errno);
|
---|
92 |
|
---|
93 | #else
|
---|
94 | if (!setenv(pszVar, pszValue, 1))
|
---|
95 | return VINF_SUCCESS;
|
---|
96 | return RTErrConvertFromErrno(errno);
|
---|
97 | #endif
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | RTDECL(int) RTEnvUnset(const char *pszVar)
|
---|
102 | {
|
---|
103 | AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Check that it exists first.
|
---|
107 | */
|
---|
108 | if (!RTEnvExist(pszVar))
|
---|
109 | return VINF_ENV_VAR_NOT_FOUND;
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Ok, try remove it.
|
---|
113 | */
|
---|
114 | #ifdef RT_OS_WINDOWS
|
---|
115 | /* Use putenv(var=) since Windows does not have unsetenv(). */
|
---|
116 | size_t cchVar = strlen(pszVar);
|
---|
117 | char *pszBuf = (char *)alloca(cchVar + 2);
|
---|
118 | memcpy(pszBuf, pszVar, cchVar);
|
---|
119 | pszBuf[cchVar] = '=';
|
---|
120 | pszBuf[cchVar + 1] = '\0';
|
---|
121 |
|
---|
122 | if (!putenv(pszBuf))
|
---|
123 | return VINF_SUCCESS;
|
---|
124 |
|
---|
125 | #else
|
---|
126 | /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
|
---|
127 | if (!unsetenv((char*)pszVar))
|
---|
128 | return VINF_SUCCESS;
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | return RTErrConvertFromErrno(errno);
|
---|
132 | }
|
---|
133 |
|
---|