aboutsummaryrefslogtreecommitdiff
path: root/libbutl/utility.cxx
diff options
context:
space:
mode:
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