Add xasprintf() and xvasprintf().
authorGuus Sliepen <guus@tinc-vpn.org>
Tue, 8 Sep 2009 16:16:58 +0000 (18:16 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Tue, 8 Sep 2009 16:16:58 +0000 (18:16 +0200)
These functions wrap asprintf() and vasprintf(), and check the return value. If
the function failed, tinc will exit with an error message, similar to xmalloc()
and friends.

lib/dropin.c
lib/dropin.h
lib/xalloc.h
lib/xmalloc.c

index a3c28b0..23d2b13 100644 (file)
@@ -125,27 +125,36 @@ char *get_current_dir_name(void)
 #endif
 
 #ifndef HAVE_ASPRINTF
 #endif
 
 #ifndef HAVE_ASPRINTF
-int asprintf(char **buf, const char *fmt, ...)
+int asprintf(char **buf, const char *fmt, ...) {
+       int result;
+       va_list ap;
+       va_start(ap, fmt);
+       result = vasprintf(buf, fmt, ap);
+       va_end(ap);
+       return result;
+}
+
+int vasprintf(char **buf, const char *fmt, va_list ap) {
 {
        int status;
 {
        int status;
-       va_list ap;
+       va_list aq;
        int len;
 
        len = 4096;
        *buf = xmalloc(len);
 
        int len;
 
        len = 4096;
        *buf = xmalloc(len);
 
-       va_start(ap, fmt);
-       status = vsnprintf(*buf, len, fmt, ap);
-       va_end(ap);
+       va_copy(aq, ap);
+       status = vsnprintf(*buf, len, fmt, aq);
+       va_end(aq);
 
        if(status >= 0)
                *buf = xrealloc(*buf, status + 1);
 
        if(status > len - 1) {
                len = status;
 
        if(status >= 0)
                *buf = xrealloc(*buf, status + 1);
 
        if(status > len - 1) {
                len = status;
-               va_start(ap, fmt);
-               status = vsnprintf(*buf, len, fmt, ap);
-               va_end(ap);
+               va_copy(aq, ap);
+               status = vsnprintf(*buf, len, fmt, aq);
+               va_end(aq);
        }
 
        return status;
        }
 
        return status;
index eabc4c4..72109c8 100644 (file)
@@ -36,6 +36,7 @@ extern char *get_current_dir_name(void);
 
 #ifndef HAVE_ASPRINTF
 extern int asprintf(char **, const char *, ...);
 
 #ifndef HAVE_ASPRINTF
 extern int asprintf(char **, const char *, ...);
+extern int vasprintf(char **, const char *, va_list ap);
 #endif
 
 #ifndef HAVE_GETNAMEINFO
 #endif
 
 #ifndef HAVE_GETNAMEINFO
index 7cb486a..51f99bd 100644 (file)
@@ -24,3 +24,6 @@ void *xcalloc PARAMS ((size_t n, size_t s));
 void *xrealloc PARAMS ((void *p, size_t n)) __attribute__ ((__malloc__));
 
 char *xstrdup PARAMS ((const char *s)) __attribute__ ((__malloc__));
 void *xrealloc PARAMS ((void *p, size_t n)) __attribute__ ((__malloc__));
 
 char *xstrdup PARAMS ((const char *s)) __attribute__ ((__malloc__));
+
+extern int xasprintf(char **strp, const char *fmt, ...);
+extern int xvasprintf(char **strp, const char *fmt, va_list ap);
index d02f41b..51356e4 100644 (file)
@@ -22,6 +22,8 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdarg.h>
+#include <errno.h>
 
 #if STDC_HEADERS
 # include <stdlib.h>
 
 #if STDC_HEADERS
 # include <stdlib.h>
@@ -138,3 +140,21 @@ xcalloc (n, s)
 }
 
 #endif /* NOT_USED */
 }
 
 #endif /* NOT_USED */
+
+int xasprintf(char **strp, const char *fmt, ...) {
+       int result;
+       va_list ap;
+       va_start(ap, fmt);
+       result = xvasprintf(strp, fmt, ap);
+       va_end(ap);
+       return result;
+}
+
+int xvasprintf(char **strp, const char *fmt, va_list ap) {
+       int result = vasprintf(strp, fmt, ap);
+       if(result < 0) {
+               fprintf(stderr, "vasprintf() failed: %s\n", strerror(errno));
+               exit(xalloc_exit_failure);
+       }
+       return result;
+}