1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "shill/shims/environment.h" 18 19 #include <cstdlib> 20 #include <unistd.h> 21 22 using std::map; 23 using std::string; 24 25 namespace shill { 26 27 namespace shims { 28 29 static base::LazyInstance<Environment> g_environment = 30 LAZY_INSTANCE_INITIALIZER; 31 Environment()32Environment::Environment() {} 33 ~Environment()34Environment::~Environment() {} 35 36 // static GetInstance()37Environment* Environment::GetInstance() { 38 return g_environment.Pointer(); 39 } 40 GetVariable(const string & name,string * value)41bool Environment::GetVariable(const string& name, string* value) { 42 char* v = getenv(name.c_str()); 43 if (v) { 44 *value = v; 45 return true; 46 } 47 return false; 48 } 49 AsMap()50map<string, string> Environment::AsMap() { 51 map<string, string> env; 52 for (char** var = environ; var && *var; var++) { 53 string v = *var; 54 size_t assign = v.find('='); 55 if (assign != string::npos) { 56 env[v.substr(0, assign)] = v.substr(assign + 1); 57 } 58 } 59 return env; 60 } 61 62 } // namespace shims 63 64 } // namespace shill 65