1# perror.m4 serial 9
2dnl Copyright (C) 2008-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_PERROR],
8[
9  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
10  AC_REQUIRE([gl_HEADER_ERRNO_H])
11  AC_REQUIRE([gl_FUNC_STRERROR_R])
12  AC_REQUIRE([gl_FUNC_STRERROR_0])
13  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
14  dnl We intentionally do not check for the broader REPLACE_STRERROR_R,
15  dnl since on glibc systems, strerror_r is replaced only for signature
16  dnl issues, and perror is just fine.  Rather, we only want to
17  dnl replace perror if strerror_r was replaced for a content fix.
18  if test "$ERRNO_H:$REPLACE_STRERROR_0" != :0; then
19    dnl The system's perror() cannot know about the new errno values we add
20    dnl to <errno.h>, or any fix for strerror(0). Replace it.
21    REPLACE_PERROR=1
22  fi
23  case ${gl_cv_func_strerror_r_works-unset} in
24    unset|*yes)
25      AC_CACHE_CHECK([whether perror matches strerror],
26        [gl_cv_func_perror_works],
27        [AC_RUN_IFELSE(
28           [AC_LANG_PROGRAM(
29              [[#include <errno.h>
30                #include <stdio.h>
31                #include <stdlib.h>
32                #include <string.h>
33              ]],
34              [[char *str = strerror (-1);
35                if (!getenv("CONFTEST_OUTPUT")) return 0;
36                if (!str) str = "";
37                puts (str);
38                errno = -1;
39                perror ("");
40                return 0;
41              ]])],
42           [if CONFTEST_OUTPUT=1 ./conftest$EXEEXT >conftest.txt1 2>conftest.txt2 \
43               && cmp conftest.txt1 conftest.txt2 >/dev/null; then
44              gl_cv_func_perror_works=yes
45            else
46              gl_cv_func_perror_works=no
47            fi
48            rm -rf conftest.txt1 conftest.txt2],
49           [gl_cv_func_perror_works=no],
50           [case "$host_os" in
51                       # Guess yes on musl systems.
52              *-musl*) gl_cv_func_perror_works="guessing yes" ;;
53                       # Guess yes on native Windows.
54              mingw*)  gl_cv_func_perror_works="guessing yes" ;;
55                       # Otherwise obey --enable-cross-guesses.
56              *)       gl_cv_func_perror_works="$gl_cross_guess_normal" ;;
57            esac
58           ])
59        ])
60      case "$gl_cv_func_perror_works" in
61        *yes) ;;
62        *) REPLACE_PERROR=1 ;;
63      esac
64      ;;
65    *)
66      dnl The system's perror() probably inherits the bugs in the
67      dnl system's strerror_r(). Replace it.
68      REPLACE_PERROR=1
69      ;;
70  esac
71])
72