1 | /* $Id: env-posix.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Environment, Posix.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/env.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/alloca.h>
|
---|
29 |
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <errno.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | RTDECL(bool) RTEnvExist(const char *pszVar)
|
---|
35 | {
|
---|
36 | return getenv(pszVar) != NULL;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | RTDECL(const char *) RTEnvGet(const char *pszVar)
|
---|
41 | {
|
---|
42 | return getenv(pszVar);
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue)
|
---|
47 | {
|
---|
48 | /** @todo putenv is a source memory leaks. deal with this on a per system basis. */
|
---|
49 | if (!putenv((char *)pszVarEqualValue))
|
---|
50 | return 0;
|
---|
51 | return RTErrConvertFromErrno(errno);
|
---|
52 | }
|
---|
53 |
|
---|
54 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue)
|
---|
55 | {
|
---|
56 | #if defined(_MSC_VER)
|
---|
57 | /* make a local copy and feed it to putenv. */
|
---|
58 | const size_t cchVar = strlen(pszVar);
|
---|
59 | const size_t cchValue = strlen(pszValue);
|
---|
60 | char *pszTmp = (char *)alloca(cchVar + cchValue + 2 + !*pszValue);
|
---|
61 | memcpy(pszTmp, pszVar, cchVar);
|
---|
62 | pszTmp[cchVar] = '=';
|
---|
63 | if (*pszValue)
|
---|
64 | memcpy(pszTmp + cchVar + 1, pszValue, cchValue + 1);
|
---|
65 | else
|
---|
66 | {
|
---|
67 | pszTmp[cchVar + 1] = ' '; /* wrong, but putenv will remove it otherwise. */
|
---|
68 | pszTmp[cchVar + 2] = '\0';
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (!putenv(pszTmp))
|
---|
72 | return 0;
|
---|
73 | return RTErrConvertFromErrno(errno);
|
---|
74 |
|
---|
75 | #else
|
---|
76 | if (!setenv(pszVar, pszValue, 1))
|
---|
77 | return VINF_SUCCESS;
|
---|
78 | return RTErrConvertFromErrno(errno);
|
---|
79 | #endif
|
---|
80 | }
|
---|
81 |
|
---|