]> arthur.barton.de Git - netdata.git/commitdiff
portable way of detecting format of strerror_r
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Tue, 27 Sep 2016 08:56:35 +0000 (11:56 +0300)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Tue, 27 Sep 2016 08:56:35 +0000 (11:56 +0300)
configure.ac
src/common.h
src/log.c

index 1cb2816057ef8c6dfce0573ed20c8dc348c21fe8..eeeeb32c07659cd58e3a50a8bfe8ed51699d060b 100644 (file)
@@ -83,6 +83,37 @@ AC_TYPE_UINT16_T
 AC_TYPE_UINT32_T
 AC_C_INLINE
 
+# https://lists.gnu.org/archive/html/autoconf-commit/2012-12/msg00004.html
+# AC_C__GENERIC
+# -------------
+# Define HAVE_C__GENERIC if _Generic works, a la C11.
+AN_IDENTIFIER([_Generic], [AC_C__GENERIC])
+AC_DEFUN([AC_C__GENERIC],
+[AC_CACHE_CHECK([for _Generic], ac_cv_c__Generic,
+[AC_COMPILE_IFELSE(
+   [AC_LANG_SOURCE(
+      [[int
+        main (int argc, char **argv)
+        {
+          int a = _Generic (argc, int: argc = 1);
+          int *b = &_Generic (argc, default: argc);
+          char ***c = _Generic (argv, int: argc, default: argv ? &argv : 0);
+          _Generic (1 ? 0 : b, int: a, default: b) = &argc;
+          _Generic (a = 1, default: a) = 3;
+          return a + !b + !c;
+        }
+      ]])],
+   [ac_cv_c__Generic=yes],
+   [ac_cv_c__Generic=no])])
+if test $ac_cv_c__Generic = yes; then
+  AC_DEFINE([HAVE_C__GENERIC], 1,
+           [Define to 1 if C11-style _Generic works.])
+fi
+])# AC_C__GENERIC
+
+AC_C__GENERIC
+AC_FUNC_STRERROR_R
+
 AC_ARG_VAR([MATH_CFLAGS], [C compiler flags for math])
 AC_ARG_VAR([MATH_LIBS], [linker flags for math])
 if test -z "${MATH_LIBS}"; then
index 7d6d9a026c577096c9ef327eaaea4571491dd10c..0dd22436750f8ee0b2003dc9e51674d05340506d 100644 (file)
 #ifdef __GNUC__
 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
 
-#if GCC_VERSION < 40900
-#define WITHOUT_C11_GENERIC 1
-#endif
-
 #if __x86_64__ || __ppc64__
 #define ENVIRONMENT64
 #else
index ee3c66dee3ba465853cd77627b57e6301acf1155..b7ec64bcb9463eb31b2eab07c37685c7f6a6d5ee 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -266,17 +266,13 @@ void info_int( const char *file, const char *function, const unsigned long line,
 // ----------------------------------------------------------------------------
 // error log
 
-#ifdef WITHOUT_C11_GENERIC
-
-#ifdef STRERROR_R_POSIX
-// POSIX version of strerror_r
-static const char *strerror_result(int a, const char *b) { (void)a; return b; }
-#else
+#if defined(STRERROR_R_CHAR_P)
 // GLIBC version of strerror_r
 static const char *strerror_result(const char *a, const char *b) { (void)b; return a; }
-#endif
-
-#else /* ! WITHOUT_C11_GENERIC */
+#elif defined(HAVE_STRERROR_R)
+// POSIX version of strerror_r
+static const char *strerror_result(int a, const char *b) { (void)a; return b; }
+#elif defined(HAVE_C__GENERIC)
 
 // what a trick!
 // http://stackoverflow.com/questions/479207/function-overloading-in-c
@@ -288,7 +284,9 @@ static const char *strerror_result_string(const char *a, const char *b) { (void)
     char *: strerror_result_string \
     )(a, b)
 
-#endif /* ! WITHOUT_C11_GENERIC */
+#else
+#error "cannot detect the format of function strerror_r()"
+#endif
 
 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
 {