From b72f23ce67bc30d3bedbeabcf0b6d9c1fbf2a64f Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 15 Jun 2018 12:18:52 +0300 Subject: Use portable environment variable manipulation functions --- build2/b.cxx | 14 ++++++-------- build2/functions-builtin.cxx | 16 ++++++---------- build2/utility.hxx | 7 +++++-- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/build2/b.cxx b/build2/b.cxx index de985e8..cd738f2 100644 --- a/build2/b.cxx +++ b/build2/b.cxx @@ -8,8 +8,6 @@ # include #endif -#include // getenv() _putenv()(_WIN32) - #ifdef __GLIBCXX__ # include #endif @@ -144,15 +142,15 @@ main (int argc, char* argv[]) // #ifdef _WIN32 { - string mp ("PATH="); - if (const char* p = getenv ("PATH")) + string mp; + if (optional p = getenv ("PATH")) { - mp += p; + mp = move (*p); mp += ';'; } mp += "/bin"; - _putenv (mp.c_str ()); + setenv ("PATH", mp); } #endif @@ -465,11 +463,11 @@ main (int argc, char* argv[]) // if (verb >= 5) { - const char* p (getenv ("PATH")); + optional p (getenv ("PATH")); trace << "work: " << work; trace << "home: " << home; - trace << "path: " << (p != nullptr ? p : ""); + trace << "path: " << (p ? *p : ""); trace << "jobs: " << jobs; } diff --git a/build2/functions-builtin.cxx b/build2/functions-builtin.cxx index 350bddf..45ad7df 100644 --- a/build2/functions-builtin.cxx +++ b/build2/functions-builtin.cxx @@ -2,28 +2,24 @@ // copyright : Copyright (c) 2014-2018 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file -#include // getenv() - #include #include -using namespace std; - namespace build2 { // Return NULL value if an environment variable is not set, untyped value // otherwise. // static inline value - getenv (const string& name) + getvar (const string& name) { - const char* v (::getenv (name.c_str ())); + optional v (getenv (name)); - if (v == nullptr) + if (!v) return value (); names r; - r.emplace_back (to_name (v)); + r.emplace_back (to_name (*v)); return value (move (r)); } @@ -49,12 +45,12 @@ namespace build2 // f["getenv"] = [](string name) { - return getenv (name); + return getvar (name); }; f["getenv"] = [](names name) { - return getenv (convert (move (name))); + return getvar (convert (move (name))); }; } } diff --git a/build2/utility.hxx b/build2/utility.hxx index 4ca12ca..81a5d87 100644 --- a/build2/utility.hxx +++ b/build2/utility.hxx @@ -16,8 +16,7 @@ #include -#include // combine_hash(), reverse_iterate(), case*(), - // etc +#include // combine_hash(), reverse_iterate(), etc #include @@ -74,6 +73,10 @@ namespace build2 using butl::make_guard; using butl::make_exception_guard; + using butl::getenv; + using butl::setenv; + using butl::unsetenv; + using butl::throw_generic_error; using butl::throw_system_error; -- cgit v1.1