blob: 11b2bc39a9e0600741fb55559ca609c44da3641a [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
[email protected]b345c482013-08-30 18:00:397#include <vector>
8
9#include "base/strings/string_piece.h"
10#include "base/strings/string_util.h"
11#include "base/strings/utf_string_conversions.h"
12
[email protected]9bc8cff2010-04-03 01:05:3913#if defined(OS_POSIX)
14#include <stdlib.h>
15#elif defined(OS_WIN)
16#include <windows.h>
17#endif
18
[email protected]b345c482013-08-30 18:00:3919namespace base {
[email protected]9bc8cff2010-04-03 01:05:3920
21namespace {
22
brettw7622fbed2015-06-09 20:20:1423class EnvironmentImpl : public Environment {
[email protected]9bc8cff2010-04-03 01:05:3924 public:
dcheng56488182014-10-21 10:54:5125 bool GetVar(const char* variable_name, std::string* result) override {
[email protected]3ba7e082010-08-07 02:57:5926 if (GetVarImpl(variable_name, result))
[email protected]9bc8cff2010-04-03 01:05:3927 return true;
28
29 // Some commonly used variable names are uppercase while others
30 // are lowercase, which is inconsistent. Let's try to be helpful
31 // and look for a variable name with the reverse case.
32 // I.e. HTTP_PROXY may be http_proxy for some users/systems.
33 char first_char = variable_name[0];
34 std::string alternate_case_var;
35 if (first_char >= 'a' && first_char <= 'z')
brettwc15100c2015-08-06 22:54:1636 alternate_case_var = ToUpperASCII(variable_name);
[email protected]9bc8cff2010-04-03 01:05:3937 else if (first_char >= 'A' && first_char <= 'Z')
brettwc15100c2015-08-06 22:54:1638 alternate_case_var = ToLowerASCII(variable_name);
[email protected]9bc8cff2010-04-03 01:05:3939 else
40 return false;
[email protected]3ba7e082010-08-07 02:57:5941 return GetVarImpl(alternate_case_var.c_str(), result);
[email protected]9bc8cff2010-04-03 01:05:3942 }
[email protected]ac7264c2010-07-08 13:32:5143
dcheng56488182014-10-21 10:54:5144 bool SetVar(const char* variable_name,
45 const std::string& new_value) override {
[email protected]c87bcf002010-08-06 01:03:3746 return SetVarImpl(variable_name, new_value);
[email protected]ac7264c2010-07-08 13:32:5147 }
48
dcheng56488182014-10-21 10:54:5149 bool UnSetVar(const char* variable_name) override {
[email protected]4fae3162010-08-04 02:13:3450 return UnSetVarImpl(variable_name);
[email protected]fc586c72010-07-31 16:55:4051 }
52
[email protected]9bc8cff2010-04-03 01:05:3953 private:
[email protected]3ba7e082010-08-07 02:57:5954 bool GetVarImpl(const char* variable_name, std::string* result) {
[email protected]9bc8cff2010-04-03 01:05:3955#if defined(OS_POSIX)
56 const char* env_value = getenv(variable_name);
57 if (!env_value)
58 return false;
59 // Note that the variable may be defined but empty.
60 if (result)
61 *result = env_value;
62 return true;
63#elif defined(OS_WIN)
64 DWORD value_length = ::GetEnvironmentVariable(
65 UTF8ToWide(variable_name).c_str(), NULL, 0);
66 if (value_length == 0)
67 return false;
68 if (result) {
[email protected]604eb052013-01-18 14:21:5869 scoped_ptr<wchar_t[]> value(new wchar_t[value_length]);
[email protected]9bc8cff2010-04-03 01:05:3970 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
71 value_length);
72 *result = WideToUTF8(value.get());
73 }
74 return true;
75#else
76#error need to port
77#endif
78 }
[email protected]ac7264c2010-07-08 13:32:5179
[email protected]c87bcf002010-08-06 01:03:3780 bool SetVarImpl(const char* variable_name, const std::string& new_value) {
[email protected]ac7264c2010-07-08 13:32:5181#if defined(OS_POSIX)
[email protected]e9032c62010-07-16 03:34:2582 // On success, zero is returned.
[email protected]c10688622010-08-17 23:33:4183 return !setenv(variable_name, new_value.c_str(), 1);
[email protected]ac7264c2010-07-08 13:32:5184#elif defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:4185 // On success, a nonzero value is returned.
86 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
87 UTF8ToWide(new_value).c_str());
[email protected]ac7264c2010-07-08 13:32:5188#endif
89 }
[email protected]fc586c72010-07-31 16:55:4090
[email protected]4fae3162010-08-04 02:13:3491 bool UnSetVarImpl(const char* variable_name) {
[email protected]fc586c72010-07-31 16:55:4092#if defined(OS_POSIX)
93 // On success, zero is returned.
[email protected]c10688622010-08-17 23:33:4194 return !unsetenv(variable_name);
[email protected]fc586c72010-07-31 16:55:4095#elif defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:4196 // On success, a nonzero value is returned.
97 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), NULL);
[email protected]fc586c72010-07-31 16:55:4098#endif
99 }
[email protected]9bc8cff2010-04-03 01:05:39100};
101
[email protected]b345c482013-08-30 18:00:39102// Parses a null-terminated input string of an environment block. The key is
103// placed into the given string, and the total length of the line, including
104// the terminating null, is returned.
105size_t ParseEnvLine(const NativeEnvironmentString::value_type* input,
106 NativeEnvironmentString* key) {
107 // Skip to the equals or end of the string, this is the key.
108 size_t cur = 0;
109 while (input[cur] && input[cur] != '=')
110 cur++;
111 *key = NativeEnvironmentString(&input[0], cur);
[email protected]9bc8cff2010-04-03 01:05:39112
[email protected]b345c482013-08-30 18:00:39113 // Now just skip to the end of the string.
114 while (input[cur])
115 cur++;
116 return cur + 1;
117}
118
119} // namespace
[email protected]9bc8cff2010-04-03 01:05:39120
[email protected]574f6f0c2010-07-21 02:59:28121namespace env_vars {
122
123#if defined(OS_POSIX)
124// On Posix systems, this variable contains the location of the user's home
125// directory. (e.g, /home/username/).
126const char kHome[] = "HOME";
127#endif
128
129} // namespace env_vars
130
[email protected]76b90d312010-08-03 03:00:50131Environment::~Environment() {}
[email protected]3a3d47472010-07-15 21:03:54132
[email protected]9bc8cff2010-04-03 01:05:39133// static
[email protected]76b90d312010-08-03 03:00:50134Environment* Environment::Create() {
135 return new EnvironmentImpl();
[email protected]9bc8cff2010-04-03 01:05:39136}
137
[email protected]9432ade2010-08-04 23:43:20138bool Environment::HasVar(const char* variable_name) {
[email protected]3ba7e082010-08-07 02:57:59139 return GetVar(variable_name, NULL);
[email protected]fc586c72010-07-31 16:55:40140}
141
[email protected]b345c482013-08-30 18:00:39142#if defined(OS_WIN)
143
144string16 AlterEnvironment(const wchar_t* env,
145 const EnvironmentMap& changes) {
146 string16 result;
147
148 // First copy all unmodified values to the output.
149 size_t cur_env = 0;
150 string16 key;
151 while (env[cur_env]) {
152 const wchar_t* line = &env[cur_env];
153 size_t line_length = ParseEnvLine(line, &key);
154
155 // Keep only values not specified in the change vector.
156 EnvironmentMap::const_iterator found_change = changes.find(key);
157 if (found_change == changes.end())
158 result.append(line, line_length);
159
160 cur_env += line_length;
161 }
162
163 // Now append all modified and new values.
164 for (EnvironmentMap::const_iterator i = changes.begin();
165 i != changes.end(); ++i) {
166 if (!i->second.empty()) {
167 result.append(i->first);
168 result.push_back('=');
169 result.append(i->second);
170 result.push_back(0);
171 }
172 }
173
174 // An additional null marks the end of the list. We always need a double-null
175 // in case nothing was added above.
176 if (result.empty())
177 result.push_back(0);
178 result.push_back(0);
179 return result;
180}
181
182#elif defined(OS_POSIX)
183
184scoped_ptr<char*[]> AlterEnvironment(const char* const* const env,
185 const EnvironmentMap& changes) {
186 std::string value_storage; // Holds concatenated null-terminated strings.
187 std::vector<size_t> result_indices; // Line indices into value_storage.
188
189 // First build up all of the unchanged environment strings. These are
190 // null-terminated of the form "key=value".
191 std::string key;
192 for (size_t i = 0; env[i]; i++) {
193 size_t line_length = ParseEnvLine(env[i], &key);
194
195 // Keep only values not specified in the change vector.
196 EnvironmentMap::const_iterator found_change = changes.find(key);
197 if (found_change == changes.end()) {
198 result_indices.push_back(value_storage.size());
199 value_storage.append(env[i], line_length);
200 }
201 }
202
203 // Now append all modified and new values.
204 for (EnvironmentMap::const_iterator i = changes.begin();
205 i != changes.end(); ++i) {
206 if (!i->second.empty()) {
207 result_indices.push_back(value_storage.size());
208 value_storage.append(i->first);
209 value_storage.push_back('=');
210 value_storage.append(i->second);
211 value_storage.push_back(0);
212 }
213 }
214
215 size_t pointer_count_required =
216 result_indices.size() + 1 + // Null-terminated array of pointers.
217 (value_storage.size() + sizeof(char*) - 1) / sizeof(char*); // Buffer.
218 scoped_ptr<char*[]> result(new char*[pointer_count_required]);
219
220 // The string storage goes after the array of pointers.
221 char* storage_data = reinterpret_cast<char*>(
222 &result.get()[result_indices.size() + 1]);
223 if (!value_storage.empty())
224 memcpy(storage_data, value_storage.data(), value_storage.size());
225
226 // Fill array of pointers at the beginning of the result.
227 for (size_t i = 0; i < result_indices.size(); i++)
228 result[i] = &storage_data[result_indices[i]];
229 result[result_indices.size()] = 0; // Null terminator.
230
231 return result.Pass();
232}
233
234#endif // OS_POSIX
235
[email protected]9bc8cff2010-04-03 01:05:39236} // namespace base