VirtualBox

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

Last change on this file since 18130 was 16526, checked in by vboxsync, 16 years ago

IMO better for the unsetenv() prototype on Darwin

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/* $Id: env-posix.cpp 16526 2009-02-05 09:43:21Z 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#ifdef RT_OS_DARWIN
36/* pick the correct prototype for unsetenv. */
37# define _POSIX_C_SOURCE 1
38#endif
39#include <iprt/env.h>
40#include <iprt/string.h>
41#include <iprt/alloca.h>
42#include <iprt/assert.h>
43
44#include <stdlib.h>
45#include <errno.h>
46
47
48RTDECL(bool) RTEnvExist(const char *pszVar)
49{
50 return getenv(pszVar) != NULL;
51}
52
53
54RTDECL(const char *) RTEnvGet(const char *pszVar)
55{
56 return getenv(pszVar);
57}
58
59
60RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
61{
62 /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
63 if (!putenv((char *)pszVarEqualValue))
64 return 0;
65 return RTErrConvertFromErrno(errno);
66}
67
68RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
69{
70#if defined(_MSC_VER)
71 /* make a local copy and feed it to putenv. */
72 const size_t cchVar = strlen(pszVar);
73 const size_t cchValue = strlen(pszValue);
74 char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
75 memcpy(pszTmp, pszVar, cchVar);
76 pszTmp[cchVar] = '=';
77 if (*pszValue)
78 memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
79 else
80 {
81 pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
82 pszTmp[cchVar + 2] = '\0';
83 }
84
85 if (!putenv(pszTmp))
86 return 0;
87 return RTErrConvertFromErrno(errno);
88
89#else
90 if (!setenv(pszVar, pszValue, 1))
91 return VINF_SUCCESS;
92 return RTErrConvertFromErrno(errno);
93#endif
94}
95
96
97RTDECL(int) RTEnvUnset(const char *pszVar)
98{
99 AssertReturn(!strchr(pszVar, '='), VERR_INVALID_PARAMETER);
100
101 /* Check that it exists first. */
102 if (!RTEnvExist(pszVar))
103 return VINF_ENV_VAR_NOT_FOUND;
104
105 /* Ok, try remove it. */
106#ifdef RT_OS_WINDOWS
107 /* Windows does not have unsetenv(). Clear the environment variable according to the MSN docs. */
108 char *pszBuf;
109 int rc = RTStrAPrintf(&pszBuf, "%s=", pszVar);
110 if (RT_FAILURE(rc))
111 return rc;
112 rc = putenv(pszBuf);
113 RTStrFree(pszBuf);
114 if (!rc)
115 return VINF_SUCCESS;
116#else
117 /* This is the preferred function as putenv() like used above does neither work on Solaris nor on Darwin. */
118 if (!unsetenv((char*)pszVar))
119 return VINF_SUCCESS;
120#endif
121
122 return RTErrConvertFromErrno(errno);
123}
124
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