summaryrefslogtreecommitdiff
path: root/com32/lib/bufprintf.c
blob: d281231158e3ccbd77b8cdee62318094d06c1435 (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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <bufprintf.h>

int vbufprintf(struct print_buf *buf, const char *format, va_list ap)
{
    va_list ap2;
    int rv;

    va_copy(ap2, ap);
    rv = vsnprintf(NULL, 0, format, ap);

    /* >= to make sure we have space for terminating null */
    if (rv + buf->len >= buf->size) {
	size_t newsize = rv + buf->len + BUFPAD;
	char *newbuf;

	newbuf = realloc(buf->buf, newsize);
	if (!newbuf) {
	    rv = -1;
	    goto bail;
	}

	buf->buf = newbuf;
	buf->size = newsize;
    }

    rv = vsnprintf(buf->buf + buf->len, buf->size - buf->len, format, ap2);
    buf->len += rv;
bail:
    va_end(ap2);
    return rv;
}

int bufprintf(struct print_buf *buf, const char *format, ...)
{
    va_list ap;
    int rv;

    va_start(ap, format);
    rv = vbufprintf(buf, format, ap);
    va_end(ap);
    return rv;
}