From 00d1d2c0e7cdc0b366300b95471edb11fd967be0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=91=D0=BE=D0=B1=D0=B1=D0=B8?= <lsm@cesanta.com>
Date: Fri, 27 Oct 2017 11:24:27 +0100
Subject: [PATCH] Make mjs & frozen vc98 & vc2017 friendly

PUBLISHED_FROM=7cde8b374d5e318b5cdd69fc5b4de86b2a9552f5
---
 mongoose.c | 16 ++++++++++++++--
 mongoose.h | 19 ++++++++++++++++++-
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 7f2ef6ee9..02d913368 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -1936,12 +1936,24 @@ int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
     *buf = NULL; /* LCOV_EXCL_START */
     while (len < 0) {
       MG_FREE(*buf);
+      if (size == 0) {
+        size = 5;
+      }
       size *= 2;
-      if ((*buf = (char *) MG_MALLOC(size)) == NULL) break;
+      if ((*buf = (char *) MG_MALLOC(size)) == NULL) {
+        len = -1;
+        break;
+      }
       va_copy(ap_copy, ap);
-      len = vsnprintf(*buf, size, fmt, ap_copy);
+      len = vsnprintf(*buf, size - 1, fmt, ap_copy);
       va_end(ap_copy);
     }
+
+    /*
+     * Microsoft version of vsnprintf() is not always null-terminated, so put
+     * the terminator manually
+     */
+    (*buf)[len] = 0;
     /* LCOV_EXCL_STOP */
   } else if (len >= (int) size) {
     /* Standard-compliant code path. Allocate a buffer that is large enough. */
diff --git a/mongoose.h b/mongoose.h
index 3b56e263e..6b8b80262 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -210,6 +210,12 @@
 #include <windows.h>
 #include <process.h>
 
+#if _MSC_VER < 1700
+typedef int bool;
+#else
+#include <stdbool.h>
+#endif
+
 #if defined(_MSC_VER) && _MSC_VER >= 1800
 #define strdup _strdup
 #endif
@@ -323,9 +329,16 @@ typedef struct _stati64 cs_stat_t;
 #define MG_NET_IF MG_NET_IF_SOCKET
 #endif
 
-int rmdir(const char *dirname);
 unsigned int sleep(unsigned int seconds);
 
+/* https://stackoverflow.com/questions/16647819/timegm-cross-platform */
+#define timegm _mkgmtime
+
+#define gmtime_r(a, b) \
+  do {                 \
+    *(b) = *gmtime(a); \
+  } while (0)
+
 #endif /* CS_PLATFORM == CS_P_WINDOWS */
 #endif /* CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_ */
 #ifdef MG_MODULE_LINES
@@ -374,6 +387,7 @@ unsigned int sleep(unsigned int seconds);
 #include <pthread.h>
 #include <signal.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -493,6 +507,7 @@ typedef struct stat cs_stat_t;
 #include <fcntl.h>
 #include <inttypes.h>
 #include <machine/endian.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <string.h>
 #include <sys/stat.h>
@@ -536,6 +551,7 @@ typedef struct stat cs_stat_t;
 #include <fcntl.h>
 #include <inttypes.h>
 #include <machine/endian.h>
+#include <stdbool.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/time.h>
@@ -642,6 +658,7 @@ int inet_pton(int af, const char *src, void *dst);
 #include <ctype.h>
 #include <errno.h>
 #include <inttypes.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <string.h>
 #include <time.h>
-- 
GitLab