VirtualBox

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

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

some comments

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