blob: cdea53c8c1f89b74b494f45654ca72792c1ff36b [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
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3917#if defined(OS_WIN)
[email protected]9bc8cff2010-04-03 01:05:3918#include <windows.h>
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3919#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
20#include <stdlib.h>
[email protected]9bc8cff2010-04-03 01:05:3921#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) {
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3959#if defined(OS_WIN)
thestig0c412e852016-06-30 08:04:4060 DWORD value_length =
61 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0);
[email protected]9bc8cff2010-04-03 01:05:3962 if (value_length == 0)
63 return false;
64 if (result) {
dcheng093de9b2016-04-04 21:25:5165 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]);
[email protected]9bc8cff2010-04-03 01:05:3966 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
67 value_length);
68 *result = WideToUTF8(value.get());
69 }
70 return true;
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3971#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
72 const char* env_value = getenv(variable_name.data());
73 if (!env_value)
74 return false;
75 // Note that the variable may be defined but empty.
76 if (result)
77 *result = env_value;
78 return true;
[email protected]9bc8cff2010-04-03 01:05:3979#endif
80 }
[email protected]ac7264c2010-07-08 13:32:5181
thestig0c412e852016-06-30 08:04:4082 bool SetVarImpl(StringPiece variable_name, const std::string& new_value) {
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3983#if defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:4184 // On success, a nonzero value is returned.
85 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
86 UTF8ToWide(new_value).c_str());
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3987#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
88 // On success, zero is returned.
89 return !setenv(variable_name.data(), new_value.c_str(), 1);
[email protected]ac7264c2010-07-08 13:32:5190#endif
91 }
[email protected]fc586c72010-07-31 16:55:4092
thestig0c412e852016-06-30 08:04:4093 bool UnSetVarImpl(StringPiece variable_name) {
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3994#if defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:4195 // On success, a nonzero value is returned.
thestig0c412e852016-06-30 08:04:4096 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr);
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3997#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
98 // On success, zero is returned.
99 return !unsetenv(variable_name.data());
[email protected]fc586c72010-07-31 16:55:40100#endif
101 }
[email protected]9bc8cff2010-04-03 01:05:39102};
103
[email protected]b345c482013-08-30 18:00:39104// Parses a null-terminated input string of an environment block. The key is
105// placed into the given string, and the total length of the line, including
106// the terminating null, is returned.
107size_t ParseEnvLine(const NativeEnvironmentString::value_type* input,
108 NativeEnvironmentString* key) {
109 // Skip to the equals or end of the string, this is the key.
110 size_t cur = 0;
111 while (input[cur] && input[cur] != '=')
112 cur++;
113 *key = NativeEnvironmentString(&input[0], cur);
[email protected]9bc8cff2010-04-03 01:05:39114
[email protected]b345c482013-08-30 18:00:39115 // Now just skip to the end of the string.
116 while (input[cur])
117 cur++;
118 return cur + 1;
119}
120
121} // namespace
[email protected]9bc8cff2010-04-03 01:05:39122
[email protected]574f6f0c2010-07-21 02:59:28123namespace env_vars {
124
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39125#if defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]574f6f0c2010-07-21 02:59:28126// On Posix systems, this variable contains the location of the user's home
127// directory. (e.g, /home/username/).
128const char kHome[] = "HOME";
129#endif
130
131} // namespace env_vars
132
Chris Watkinsbb7211c2017-11-29 07:16:38133Environment::~Environment() = default;
[email protected]3a3d47472010-07-15 21:03:54134
[email protected]9bc8cff2010-04-03 01:05:39135// static
thestig0c412e852016-06-30 08:04:40136std::unique_ptr<Environment> Environment::Create() {
Jeremy Roman9532f252017-08-16 23:27:24137 return std::make_unique<EnvironmentImpl>();
[email protected]9bc8cff2010-04-03 01:05:39138}
139
thestig0c412e852016-06-30 08:04:40140bool Environment::HasVar(StringPiece variable_name) {
141 return GetVar(variable_name, nullptr);
[email protected]fc586c72010-07-31 16:55:40142}
143
[email protected]b345c482013-08-30 18:00:39144#if defined(OS_WIN)
145
146string16 AlterEnvironment(const wchar_t* env,
147 const EnvironmentMap& changes) {
148 string16 result;
149
150 // First copy all unmodified values to the output.
151 size_t cur_env = 0;
152 string16 key;
153 while (env[cur_env]) {
154 const wchar_t* line = &env[cur_env];
155 size_t line_length = ParseEnvLine(line, &key);
156
157 // Keep only values not specified in the change vector.
158 EnvironmentMap::const_iterator found_change = changes.find(key);
159 if (found_change == changes.end())
160 result.append(line, line_length);
161
162 cur_env += line_length;
163 }
164
165 // Now append all modified and new values.
166 for (EnvironmentMap::const_iterator i = changes.begin();
167 i != changes.end(); ++i) {
168 if (!i->second.empty()) {
169 result.append(i->first);
170 result.push_back('=');
171 result.append(i->second);
172 result.push_back(0);
173 }
174 }
175
176 // An additional null marks the end of the list. We always need a double-null
177 // in case nothing was added above.
178 if (result.empty())
179 result.push_back(0);
180 result.push_back(0);
181 return result;
182}
183
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39184#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]b345c482013-08-30 18:00:39185
dcheng093de9b2016-04-04 21:25:51186std::unique_ptr<char* []> AlterEnvironment(const char* const* const env,
187 const EnvironmentMap& changes) {
[email protected]b345c482013-08-30 18:00:39188 std::string value_storage; // Holds concatenated null-terminated strings.
189 std::vector<size_t> result_indices; // Line indices into value_storage.
190
191 // First build up all of the unchanged environment strings. These are
192 // null-terminated of the form "key=value".
193 std::string key;
194 for (size_t i = 0; env[i]; i++) {
195 size_t line_length = ParseEnvLine(env[i], &key);
196
197 // Keep only values not specified in the change vector.
198 EnvironmentMap::const_iterator found_change = changes.find(key);
199 if (found_change == changes.end()) {
200 result_indices.push_back(value_storage.size());
201 value_storage.append(env[i], line_length);
202 }
203 }
204
205 // Now append all modified and new values.
206 for (EnvironmentMap::const_iterator i = changes.begin();
207 i != changes.end(); ++i) {
208 if (!i->second.empty()) {
209 result_indices.push_back(value_storage.size());
210 value_storage.append(i->first);
211 value_storage.push_back('=');
212 value_storage.append(i->second);
213 value_storage.push_back(0);
214 }
215 }
216
217 size_t pointer_count_required =
218 result_indices.size() + 1 + // Null-terminated array of pointers.
219 (value_storage.size() + sizeof(char*) - 1) / sizeof(char*); // Buffer.
dcheng093de9b2016-04-04 21:25:51220 std::unique_ptr<char* []> result(new char*[pointer_count_required]);
[email protected]b345c482013-08-30 18:00:39221
222 // The string storage goes after the array of pointers.
223 char* storage_data = reinterpret_cast<char*>(
224 &result.get()[result_indices.size() + 1]);
225 if (!value_storage.empty())
226 memcpy(storage_data, value_storage.data(), value_storage.size());
227
228 // Fill array of pointers at the beginning of the result.
229 for (size_t i = 0; i < result_indices.size(); i++)
230 result[i] = &storage_data[result_indices[i]];
231 result[result_indices.size()] = 0; // Null terminator.
232
danakj0c8d4aa2015-11-25 05:29:58233 return result;
[email protected]b345c482013-08-30 18:00:39234}
235
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39236#endif // OS_WIN
[email protected]b345c482013-08-30 18:00:39237
[email protected]9bc8cff2010-04-03 01:05:39238} // namespace base