VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/env-win.cpp@ 76359

Last change on this file since 76359 was 76359, checked in by vboxsync, 6 years ago

*: string.h sans err.h fix. bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.9 KB
Line 
1/* $Id: env-win.cpp 76359 2018-12-22 01:33:33Z vboxsync $ */
2/** @file
3 * IPRT - Environment, Posix.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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#include <iprt/env.h>
32
33#include <iprt/alloca.h>
34#include <iprt/assert.h>
35#include <iprt/err.h>
36#include <iprt/string.h>
37#include <iprt/mem.h>
38
39#include <stdlib.h>
40#include <errno.h>
41
42
43RTDECL(bool) RTEnvExistsBad(const char *pszVar)
44{
45 return RTEnvGetBad(pszVar) != NULL;
46}
47
48
49RTDECL(bool) RTEnvExist(const char *pszVar)
50{
51 return RTEnvExistsBad(pszVar);
52}
53
54
55RTDECL(bool) RTEnvExistsUtf8(const char *pszVar)
56{
57 AssertReturn(strchr(pszVar, '=') == NULL, false);
58
59 PRTUTF16 pwszVar;
60 int rc = RTStrToUtf16(pszVar, &pwszVar);
61 AssertRCReturn(rc, false);
62 bool fRet = _wgetenv(pwszVar) != NULL;
63 RTUtf16Free(pwszVar);
64 return fRet;
65}
66
67
68RTDECL(const char *) RTEnvGetBad(const char *pszVar)
69{
70 AssertReturn(strchr(pszVar, '=') == NULL, NULL);
71 return getenv(pszVar);
72}
73
74
75RTDECL(const char *) RTEnvGet(const char *pszVar)
76{
77 return RTEnvGetBad(pszVar);
78}
79
80RTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual)
81{
82 AssertPtrReturn(pszVar, VERR_INVALID_POINTER);
83 AssertPtrNullReturn(pszValue, VERR_INVALID_POINTER);
84 AssertReturn(pszValue || !cbValue, VERR_INVALID_PARAMETER);
85 AssertPtrNullReturn(pcchActual, VERR_INVALID_POINTER);
86 AssertReturn(pcchActual || (pszValue && cbValue), VERR_INVALID_PARAMETER);
87 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
88
89 if (pcchActual)
90 *pcchActual = 0;
91
92 PRTUTF16 pwszVar;
93 int rc = RTStrToUtf16(pszVar, &pwszVar);
94 AssertRCReturn(rc, rc);
95
96 /** @todo Consider _wgetenv_s or GetEnvironmentVariableW here to avoid the
97 * potential race with a concurrent _wputenv/_putenv. */
98 PCRTUTF16 pwszValue = _wgetenv(pwszVar);
99 RTUtf16Free(pwszVar);
100 if (pwszValue)
101 {
102 if (cbValue)
103 rc = RTUtf16ToUtf8Ex(pwszValue, RTSTR_MAX, &pszValue, cbValue, pcchActual);
104 else
105 rc = RTUtf16CalcUtf8LenEx(pwszValue, RTSTR_MAX, pcchActual);
106 }
107 else
108 rc = VERR_ENV_VAR_NOT_FOUND;
109 return rc;
110}
111
112
113RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue)
114{
115 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
116 if (!putenv((char *)pszVarEqualValue))
117 return 0;
118 return RTErrConvertFromErrno(errno);
119}
120
121
122RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
123{
124 return RTEnvPutBad(pszVarEqualValue);
125}
126
127
128RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue)
129{
130 PRTUTF16 pwszVarEqualValue;
131 int rc = RTStrToUtf16(pszVarEqualValue, &pwszVarEqualValue);
132 if (RT_SUCCESS(rc))
133 {
134 if (!_wputenv(pwszVarEqualValue))
135 rc = VINF_SUCCESS;
136 else
137 rc = RTErrConvertFromErrno(errno);
138 RTUtf16Free(pwszVarEqualValue);
139 }
140 return rc;
141}
142
143
144
145RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue)
146{
147 AssertMsgReturn(strchr(pszVar, '=') == NULL, ("'%s'\n", pszVar), VERR_ENV_INVALID_VAR_NAME);
148
149 /* make a local copy and feed it to putenv. */
150 const size_t cchVar = strlen(pszVar);
151 const size_t cchValue = strlen(pszValue);
152 char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
153 memcpy(pszTmp, pszVar, cchVar);
154 pszTmp[cchVar] = '=';
155 if (*pszValue)
156 memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
157 else
158 {
159 pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
160 pszTmp[cchVar + 2] = '\0';
161 }
162
163 if (!putenv(pszTmp))
164 return 0;
165 return RTErrConvertFromErrno(errno);
166}
167
168
169RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
170{
171 return RTEnvSetBad(pszVar, pszValue);
172}
173
174RTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue)
175{
176 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
177
178 size_t cwcVar;
179 int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar);
180 if (RT_SUCCESS(rc))
181 {
182 size_t cwcValue;
183 rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcValue);
184 if (RT_SUCCESS(rc))
185 {
186 PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + cwcValue + 1) * sizeof(RTUTF16));
187 if (pwszTmp)
188 {
189 rc = RTStrToUtf16Ex(pszVar, RTSTR_MAX, &pwszTmp, cwcVar + 1, NULL);
190 if (RT_SUCCESS(rc))
191 {
192 PRTUTF16 pwszTmpValue = &pwszTmp[cwcVar];
193 *pwszTmpValue++ = '=';
194 rc = RTStrToUtf16Ex(pszValue, RTSTR_MAX, &pwszTmpValue, cwcValue + 1, NULL);
195 if (RT_SUCCESS(rc))
196 {
197 if (!_wputenv(pwszTmp))
198 rc = VINF_SUCCESS;
199 else
200 rc = RTErrConvertFromErrno(errno);
201 }
202 }
203 RTMemTmpFree(pwszTmp);
204 }
205 else
206 rc = VERR_NO_TMP_MEMORY;
207 }
208 }
209 return rc;
210}
211
212
213RTDECL(int) RTEnvUnsetBad(const char *pszVar)
214{
215 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
216
217 /*
218 * Check that it exists first.
219 */
220 if (!RTEnvExist(pszVar))
221 return VINF_ENV_VAR_NOT_FOUND;
222
223 /*
224 * Ok, try remove it.
225 */
226#ifdef RT_OS_WINDOWS
227 /* Use putenv(var=) since Windows does not have unsetenv(). */
228 size_t cchVar = strlen(pszVar);
229 char *pszBuf = (char *)alloca(cchVar + 2);
230 memcpy(pszBuf, pszVar, cchVar);
231 pszBuf[cchVar] = '=';
232 pszBuf[cchVar + 1] = '\0';
233
234 if (!putenv(pszBuf))
235 return VINF_SUCCESS;
236
237#else
238 /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
239 if (!unsetenv((char*)pszVar))
240 return VINF_SUCCESS;
241#endif
242
243 return RTErrConvertFromErrno(errno);
244}
245
246
247RTDECL(int) RTEnvUnset(const char *pszVar)
248{
249 return RTEnvUnsetBad(pszVar);
250}
251
252
253RTDECL(int) RTEnvUnsetUtf8(const char *pszVar)
254{
255 AssertReturn(strchr(pszVar, '=') == NULL, VERR_ENV_INVALID_VAR_NAME);
256
257 size_t cwcVar;
258 int rc = RTStrCalcUtf16LenEx(pszVar, RTSTR_MAX, &cwcVar);
259 if (RT_SUCCESS(rc))
260 {
261 PRTUTF16 pwszTmp = (PRTUTF16)RTMemTmpAlloc((cwcVar + 1 + 1) * sizeof(RTUTF16));
262 if (pwszTmp)
263 {
264 rc = RTStrToUtf16Ex(pszVar, RTSTR_MAX, &pwszTmp, cwcVar + 1, NULL);
265 if (RT_SUCCESS(rc))
266 {
267 pwszTmp[cwcVar] = '=';
268 pwszTmp[cwcVar + 1] = '\0';
269 if (!_wputenv(pwszTmp))
270 rc = VINF_SUCCESS;
271 else
272 rc = RTErrConvertFromErrno(errno);
273 }
274 RTMemTmpFree(pwszTmp);
275 }
276 }
277 return rc;
278}
279
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