strtonum: fix `TOOLARGE` macro name
[single-header-libcext.git] / asprintf.h
index fc66ba5c294603abe39add0fe8810167bf863d2e..ddce911bff42210a5b1888d286c0846643783e59 100644 (file)
 
 /* https://git.datadissipation.net */
 #ifndef ASPRINTF_H_
- #define ASPRINTF_H_ 1
+ #define ASPRINTF_H_            1
 
-#ifdef ASPRINTF_INCLUDE_LIBC
+ #ifdef ASPRINTF_INCLUDE_LIBC
   #include <stdarg.h>
   #include <stdio.h>
   #include <stdlib.h>
-#endif /* ASPRINTF_INCLUDE_LIBC */
+ #endif /* ASPRINTF_INCLUDE_LIBC */
 
-#ifndef HAVE_ASPRINTF
-  #define HAVE_ASPRINTF 1
-  #define asprintf  i_asprintf_
-  #define vasprintf i_vasprintf_
-  int i_vasprintf_(char **strp, const char *fmt, va_list ap);
-  int i_asprintf_(char **strp, const char *fmt, ...);
-#endif /* !HAVE_ASPRINTF */
+ #ifndef HAVE_ASPRINTF
+  #define HAVE_ASPRINTF         1
+  #define asprintf              i_asprintf_
+  #define vasprintf             i_vasprintf_
+int i_vasprintf_(char **strp, const char *fmt, va_list ap);
+int i_asprintf_(char **strp, const char *fmt, ...);
+ #endif /* !HAVE_ASPRINTF */
 
-#ifdef ASPRINTF_IMPLEMENTATION
+ #ifdef ASPRINTF_IMPLEMENTATION
 int
 i_vasprintf_(char **strp, const char *fmt, va_list ap)
 {
-       int size, ret;
-       va_list tp;
+       int size;
+       va_list tp;
 
        va_copy(tp, ap);
        size = vsnprintf(NULL, 0, fmt, tp);
        va_end(tp);
 
-       if (size < 0) {
+       if (size < 0 || !(*strp = malloc(size + 1)))
                return -1;
-       }
-       *strp = malloc(size + 1);
-       if (*strp == NULL) {
-               return -1;
-       }
-
-       ret = vsnprintf(*strp, size + 1, fmt, ap);
-       if (ret < 0) {
-               free(*strp);
-               return -1;
-       }
 
-       return ret;
+       return vsnprintf(*strp, size + 1, fmt, ap);
 }
 
 int
@@ -83,5 +72,5 @@ i_asprintf_(char **strp, const char *fmt, ...)
 
        return ret;
 }
-#endif /* ASPRINTF_IMPLEMENTATION */
+ #endif /* ASPRINTF_IMPLEMENTATION */
 #endif /* !ASPRINTF_H_ */