]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Add vasprintf() and asprintf() functions missing on AIX
authorGuillem Jover <guillem@hadrons.org>
Mon, 12 Feb 2024 00:04:37 +0000 (01:04 +0100)
committerGuillem Jover <guillem@hadrons.org>
Sun, 25 Feb 2024 03:18:51 +0000 (04:18 +0100)
These functions are used by code in the library, even though these
functions started as GNU extensions, they are present in all BSDs,
so we expose them as part of our interface on AIX.

COPYING
configure.ac
include/bsd/stdio.h
src/Makefile.am
src/asprintf.c [new file with mode: 0644]
src/libbsd.map.in
src/vasprintf.c [new file with mode: 0644]

diff --git a/COPYING b/COPYING
index a3b2cd9376bcb34e5974ad91b1e271515a50cf52..7814d8f488cb6b44b82635c11c1d01df8f10f1e0 100644 (file)
--- a/COPYING
+++ b/COPYING
@@ -3,7 +3,7 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
 Files:
  *
 Copyright:
- Copyright © 2004-2006, 2008-2022 Guillem Jover <guillem@hadrons.org>
+ Copyright © 2004-2024 Guillem Jover <guillem@hadrons.org>
 License: BSD-3-clause
 
 Files:
index c00aa0f30edeb6083fc34a0ad3b24f4a88017d4e..1df73def9e49de1e531fa41354c0e010e7f4745f 100644 (file)
@@ -44,6 +44,7 @@ is_windows=no
 abi_accmode=unknown
 abi_arc4random=unknown
 abi_arc4random_stir=unknown
+abi_asprintf=unknown
 abi_bsd_getopt=unknown
 abi_closefrom=unknown
 abi_err=unknown
@@ -89,6 +90,7 @@ AS_CASE([$host_os],
     #abi_arc4random=no
     abi_arc4random=yes
     abi_arc4random_stir=yes
+    abi_asprintf=no
     abi_bsd_getopt=yes
     # DROP: On glibc >= 2.34.
     #abi_closefrom=no
@@ -145,6 +147,7 @@ AS_CASE([$host_os],
     abi_accmode=yes
     abi_arc4random=yes
     abi_arc4random_stir=yes
+    abi_asprintf=no
     # DROP: On musl >= 0.9.7, optreset got implemented.
     #abi_bsd_getopt=no
     abi_bsd_getopt=yes
@@ -208,6 +211,7 @@ AS_CASE([$host_os],
     abi_accmode=no
     abi_arc4random=no
     abi_arc4random_stir=no
+    abi_asprintf=no
     abi_bsd_getopt=no
     abi_closefrom=yes
     abi_err=no
@@ -253,6 +257,7 @@ AS_CASE([$host_os],
     abi_accmode=yes
     abi_arc4random=no
     abi_arc4random_stir=no
+    abi_asprintf=no
     abi_bsd_getopt=yes
     abi_closefrom=no
     abi_err=no
@@ -465,6 +470,8 @@ LIBBSD_CHECK_PROGNAME
 LIBBSD_CHECK_REGISTER_ATFORK
 
 AC_CHECK_FUNCS([\
+  vasprintf \
+  asprintf \
   clearenv \
   dirfd \
   flock \
@@ -490,6 +497,7 @@ AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xyes"])
 LIBBSD_SELECT_ABI([accmode], [setmode()/getmode()])
 LIBBSD_SELECT_ABI([arc4random], [arc4random() API])
 LIBBSD_SELECT_ABI([arc4random_stir], [arc4random_stir()/arc4random_addrandom()])
+LIBBSD_SELECT_ABI([asprintf], [vasprintf()/asprintf()])
 LIBBSD_SELECT_ABI([bsd_getopt], [BSD getopt()])
 LIBBSD_SELECT_ABI([closefrom], [closefrom()])
 LIBBSD_SELECT_ABI([err], [err API])
index 4b699832c02231ef77f06f8698a205ca8cdf1686..d67aab19fa0c0adef5e571897b4a7fa5b8ef717c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2004-2005, 2009, 2011-2013 Guillem Jover <guillem@hadrons.org>
+ * Copyright © 2004-2024 Guillem Jover <guillem@hadrons.org>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #include <bsd/sys/cdefs.h>
 #endif
 #include <sys/types.h>
+#if defined(_AIX)
+#include <stdarg.h>
+#endif
 
 __BEGIN_DECLS
+#if defined(_AIX)
+int asprintf(char **strp, char const *fmt, ...)
+       __printflike(2, 3);
+int vasprintf(char **strp, char const *fmt, va_list args)
+       __printflike(2, 0);
+#endif
+
 const char *fmtcheck(const char *, const char *);
 
 /* XXX: The function requires cooperation from the system libc to store the
index a85b705271f76ebba35ed52f78362b3a1368a24e..00181bd9607ba9f523ffb662efd32685e7f72022 100644 (file)
@@ -103,6 +103,13 @@ libbsd_la_SOURCES += \
        # EOL
 endif
 
+if ABI_ASPRINTF
+libbsd_la_SOURCES += \
+       asprintf.c \
+       vasprintf.c \
+       # EOL
+endif
+
 if ABI_BSD_GETOPT
 libbsd_la_SOURCES += \
        bsd_getopt.c \
diff --git a/src/asprintf.c b/src/asprintf.c
new file mode 100644 (file)
index 0000000..23576e3
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2010-2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+
+int
+asprintf(char **strp, char const *fmt, ...)
+{
+       va_list args;
+       int n;
+
+       va_start(args, fmt);
+       n = vasprintf(strp, fmt, args);
+       va_end(args);
+
+       return n;
+}
index 81e431f84bad66f89526e71afb53cada883e0569..9670d4cb50718fb1896421a79440acbdc6931546 100644 (file)
@@ -299,3 +299,10 @@ LIBBSD_0.11.0 {
     group_from_gid;
 #endif
 } LIBBSD_0.10.0;
+
+LIBBSD_0.12.0 {
+#if LIBBSD_ABI_ASPRINTF
+    vasprintf;
+    asprintf;
+#endif
+} LIBBSD_0.11.0;
diff --git a/src/vasprintf.c b/src/vasprintf.c
new file mode 100644 (file)
index 0000000..d9ae8bb
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright © 2010-2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+vasprintf(char **strp, char const *fmt, va_list args)
+{
+       va_list args_copy;
+       int needed, n;
+       char *str;
+
+       va_copy(args_copy, args);
+       needed = vsnprintf(NULL, 0, fmt, args_copy);
+       va_end(args_copy);
+
+       if (needed < 0) {
+               *strp = NULL;
+               return -1;
+       }
+
+       str = malloc(needed + 1);
+       if (str == NULL) {
+               *strp = NULL;
+               return -1;
+       }
+
+       n = vsnprintf(str, needed + 1, fmt, args);
+       if (n < 0) {
+               free(str);
+               str = NULL;
+       }
+
+       *strp = str;
+
+       return n;
+}