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 partially resolves bug #21008. Signed-off-by: Saleem Abdulrasool <abdul...@google.com> --- configure.ac | 3 +++ lib/ChangeLog | 7 +++++++ lib/Makefile.am | 2 +- lib/error.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/system.h | 13 ++++++++++++- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 lib/error.c 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/ChangeLog b/lib/ChangeLog index a95f8041..101a4e6f 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,10 @@ +2021-08-20 Saleem Abdulrasool <abdul...@google.com> + + * error.c: New file, adds error_number_count and error fallbacks. + * system.h: Declare error_number_count and error fallbacks for when + error.h is unavailable. + * Makefile.am (libeu_a_SOURCES): add error.c. + 2021-08-20 Saleem Abdulrasool <abdul...@google.com> * fixedsizehash.h: Remove sys/cdefs.h include. Unconditionally diff --git a/lib/Makefile.am b/lib/Makefile.am index 97bf7329..766fbcd7 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -35,7 +35,7 @@ noinst_LIBRARIES = libeu.a libeu_a_SOURCES = xstrdup.c xstrndup.c xmalloc.c next_prime.c \ crc32.c crc32_file.c \ - color.c printversion.c + color.c error.c printversion.c noinst_HEADERS = fixedsizehash.h libeu.h system.h dynamicsizehash.h list.h \ eu-config.h color.h printversion.h bpf.h \ diff --git a/lib/error.c b/lib/error.c new file mode 100644 index 00000000..b0264efb --- /dev/null +++ b/lib/error.c @@ -0,0 +1,49 @@ +/* Definitions for error handling fallback functions. + Copyright (C) 2021 Google, Inc. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +#if !defined(HAVE_ERROR_H) && defined(HAVE_ERR_H) +#include <stdarg.h> +#include <stdlib.h> +#include <err.h> + +unsigned int error_message_count = 0; + +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; +} +#endif diff --git a/lib/system.h b/lib/system.h index 58d9deee..edbc8488 100644 --- a/lib/system.h +++ b/lib/system.h @@ -29,8 +29,9 @@ #ifndef LIB_SYSTEM_H #define LIB_SYSTEM_H 1 +#include <config.h> + #include <errno.h> -#include <error.h> #include <stddef.h> #include <stdint.h> #include <sys/param.h> @@ -38,8 +39,18 @@ #include <byteswap.h> #include <unistd.h> #include <string.h> +#include <stdarg.h> #include <stdlib.h> +#if defined(HAVE_ERROR_H) +#include <error.h> +#elif defined(HAVE_ERR_H) +extern int error_message_count; +void error(int status, int errnum, const char *format, ...); +#else +#error "err.h or error.h must be available" +#endif + #if __BYTE_ORDER == __LITTLE_ENDIAN # define LE32(n) (n) # define LE64(n) (n) -- 2.33.0.259.gc128427fd7-goog