1249261Sdim# strerror.m4 serial 21
2249261Sdimdnl Copyright (C) 2002, 2007-2020 Free Software Foundation, Inc.
3249261Sdimdnl This file is free software; the Free Software Foundation
4249261Sdimdnl gives unlimited permission to copy and/or distribute it,
5249261Sdimdnl with or without modifications, as long as this notice is preserved.
6249261Sdim
7249261SdimAC_DEFUN([gl_FUNC_STRERROR],
8249261Sdim[
9249261Sdim  AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
10263509Sdim  AC_REQUIRE([gl_HEADER_ERRNO_H])
11263509Sdim  AC_REQUIRE([gl_FUNC_STRERROR_0])
12263509Sdim  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13249261Sdim  m4_ifdef([gl_FUNC_STRERROR_R_WORKS], [
14249261Sdim    AC_REQUIRE([gl_FUNC_STRERROR_R_WORKS])
15249261Sdim  ])
16249261Sdim  if test "$ERRNO_H:$REPLACE_STRERROR_0" = :0; then
17249261Sdim    AC_CACHE_CHECK([for working strerror function],
18249261Sdim     [gl_cv_func_working_strerror],
19263509Sdim     [AC_RUN_IFELSE(
20249261Sdim        [AC_LANG_PROGRAM(
21263509Sdim           [[#include <string.h>
22249261Sdim           ]],
23263509Sdim           [[if (!*strerror (-2)) return 1;]])],
24249261Sdim        [gl_cv_func_working_strerror=yes],
25263509Sdim        [gl_cv_func_working_strerror=no],
26249261Sdim        [case "$host_os" in
27249261Sdim                          # Guess yes on glibc systems.
28263509Sdim           *-gnu* | gnu*) gl_cv_func_working_strerror="guessing yes" ;;
29263509Sdim                          # Guess yes on musl systems.
30263509Sdim           *-musl*)       gl_cv_func_working_strerror="guessing yes" ;;
31263509Sdim                          # If we don't know, obey --enable-cross-guesses.
32263509Sdim           *)             gl_cv_func_working_strerror="$gl_cross_guess_normal" ;;
33263509Sdim         esac
34249261Sdim        ])
35263509Sdim    ])
36263509Sdim    case "$gl_cv_func_working_strerror" in
37263509Sdim      *yes) ;;
38249261Sdim      *)
39249261Sdim        dnl The system's strerror() fails to return a string for out-of-range
40263509Sdim        dnl integers. Replace it.
41263509Sdim        REPLACE_STRERROR=1
42263509Sdim        ;;
43263509Sdim    esac
44263509Sdim    m4_ifdef([gl_FUNC_STRERROR_R_WORKS], [
45263509Sdim      dnl If the system's strerror_r or __xpg_strerror_r clobbers strerror's
46263509Sdim      dnl buffer, we must replace strerror.
47263509Sdim      case "$gl_cv_func_strerror_r_works" in
48263509Sdim        *no) REPLACE_STRERROR=1 ;;
49263509Sdim      esac
50263509Sdim    ])
51263509Sdim  else
52263509Sdim    dnl The system's strerror() cannot know about the new errno values we add
53263509Sdim    dnl to <errno.h>, or any fix for strerror(0). Replace it.
54263509Sdim    REPLACE_STRERROR=1
55263509Sdim  fi
56263509Sdim])
57263509Sdim
58263509Sdimdnl Detect if strerror(0) passes (that is, does not set errno, and does not
59263509Sdimdnl return a string that matches strerror(-1)).
60263509SdimAC_DEFUN([gl_FUNC_STRERROR_0],
61263509Sdim[
62263509Sdim  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
63263509Sdim  REPLACE_STRERROR_0=0
64263509Sdim  AC_CACHE_CHECK([whether strerror(0) succeeds],
65263509Sdim   [gl_cv_func_strerror_0_works],
66263509Sdim   [AC_RUN_IFELSE(
67263509Sdim      [AC_LANG_PROGRAM(
68263509Sdim         [[#include <string.h>
69263509Sdim           #include <errno.h>
70263509Sdim         ]],
71263509Sdim         [[int result = 0;
72263509Sdim           char *str;
73263509Sdim           errno = 0;
74263509Sdim           str = strerror (0);
75263509Sdim           if (!*str) result |= 1;
76263509Sdim           if (errno) result |= 2;
77263509Sdim           if (strstr (str, "nknown") || strstr (str, "ndefined"))
78263509Sdim             result |= 4;
79263509Sdim           return result;]])],
80263509Sdim      [gl_cv_func_strerror_0_works=yes],
81263509Sdim      [gl_cv_func_strerror_0_works=no],
82263509Sdim      [case "$host_os" in
83263509Sdim                        # Guess yes on glibc systems.
84263509Sdim         *-gnu* | gnu*) gl_cv_func_strerror_0_works="guessing yes" ;;
85263509Sdim                        # Guess yes on musl systems.
86263509Sdim         *-musl*)       gl_cv_func_strerror_0_works="guessing yes" ;;
87263509Sdim                        # Guess yes on native Windows.
88263509Sdim         mingw*)        gl_cv_func_strerror_0_works="guessing yes" ;;
89263509Sdim                        # If we don't know, obey --enable-cross-guesses.
90263509Sdim         *)             gl_cv_func_strerror_0_works="$gl_cross_guess_normal" ;;
91263509Sdim       esac
92263509Sdim      ])
93263509Sdim  ])
94263509Sdim  case "$gl_cv_func_strerror_0_works" in
95263509Sdim    *yes) ;;
96263509Sdim    *)
97263509Sdim      REPLACE_STRERROR_0=1
98263509Sdim      AC_DEFINE([REPLACE_STRERROR_0], [1], [Define to 1 if strerror(0)
99263509Sdim        does not return a message implying success.])
100263509Sdim      ;;
101263509Sdim  esac
102263509Sdim])
103263509Sdim