blob: 5a600fc72c615a1a2e9bb1b613f5dd00ec21cae5 [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
H. Peter Anvinfe501952007-10-02 21:53:51 -07008#include "compiler.h"
9
H. Peter Anvin304b6052007-09-28 10:50:20 -070010#include <stdio.h>
11#include <stdlib.h>
12#include <stdarg.h>
13#include <string.h>
14
15#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080016#include "error.h"
H. Peter Anvin304b6052007-09-28 10:50:20 -070017
H. Peter Anvincc147f72016-03-08 02:06:39 -080018#if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF)
19
Cyrill Gorcunov5d269782010-04-01 01:09:35 +040020#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
H. Peter Anvin304b6052007-09-28 10:50:20 -070021
22static char snprintf_buffer[BUFFER_SIZE];
23
24int vsnprintf(char *str, size_t size, const char *format, va_list ap)
25{
26 int rv, bytes;
27
28 if (size > BUFFER_SIZE) {
H. Peter Anvin41087062016-03-03 14:27:34 -080029 nasm_panic(ERR_NOFILE,
Cyrill Gorcunovcdcd1f72010-04-01 01:17:00 +040030 "vsnprintf: size (%d) > BUFFER_SIZE (%d)",
31 size, BUFFER_SIZE);
Cyrill Gorcunov5d269782010-04-01 01:09:35 +040032 size = BUFFER_SIZE;
H. Peter Anvin304b6052007-09-28 10:50:20 -070033 }
34
35 rv = vsprintf(snprintf_buffer, format, ap);
Cyrill Gorcunovcdcd1f72010-04-01 01:17:00 +040036 if (rv >= BUFFER_SIZE)
H. Peter Anvin41087062016-03-03 14:27:34 -080037 nasm_panic(ERR_NOFILE, "vsnprintf buffer overflow");
H. Peter Anvin304b6052007-09-28 10:50:20 -070038
H. Peter Anvin304b6052007-09-28 10:50:20 -070039 if (size > 0) {
Cyrill Gorcunov5d269782010-04-01 01:09:35 +040040 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';
H. Peter Anvin304b6052007-09-28 10:50:20 -070046 }
47
48 return rv;
49}
H. Peter Anvincc147f72016-03-08 02:06:39 -080050
51#endif