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 + Copyright © 2004-2024 Guillem Jover 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 + * Copyright © 2004-2024 Guillem Jover * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -44,8 +44,18 @@ #include #endif #include +#if defined(_AIX) +#include +#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 + * + * 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 + +#include +#include + +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 + * + * 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 + +#include +#include +#include + +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; +}