summaryrefslogtreecommitdiff
path: root/stdlib/vsnprintf.c
blob: 284cc19424324a4592a35c68b475228c65761609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * vsnprintf()
 *
 * Poor substitute for a real vsnprintf() function for systems
 * that don't have them...
 */

#include "compiler.h"


#include "nasmlib.h"
#include "error.h"

#if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF)

#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_panic("vsnprintf: size (%d) > BUFFER_SIZE (%d)",
                   size, BUFFER_SIZE);
        size = BUFFER_SIZE;
    }

    rv = vsprintf(snprintf_buffer, format, ap);
    if (rv >= BUFFER_SIZE)
        nasm_panic("vsnprintf buffer overflow");

    if (size > 0) {
        if ((size_t)rv < size-1)
            bytes = rv;
        else
            bytes = size-1;
        memcpy(str, snprintf_buffer, bytes);
        str[bytes] = '\0';
    }

    return rv;
}

#endif