Apache Mesos
getenv.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_POSIX_GETENV_HPP__
14 #define __STOUT_OS_POSIX_GETENV_HPP__
15 
16 #include <stdlib.h>
17 
18 #include <string>
19 
20 #include <stout/none.hpp>
21 #include <stout/option.hpp>
22 
23 
24 namespace os {
25 
26 // Looks in the environment variables for the specified key and
27 // returns a string representation of its value. If no environment
28 // variable matching key is found, None() is returned.
29 inline Option<std::string> getenv(const std::string& key)
30 {
31  char* value = ::getenv(key.c_str());
32 
33  if (value == nullptr) {
34  return None();
35  }
36 
37  return std::string(value);
38 }
39 
40 } // namespace os {
41 
42 #endif // __STOUT_OS_POSIX_GETENV_HPP__
Option< std::string > getenv(const std::string &key)
Definition: getenv.hpp:29
Definition: none.hpp:27