Add vasprintf() and asprintf() functions missing on AIX 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
diff --git a/COPYING b/COPYING
index a3b2cd9..7814d8f 100644
--- 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:
diff --git a/configure.ac b/configure.ac
index c00aa0f..1df73de 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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])
diff --git a/include/bsd/stdio.h b/include/bsd/stdio.h
index 4b69983..d67aab1 100644
--- a/include/bsd/stdio.h
+++ b/include/bsd/stdio.h
@@ -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
@@ -44,8 +44,18 @@
#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
diff --git a/src/Makefile.am b/src/Makefile.am
index a85b705..00181bd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -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
index 0000000..23576e3
--- /dev/null
+++ b/src/asprintf.c
@@ -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;
+}
diff --git a/src/libbsd.map.in b/src/libbsd.map.in
index 81e431f..9670d4c 100644
--- a/src/libbsd.map.in
+++ b/src/libbsd.map.in
@@ -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
index 0000000..d9ae8bb
--- /dev/null
+++ b/src/vasprintf.c
@@ -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;
+}