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.
This commit is contained in:
Guillem Jover 2024-02-12 01:04:37 +01:00
parent 5392f0c1a4
commit ec7f5ee984
7 changed files with 141 additions and 2 deletions

View File

@ -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:

View 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])

View 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
@ -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

View 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 \

43
src/asprintf.c Normal file
View File

@ -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;
}

View 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;

64
src/vasprintf.c Normal file
View File

@ -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;
}