X-Git-Url: https://git.datadissipation.net/libcext/blobdiff_plain/5db824c730835f1adbdf2d77f4676f6ab9266407..9ed4f24103433e8662953272fc20fc90a206828f:/asprintf.h diff --git a/asprintf.h b/asprintf.h index fc66ba5..ddce911 100644 --- a/asprintf.h +++ b/asprintf.h @@ -27,48 +27,37 @@ /* https://git.datadissipation.net */ #ifndef ASPRINTF_H_ - #define ASPRINTF_H_ 1 + #define ASPRINTF_H_ 1 -#ifdef ASPRINTF_INCLUDE_LIBC + #ifdef ASPRINTF_INCLUDE_LIBC #include #include #include -#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_ */