summaryrefslogtreecommitdiffhomepage
path: root/compat/setenv.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'compat/setenv.cpp')
-rw-r--r--compat/setenv.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/compat/setenv.cpp b/compat/setenv.cpp
new file mode 100644
index 00000000..cb98fa3d
--- /dev/null
+++ b/compat/setenv.cpp
@@ -0,0 +1,37 @@
+#include "setenv.hpp"
+#include <cerrno>
+#include <cstdlib>
+#include <cstring>
+#include <Corrade/Containers/String.h>
+#include <Corrade/Containers/StringIterable.h>
+
+namespace floormat {
+
+#ifdef _WIN32
+int setenv(const char* name, const char* value, int overwrite)
+{
+ if (!std::strchr(name, '=') || !value || !*value)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+ if (!overwrite)
+ if (const auto* s = std::getenv(name); s && *s)
+ return 0;
+
+ return _putenv("="_s.join(StringIterable{name, value}).data());
+}
+
+int unsetenv(const char* name)
+{
+ if (!name || !*name)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ return _putenv("="_s.join(StringIterable{name, ""_s}).data());
+}
+#endif
+
+} // namespace floormat