Pali Rohár | 73cf399 | 2024-11-15 17:59:51 +0100 | [diff] [blame] | 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 | #define __CRT__NO_INLINE |
| 7 | #include <stdarg.h> |
| 8 | #include <stdio.h> |
| 9 | |
| 10 | int __cdecl __ms_vswprintf(wchar_t *__restrict__ ws, size_t n, const wchar_t *__restrict__ format, va_list arg) |
| 11 | { |
| 12 | int retval; |
| 13 | |
| 14 | /* ISO C95+ vswprintf() returns negative value if buffer has zero size */ |
| 15 | if (n == 0) |
| 16 | return -1; |
| 17 | |
| 18 | /* |
| 19 | * _vsnwprintf() returns number of filled wide chars up to the N or -1 otherwise. |
| 20 | * ISO C95+ vswprintf() returns number of filled wide chars excluding the |
| 21 | * trailing nul (therefore up to the N-1) or -1 otherwise. |
| 22 | * So call _vsnwprintf() with n-1 to have correct return value (-1) when more |
| 23 | * wide chars (inc nul) were requested to write into the output buffer than N. |
| 24 | */ |
| 25 | retval = _vsnwprintf(ws, n-1, format, arg); |
| 26 | |
| 27 | /* _vsnwprintf() does not fill trailing nul wide char if there is not place for it */ |
| 28 | if (retval < 0 || (size_t)retval == n-1) |
| 29 | ws[n-1] = L'\0'; |
| 30 | |
| 31 | return retval; |
| 32 | } |