rsleevi@chromium.org | de3a6cf | 2012-04-06 12:53:02 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 5 | #include "base/environment.h" |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 6 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
| 13 | #include "base/strings/string_util.h" |
| 14 | #include "base/strings/utf_string_conversions.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 15 | #include "build/build_config.h" |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 16 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 17 | #if defined(OS_POSIX) |
| 18 | #include <stdlib.h> |
| 19 | #elif defined(OS_WIN) |
| 20 | #include <windows.h> |
| 21 | #endif |
| 22 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 23 | namespace base { |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
brettw | bb5442f | 2015-06-10 05:20:14 +0900 | [diff] [blame] | 27 | class EnvironmentImpl : public Environment { |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 28 | public: |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 29 | bool GetVar(StringPiece variable_name, std::string* result) override { |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 30 | if (GetVarImpl(variable_name, result)) |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 31 | 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; |
zhongyi | 729ef58 | 2016-04-13 08:13:20 +0900 | [diff] [blame] | 39 | if (IsAsciiLower(first_char)) |
brettw | 54cd33b | 2015-08-07 07:54:16 +0900 | [diff] [blame] | 40 | alternate_case_var = ToUpperASCII(variable_name); |
zhongyi | 729ef58 | 2016-04-13 08:13:20 +0900 | [diff] [blame] | 41 | else if (IsAsciiUpper(first_char)) |
brettw | 54cd33b | 2015-08-07 07:54:16 +0900 | [diff] [blame] | 42 | alternate_case_var = ToLowerASCII(variable_name); |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 43 | else |
| 44 | return false; |
iceman | e9bb39d | 2017-03-28 21:33:21 +0900 | [diff] [blame] | 45 | return GetVarImpl(alternate_case_var, result); |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 46 | } |
tfarina@chromium.org | 08225be | 2010-07-08 22:32:51 +0900 | [diff] [blame] | 47 | |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 48 | bool SetVar(StringPiece variable_name, |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 49 | const std::string& new_value) override { |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 50 | return SetVarImpl(variable_name, new_value); |
tfarina@chromium.org | 08225be | 2010-07-08 22:32:51 +0900 | [diff] [blame] | 51 | } |
| 52 | |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 53 | bool UnSetVar(StringPiece variable_name) override { |
tfarina@chromium.org | 6b2d9cc | 2010-08-04 11:13:34 +0900 | [diff] [blame] | 54 | return UnSetVarImpl(variable_name); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 55 | } |
| 56 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 57 | private: |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 58 | bool GetVarImpl(StringPiece variable_name, std::string* result) { |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 59 | #if defined(OS_POSIX) |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 60 | const char* env_value = getenv(variable_name.data()); |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 61 | 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) |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 68 | DWORD value_length = |
| 69 | ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0); |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 70 | if (value_length == 0) |
| 71 | return false; |
| 72 | if (result) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 73 | std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]); |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 74 | ::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.org | 08225be | 2010-07-08 22:32:51 +0900 | [diff] [blame] | 83 | |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 84 | bool SetVarImpl(StringPiece variable_name, const std::string& new_value) { |
tfarina@chromium.org | 08225be | 2010-07-08 22:32:51 +0900 | [diff] [blame] | 85 | #if defined(OS_POSIX) |
tfarina@chromium.org | a5d6f66 | 2010-07-16 12:34:25 +0900 | [diff] [blame] | 86 | // On success, zero is returned. |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 87 | return !setenv(variable_name.data(), new_value.c_str(), 1); |
tfarina@chromium.org | 08225be | 2010-07-08 22:32:51 +0900 | [diff] [blame] | 88 | #elif defined(OS_WIN) |
pkasting@chromium.org | a9966b1 | 2010-08-18 08:33:41 +0900 | [diff] [blame] | 89 | // On success, a nonzero value is returned. |
| 90 | return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), |
| 91 | UTF8ToWide(new_value).c_str()); |
tfarina@chromium.org | 08225be | 2010-07-08 22:32:51 +0900 | [diff] [blame] | 92 | #endif |
| 93 | } |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 94 | |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 95 | bool UnSetVarImpl(StringPiece variable_name) { |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 96 | #if defined(OS_POSIX) |
| 97 | // On success, zero is returned. |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 98 | return !unsetenv(variable_name.data()); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 99 | #elif defined(OS_WIN) |
pkasting@chromium.org | a9966b1 | 2010-08-18 08:33:41 +0900 | [diff] [blame] | 100 | // On success, a nonzero value is returned. |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 101 | return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 102 | #endif |
| 103 | } |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 104 | }; |
| 105 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 106 | // 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. |
| 109 | size_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.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 116 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 117 | // 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.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 124 | |
tfarina@chromium.org | 49c8f3a | 2010-07-21 11:59:28 +0900 | [diff] [blame] | 125 | namespace 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/). |
| 130 | const char kHome[] = "HOME"; |
| 131 | #endif |
| 132 | |
| 133 | } // namespace env_vars |
| 134 | |
Chris Watkins | d155d9f | 2017-11-29 16:16:38 +0900 | [diff] [blame] | 135 | Environment::~Environment() = default; |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 136 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 137 | // static |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 138 | std::unique_ptr<Environment> Environment::Create() { |
Jeremy Roman | cd0c467 | 2017-08-17 08:27:24 +0900 | [diff] [blame] | 139 | return std::make_unique<EnvironmentImpl>(); |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 140 | } |
| 141 | |
thestig | aed1725 | 2016-06-30 17:04:40 +0900 | [diff] [blame] | 142 | bool Environment::HasVar(StringPiece variable_name) { |
| 143 | return GetVar(variable_name, nullptr); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 144 | } |
| 145 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 146 | #if defined(OS_WIN) |
| 147 | |
| 148 | string16 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 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 188 | std::unique_ptr<char* []> AlterEnvironment(const char* const* const env, |
| 189 | const EnvironmentMap& changes) { |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 190 | 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. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 222 | std::unique_ptr<char* []> result(new char*[pointer_count_required]); |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 223 | |
| 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 | |
danakj | 800d2ea | 2015-11-25 14:29:58 +0900 | [diff] [blame] | 235 | return result; |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | #endif // OS_POSIX |
| 239 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 240 | } // namespace base |