Adding upstream version 6.02~pre8+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / bufprintf.c
blobd281231158e3ccbd77b8cdee62318094d06c1435
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <bufprintf.h>
6 int vbufprintf(struct print_buf *buf, const char *format, va_list ap)
8 va_list ap2;
9 int rv;
11 va_copy(ap2, ap);
12 rv = vsnprintf(NULL, 0, format, ap);
14 /* >= to make sure we have space for terminating null */
15 if (rv + buf->len >= buf->size) {
16 size_t newsize = rv + buf->len + BUFPAD;
17 char *newbuf;
19 newbuf = realloc(buf->buf, newsize);
20 if (!newbuf) {
21 rv = -1;
22 goto bail;
25 buf->buf = newbuf;
26 buf->size = newsize;
29 rv = vsnprintf(buf->buf + buf->len, buf->size - buf->len, format, ap2);
30 buf->len += rv;
31 bail:
32 va_end(ap2);
33 return rv;
36 int bufprintf(struct print_buf *buf, const char *format, ...)
38 va_list ap;
39 int rv;
41 va_start(ap, format);
42 rv = vbufprintf(buf, format, ap);
43 va_end(ap);
44 return rv;