mirror of
https://github.com/netwide-assembler/nasm.git
synced 2024-12-21 09:19:31 +08:00
304b605563
To deal with fools^Wpeople trying to keep really old systems alive, create a proper framework for substitution functions, and make it possible to deal with the lack of snprintf/vsnprintf in particular.
50 lines
957 B
C
50 lines
957 B
C
/*
|
|
* vsnprintf()
|
|
*
|
|
* Poor substitute for a real vsnprintf() function for systems
|
|
* that don't have them...
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
|
|
#include "nasmlib.h"
|
|
|
|
extern efunc nasm_malloc_error;
|
|
|
|
#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
|
|
|
|
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) {
|
|
nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
|
|
"snprintf: size (%d) > BUFFER_SIZE (%d)",
|
|
size, BUFFER_SIZE);
|
|
size = BUFFER_SIZE;
|
|
}
|
|
|
|
rv = vsprintf(snprintf_buffer, format, ap);
|
|
if (rv > BUFFER_SIZE) {
|
|
nasm_malloc_error(ERR_PANIC|ERR_NOFILE,
|
|
"snprintf buffer overflow");
|
|
}
|
|
|
|
if (rv < (int)size-1)
|
|
bytes = rv;
|
|
else
|
|
bytes = size-1;
|
|
|
|
if (size > 0) {
|
|
memcpy(str, snprintf_buffer, bytes);
|
|
str[bytes] = '\0';
|
|
}
|
|
|
|
return rv;
|
|
}
|