From 02ada52a38576331f58744ffa83d9cbba6b53827 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 15 Jun 2018 11:55:47 +0300 Subject: Use portable environment variable manipulation functions --- bpkg/bpkg.cxx | 24 +++++++++++++++++------- bpkg/database.cxx | 14 ++------------ bpkg/utility.hxx | 4 ++++ 3 files changed, 23 insertions(+), 19 deletions(-) (limited to 'bpkg') diff --git a/bpkg/bpkg.cxx b/bpkg/bpkg.cxx index b9836ad..831b742 100644 --- a/bpkg/bpkg.cxx +++ b/bpkg/bpkg.cxx @@ -4,8 +4,6 @@ #ifndef _WIN32 # include // signal() -#else -# include // getenv(), _putenv() #endif #include // strcmp() @@ -50,6 +48,12 @@ using namespace std; using namespace butl; using namespace bpkg; +namespace bpkg +{ + int + main (int argc, char* argv[]); +} + // Get -d|--directory value if the option class O has it and empty path // otherwise. Note that for some commands (like rep-info) that allow // specifying empty path, the returned value is a string, not a dir_path. @@ -134,7 +138,7 @@ init (const common_options& co, return o; } -int +int bpkg:: main (int argc, char* argv[]) try { @@ -155,15 +159,15 @@ try // #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 @@ -368,3 +372,9 @@ catch (const std::exception& e) return 1; } */ + +int +main (int argc, char* argv[]) +{ + return bpkg::main (argc, argv); +} diff --git a/bpkg/database.cxx b/bpkg/database.cxx index f5102f0..3c55237 100644 --- a/bpkg/database.cxx +++ b/bpkg/database.cxx @@ -4,8 +4,6 @@ #include -#include // getenv() setenv()/_putenv() - #include #include @@ -25,7 +23,7 @@ namespace bpkg // BPKG_OPEN_CONFIG environment variable. A bit heavy-weight but seems like // the best option. // - static const char open_name[] = "BPKG_OPEN_CONFIG"; + static const string open_name ("BPKG_OPEN_CONFIG"); class conn_factory: public single_connection_factory // No need for pool. { @@ -36,21 +34,13 @@ namespace bpkg v.complete (); v.normalize (); -#ifndef _WIN32 - setenv (open_name, v.string ().c_str (), 1 /* overwrite */); -#else - _putenv ((string (open_name) + '=' + v.string ()).c_str ()); -#endif + setenv (open_name, v.string ()); } virtual ~conn_factory () { -#ifndef _WIN32 unsetenv (open_name); -#else - _putenv ((string (open_name) + '=').c_str ()); -#endif } }; diff --git a/bpkg/utility.hxx b/bpkg/utility.hxx index 367f03d..19d664e 100644 --- a/bpkg/utility.hxx +++ b/bpkg/utility.hxx @@ -40,6 +40,10 @@ namespace bpkg using butl::make_guard; using butl::make_exception_guard; + using butl::getenv; + using butl::setenv; + using butl::unsetenv; + // // using butl::auto_rmfile; -- cgit v1.1