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 | #include <stdio.h> |
11 | #include <stdlib.h> |
12 | #include <stdarg.h> |
13 | #include <string.h> |
14 | |
15 | #include "nasmlib.h" |
16 | #include "error.h" |
17 | |
18 | #if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF) |
19 | |
20 | #define BUFFER_SIZE 65536 /* Bigger than any string we might print... */ |
21 | |
22 | static char snprintf_buffer[BUFFER_SIZE]; |
23 | |
24 | int vsnprintf(char *str, size_t size, const char *format, va_list ap) |
25 | { |
26 | int rv, bytes; |
27 | |
28 | if (size > BUFFER_SIZE) { |
29 | nasm_panic(ERR_NOFILE, |
30 | "vsnprintf: size (%d) > BUFFER_SIZE (%d)" , |
31 | size, BUFFER_SIZE); |
32 | size = BUFFER_SIZE; |
33 | } |
34 | |
35 | rv = vsprintf(snprintf_buffer, format, ap); |
36 | if (rv >= BUFFER_SIZE) |
37 | nasm_panic(ERR_NOFILE, "vsnprintf buffer overflow" ); |
38 | |
39 | if (size > 0) { |
40 | if ((size_t)rv < size-1) |
41 | bytes = rv; |
42 | else |
43 | bytes = size-1; |
44 | memcpy(str, snprintf_buffer, bytes); |
45 | str[bytes] = '\0'; |
46 | } |
47 | |
48 | return rv; |
49 | } |
50 | |
51 | #endif |
52 | |