4 /* snprintf() emulation for platforms which don't have it (yet).
8 The number of characters printed (not including the trailing
9 `\0' used to end output to strings) or a negative number in
12 PyOS_snprintf and PyOS_vsnprintf do not write more than size
13 bytes (including the trailing '\0').
15 If the output would have been truncated, they return the number
16 of characters (excluding the trailing '\0') which would have
17 been written to the final string if enough space had been
18 available. This is inline with the C99 standard.
27 int myvsnprintf(char *str
, size_t size
, const char *format
, va_list va
)
29 char *buffer
= PyMem_Malloc(size
+ 512);
34 len
= vsprintf(buffer
, format
, va
);
41 if ((size_t)len
> size
+ 512)
42 Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
43 if ((size_t)len
> size
) {
47 memcpy(str
, buffer
, len
);
52 int PyOS_snprintf(char *str
, size_t size
, const char *format
, ...)
58 rc
= myvsnprintf(str
, size
, format
, va
);
63 int PyOS_vsnprintf(char *str
, size_t size
, const char *format
, va_list va
)
65 return myvsnprintf(str
, size
, format
, va
);
70 /* Make sure that a C API is included in the lib */
76 int PyOS_snprintf(char *str
, size_t size
, const char *format
, ...)
82 rc
= vsnprintf(str
, size
, format
, va
);
88 # undef PyOS_vsnprintf
91 int PyOS_vsnprintf(char *str
, size_t size
, const char *format
, va_list va
)
93 return vsnprintf(str
, size
, format
, va
);