MSVC: compat: implement setenv()
diff --git a/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake
index eb4b103..08c29a2 100644
--- a/CMakeModules/UseCompat.cmake
+++ b/CMakeModules/UseCompat.cmake
@@ -56,6 +56,7 @@
check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
check_symbol_exists(dirname "libgen.h" HAVE_DIRNAME)
+ check_symbol_exists(setenv "stdlib.h" HAVE_SETENV)
unset(CMAKE_REQUIRED_DEFINITIONS)
unset(CMAKE_REQUIRED_LIBRARIES)
diff --git a/compat/compat.c b/compat/compat.c
index 6763413..1290cac 100644
--- a/compat/compat.c
+++ b/compat/compat.c
@@ -304,3 +304,25 @@
#error No dirname() implementation for this platform is available.
#endif
#endif
+
+#ifndef HAVE_SETENV
+#ifdef _WIN32
+int
+setenv(const char *name, const char *value, int overwrite)
+{
+ int errcode = 0;
+
+ if (!overwrite) {
+ size_t envsize = 0;
+ errcode = getenv_s(&envsize, NULL, 0, name);
+ if (errcode || envsize) {
+ return errcode;
+ }
+ }
+ return _putenv_s(name, value);
+}
+
+#else
+#error No setenv() implementation for this platform is available.
+#endif
+#endif
diff --git a/compat/compat.h.in b/compat/compat.h.in
index 7c641f7..df9449b 100644
--- a/compat/compat.h.in
+++ b/compat/compat.h.in
@@ -67,6 +67,7 @@
#cmakedefine HAVE_MMAP
#cmakedefine HAVE_DIRNAME
#cmakedefine HAVE_STRCASECMP
+#cmakedefine HAVE_SETENV
#ifndef bswap64
#define bswap64(val) \
@@ -188,4 +189,8 @@
# define strtok_r strtok_s
#endif
+#ifndef HAVE_SETENV
+int setenv(const char *name, const char *value, int overwrite);
+#endif
+
#endif /* _COMPAT_H_ */