aboutsummaryrefslogtreecommitdiff
path: root/libbutl/utility.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-06-14 18:13:04 +0200
committerKaren Arutyunov <karen@codesynthesis.com>2018-06-15 14:21:58 +0300
commit481f9ba1aee62fea092184f2243d210a8686781f (patch)
treea54fc536d835c5277038e145fabc1255c42ff8ac /libbutl/utility.cxx
parent5d424ea127333859a32addaf9e28eae07a4dc9f6 (diff)
Add portable environment variable manipulation functions
Diffstat (limited to 'libbutl/utility.cxx')
-rw-r--r--libbutl/utility.cxx32
1 files changed, 32 insertions, 0 deletions
diff --git a/libbutl/utility.cxx b/libbutl/utility.cxx
index 8e2422aa..502586c 100644
--- a/libbutl/utility.cxx
+++ b/libbutl/utility.cxx
@@ -10,6 +10,8 @@
#include <libbutl/win32-utility.hxx>
#endif
+#include <stdlib.h> // setenv(), unsetenv(), _putenv()
+
#ifndef __cpp_lib_modules
#include <string>
#include <cstddef>
@@ -124,6 +126,36 @@ namespace butl
return l;
}
+
+ void
+ setenv (const string& name, const string& value)
+ {
+#ifndef _WIN32
+ if (::setenv (name.c_str (), value.c_str (), 1 /* overwrite */) == -1)
+ throw_generic_error (errno);
+#else
+ // The documentation doesn't say how to obtain the failure reason, so we
+ // will assume it to always be EINVAL (as the most probable one).
+ //
+ if (_putenv (string (name + '=' + value).c_str ()) == -1)
+ throw_generic_error (EINVAL);
+#endif
+ }
+
+ void
+ unsetenv (const string& name)
+ {
+#ifndef _WIN32
+ if (::unsetenv (name.c_str ()) == -1)
+ throw_generic_error (errno);
+#else
+ // The documentation doesn't say how to obtain the failure reason, so we
+ // will assume it to always be EINVAL (as the most probable one).
+ //
+ if (_putenv (string (name + '=').c_str ()) == -1)
+ throw_generic_error (EINVAL);
+#endif
+ }
}
namespace std