summaryrefslogtreecommitdiffstats
path: root/contrib/ccan/asprintf
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-06 00:55:53 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-06 00:55:53 +0000
commit3d0386f27ca66379acf50199e1d1298386eeeeb8 (patch)
treef87bd4a126b3a843858eb447e8fd5893c3ee3882 /contrib/ccan/asprintf
parentInitial commit. (diff)
downloadknot-resolver-upstream.tar.xz
knot-resolver-upstream.zip
Adding upstream version 3.2.1.upstream/3.2.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'contrib/ccan/asprintf')
-rw-r--r--contrib/ccan/asprintf/LICENSE17
-rw-r--r--contrib/ccan/asprintf/_info45
-rw-r--r--contrib/ccan/asprintf/asprintf.c56
-rw-r--r--contrib/ccan/asprintf/asprintf.h50
4 files changed, 168 insertions, 0 deletions
diff --git a/contrib/ccan/asprintf/LICENSE b/contrib/ccan/asprintf/LICENSE
new file mode 100644
index 0000000..89de354
--- /dev/null
+++ b/contrib/ccan/asprintf/LICENSE
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/contrib/ccan/asprintf/_info b/contrib/ccan/asprintf/_info
new file mode 100644
index 0000000..f72d668
--- /dev/null
+++ b/contrib/ccan/asprintf/_info
@@ -0,0 +1,45 @@
+#include "config.h"
+#include <stdio.h>
+#include <string.h>
+
+/**
+ * asprintf - asprintf wrapper (and if necessary, implementation).
+ *
+ * This provides a convenient wrapper for asprintf, and also implements
+ * asprintf if necessary.
+ *
+ * Author: Rusty Russell <rusty@rustcorp.com.au>
+ *
+ * License: MIT
+ *
+ * Example:
+ * #include <ccan/asprintf/asprintf.h>
+ * #include <unistd.h>
+ * #include <err.h>
+ *
+ * int main(int argc, char *argv[])
+ * {
+ * char *p = afmt("This program has %i arguments", argc);
+ * int ret;
+ *
+ * while ((ret = write(STDOUT_FILENO, p, strlen(p))) > 0) {
+ * p += ret;
+ * if (!*p)
+ * exit(0);
+ * }
+ * err(1, "Writing to stdout");
+ * }
+ */
+int main(int argc, char *argv[])
+{
+ /* Expect exactly one argument */
+ if (argc != 2)
+ return 1;
+
+ if (strcmp(argv[1], "depends") == 0) {
+ printf("ccan/compiler\n");
+ return 0;
+ }
+
+ return 1;
+}
diff --git a/contrib/ccan/asprintf/asprintf.c b/contrib/ccan/asprintf/asprintf.c
new file mode 100644
index 0000000..9e66e13
--- /dev/null
+++ b/contrib/ccan/asprintf/asprintf.c
@@ -0,0 +1,56 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
+#include <ccan/asprintf/asprintf.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+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 && !defined(__USE_FORTIFY_LEVEL)
+#include <stdarg.h>
+#include <stdlib.h>
+
+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..37da83e
--- /dev/null
+++ b/contrib/ccan/asprintf/asprintf.h
@@ -0,0 +1,50 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
+#ifndef CCAN_ASPRINTF_H
+#define CCAN_ASPRINTF_H
+#include "config.h"
+#include <ccan/compiler/compiler.h>
+
+/**
+ * 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 || defined(__USE_FORTIFY_LEVEL)
+#include <stdio.h>
+#else
+#include <stdarg.h>
+/**
+ * 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 */