[syslinux:master] bufprintf: Adding bufprintf

syslinux-bot for Erwan Velu erwanaliasr1 at gmail.com
Mon Apr 25 15:27:57 PDT 2011


Commit-ID:  95e239ee7700114c9ca6a1765d9bdd74241a1eb8
Gitweb:     http://syslinux.zytor.com/commit/95e239ee7700114c9ca6a1765d9bdd74241a1eb8
Author:     Erwan Velu <erwanaliasr1 at gmail.com>
AuthorDate: Sat, 19 Mar 2011 21:02:53 +0100
Committer:  Erwan Velu <erwanaliasr1 at gmail.com>
CommitDate: Sat, 19 Mar 2011 21:02:53 +0100

bufprintf: Adding bufprintf



---
 com32/include/bufprintf.h |   10 ++++++++++
 com32/lib/Makefile        |    1 +
 com32/lib/bufprintf.c     |   41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/com32/include/bufprintf.h b/com32/include/bufprintf.h
new file mode 100644
index 0000000..5cbeaa4
--- /dev/null
+++ b/com32/include/bufprintf.h
@@ -0,0 +1,10 @@
+#define BUFPAD	4096
+
+struct print_buf {
+    char *buf;
+    size_t len;
+    size_t size;
+};
+
+int vbufprintf(struct print_buf *buf, const char *format, va_list ap);
+int bufprintf(struct print_buf *buf, const char *format, ...);
diff --git a/com32/lib/Makefile b/com32/lib/Makefile
index d65976b..0614cf3 100644
--- a/com32/lib/Makefile
+++ b/com32/lib/Makefile
@@ -29,6 +29,7 @@ LIBOBJS = \
 	vsscanf.o zalloc.o						\
 	skipspace.o							\
 	chrreplace.o							\
+	bufprintf.o							\
 	\
 	lmalloc.o lstrdup.o						\
 	\
diff --git a/com32/lib/bufprintf.c b/com32/lib/bufprintf.c
new file mode 100644
index 0000000..939bcec
--- /dev/null
+++ b/com32/lib/bufprintf.c
@@ -0,0 +1,41 @@
+#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)
+	    return -1;
+
+	buf->buf = newbuf;
+	buf->size = newsize;
+    }
+
+    rv = vsnprintf(buf->buf + buf->len, buf->size - buf->len, format, ap2);
+    buf->len += rv;
+    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;
+}



More information about the Syslinux-commits mailing list