From c6599f0200254a24bb95cfaf6b6e536689c5b9a7 Mon Sep 17 00:00:00 2001 From: Aric Belsito Date: Fri, 13 Jan 2017 16:23:18 -0800 Subject: [PATCH] Add obstack_printf function. --- Makefile.am | 2 +- obstack.h | 2 ++ obstack_printf.c | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 obstack_printf.c diff --git a/Makefile.am b/Makefile.am index 51d2d81..6703fbd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ ## Makefile.am - procress this file with automake to produce Makefile.in lib_LTLIBRARIES = libobstack.la -libobstack_la_SOURCES = obstack.c +libobstack_la_SOURCES = obstack.c obstack_printf.c libobstack_la_HEADERS = obstack.h libobstack_ladir = $(includedir) ACLOCAL_AMFLAGS = -Im4 diff --git a/obstack.h b/obstack.h index 23487ba..7d57bf6 100644 --- a/obstack.h +++ b/obstack.h @@ -235,6 +235,8 @@ int obstack_alignment_mask (struct obstack *obstack); int obstack_chunk_size (struct obstack *obstack); int obstack_memory_used (struct obstack *obstack); +int obstack_printf(struct obstack *obstack, const char *__restrict fmt, ...); + /* Error handler called when `obstack_chunk_alloc' failed to allocate more memory. This can be set to a user defined function. The default action is to print a message and abort. */ diff --git a/obstack_printf.c b/obstack_printf.c new file mode 100644 index 0000000..ac5a7a3 --- /dev/null +++ b/obstack_printf.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include "obstack.h" + +int obstack_printf(struct obstack *obstack, const char *__restrict fmt, ...) +{ + char buf[1024]; + va_list ap; + int len; + + va_start(ap, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, ap); + obstack_grow(obstack, buf, len); + va_end(ap); + + return len; +}