X-Git-Url: https://git.datadissipation.net/libcext/blobdiff_plain/5db824c730835f1adbdf2d77f4676f6ab9266407..refs/heads/master:/asprintf.h diff --git a/asprintf.h b/asprintf.h index fc66ba5..47b9752 100644 --- a/asprintf.h +++ b/asprintf.h @@ -26,62 +26,51 @@ */ /* https://git.datadissipation.net */ -#ifndef ASPRINTF_H_ - #define ASPRINTF_H_ 1 +#ifndef LCE_ASPRINTF_H + #define LCE_ASPRINTF_H 1 -#ifdef ASPRINTF_INCLUDE_LIBC + #ifdef LCE_ASPRINTF_INCLUDE_LIBC #include #include #include -#endif /* ASPRINTF_INCLUDE_LIBC */ + #endif /* LCE_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 lce_asprintf_ + #define vasprintf lce_vasprintf_ +int lce_vasprintf_(char **strp, const char *fmt, va_list ap); +int lce_asprintf_(char **strp, const char *fmt, ...); + #endif /* !HAVE_ASPRINTF */ -#ifdef ASPRINTF_IMPLEMENTATION + #ifdef LCE_ASPRINTF_IMPLEMENTATION int -i_vasprintf_(char **strp, const char *fmt, va_list ap) +lce_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 -i_asprintf_(char **strp, const char *fmt, ...) +lce_asprintf_(char **strp, const char *fmt, ...) { int ret; va_list ap; va_start(ap, fmt); - ret = i_vasprintf_(strp, fmt, ap); + ret = lce_vasprintf_(strp, fmt, ap); va_end(ap); return ret; } -#endif /* ASPRINTF_IMPLEMENTATION */ -#endif /* !ASPRINTF_H_ */ + #endif /* LCE_ASPRINTF_IMPLEMENTATION */ +#endif /* !LCE_ASPRINTF_H */