blob: b2b19d9dc70d69d0a6cd37bb6c4fe5e91c35407b [file] [log] [blame]
H. Peter Anvin304b6052007-09-28 10:50:20 -07001/*
2 * vsnprintf()
3 *
4 * Poor substitute for a real vsnprintf() function for systems
5 * that don't have them...
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <stdarg.h>
11#include <string.h>
12
13#include "nasmlib.h"
14
15extern efunc nasm_malloc_error;
16
17#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
18
19static char snprintf_buffer[BUFFER_SIZE];
20
21int vsnprintf(char *str, size_t size, const char *format, va_list ap)
22{
23 int rv, bytes;
24
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;
30 }
31
32 rv = vsprintf(snprintf_buffer, format, ap);
33 if (rv > BUFFER_SIZE) {
34 nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
35 "snprintf buffer overflow");
36 }
37
38 if (rv < (int)size-1)
39 bytes = rv;
40 else
41 bytes = size-1;
42
43 if (size > 0) {
44 memcpy(str, snprintf_buffer, bytes);
45 str[bytes] = '\0';
46 }
47
48 return rv;
49}