MSVC: compat: implement setenv()
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_ */