blob: f1c032eaa352e824bbfc2e2103ae2672175e226b [file] [log] [blame]
Darin Petkov804e8d02012-10-10 16:44:36 +02001// 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 Shenc854aa72012-10-18 15:25:11 -07008#include <unistd.h>
Darin Petkov804e8d02012-10-10 16:44:36 +02009
10using std::map;
11using std::string;
12
13namespace shill {
14
15namespace shims {
16
17static base::LazyInstance<Environment> g_environment =
18 LAZY_INSTANCE_INITIALIZER;
19
20Environment::Environment() {}
21
22Environment::~Environment() {}
23
24// static
25Environment *Environment::GetInstance() {
26 return g_environment.Pointer();
27}
28
29bool 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
38map<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