authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-28 02:22:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 15:12:47-07:00
logb19a29c70f39877b326360e121508898bed220f4
tree20df00eaa173843f590991ff7cfad25002a3709b
parent55be10486ef6aad1f497fb0ab85ac36043b79652

mingw: add missing vscprintf.c file

closes #13733 Thanks to @kcbanner for finding this.

2 files changed, 87 insertions(+), 0 deletions(-)

lib/libc/mingw/stdio/_vscprintf.c created+86
......@@ -0,0 +1,86 @@
1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6#include <windows.h>
7#include <stdarg.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <errno.h>
12
13/* emulation of _vscprintf() via _vsnprintf() */
14static int __cdecl emu_vscprintf(const char * __restrict__ format, va_list arglist)
15{
16 char *buffer, *new_buffer;
17 size_t size;
18 int ret;
19
20 /* if format is a null pointer, _vscprintf() returns -1 and sets errno to EINVAL */
21 if (!format) {
22 _set_errno(EINVAL);
23 return -1;
24 }
25
26 /* size for _vsnprintf() must be non-zero and buffer must have place for terminating null character */
27 size = strlen(format) * 2 + 1;
28 buffer = malloc(size);
29
30 if (!buffer) {
31 _set_errno(ENOMEM);
32 return -1;
33 }
34
35 /* if the number of characters to write is greater than size, _vsnprintf() returns -1 */
36 while (size < SIZE_MAX/2 && (ret = _vsnprintf(buffer, size, format, arglist)) < 0) {
37 /* in this case try with larger buffer */
38 size *= 2;
39 new_buffer = realloc(buffer, size);
40 if (!new_buffer)
41 break;
42 buffer = new_buffer;
43 }
44
45 free(buffer);
46
47 if (ret < 0) {
48 _set_errno(ENOMEM);
49 return -1;
50 }
51
52 return ret;
53}
54
55#ifndef __LIBMSVCRT_OS__
56
57int (__cdecl *__MINGW_IMP_SYMBOL(_vscprintf))(const char * __restrict__, va_list) = emu_vscprintf;
58
59#else
60
61#include <msvcrt.h>
62
63static int __cdecl init_vscprintf(const char * __restrict__ format, va_list arglist);
64
65int (__cdecl *__MINGW_IMP_SYMBOL(_vscprintf))(const char * __restrict__, va_list) = init_vscprintf;
66
67static int __cdecl init_vscprintf(const char * __restrict__ format, va_list arglist)
68{
69 HMODULE msvcrt = __mingw_get_msvcrt_handle();
70 int (__cdecl *func)(const char * __restrict__, va_list) = NULL;
71
72 if (msvcrt)
73 func = (int (__cdecl *)(const char * __restrict__, va_list))GetProcAddress(msvcrt, "_vscprintf");
74
75 if (!func)
76 func = emu_vscprintf;
77
78 return (__MINGW_IMP_SYMBOL(_vscprintf) = func)(format, arglist);
79}
80
81#endif
82
83int __cdecl _vscprintf(const char * __restrict__ format, va_list arglist)
84{
85 return __MINGW_IMP_SYMBOL(_vscprintf)(format, arglist);
86}
src/mingw.zig+1
......@@ -539,6 +539,7 @@ const msvcrt_common_src = [_][]const u8{
539539 "stdio" ++ path.sep_str ++ "acrt_iob_func.c",
540540 "stdio" ++ path.sep_str ++ "snprintf_alias.c",
541541 "stdio" ++ path.sep_str ++ "vsnprintf_alias.c",
542 "stdio" ++ path.sep_str ++ "_vscprintf.c",
542543 "misc" ++ path.sep_str ++ "_configthreadlocale.c",
543544 "misc" ++ path.sep_str ++ "_get_current_locale.c",
544545 "misc" ++ path.sep_str ++ "invalid_parameter_handler.c",