From patchwork Fri Aug 20 20:28:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Saleem Abdulrasool X-Patchwork-Id: 45853 From: abdulras@google.com (Saleem Abdulrasool) Date: Fri, 20 Aug 2021 20:28:23 +0000 Subject: [PATCH v2] handle libc implemntations which do not provide `error.h` Message-ID: <20210820202823.2865202-1-abdulras@google.com> Introduce a configure time check for the presence of `error.h`. In the case that `error.h` is not available, we can fall back to `err.h`. Although `err.h` is not a C standard header (it is a BSD extension), many libc implementations provide. If there are targets which do not provide an implementation of `err.h`, it would be possible to further extend the implementation to be more portable. This resolves bug #21008. Signed-off-by: Saleem Abdulrasool --- configure.ac | 3 +++ lib/system.h | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 7caff2c5..177bb1a2 100644 --- a/configure.ac +++ b/configure.ac @@ -431,6 +431,9 @@ AC_CHECK_DECLS([reallocarray],[],[], AC_CHECK_FUNCS([process_vm_readv]) +AC_CHECK_HEADERS([error.h]) +AC_CHECK_HEADERS([err.h]) + old_CFLAGS="$CFLAGS" CFLAGS="$CFLAGS -D_GNU_SOURCE" AC_FUNC_STRERROR_R() diff --git a/lib/system.h b/lib/system.h index 58d9deee..b963fd15 100644 --- a/lib/system.h +++ b/lib/system.h @@ -29,8 +29,9 @@ #ifndef LIB_SYSTEM_H #define LIB_SYSTEM_H 1 +#include + #include -#include #include #include #include @@ -38,8 +39,31 @@ #include #include #include +#include #include +#if defined(HAVE_ERROR_H) +#include +#elif defined(HAVE_ERR_H) +#include + +static int error_message_count = 0; + +static inline void error(int status, int errnum, const char *format, ...) { + va_list argp; + + va_start(argp, format); + verr(status, format, argp); + va_end(argp); + + if (status) + exit(status); + ++error_message_count; +} +#else +#error "err.h or error.h must be available" +#endif + #if __BYTE_ORDER == __LITTLE_ENDIAN # define LE32(n) (n) # define LE64(n) (n)