From 830407e88f9d40d954356c3754f2647f91d5c06a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:26:00 +0200 Subject: Adding upstream version 5.6.0. Signed-off-by: Daniel Baumann --- contrib/ccan/asprintf/LICENSE | 1 + contrib/ccan/asprintf/asprintf.c | 57 +++++++++++++++++++++++++++++++++++++ contrib/ccan/asprintf/asprintf.h | 51 +++++++++++++++++++++++++++++++++ contrib/ccan/asprintf/asprintf.spdx | 10 +++++++ 4 files changed, 119 insertions(+) create mode 120000 contrib/ccan/asprintf/LICENSE create mode 100644 contrib/ccan/asprintf/asprintf.c create mode 100644 contrib/ccan/asprintf/asprintf.h create mode 100644 contrib/ccan/asprintf/asprintf.spdx (limited to 'contrib/ccan/asprintf') diff --git a/contrib/ccan/asprintf/LICENSE b/contrib/ccan/asprintf/LICENSE new file mode 120000 index 0000000..2354d12 --- /dev/null +++ b/contrib/ccan/asprintf/LICENSE @@ -0,0 +1 @@ +../../licenses/BSD-MIT \ No newline at end of file diff --git a/contrib/ccan/asprintf/asprintf.c b/contrib/ccan/asprintf/asprintf.c new file mode 100644 index 0000000..4077599 --- /dev/null +++ b/contrib/ccan/asprintf/asprintf.c @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: MIT + * Source: https://ccodearchive.net/info/asprintf.html */ +#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 */ diff --git a/contrib/ccan/asprintf/asprintf.h b/contrib/ccan/asprintf/asprintf.h new file mode 100644 index 0000000..d4cc5ca --- /dev/null +++ b/contrib/ccan/asprintf/asprintf.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: MIT + * Source: https://ccodearchive.net/info/asprintf.html */ +#ifndef CCAN_ASPRINTF_H +#define CCAN_ASPRINTF_H +#include "config.h" +#include + +/** + * afmt - allocate and populate a string with the given format. + * @fmt: printf-style format. + * + * This is a simplified asprintf interface. Returns NULL on error. + */ +char *PRINTF_FMT(1, 2) afmt(const char *fmt, ...); + +#if HAVE_ASPRINTF +#include +#else +#include +/** + * asprintf - printf to a dynamically-allocated string. + * @strp: pointer to the string to allocate. + * @fmt: printf-style format. + * + * Returns -1 (and leaves @strp undefined) on an error. Otherwise returns + * number of bytes printed into @strp. + * + * Example: + * static char *greeting(const char *name) + * { + * char *str; + * int len = asprintf(&str, "Hello %s", name); + * if (len < 0) + * return NULL; + * return str; + * } + */ +int PRINTF_FMT(2, 3) asprintf(char **strp, const char *fmt, ...); + +/** + * vasprintf - vprintf to a dynamically-allocated string. + * @strp: pointer to the string to allocate. + * @fmt: printf-style format. + * + * Returns -1 (and leaves @strp undefined) on an error. Otherwise returns + * number of bytes printed into @strp. + */ +int vasprintf(char **strp, const char *fmt, va_list ap); +#endif + +#endif /* CCAN_ASPRINTF_H */ diff --git a/contrib/ccan/asprintf/asprintf.spdx b/contrib/ccan/asprintf/asprintf.spdx new file mode 100644 index 0000000..175078a --- /dev/null +++ b/contrib/ccan/asprintf/asprintf.spdx @@ -0,0 +1,10 @@ +SPDXVersion: SPDX-2.1 +DataLicense: CC0-1.0 +SPDXID: SPDXRef-DOCUMENT +DocumentName: ccan-asprintf +DocumentNamespace: http://spdx.org/spdxdocs/spdx-v2.1-40d4b71d-00e9-4e75-b6da-559203e6b815 + +PackageName: asprintf +PackageDownloadLocation: git+https://github.com/rustyrussell/ccan@fb1dfd092940905883ea6473162f5f6e36624da2#ccan/asprintf +PackageOriginator: Person: Rusty Russell (rusty@rustcorp.com.au) +PackageLicenseDeclared: MIT -- cgit v1.2.3