Marc-André Lemburg | e5006eb | 2001-07-31 13:24:44 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
| 3 | |
| 4 | /* snprintf() emulation for platforms which don't have it (yet). |
| 5 | |
| 6 | Return value |
| 7 | |
| 8 | The number of characters printed (not including the trailing |
| 9 | `\0' used to end output to strings) or a negative number in |
| 10 | case of an error. |
| 11 | |
| 12 | PyOS_snprintf and PyOS_vsnprintf do not write more than size |
| 13 | bytes (including the trailing '\0'). |
| 14 | |
| 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. |
| 19 | |
| 20 | */ |
| 21 | |
| 22 | #include <ctype.h> |
| 23 | |
| 24 | #ifndef HAVE_SNPRINTF |
| 25 | |
| 26 | static |
| 27 | int myvsnprintf(char *str, size_t size, const char *format, va_list va) |
| 28 | { |
| 29 | char *buffer = PyMem_Malloc(size + 512); |
| 30 | int len; |
| 31 | |
| 32 | if (buffer == NULL) |
| 33 | return -1; |
| 34 | len = vsprintf(buffer, format, va); |
| 35 | if (len < 0) { |
| 36 | PyMem_Free(buffer); |
| 37 | return len; |
| 38 | } |
| 39 | len++; |
Tim Peters | aa6111f | 2001-07-31 22:10:29 +0000 | [diff] [blame^] | 40 | assert(len >= 0); |
| 41 | if ((size_t)len > size + 512) |
Marc-André Lemburg | e5006eb | 2001-07-31 13:24:44 +0000 | [diff] [blame] | 42 | Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf"); |
Tim Peters | aa6111f | 2001-07-31 22:10:29 +0000 | [diff] [blame^] | 43 | if ((size_t)len > size) { |
Marc-André Lemburg | e5006eb | 2001-07-31 13:24:44 +0000 | [diff] [blame] | 44 | PyMem_Free(buffer); |
| 45 | return len - 1; |
| 46 | } |
| 47 | memcpy(str, buffer, len); |
| 48 | PyMem_Free(buffer); |
| 49 | return len - 1; |
| 50 | } |
| 51 | |
| 52 | int PyOS_snprintf(char *str, size_t size, const char *format, ...) |
| 53 | { |
| 54 | int rc; |
| 55 | va_list va; |
| 56 | |
| 57 | va_start(va, format); |
| 58 | rc = myvsnprintf(str, size, format, va); |
| 59 | va_end(va); |
| 60 | return rc; |
| 61 | } |
| 62 | |
| 63 | int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) |
| 64 | { |
| 65 | return myvsnprintf(str, size, format, va); |
| 66 | } |
| 67 | |
| 68 | #else |
| 69 | |
| 70 | /* Make sure that a C API is included in the lib */ |
| 71 | |
| 72 | #ifdef PyOS_snprintf |
| 73 | # undef PyOS_snprintf |
| 74 | #endif |
| 75 | |
| 76 | int PyOS_snprintf(char *str, size_t size, const char *format, ...) |
| 77 | { |
| 78 | int rc; |
| 79 | va_list va; |
| 80 | |
| 81 | va_start(va, format); |
| 82 | rc = vsnprintf(str, size, format, va); |
| 83 | va_end(va); |
| 84 | return rc; |
| 85 | } |
| 86 | |
| 87 | #ifdef PyOS_vsnprintf |
| 88 | # undef PyOS_vsnprintf |
| 89 | #endif |
| 90 | |
| 91 | int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) |
| 92 | { |
| 93 | return vsnprintf(str, size, format, va); |
| 94 | } |
| 95 | |
| 96 | #endif |
| 97 | |