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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "nasmlib.h"
|
|
|
|
|
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) {
|
2010-04-01 05:17:00 +08:00
|
|
|
nasm_error(ERR_PANIC|ERR_NOFILE,
|
|
|
|
"vsnprintf: size (%d) > BUFFER_SIZE (%d)",
|
|
|
|
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)
|
|
|
|
nasm_error(ERR_PANIC|ERR_NOFILE, "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;
|
|
|
|
}
|