blob: f6655d9a917ec27e65b4e711d42640e0bf21d0dd [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9bc8cff2010-04-03 01:05:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]76b90d312010-08-03 03:00:505#include "base/environment.h"
[email protected]9bc8cff2010-04-03 01:05:396
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8
[email protected]b345c482013-08-30 18:00:399#include <vector>
10
thestig0c412e852016-06-30 08:04:4011#include "base/memory/ptr_util.h"
[email protected]b345c482013-08-30 18:00:3912#include "base/strings/string_piece.h"
13#include "base/strings/string_util.h"
14#include "base/strings/utf_string_conversions.h"
avi9b6f42932015-12-26 22:15:1415#include "build/build_config.h"
[email protected]b345c482013-08-30 18:00:3916
[email protected]9bc8cff2010-04-03 01:05:3917#if defined(OS_POSIX)
18#include <stdlib.h>
19#elif defined(OS_WIN)
20#include <windows.h>
21#endif
22
[email protected]b345c482013-08-30 18:00:3923namespace base {
[email protected]9bc8cff2010-04-03 01:05:3924
25namespace {
26
brettw7622fbed2015-06-09 20:20:1427class EnvironmentImpl : public Environment {
[email protected]9bc8cff2010-04-03 01:05:3928 public:
thestig0c412e852016-06-30 08:04:4029 bool GetVar(StringPiece variable_name, std::string* result) override {
[email protected]3ba7e082010-08-07 02:57:5930 if (GetVarImpl(variable_name, result))
[email protected]9bc8cff2010-04-03 01:05:3931 return true;
32
33 // Some commonly used variable names are uppercase while others
34 // are lowercase, which is inconsistent. Let's try to be helpful
35 // and look for a variable name with the reverse case.
36 // I.e. HTTP_PROXY may be http_proxy for some users/systems.
37 char first_char = variable_name[0];
38 std::string alternate_case_var;
zhongyi23960342016-04-12 23:13:2039 if (IsAsciiLower(first_char))
brettwc15100c2015-08-06 22:54:1640 alternate_case_var = ToUpperASCII(variable_name);
zhongyi23960342016-04-12 23:13:2041 else if (IsAsciiUpper(first_char))
brettwc15100c2015-08-06 22:54:1642 alternate_case_var = ToLowerASCII(variable_name);
[email protected]9bc8cff2010-04-03 01:05:3943 else
44 return false;
iceman4b80ef12017-03-28 12:33:2145 return GetVarImpl(alternate_case_var, result);
[email protected]9bc8cff2010-04-03 01:05:3946 }
[email protected]ac7264c2010-07-08 13:32:5147
thestig0c412e852016-06-30 08:04:4048 bool SetVar(StringPiece variable_name,
dcheng56488182014-10-21 10:54:5149 const std::string& new_value) override {
[email protected]c87bcf002010-08-06 01:03:3750 return SetVarImpl(variable_name, new_value);
[email protected]ac7264c2010-07-08 13:32:5151 }
52
thestig0c412e852016-06-30 08:04:4053 bool UnSetVar(StringPiece variable_name) override {
[email protected]4fae3162010-08-04 02:13:3454 return UnSetVarImpl(variable_name);
[email protected]fc586c72010-07-31 16:55:4055 }
56
[email protected]9bc8cff2010-04-03 01:05:3957 private:
thestig0c412e852016-06-30 08:04:4058 bool GetVarImpl(StringPiece variable_name, std::string* result) {
[email protected]9bc8cff2010-04-03 01:05:3959#if defined(OS_POSIX)
thestig0c412e852016-06-30 08:04:4060 const char* env_value = getenv(variable_name.data());
[email protected]9bc8cff2010-04-03 01:05:3961 if (!env_value)
62 return false;
63 // Note that the variable may be defined but empty.
64 if (result)
65 *result = env_value;
66 return true;
67#elif defined(OS_WIN)
thestig0c412e852016-06-30 08:04:4068 DWORD value_length =
69 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0);
[email protected]9bc8cff2010-04-03 01:05:3970 if (value_length == 0)
71 return false;
72 if (result) {
dcheng093de9b2016-04-04 21:25:5173 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]);
[email protected]9bc8cff2010-04-03 01:05:3974 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
75 value_length);
76 *result = WideToUTF8(value.get());
77 }
78 return true;
79#else
80#error need to port
81#endif
82 }
[email protected]ac7264c2010-07-08 13:32:5183
thestig0c412e852016-06-30 08:04:4084 bool SetVarImpl(StringPiece variable_name, const std::string& new_value) {
[email protected]ac7264c2010-07-08 13:32:5185#if defined(OS_POSIX)
[email protected]e9032c62010-07-16 03:34:2586 // On success, zero is returned.
thestig0c412e852016-06-30 08:04:4087 return !setenv(variable_name.data(), new_value.c_str(), 1);
[email protected]ac7264c2010-07-08 13:32:5188#elif defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:4189 // On success, a nonzero value is returned.
90 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
91 UTF8ToWide(new_value).c_str());
[email protected]ac7264c2010-07-08 13:32:5192#endif
93 }
[email protected]fc586c72010-07-31 16:55:4094
thestig0c412e852016-06-30 08:04:4095 bool UnSetVarImpl(StringPiece variable_name) {
[email protected]fc586c72010-07-31 16:55:4096#if defined(OS_POSIX)
97 // On success, zero is returned.
thestig0c412e852016-06-30 08:04:4098 return !unsetenv(variable_name.data());
[email protected]fc586c72010-07-31 16:55:4099#elif defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:41100 // On success, a nonzero value is returned.
thestig0c412e852016-06-30 08:04:40101 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr);
[email protected]fc586c72010-07-31 16:55:40102#endif
103 }
[email protected]9bc8cff2010-04-03 01:05:39104};
105
[email protected]b345c482013-08-30 18:00:39106// Parses a null-terminated input string of an environment block. The key is
107// placed into the given string, and the total length of the line, including
108// the terminating null, is returned.
109size_t ParseEnvLine(const NativeEnvironmentString::value_type* input,
110 NativeEnvironmentString* key) {
111 // Skip to the equals or end of the string, this is the key.
112 size_t cur = 0;
113 while (input[cur] && input[cur] != '=')
114 cur++;
115 *key = NativeEnvironmentString(&input[0], cur);
[email protected]9bc8cff2010-04-03 01:05:39116
[email protected]b345c482013-08-30 18:00:39117 // Now just skip to the end of the string.
118 while (input[cur])
119 cur++;
120 return cur + 1;
121}
122
123} // namespace
[email protected]9bc8cff2010-04-03 01:05:39124
[email protected]574f6f0c2010-07-21 02:59:28125namespace env_vars {
126
127#if defined(OS_POSIX)
128// On Posix systems, this variable contains the location of the user's home
129// directory. (e.g, /home/username/).
130const char kHome[] = "HOME";
131#endif
132
133} // namespace env_vars
134
Chris Watkinsbb7211c2017-11-29 07:16:38135Environment::~Environment() = default;
[email protected]3a3d47472010-07-15 21:03:54136
[email protected]9bc8cff2010-04-03 01:05:39137// static
thestig0c412e852016-06-30 08:04:40138std::unique_ptr<Environment> Environment::Create() {
Jeremy Roman9532f252017-08-16 23:27:24139 return std::make_unique<EnvironmentImpl>();
[email protected]9bc8cff2010-04-03 01:05:39140}
141
thestig0c412e852016-06-30 08:04:40142bool Environment::HasVar(StringPiece variable_name) {
143 return GetVar(variable_name, nullptr);
[email protected]fc586c72010-07-31 16:55:40144}
145
[email protected]b345c482013-08-30 18:00:39146#if defined(OS_WIN)
147
148string16 AlterEnvironment(const wchar_t* env,
149 const EnvironmentMap& changes) {
150 string16 result;
151
152 // First copy all unmodified values to the output.
153 size_t cur_env = 0;
154 string16 key;
155 while (env[cur_env]) {
156 const wchar_t* line = &env[cur_env];
157 size_t line_length = ParseEnvLine(line, &key);
158
159 // Keep only values not specified in the change vector.
160 EnvironmentMap::const_iterator found_change = changes.find(key);
161 if (found_change == changes.end())
162 result.append(line, line_length);
163
164 cur_env += line_length;
165 }
166
167 // Now append all modified and new values.
168 for (EnvironmentMap::const_iterator i = changes.begin();
169 i != changes.end(); ++i) {
170 if (!i->second.empty()) {
171 result.append(i->first);
172 result.push_back('=');
173 result.append(i->second);
174 result.push_back(0);
175 }
176 }
177
178 // An additional null marks the end of the list. We always need a double-null
179 // in case nothing was added above.
180 if (result.empty())
181 result.push_back(0);
182 result.push_back(0);
183 return result;
184}
185
186#elif defined(OS_POSIX)
187
dcheng093de9b2016-04-04 21:25:51188std::unique_ptr<char* []> AlterEnvironment(const char* const* const env,
189 const EnvironmentMap& changes) {
[email protected]b345c482013-08-30 18:00:39190 std::string value_storage; // Holds concatenated null-terminated strings.
191 std::vector<size_t> result_indices; // Line indices into value_storage.
192
193 // First build up all of the unchanged environment strings. These are
194 // null-terminated of the form "key=value".
195 std::string key;
196 for (size_t i = 0; env[i]; i++) {
197 size_t line_length = ParseEnvLine(env[i], &key);
198
199 // Keep only values not specified in the change vector.
200 EnvironmentMap::const_iterator found_change = changes.find(key);
201 if (found_change == changes.end()) {
202 result_indices.push_back(value_storage.size());
203 value_storage.append(env[i], line_length);
204 }
205 }
206
207 // Now append all modified and new values.
208 for (EnvironmentMap::const_iterator i = changes.begin();
209 i != changes.end(); ++i) {
210 if (!i->second.empty()) {
211 result_indices.push_back(value_storage.size());
212 value_storage.append(i->first);
213 value_storage.push_back('=');
214 value_storage.append(i->second);
215 value_storage.push_back(0);
216 }
217 }
218
219 size_t pointer_count_required =
220 result_indices.size() + 1 + // Null-terminated array of pointers.
221 (value_storage.size() + sizeof(char*) - 1) / sizeof(char*); // Buffer.
dcheng093de9b2016-04-04 21:25:51222 std::unique_ptr<char* []> result(new char*[pointer_count_required]);
[email protected]b345c482013-08-30 18:00:39223
224 // The string storage goes after the array of pointers.
225 char* storage_data = reinterpret_cast<char*>(
226 &result.get()[result_indices.size() + 1]);
227 if (!value_storage.empty())
228 memcpy(storage_data, value_storage.data(), value_storage.size());
229
230 // Fill array of pointers at the beginning of the result.
231 for (size_t i = 0; i < result_indices.size(); i++)
232 result[i] = &storage_data[result_indices[i]];
233 result[result_indices.size()] = 0; // Null terminator.
234
danakj0c8d4aa2015-11-25 05:29:58235 return result;
[email protected]b345c482013-08-30 18:00:39236}
237
238#endif // OS_POSIX
239
[email protected]9bc8cff2010-04-03 01:05:39240} // namespace base