1# strerror.m4 serial 21
2dnl Copyright (C) 2002, 2007-2020 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN([gl_FUNC_STRERROR],
8[
9  AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
10  AC_REQUIRE([gl_HEADER_ERRNO_H])
11  AC_REQUIRE([gl_FUNC_STRERROR_0])
12  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13  m4_ifdef([gl_FUNC_STRERROR_R_WORKS], [
14    AC_REQUIRE([gl_FUNC_STRERROR_R_WORKS])
15  ])
16  if test "$ERRNO_H:$REPLACE_STRERROR_0" = :0; then
17    AC_CACHE_CHECK([for working strerror function],
18     [gl_cv_func_working_strerror],
19     [AC_RUN_IFELSE(
20        [AC_LANG_PROGRAM(
21           [[#include <string.h>
22           ]],
23           [[if (!*strerror (-2)) return 1;]])],
24        [gl_cv_func_working_strerror=yes],
25        [gl_cv_func_working_strerror=no],
26        [case "$host_os" in
27                          # Guess yes on glibc systems.
28           *-gnu* | gnu*) gl_cv_func_working_strerror="guessing yes" ;;
29                          # Guess yes on musl systems.
30           *-musl*)       gl_cv_func_working_strerror="guessing yes" ;;
31                          # If we don't know, obey --enable-cross-guesses.
32           *)             gl_cv_func_working_strerror="$gl_cross_guess_normal" ;;
33         esac
34        ])
35    ])
36    case "$gl_cv_func_working_strerror" in
37      *yes) ;;
38      *)
39        dnl The system's strerror() fails to return a string for out-of-range
40        dnl integers. Replace it.
41        REPLACE_STRERROR=1
42        ;;
43    esac
44    m4_ifdef([gl_FUNC_STRERROR_R_WORKS], [
45      dnl If the system's strerror_r or __xpg_strerror_r clobbers strerror's
46      dnl buffer, we must replace strerror.
47      case "$gl_cv_func_strerror_r_works" in
48        *no) REPLACE_STRERROR=1 ;;
49      esac
50    ])
51  else
52    dnl The system's strerror() cannot know about the new errno values we add
53    dnl to <errno.h>, or any fix for strerror(0). Replace it.
54    REPLACE_STRERROR=1
55  fi
56])
57
58dnl Detect if strerror(0) passes (that is, does not set errno, and does not
59dnl return a string that matches strerror(-1)).
60AC_DEFUN([gl_FUNC_STRERROR_0],
61[
62  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
63  REPLACE_STRERROR_0=0
64  AC_CACHE_CHECK([whether strerror(0) succeeds],
65   [gl_cv_func_strerror_0_works],
66   [AC_RUN_IFELSE(
67      [AC_LANG_PROGRAM(
68         [[#include <string.h>
69           #include <errno.h>
70         ]],
71         [[int result = 0;
72           char *str;
73           errno = 0;
74           str = strerror (0);
75           if (!*str) result |= 1;
76           if (errno) result |= 2;
77           if (strstr (str, "nknown") || strstr (str, "ndefined"))
78             result |= 4;
79           return result;]])],
80      [gl_cv_func_strerror_0_works=yes],
81      [gl_cv_func_strerror_0_works=no],
82      [case "$host_os" in
83                        # Guess yes on glibc systems.
84         *-gnu* | gnu*) gl_cv_func_strerror_0_works="guessing yes" ;;
85                        # Guess yes on musl systems.
86         *-musl*)       gl_cv_func_strerror_0_works="guessing yes" ;;
87                        # Guess yes on native Windows.
88         mingw*)        gl_cv_func_strerror_0_works="guessing yes" ;;
89                        # If we don't know, obey --enable-cross-guesses.
90         *)             gl_cv_func_strerror_0_works="$gl_cross_guess_normal" ;;
91       esac
92      ])
93  ])
94  case "$gl_cv_func_strerror_0_works" in
95    *yes) ;;
96    *)
97      REPLACE_STRERROR_0=1
98      AC_DEFINE([REPLACE_STRERROR_0], [1], [Define to 1 if strerror(0)
99        does not return a message implying success.])
100      ;;
101  esac
102])
103