Darin Petkov | 804e8d0 | 2012-10-10 16:44:36 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/shims/environment.h" |
| 6 | |
| 7 | #include <cstdlib> |
Han Shen | c854aa7 | 2012-10-18 15:25:11 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
Darin Petkov | 804e8d0 | 2012-10-10 16:44:36 +0200 | [diff] [blame] | 9 | |
| 10 | using std::map; |
| 11 | using std::string; |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | namespace shims { |
| 16 | |
| 17 | static base::LazyInstance<Environment> g_environment = |
| 18 | LAZY_INSTANCE_INITIALIZER; |
| 19 | |
| 20 | Environment::Environment() {} |
| 21 | |
| 22 | Environment::~Environment() {} |
| 23 | |
| 24 | // static |
| 25 | Environment *Environment::GetInstance() { |
| 26 | return g_environment.Pointer(); |
| 27 | } |
| 28 | |
| 29 | bool Environment::GetVariable(const string &name, string *value) { |
| 30 | char *v = getenv(name.c_str()); |
| 31 | if (v) { |
| 32 | *value = v; |
| 33 | return true; |
| 34 | } |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | map<string, string> Environment::AsMap() { |
| 39 | map<string, string> env; |
| 40 | for (char **var = environ; var && *var; var++) { |
| 41 | string v = *var; |
| 42 | size_t assign = v.find('='); |
| 43 | if (assign != string::npos) { |
| 44 | env[v.substr(0, assign)] = v.substr(assign + 1); |
| 45 | } |
| 46 | } |
| 47 | return env; |
| 48 | } |
| 49 | |
| 50 | } // namespace shims |
| 51 | |
| 52 | } // namespace shill |