| 1 |
/* |
| 2 |
* vsnprintf() |
| 3 |
* |
| 4 |
* Poor substitute for a real vsnprintf() function for systems |
| 5 |
* that don't have them... |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "compiler.h" |
| 9 |
|
| 10 |
|
| 11 |
#include "nasmlib.h" |
| 12 |
#include "error.h" |
| 13 |
|
| 14 |
#if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF) |
| 15 |
|
| 16 |
#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */ |
| 17 |
|
| 18 |
static char snprintf_buffer[BUFFER_SIZE]; |
| 19 |
|
| 20 |
int vsnprintf(char *str, size_t size, const char *format, va_list ap) |
| 21 |
{ |
| 22 |
int rv, bytes; |
| 23 |
|
| 24 |
if (size > BUFFER_SIZE) { |
| 25 |
nasm_panic("vsnprintf: size (%llu) > BUFFER_SIZE (%d)", |
| 26 |
(unsigned long long)size, BUFFER_SIZE); |
| 27 |
size = BUFFER_SIZE; |
| 28 |
} |
| 29 |
|
| 30 |
rv = vsprintf(snprintf_buffer, format, ap); |
| 31 |
if (rv >= BUFFER_SIZE) |
| 32 |
nasm_panic("vsnprintf buffer overflow"); |
| 33 |
|
| 34 |
if (size > 0) { |
| 35 |
if ((size_t)rv < size-1) |
| 36 |
bytes = rv; |
| 37 |
else |
| 38 |
bytes = size-1; |
| 39 |
memcpy(str, snprintf_buffer, bytes); |
| 40 |
str[bytes] = '\0'; |
| 41 |
} |
| 42 |
|
| 43 |
return rv; |
| 44 |
} |
| 45 |
|
| 46 |
#endif |