VirtualBox

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

Last change on this file since 25659 was 20821, checked in by vboxsync, 15 years ago

IPRT: alignment check hacking.

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