VirtualBox

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

Last change on this file since 54985 was 50408, checked in by vboxsync, 11 years ago

RTEnv: Use the unicode CRT APIs on windows to avoid lost-in-translation issues.

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