w.deng | e87b500 | 2025-08-20 10:43:03 +0800 | [diff] [blame^] | 1 | #ifndef __snprintf_compat_h |
| 2 | #define __snprintf_compat_h |
| 3 | |
| 4 | /** |
| 5 | * @file |
| 6 | * @brief Do not use, json-c internal, may be changed or removed at any time. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Microsoft's _vsnprintf and _snprint don't always terminate |
| 11 | * the string, so use wrappers that ensure that. |
| 12 | */ |
| 13 | |
| 14 | #include <stdarg.h> |
| 15 | |
| 16 | #if !defined(HAVE_SNPRINTF) && (defined(_MSC_VER) || defined(__MINGW32__)) |
| 17 | static int json_c_vsnprintf(char *str, size_t size, const char *format, va_list ap) |
| 18 | { |
| 19 | int ret; |
| 20 | ret = _vsnprintf(str, size, format, ap); |
| 21 | str[size - 1] = '\0'; |
| 22 | return ret; |
| 23 | } |
| 24 | #define vsnprintf json_c_vsnprintf |
| 25 | |
| 26 | static int json_c_snprintf(char *str, size_t size, const char *format, ...) |
| 27 | { |
| 28 | va_list ap; |
| 29 | int ret; |
| 30 | va_start(ap, format); |
| 31 | ret = json_c_vsnprintf(str, size, format, ap); |
| 32 | va_end(ap); |
| 33 | return ret; |
| 34 | } |
| 35 | #define snprintf json_c_snprintf |
| 36 | |
| 37 | #elif !defined(HAVE_SNPRINTF) /* !HAVE_SNPRINTF */ |
| 38 | #error Need vsnprintf! |
| 39 | #endif /* !HAVE_SNPRINTF && defined(WIN32) */ |
| 40 | |
| 41 | #endif /* __snprintf_compat_h */ |