Add custom asprintf() clone to rpmstring
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 18 Mar 2008 08:17:03 +0000 (10:17 +0200)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 18 Mar 2008 08:17:03 +0000 (10:17 +0200)
rpmio/rpmstring.c
rpmio/rpmstring.h

index fbac425..3a75c36 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "system.h"
 
+#include <stdarg.h>
+
 #include <rpm/rpmstring.h>
 #include "debug.h"
 
@@ -176,3 +178,32 @@ int rstrncasecmp(const char *s1, const char *s2, size_t n)
 
   return (int)(c1 - c2);
 }
+
+/* 
+ * Simple and stupid asprintf() clone.
+ * FIXME: write to work with non-C99 vsnprintf or check for one in configure.
+ */
+int rasprintf(char **strp, const char *fmt, ...)
+{
+    int n;
+    va_list ap;
+    char * p = NULL;
+  
+    if (strp == NULL) 
+       return -1;
+
+    va_start(ap, fmt);
+    n = vsnprintf(NULL, 0, fmt, ap);
+    va_end(ap);
+
+    if (n >= -1) {
+       size_t nb = n + 1;
+       p = xmalloc(nb);
+       va_start(ap, fmt);
+        n = vsnprintf(p, nb + 1, fmt, ap);
+       va_end(ap);
+    } 
+    *strp = p;
+    return n;
+}
+
index aaf4b34..cb8bc7a 100644 (file)
@@ -8,6 +8,8 @@
 
 #include <stddef.h>
 
+#include <rpm/rpmutil.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -86,6 +88,11 @@ int rstrcasecmp(const char * s1, const char * s2)            ;
 int rstrncasecmp(const char *s1, const char * s2, size_t n)    ;
 
 /** \ingroup rpmstring
+ * asprintf() clone
+ */
+int rasprintf(char **strp, const char *fmt, ...) RPM_GNUC_PRINTF(2, 3);
+
+/** \ingroup rpmstring
  * Split string into fields separated by a character.
  * @param str          string
  * @param length       length of string