macho: Improve macho_calculate_sizes
[nasm.git] / stdlib / vsnprintf.c
blobb8fd082ab6e9744d05d047ae4ff40cb956f8b6db
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 #if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF)
19 #define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
21 static char snprintf_buffer[BUFFER_SIZE];
23 int vsnprintf(char *str, size_t size, const char *format, va_list ap)
25 int rv, bytes;
27 if (size > BUFFER_SIZE) {
28 nasm_panic(ERR_NOFILE,
29 "vsnprintf: size (%d) > BUFFER_SIZE (%d)",
30 size, BUFFER_SIZE);
31 size = BUFFER_SIZE;
34 rv = vsprintf(snprintf_buffer, format, ap);
35 if (rv >= BUFFER_SIZE)
36 nasm_panic(ERR_NOFILE, "vsnprintf buffer overflow");
38 if (size > 0) {
39 if ((size_t)rv < size-1)
40 bytes = rv;
41 else
42 bytes = size-1;
43 memcpy(str, snprintf_buffer, bytes);
44 str[bytes] = '\0';
47 return rv;
50 #endif