changes.src: update for a 2.12.03 release
[nasm.git] / lib / vsnprintf.c
blobecca3e661ab244543806de1cf089ff56de89b26e
1 /*
2 * vsnprintf()
4 * Poor substitute for a real vsnprintf() function for systems
5 * that don't have them...
6 */
8 #include "compiler.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
15 #include "nasmlib.h"
17 #define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
19 static char snprintf_buffer[BUFFER_SIZE];
21 int vsnprintf(char *str, size_t size, const char *format, va_list ap)
23 int rv, bytes;
25 if (size > BUFFER_SIZE) {
26 nasm_panic(ERR_NOFILE,
27 "vsnprintf: size (%d) > BUFFER_SIZE (%d)",
28 size, BUFFER_SIZE);
29 size = BUFFER_SIZE;
32 rv = vsprintf(snprintf_buffer, format, ap);
33 if (rv >= BUFFER_SIZE)
34 nasm_panic(ERR_NOFILE, "vsnprintf buffer overflow");
36 if (size > 0) {
37 if ((size_t)rv < size-1)
38 bytes = rv;
39 else
40 bytes = size-1;
41 memcpy(str, snprintf_buffer, bytes);
42 str[bytes] = '\0';
45 return rv;