blob: 6c13f5f2fc29b845af4e087577826d9571ab45a8 [file] [log] [blame]
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +09005#include "base/environment.h"
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +09006
avia6a6a682015-12-27 07:15:14 +09007#include <stddef.h>
8
brettw@chromium.org50484af2013-08-31 03:00:39 +09009#include <vector>
10
thestigaed17252016-06-30 17:04:40 +090011#include "base/memory/ptr_util.h"
brettw@chromium.org50484af2013-08-31 03:00:39 +090012#include "base/strings/string_piece.h"
13#include "base/strings/string_util.h"
14#include "base/strings/utf_string_conversions.h"
avia6a6a682015-12-27 07:15:14 +090015#include "build/build_config.h"
brettw@chromium.org50484af2013-08-31 03:00:39 +090016
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090017#if defined(OS_POSIX)
18#include <stdlib.h>
19#elif defined(OS_WIN)
20#include <windows.h>
21#endif
22
brettw@chromium.org50484af2013-08-31 03:00:39 +090023namespace base {
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090024
25namespace {
26
brettwbb5442f2015-06-10 05:20:14 +090027class EnvironmentImpl : public Environment {
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090028 public:
thestigaed17252016-06-30 17:04:40 +090029 bool GetVar(StringPiece variable_name, std::string* result) override {
tfarina@chromium.org8f115a82010-08-07 11:57:59 +090030 if (GetVarImpl(variable_name, result))
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090031 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;
zhongyi729ef582016-04-13 08:13:20 +090039 if (IsAsciiLower(first_char))
brettw54cd33b2015-08-07 07:54:16 +090040 alternate_case_var = ToUpperASCII(variable_name);
zhongyi729ef582016-04-13 08:13:20 +090041 else if (IsAsciiUpper(first_char))
brettw54cd33b2015-08-07 07:54:16 +090042 alternate_case_var = ToLowerASCII(variable_name);
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090043 else
44 return false;
icemane9bb39d2017-03-28 21:33:21 +090045 return GetVarImpl(alternate_case_var, result);
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090046 }
tfarina@chromium.org08225be2010-07-08 22:32:51 +090047
thestigaed17252016-06-30 17:04:40 +090048 bool SetVar(StringPiece variable_name,
dcheng7dc8df52014-10-21 19:54:51 +090049 const std::string& new_value) override {
tfarina@chromium.org5dc66732010-08-06 10:03:37 +090050 return SetVarImpl(variable_name, new_value);
tfarina@chromium.org08225be2010-07-08 22:32:51 +090051 }
52
thestigaed17252016-06-30 17:04:40 +090053 bool UnSetVar(StringPiece variable_name) override {
tfarina@chromium.org6b2d9cc2010-08-04 11:13:34 +090054 return UnSetVarImpl(variable_name);
tfarina@chromium.orga5122652010-08-01 01:55:40 +090055 }
56
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090057 private:
thestigaed17252016-06-30 17:04:40 +090058 bool GetVarImpl(StringPiece variable_name, std::string* result) {
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090059#if defined(OS_POSIX)
thestigaed17252016-06-30 17:04:40 +090060 const char* env_value = getenv(variable_name.data());
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090061 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)
thestigaed17252016-06-30 17:04:40 +090068 DWORD value_length =
69 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0);
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090070 if (value_length == 0)
71 return false;
72 if (result) {
dchengcc8e4d82016-04-05 06:25:51 +090073 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]);
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090074 ::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 }
tfarina@chromium.org08225be2010-07-08 22:32:51 +090083
thestigaed17252016-06-30 17:04:40 +090084 bool SetVarImpl(StringPiece variable_name, const std::string& new_value) {
tfarina@chromium.org08225be2010-07-08 22:32:51 +090085#if defined(OS_POSIX)
tfarina@chromium.orga5d6f662010-07-16 12:34:25 +090086 // On success, zero is returned.
thestigaed17252016-06-30 17:04:40 +090087 return !setenv(variable_name.data(), new_value.c_str(), 1);
tfarina@chromium.org08225be2010-07-08 22:32:51 +090088#elif defined(OS_WIN)
pkasting@chromium.orga9966b12010-08-18 08:33:41 +090089 // On success, a nonzero value is returned.
90 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
91 UTF8ToWide(new_value).c_str());
tfarina@chromium.org08225be2010-07-08 22:32:51 +090092#endif
93 }
tfarina@chromium.orga5122652010-08-01 01:55:40 +090094
thestigaed17252016-06-30 17:04:40 +090095 bool UnSetVarImpl(StringPiece variable_name) {
tfarina@chromium.orga5122652010-08-01 01:55:40 +090096#if defined(OS_POSIX)
97 // On success, zero is returned.
thestigaed17252016-06-30 17:04:40 +090098 return !unsetenv(variable_name.data());
tfarina@chromium.orga5122652010-08-01 01:55:40 +090099#elif defined(OS_WIN)
pkasting@chromium.orga9966b12010-08-18 08:33:41 +0900100 // On success, a nonzero value is returned.
thestigaed17252016-06-30 17:04:40 +0900101 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr);
tfarina@chromium.orga5122652010-08-01 01:55:40 +0900102#endif
103 }
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +0900104};
105
brettw@chromium.org50484af2013-08-31 03:00:39 +0900106// 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);
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +0900116
brettw@chromium.org50484af2013-08-31 03:00:39 +0900117 // Now just skip to the end of the string.
118 while (input[cur])
119 cur++;
120 return cur + 1;
121}
122
123} // namespace
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +0900124
tfarina@chromium.org49c8f3a2010-07-21 11:59:28 +0900125namespace 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
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +0900135Environment::~Environment() {}
erg@chromium.org493f5f62010-07-16 06:03:54 +0900136
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +0900137// static
thestigaed17252016-06-30 17:04:40 +0900138std::unique_ptr<Environment> Environment::Create() {
Jeremy Romancd0c4672017-08-17 08:27:24 +0900139 return std::make_unique<EnvironmentImpl>();
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +0900140}
141
thestigaed17252016-06-30 17:04:40 +0900142bool Environment::HasVar(StringPiece variable_name) {
143 return GetVar(variable_name, nullptr);
tfarina@chromium.orga5122652010-08-01 01:55:40 +0900144}
145
brettw@chromium.org50484af2013-08-31 03:00:39 +0900146#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
dchengcc8e4d82016-04-05 06:25:51 +0900188std::unique_ptr<char* []> AlterEnvironment(const char* const* const env,
189 const EnvironmentMap& changes) {
brettw@chromium.org50484af2013-08-31 03:00:39 +0900190 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.
dchengcc8e4d82016-04-05 06:25:51 +0900222 std::unique_ptr<char* []> result(new char*[pointer_count_required]);
brettw@chromium.org50484af2013-08-31 03:00:39 +0900223
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
danakj800d2ea2015-11-25 14:29:58 +0900235 return result;
brettw@chromium.org50484af2013-08-31 03:00:39 +0900236}
237
238#endif // OS_POSIX
239
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +0900240} // namespace base