Reworked test files for better error reporting
[nasm/perl-rewrite.git] / lib / vsnprintf.c
blobf5ae2feb15c405d16296ce8e4d7aeb9ccadb0249
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_malloc_error(ERR_PANIC|ERR_NOFILE,
27 "snprintf: 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_malloc_error(ERR_PANIC|ERR_NOFILE,
35 "snprintf buffer overflow");
38 if (size > 0) {
39 if ((size_t)rv < size-1)
40 bytes = rv;
41 else
42 bytes = size-1;
44 memcpy(str, snprintf_buffer, bytes);
45 str[bytes] = '\0';
48 return rv;