1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/environment.h"
7 #include "base/memory/ptr_util.h"
8 #include "base/strings/string_piece.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "build/build_config.h"
15 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
23 class EnvironmentImpl : public Environment {
25 bool GetVar(StringPiece variable_name, std::string* result) override {
26 if (GetVarImpl(variable_name, result))
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 (IsAsciiLower(first_char))
36 alternate_case_var = ToUpperASCII(variable_name);
37 else if (IsAsciiUpper(first_char))
38 alternate_case_var = ToLowerASCII(variable_name);
41 return GetVarImpl(alternate_case_var, result);
44 bool SetVar(StringPiece variable_name,
45 const std::string& new_value) override {
46 return SetVarImpl(variable_name, new_value);
49 bool UnSetVar(StringPiece variable_name) override {
50 return UnSetVarImpl(variable_name);
54 bool GetVarImpl(StringPiece variable_name, std::string* result) {
57 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0);
58 if (value_length == 0)
61 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]);
62 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
64 *result = WideToUTF8(value.get());
67 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
68 const char* env_value = getenv(variable_name.data());
71 // Note that the variable may be defined but empty.
78 bool SetVarImpl(StringPiece variable_name, const std::string& new_value) {
80 // On success, a nonzero value is returned.
81 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
82 UTF8ToWide(new_value).c_str());
83 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
84 // On success, zero is returned.
85 return !setenv(variable_name.data(), new_value.c_str(), 1);
89 bool UnSetVarImpl(StringPiece variable_name) {
91 // On success, a nonzero value is returned.
92 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr);
93 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
94 // On success, zero is returned.
95 return !unsetenv(variable_name.data());
104 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
105 // On Posix systems, this variable contains the location of the user's home
106 // directory. (e.g, /home/username/).
107 const char kHome[] = "HOME";
110 } // namespace env_vars
112 Environment::~Environment() = default;
115 std::unique_ptr<Environment> Environment::Create() {
116 return std::make_unique<EnvironmentImpl>();
119 bool Environment::HasVar(StringPiece variable_name) {
120 return GetVar(variable_name, nullptr);