VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/env-posix.cpp@ 62514

Last change on this file since 62514 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.6 KB
Line 
1/* $Id: env-posix.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Environment, Posix.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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
49RTDECL(bool) RTEnvExistsBad(const char *pszVar)
50{
51 AssertReturn(strchr(pszVar, '=') == NULL, false);
52 return RTEnvGetBad(pszVar) != NULL;
53}
54
55
56RTDECL(bool) RTEnvExist(const char *pszVar)
57{
58 return RTEnvExistsBad(pszVar);
59}
60
61
62RTDECL(const char *) RTEnvGetBad(const char *pszVar)
63{
64 AssertReturn(strchr(pszVar, '=') == NULL, NULL);
65
66 IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc causes trouble */
67 const char *pszValue = getenv(pszVar);
68 IPRT_ALIGNMENT_CHECKS_ENABLE();
69 return pszValue;
70}
71
72
73RTDECL(const char *) RTEnvGet(const char *pszVar)
74{
75 return RTEnvGetBad(pszVar);
76}
77
78
79RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
80{
81 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
82 if (!putenv((char *)pszVarEqualValue))
83 return 0;
84 return RTErrConvertFromErrno(errno);
85}
86
87
88RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
89{
90 return RTEnvPutBad(pszVarEqualValue);
91}
92
93
94RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
95{
96 AssertMsgReturn(strchr(pszVar, '=') == NULL, ("'%s'\n", pszVar), VERR_ENV_INVALID_VAR_NAME);
97
98#if defined(_MSC_VER)
99 /* make a local copy and feed it to putenv. */
100 const size_t cchVar = strlen(pszVar);
101 const size_t cchValue = strlen(pszValue);
102 char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
103 memcpy(pszTmp, pszVar, cchVar);
104 pszTmp[cchVar] = '=';
105 if (*pszValue)
106 memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
107 else
108 {
109 pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
110 pszTmp[cchVar + 2] = '\0';
111 }
112
113 if (!putenv(pszTmp))
114 return 0;
115 return RTErrConvertFromErrno(errno);
116
117#else
118 if (!setenv(pszVar, pszValue, 1))
119 return VINF_SUCCESS;
120 return RTErrConvertFromErrno(errno);
121#endif
122}
123
124
125RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
126{
127 return RTEnvSetBad(pszVar, pszValue);
128}
129
130RTDECL(int) RTEnvUnsetBad(const char *pszVar)
131{
132 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
133
134 /*
135 * Check that it exists first.
136 */
137 if (!RTEnvExist(pszVar))
138 return VINF_ENV_VAR_NOT_FOUND;
139
140 /*
141 * Ok, try remove it.
142 */
143#ifdef RT_OS_WINDOWS
144 /* Use putenv(var=) since Windows does not have unsetenv(). */
145 size_t cchVar = strlen(pszVar);
146 char *pszBuf = (char *)alloca(cchVar + 2);
147 memcpy(pszBuf, pszVar, cchVar);
148 pszBuf[cchVar] = '=';
149 pszBuf[cchVar + 1] = '\0';
150
151 if (!putenv(pszBuf))
152 return VINF_SUCCESS;
153
154#else
155 /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
156 if (!unsetenv((char*)pszVar))
157 return VINF_SUCCESS;
158#endif
159
160 return RTErrConvertFromErrno(errno);
161}
162
163RTDECL(int) RTEnvUnset(const char *pszVar)
164{
165 return RTEnvUnsetBad(pszVar);
166}
167
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette