]> mj.ucw.cz Git - libucw.git/commitdiff
added v?xprintf() functions, they will be used in the filter dumper
authorRobert Spalek <robert@ucw.cz>
Wed, 10 Jul 2002 14:47:26 +0000 (14:47 +0000)
committerRobert Spalek <robert@ucw.cz>
Wed, 10 Jul 2002 14:47:26 +0000 (14:47 +0000)
it is usable, for example, for printf()'ing to anything (like fastbufs)

lib/Makefile
lib/printf.c [new file with mode: 0644]
lib/printf.h [new file with mode: 0644]

index c6ef7e945e56c4361ced93037e3a50edc04f1d1c..631a743752e64b03790ed4b0c168a30957c7fb93 100644 (file)
@@ -8,7 +8,7 @@ SHLIB_OBJS=alloc.o alloc_str.o ctmatch.o db.o fastbuf.o fb-file.o fb-mem.o lists
        prime.o random.o realloc.o regex.o timer.o url.o wildmatch.o \
        wordsplit.o str_ctype.o str_upper.o bucket.o conf.o object.o sorter.o \
        finger.o proctitle.o ipaccess.o profile.o bitsig.o randomkey.o \
-       hashfunc.o base224.o fb-temp.o
+       hashfunc.o base224.o fb-temp.o printf.o
 
 obj/lib/libsh.a: $(addprefix obj/lib/,$(SHLIB_OBJS)) $(CUSTOM_MODULES)
 
diff --git a/lib/printf.c b/lib/printf.c
new file mode 100644 (file)
index 0000000..f8c801f
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ *     Sherlock Library -- auto-resizable printf() functions
+ *
+ *     (c) 2002, Robert Spalek <robert@ucw.cz>
+ */
+
+#include "lib/lib.h"
+#include "lib/printf.h"
+
+#include <stdio.h>
+
+byte *
+vxprintf(char *msg, va_list v)
+{
+       static byte *buf = NULL;
+       static int buf_len = 0;
+       int len;
+       if (!buf)
+       {
+               buf_len = 1024;
+               buf = xmalloc(buf_len);
+       }
+       while (1)
+       {
+               len = vsnprintf(buf, buf_len, msg, v);
+               if (len >= 0 && len < buf_len)
+                       return buf;
+               else
+               {
+                       buf_len *= 2;
+                       if (len >= buf_len)
+                               buf_len = len + 1;
+                       buf = xrealloc(buf, buf_len);
+               }
+       }
+}
+
+byte *
+xprintf(char *msg, ...)
+{
+       byte *txt;
+       va_list v;
+       va_start(v, msg);
+       txt = vxprintf(msg, v);
+       va_end(v);
+       return txt;
+}
diff --git a/lib/printf.h b/lib/printf.h
new file mode 100644 (file)
index 0000000..c0d26bc
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ *     Sherlock Library -- auto-resizable printf() functions
+ *
+ *     (c) 2002, Robert Spalek <robert@ucw.cz>
+ */
+
+#ifndef _LIB_PRINTF_H
+#define _LIB_PRINTF_H
+
+#include <stdarg.h>
+
+/* The following functions are NOT reentrable.  */
+
+byte *vxprintf(char *msg, va_list v);
+byte *xprintf(char *msg, ...) __attribute__((format(printf,1,2)));
+
+#endif