2007-09-29 01:50:20 +08:00
|
|
|
/*
|
|
|
|
* vsnprintf()
|
|
|
|
*
|
|
|
|
* Poor substitute for a real vsnprintf() function for systems
|
|
|
|
* that don't have them...
|
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2007-09-29 01:50:20 +08:00
|
|
|
|
|
|
|
#include "nasmlib.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "error.h"
|
2007-09-29 01:50:20 +08:00
|
|
|
|
2016-03-08 18:06:39 +08:00
|
|
|
#if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF)
|
|
|
|
|
2010-04-01 05:09:35 +08:00
|
|
|
#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
|
2007-09-29 01:50:20 +08:00
|
|
|
|
|
|
|
static char snprintf_buffer[BUFFER_SIZE];
|
|
|
|
|
|
|
|
int vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
int rv, bytes;
|
|
|
|
|
|
|
|
if (size > BUFFER_SIZE) {
|
2018-06-16 09:20:17 +08:00
|
|
|
nasm_panic("vsnprintf: size (%d) > BUFFER_SIZE (%d)",
|
2010-04-01 05:17:00 +08:00
|
|
|
size, BUFFER_SIZE);
|
2010-04-01 05:09:35 +08:00
|
|
|
size = BUFFER_SIZE;
|
2007-09-29 01:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = vsprintf(snprintf_buffer, format, ap);
|
2010-04-01 05:17:00 +08:00
|
|
|
if (rv >= BUFFER_SIZE)
|
2018-06-16 09:20:17 +08:00
|
|
|
nasm_panic("vsnprintf buffer overflow");
|
2007-09-29 01:50:20 +08:00
|
|
|
|
|
|
|
if (size > 0) {
|
2010-04-01 05:09:35 +08:00
|
|
|
if ((size_t)rv < size-1)
|
|
|
|
bytes = rv;
|
|
|
|
else
|
|
|
|
bytes = size-1;
|
|
|
|
memcpy(str, snprintf_buffer, bytes);
|
|
|
|
str[bytes] = '\0';
|
2007-09-29 01:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2016-03-08 18:06:39 +08:00
|
|
|
|
|
|
|
#endif
|