From 838e5d89413df78058feaf7b0af973b01180ac23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rainer=20M=C3=BCller?= Date: Wed, 20 Feb 2013 20:14:29 +0100 Subject: [PATCH] configure: Check for error.h For portability, check for error.h during configure and define HAVE_ERROR_H accordingly. If this header is not available, emulate the functionality of error() from glibc with an inline wrapper in include/c.h. --- configure.ac | 2 ++ include/c.h | 21 +++++++++++++++++++++ lib/fileutils.c | 7 ++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 08f59e3d..6e0883b6 100644 --- a/configure.ac +++ b/configure.ac @@ -137,6 +137,8 @@ dnl else dnl ALL_LINGUAS="af am ar as be bg bn_IN bn ca cs cy da de el en_GB es et eu_ES fa fi fr gl gu he hi hr hu hy id is it ja ka kn ko ku lo lt lv mk ml mr ms my nb nl nn no nso or pa pl pt_BR pt ro ru si sk sl sq sr@Latn sr sv ta te th tr uk ur vi zh_CN zh_TW zu" dnl fi +AC_CHECK_HEADERS(error.h, [], [], AC_INCLUDES_DEFAULT) + AC_CHECK_HEADERS(stdio_ext.h, [], [], AC_INCLUDES_DEFAULT) AC_MSG_CHECKING(whether program_invocation_name is defined) diff --git a/include/c.h b/include/c.h index 737630bb..6bcdf5f5 100644 --- a/include/c.h +++ b/include/c.h @@ -16,7 +16,11 @@ #include #include #include +#ifdef HAVE_ERROR_H #include +#else +#include +#endif /* * Compiler specific stuff @@ -103,6 +107,23 @@ static inline char *prog_inv_sh_nm_from_file(char *f, char stripext) /* * Error printing. */ +#ifndef HAVE_ERROR_H +/* Emulate the error() function from glibc */ +__attribute__((__format__(__printf__, 3, 4))) +static void error(int status, int errnum, const char *format, ...) +{ + va_list argp; + fprintf(stderr, "%s: ", program_invocation_short_name); + va_start(argp, format); + vfprintf(stderr, format, argp); + va_end(argp); + if (errnum != 0) + fprintf(stderr, ": error code %d", errnum); + fprintf(stderr, "\n"); + if (status != 0) + exit(status); +} +#endif #define xwarn(...) error(0, errno, __VA_ARGS__) #define xwarnx(...) error(0, 0, __VA_ARGS__) #define xerr(STATUS, ...) error(STATUS, errno, __VA_ARGS__) diff --git a/lib/fileutils.c b/lib/fileutils.c index c50d6aa9..a9ef2ff5 100644 --- a/lib/fileutils.c +++ b/lib/fileutils.c @@ -1,5 +1,7 @@ #include -#include +#ifdef HAVE_ERROR_H +# include +#endif #ifdef HAVE_STDIO_EXT_H # include #else @@ -12,6 +14,9 @@ #include "nls.h" #include "fileutils.h" +#ifndef HAVE_ERROR_H +# include "c.h" /* for error() emulation */ +#endif int close_stream(FILE * stream) {