[git commit] libc/misc/gnu/obprintf.c: implement obstack_printf and obstack_vprintf

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Fri Jun 14 08:44:08 UTC 2013


commit: http://git.uclibc.org/uClibc/commit/?id=10d12e77d5cdffd064719356a87f839225916a4a
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/master

This adds a straight forward implementation for obstack_printf and
obstack_vprintf on uClibc's already existing obstack_grow and
vasprintf.  It does not attempt to port over glibc's implementation
in terms of _IO_* structs and functions.

Signed-off-by: Anthony G. Basile <blueness at gentoo.org>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop at gmail.com>
---
 include/stdio.h           |    4 ++--
 libc/misc/gnu/Makefile.in |    2 +-
 libc/misc/gnu/obprintf.c  |   35 +++++++++++++++++++++++++++++++++++
 test/malloc/tst-obstack.c |   44 ++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 80 insertions(+), 5 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h
index e0006d2..831aa1c 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -854,7 +854,7 @@ extern char *cuserid (char *__s);
 #endif /* Use X/Open, but not issue 6.  */
 
 
-#if 0 /* def	__USE_GNU uClibc note: not supported */
+#if defined __USE_GNU && defined __UCLIBC_HAS_OBSTACK__
 struct obstack;			/* See <obstack.h>.  */
 
 /* Write formatted output to an obstack.  */
@@ -865,7 +865,7 @@ extern int obstack_vprintf (struct obstack *__restrict __obstack,
 			    const char *__restrict __format,
 			    __gnuc_va_list __args)
      __THROW __attribute__ ((__format__ (__printf__, 2, 0)));
-#endif /* Use GNU.  */
+#endif /* USE_GNU && UCLIBC_HAS_OBSTACK.  */
 
 
 #if defined __USE_POSIX || defined __USE_MISC
diff --git a/libc/misc/gnu/Makefile.in b/libc/misc/gnu/Makefile.in
index d820746..5106b47 100644
--- a/libc/misc/gnu/Makefile.in
+++ b/libc/misc/gnu/Makefile.in
@@ -7,7 +7,7 @@
 
 subdirs += libc/misc/gnu
 
-CSRC-$(UCLIBC_HAS_OBSTACK) := obstack.c
+CSRC-$(UCLIBC_HAS_OBSTACK) := obstack.c obprintf.c
 
 MISC_GNU_DIR := $(top_srcdir)libc/misc/gnu
 MISC_GNU_OUT := $(top_builddir)libc/misc/gnu
diff --git a/libc/misc/gnu/obprintf.c b/libc/misc/gnu/obprintf.c
new file mode 100644
index 0000000..70c058f
--- /dev/null
+++ b/libc/misc/gnu/obprintf.c
@@ -0,0 +1,35 @@
+/* Copyright (C) 2013 Gentoo Foundation
+ * Licensed under LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#ifdef _LIBC
+# include <obstack.h>
+#else
+# include "obstack.h"
+#endif
+
+int
+_obstack_vprintf (struct obstack *obstack, const char *format, va_list args)
+{
+  int n;
+  char *s;
+  n = vasprintf(&s, format, args);
+  obstack_grow(obstack, s, n);
+  return n;
+}
+weak_alias (_obstack_vprintf, obstack_vprintf)
+
+int
+_obstack_printf (struct obstack *obstack, const char *format, ...)
+{
+  int n;
+  va_list ap;
+  va_start (ap, format);
+  n = _obstack_vprintf (obstack, format, ap);
+  va_end (ap);
+  return n;
+}
+weak_alias (_obstack_printf, obstack_printf)
diff --git a/test/malloc/tst-obstack.c b/test/malloc/tst-obstack.c
index 769697f..1841946 100644
--- a/test/malloc/tst-obstack.c
+++ b/test/malloc/tst-obstack.c
@@ -1,4 +1,8 @@
-/* Test case by Alexandre Duret-Lutz <duret_g at epita.fr>.  */
+/* Test case by Alexandre Duret-Lutz <duret_g at epita.fr>.
+ * test_obstack_printf() added by Anthony G. Basile <blueness.gentoo.org>.
+ */
+
+#include <features.h>
 #include <obstack.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -26,7 +30,7 @@ verbose_free (void *buf)
 }
 
 int
-main (void)
+test_obstack_alloc (void)
 {
   int result = 0;
   int align = 2;
@@ -62,3 +66,39 @@ main (void)
 
   return result;
 }
+
+int
+test_obstack_printf (void)
+{
+  int result = 0;
+  int n;
+  char *s;
+  struct obstack ob;
+
+  obstack_init (&ob);
+
+  n = obstack_printf (&ob, "%s%d%c", "testing 1 ... 2 ... ", 3, '\n');
+  result |= (n != 22);
+  printf("obstack_printf => %d\n", n);
+
+  n = obstack_printf (&ob, "%s%d%c", "testing 3 ... 2 ... ", 1, '\0');
+  result |= (n != 22);
+  printf("obstack_printf => %d\n", n);
+
+  s = obstack_finish (&ob);
+  printf("obstack_printf => %s\n", s);
+  obstack_free (&ob, NULL);
+
+  return result;
+}
+
+int
+main (void)
+{
+   int result = 0;
+
+   result |= test_obstack_alloc();
+   result |= test_obstack_printf();
+
+   return result;
+}


More information about the uClibc-cvs mailing list