diff --git a/src/include/w32functions.h b/src/include/w32functions.h index c7a885a6..e68b90d6 100644 --- a/src/include/w32functions.h +++ b/src/include/w32functions.h @@ -191,6 +191,9 @@ int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]); */ int MHD_W32_random_(void); +/* Emulate snprintf function on W32 */ +int W32_snprintf(char *__restrict s, size_t n, const char *__restrict format, ...); + #ifdef __cplusplus } #endif diff --git a/src/platform/w32functions.c b/src/platform/w32functions.c index 7a0686e8..e4f93db0 100644 --- a/src/platform/w32functions.c +++ b/src/platform/w32functions.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include /** @@ -640,3 +642,27 @@ int MHD_W32_random_(void) & 0x7fffffff; return (int)rnd_val; } + +/* Emulate snprintf function on W32 */ +int W32_snprintf(char *__restrict s, size_t n, const char *__restrict format, ...) +{ + int ret; + va_list args; + if (0 != n && NULL != s ) + { + va_start(args, format); + ret = _vsnprintf(s, n, format, args); + va_end(args); + if (n == ret) + s[n - 1] = 0; + if (ret >= 0) + return ret; + } + va_start(args, format); + ret = _vscprintf(format, args); + va_end(args); + if (0 <= ret && 0 != n && NULL == s) + return -1; + + return ret; +}