|
| 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() */ |
| 14 | +static 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 | + |
| 57 | +int (__cdecl *__MINGW_IMP_SYMBOL(_vscprintf))(const char * __restrict__, va_list) = emu_vscprintf; |
| 58 | + |
| 59 | +#else |
| 60 | + |
| 61 | +#include <msvcrt.h> |
| 62 | + |
| 63 | +static int __cdecl init_vscprintf(const char * __restrict__ format, va_list arglist); |
| 64 | + |
| 65 | +int (__cdecl *__MINGW_IMP_SYMBOL(_vscprintf))(const char * __restrict__, va_list) = init_vscprintf; |
| 66 | + |
| 67 | +static 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 | + |
| 83 | +int __cdecl _vscprintf(const char * __restrict__ format, va_list arglist) |
| 84 | +{ |
| 85 | + return __MINGW_IMP_SYMBOL(_vscprintf)(format, arglist); |
| 86 | +} |
0 commit comments