X-Git-Url: https://git.datadissipation.net/single-header-libcext/blobdiff_plain/a3915836c8f17278519197734a752a449e95f4eb..HEAD:/asprintf.h diff --git a/asprintf.h b/asprintf.h index 6d3341a..ddce911 100644 --- a/asprintf.h +++ b/asprintf.h @@ -27,7 +27,7 @@ /* https://git.datadissipation.net */ #ifndef ASPRINTF_H_ - #define ASPRINTF_H_ 1 + #define ASPRINTF_H_ 1 #ifdef ASPRINTF_INCLUDE_LIBC #include @@ -36,52 +36,41 @@ #endif /* ASPRINTF_INCLUDE_LIBC */ #ifndef HAVE_ASPRINTF - #define HAVE_ASPRINTF 1 - #define asprintf i_asprintf_ - #define vasprintf i_vasprintf_ - int i_vasprintf_(char **restrict strp, const char *restrict fmt, va_list ap); - int i_asprintf_(char **restrict strp, const char *restrict fmt, ...); + #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 - int - i_vasprintf_(char **restrict strp, const char *restrict fmt, va_list ap) - { - int size, ret; - va_list tp; +int +i_vasprintf_(char **strp, const char *fmt, va_list ap) +{ + int size; + va_list tp; - va_copy(tp, ap); - size = vsnprintf(NULL, 0, fmt, tp); - va_end(tp); + va_copy(tp, ap); + size = vsnprintf(NULL, 0, fmt, tp); + va_end(tp); - if (size < 0) { - return -1; - } - *strp = malloc(size + 1); - if (*strp == NULL) { - return -1; - } + if (size < 0 || !(*strp = malloc(size + 1))) + return -1; - ret = vsnprintf(*strp, size + 1, fmt, ap); - if (ret < 0) { - free(*strp); - return -1; - } + return vsnprintf(*strp, size + 1, fmt, ap); +} - return ret; - } +int +i_asprintf_(char **strp, const char *fmt, ...) +{ + int ret; + va_list ap; - int - i_asprintf_(char **restrict strp, const char *restrict fmt, ...) - { - int ret; - va_list ap; + va_start(ap, fmt); + ret = i_vasprintf_(strp, fmt, ap); + va_end(ap); - va_start(ap, fmt); - ret = i_vasprintf_(strp, fmt, ap); - va_end(ap); - - return ret; - } + return ret; +} #endif /* ASPRINTF_IMPLEMENTATION */ #endif /* !ASPRINTF_H_ */