Original MSVC 6.0 msvcrt.dll library does not provide _vscprintf()
function. MinGW-w64 __ms_snprintf() and __ms_vsnprintf() implementations
call _vscprintf() function. So include fallback _vscprintf() implementation
into MinGW-w64 crt code to allow applications to use snprintf() with
original MSVC 6.0 msvcrt.dll library.
---
 mingw-w64-crt/Makefile.am              |  1 +
 mingw-w64-crt/lib-common/msvcrt.def.in |  4 ++
 mingw-w64-crt/misc/_vscprintf.c        | 72 ++++++++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 mingw-w64-crt/misc/_vscprintf.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index e6b644ec1d44..1be9250c1e53 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -282,6 +282,7 @@ src_msvcrt32=\
   misc/_create_locale.c \
   misc/_free_locale.c \
   misc/_get_current_locale.c \
+  misc/_vscprintf.c \
   misc/lc_locale_func.c \
   misc/wassert.c
 
diff --git a/mingw-w64-crt/lib-common/msvcrt.def.in 
b/mingw-w64-crt/lib-common/msvcrt.def.in
index 0ea9d388fbe4..17bc7e9a076e 100644
--- a/mingw-w64-crt/lib-common/msvcrt.def.in
+++ b/mingw-w64-crt/lib-common/msvcrt.def.in
@@ -1111,7 +1111,11 @@ _vprintf_l
 _vprintf_p
 _vprintf_p_l
 _vprintf_s_l
+#ifdef DEF_I386
+; _vscprintf Replaced by emu
+#else
 _vscprintf
+#endif
 _vscprintf_l
 _vscprintf_p_l
 _vscwprintf
diff --git a/mingw-w64-crt/misc/_vscprintf.c b/mingw-w64-crt/misc/_vscprintf.c
new file mode 100644
index 000000000000..924167d2427f
--- /dev/null
+++ b/mingw-w64-crt/misc/_vscprintf.c
@@ -0,0 +1,72 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+#include <windows.h>
+#include <msvcrt.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+/* emulation of _vscprintf() via _vsnprintf() */
+static int __cdecl emu_vscprintf(const char * __restrict__ format, va_list 
arglist)
+{
+    char *buffer, *new_buffer;
+    size_t size;
+    int ret;
+
+    /* if format is a null pointer, _vscprintf() returns -1 and sets errno to 
EINVAL */
+    if (!format) {
+        _set_errno(EINVAL);
+        return -1;
+    }
+
+    /* size for _vsnprintf() must be non-zero and buffer must have place for 
terminating null character */
+    size = strlen(format) * 2 + 1;
+    buffer = malloc(size);
+
+    if (!buffer) {
+        _set_errno(ENOMEM);
+        return -1;
+    }
+
+    /* if the number of characters to write is greater than size, _vsnprintf() 
returns -1 */
+    while (size < SIZE_MAX/2 && (ret = _vsnprintf(buffer, size, format, 
arglist)) < 0) {
+        /* in this case try with larger buffer */
+        size *= 2;
+        new_buffer = realloc(buffer, size);
+        if (!new_buffer)
+            break;
+        buffer = new_buffer;
+    }
+
+    free(buffer);
+
+    if (ret < 0) {
+        _set_errno(ENOMEM);
+        return -1;
+    }
+
+    return ret;
+}
+
+static int __cdecl init_vscprintf(const char * __restrict__ format, va_list 
arglist);
+
+int (__cdecl *__MINGW_IMP_SYMBOL(_vscprintf))(const char * __restrict__, 
va_list) = init_vscprintf;
+
+static int __cdecl init_vscprintf(const char * __restrict__ format, va_list 
arglist)
+{
+    HMODULE msvcrt = __mingw_get_msvcrt_handle();
+    int (__cdecl *func)(const char * __restrict__, va_list) = NULL;
+
+    if (msvcrt)
+        func = (int (__cdecl *)(const char * __restrict__, 
va_list))GetProcAddress(msvcrt, "_vscprintf");
+
+    if (!func)
+        func = emu_vscprintf;
+
+    return (__MINGW_IMP_SYMBOL(_vscprintf) = func)(format, arglist);
+}
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to