1 /*
2 * Win32Environment.cpp
3 *
4 * Copyright (C) 2021 by RStudio, PBC
5 *
6 * Unless you have received this program directly from RStudio pursuant
7 * to the terms of a commercial license agreement with RStudio, then
8 * this program is licensed to you under the terms of version 3 of the
9 * GNU Affero General Public License. This program is distributed WITHOUT
10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13 *
14 */
15
16
17 #include <core/system/Environment.hpp>
18
19 #include <windows.h>
20
21 #include <vector>
22
23 #include <boost/algorithm/string/predicate.hpp>
24
25 #include <core/Log.hpp>
26 #include <core/StringUtils.hpp>
27
28 #include <shared_core/system/User.hpp> // For detail::getenv
29
30 namespace rstudio {
31 namespace core {
32 namespace system {
33
34 namespace impl {
35
optionIsNamed(const Option & option,const std::string & name)36 bool optionIsNamed(const Option& option, const std::string& name)
37 {
38 return boost::algorithm::iequals(option.first, name);
39 }
40
41 } // namespace impl
42
43
environment(Options * pEnvironment)44 void environment(Options* pEnvironment)
45 {
46 // get all environment strings (as unicode)
47 LPWSTR lpEnv = ::GetEnvironmentStringsW();
48 if (lpEnv == nullptr)
49 {
50 LOG_ERROR(LAST_SYSTEM_ERROR());
51 return;
52 }
53
54 // iterate over them
55 LPWSTR lpszEnvVar = nullptr;
56 for (lpszEnvVar = lpEnv; *lpszEnvVar; lpszEnvVar++)
57 {
58 // get the variable
59 std::wstring envVarWide;
60 while (*lpszEnvVar)
61 {
62 wchar_t ch = *lpszEnvVar;
63 envVarWide.append(1, ch);
64 lpszEnvVar++;
65 }
66
67 // convert to utf8 and parse
68 Option envVar;
69 if (parseEnvVar(string_utils::wideToUtf8(envVarWide), &envVar))
70 pEnvironment->push_back(envVar);
71 }
72
73
74 // free environment strings
75 if (!::FreeEnvironmentStringsW(lpEnv))
76 {
77 LOG_ERROR(LAST_SYSTEM_ERROR());
78 }
79 }
80
81 // Value returned is UTF-8 encoded
getenv(const std::string & name)82 std::string getenv(const std::string& name)
83 {
84 return detail::getenv(name);
85 }
86
setenv(const std::string & name,const std::string & value)87 void setenv(const std::string& name, const std::string& value)
88 {
89 ::SetEnvironmentVariableW(string_utils::utf8ToWide(name).c_str(),
90 string_utils::utf8ToWide(value).c_str());
91 }
92
unsetenv(const std::string & name)93 void unsetenv(const std::string& name)
94 {
95 ::SetEnvironmentVariable(name.c_str(), nullptr);
96 }
97
98
99 } // namespace system
100 } // namespace core
101 } // namespace rstudio
102
103