X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Fasprintf%2Fasprintf.c;fp=ccan%2Fasprintf%2Fasprintf.c;h=b6ccce6a4b22bdf05984dd4933da161e62e84734;hb=fb1dfd092940905883ea6473162f5f6e36624da2;hp=0000000000000000000000000000000000000000;hpb=5079b95c1121e7a8e5b0b18b094ec7d8a539c7c7;p=ccan diff --git a/ccan/asprintf/asprintf.c b/ccan/asprintf/asprintf.c new file mode 100644 index 00000000..b6ccce6a --- /dev/null +++ b/ccan/asprintf/asprintf.c @@ -0,0 +1,55 @@ +#include +#include +#include + +char *PRINTF_FMT(1, 2) afmt(const char *fmt, ...) +{ + va_list ap; + char *ptr; + + va_start(ap, fmt); + /* The BSD version apparently sets ptr to NULL on fail. GNU loses. */ + if (vasprintf(&ptr, fmt, ap) < 0) + ptr = NULL; + va_end(ap); + return ptr; +} + +#if !HAVE_ASPRINTF +#include +#include + +int vasprintf(char **strp, const char *fmt, va_list ap) +{ + int len; + va_list ap_copy; + + /* We need to make a copy of ap, since it's a use-once. */ + va_copy(ap_copy, ap); + len = vsnprintf(NULL, 0, fmt, ap_copy); + va_end(ap_copy); + + /* Until version 2.0.6 glibc would return -1 on truncated output. + * OTOH, they had asprintf. */ + if (len < 0) + return -1; + + *strp = malloc(len+1); + if (!*strp) + return -1; + + return vsprintf(*strp, fmt, ap); +} + +int asprintf(char **strp, const char *fmt, ...) +{ + va_list ap; + int len; + + va_start(ap, fmt); + len = vasprintf(strp, fmt, ap); + va_end(ap); + + return len; +} +#endif /* !HAVE_ASPRINTF */