strerror_r-posix: fix on MacOS
[gnulib.git] / m4 / strerror.m4
1 # strerror.m4 serial 15
2 dnl Copyright (C) 2002, 2007-2011 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_STRERROR],
8 [
9   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
10   AC_REQUIRE([gl_HEADER_ERRNO_H])
11   m4_ifdef([gl_FUNC_STRERROR_R_WORKS], [
12     AC_REQUIRE([gl_FUNC_STRERROR_R_WORKS])
13   ])
14   if test -z "$ERRNO_H"; then
15     AC_CACHE_CHECK([for working strerror function],
16      [gl_cv_func_working_strerror],
17      [AC_RUN_IFELSE(
18         [AC_LANG_PROGRAM(
19            [[#include <string.h>
20              #include <errno.h>
21            ]],
22            [[int result = 0;
23              char *str;
24              if (!*strerror (-2)) result |= 1;
25              errno = 0;
26              str = strerror (0);
27              if (!*str) result |= 2;
28              if (errno) result |= 4;
29              if (strstr (str, "nknown") || strstr (str, "ndefined"))
30                result |= 8;
31              return result;]])],
32         [gl_cv_func_working_strerror=yes],
33         [gl_cv_func_working_strerror=no],
34         [dnl Be pessimistic on cross-compiles for now.
35          gl_cv_func_working_strerror=no])
36     ])
37     if test $gl_cv_func_working_strerror = no; then
38       dnl The system's strerror() fails to return a string for out-of-range
39       dnl integers. Replace it.
40       REPLACE_STRERROR=1
41     fi
42     m4_ifdef([gl_FUNC_STRERROR_R_WORKS], [
43       dnl If the system's strerror_r or __xpg_strerror_r clobbers strerror's
44       dnl buffer, we must replace strerror.
45       case "$gl_cv_func_strerror_r_works" in
46         *no) REPLACE_STRERROR=1 ;;
47       esac
48     ])
49   else
50     dnl The system's strerror() cannot know about the new errno values we add
51     dnl to <errno.h>. Replace it.
52     REPLACE_STRERROR=1
53   fi
54 ])