strerror: enforce POSIX ruling on strerror(0)
[gnulib.git] / m4 / strerror_r.m4
1 # strerror_r.m4 serial 4
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_R],
8 [
9   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
10   AC_REQUIRE([gl_HEADER_ERRNO_H])
11   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
12
13   dnl Persuade Solaris <string.h> to declare strerror_r().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15
16   dnl Some systems don't declare strerror_r() if _THREAD_SAFE and _REENTRANT
17   dnl are not defined.
18   AC_CHECK_DECLS_ONCE([strerror_r])
19   if test $ac_cv_have_decl_strerror_r = no; then
20     HAVE_DECL_STRERROR_R=0
21   fi
22
23   AC_CHECK_FUNCS([strerror_r])
24   if test $ac_cv_func_strerror_r = yes; then
25     if test -z "$ERRNO_H"; then
26       dnl The POSIX prototype is:  int strerror_r (int, char *, size_t);
27       dnl glibc, Cygwin:           char *strerror_r (int, char *, size_t);
28       dnl AIX 5.1, OSF/1 5.1:      int strerror_r (int, char *, int);
29       AC_CACHE_CHECK([for strerror_r with POSIX signature],
30         [gl_cv_func_strerror_r_posix_signature],
31         [AC_COMPILE_IFELSE(
32            [AC_LANG_PROGRAM(
33               [[#include <string.h>
34                 int strerror_r (int, char *, size_t);
35               ]],
36               [[return strerror (0);]])],
37            [gl_cv_func_strerror_r_posix_signature=yes],
38            [gl_cv_func_strerror_r_posix_signature=no])
39         ])
40       if test $gl_cv_func_strerror_r_posix_signature = yes; then
41         dnl AIX 6.1 strerror_r fails by returning -1, not an error number.
42         dnl HP-UX 11.31 strerror_r always fails when the buffer length argument
43         dnl is less than 80.
44         dnl FreeBSD 8.s strerror_r claims failure on 0
45         AC_CACHE_CHECK([whether strerror_r works],
46           [gl_cv_func_strerror_r_works],
47           [AC_RUN_IFELSE(
48              [AC_LANG_PROGRAM(
49                 [[#include <errno.h>
50                   #include <string.h>
51                   int strerror_r (int, char *, size_t);
52                 ]],
53                 [[int result = 0;
54                   char buf[79];
55                   if (strerror_r (EACCES, buf, 0) < 0)
56                     result |= 1;
57                   errno = 0;
58                   if (strerror_r (EACCES, buf, sizeof buf) != 0)
59                     result |= 2;
60                   if (strerror_r (0, buf, sizeof buf) != 0)
61                     result |= 4;
62                   if (errno)
63                     result |= 8;
64                   return result;
65                 ]])],
66              [gl_cv_func_strerror_r_works=yes],
67              [gl_cv_func_strerror_r_works=no],
68              [
69 changequote(,)dnl
70               case "$host_os" in
71                        # Guess no on AIX.
72                 aix*)  gl_cv_func_strerror_r_works="guessing no";;
73                        # Guess no on HP-UX.
74                 hpux*) gl_cv_func_strerror_r_works="guessing no";;
75                        # Guess no on FreeBSD.
76                 freebsd*)  gl_cv_func_strerror_r_works="guessing no";;
77                        # Guess yes otherwise.
78                 *)     gl_cv_func_strerror_r_works="guessing yes";;
79               esac
80 changequote([,])dnl
81              ])
82           ])
83         case "$gl_cv_func_strerror_r_works" in
84           *no) REPLACE_STRERROR_R=1 ;;
85         esac
86       else
87         dnl The system's strerror() has a wrong signature. Replace it.
88         REPLACE_STRERROR_R=1
89         dnl glibc >= 2.3.4 and cygwin 1.7.9 have a function __xpg_strerror_r.
90         AC_CHECK_FUNCS([__xpg_strerror_r])
91       fi
92     else
93       dnl The system's strerror_r() cannot know about the new errno values we
94       dnl add to <errno.h>. Replace it.
95       REPLACE_STRERROR_R=1
96       AC_DEFINE([EXTEND_STRERROR_R], [1],
97         [Define to 1 if strerror_r needs to be extended so that it handles the
98          extra errno values.])
99     fi
100   fi
101   if test $HAVE_DECL_STRERROR_R = 0 || test $REPLACE_STRERROR_R = 1; then
102     AC_LIBOBJ([strerror_r])
103     gl_PREREQ_STRERROR_R
104   fi
105 ])
106
107 # Prerequisites of lib/strerror_r.c.
108 AC_DEFUN([gl_PREREQ_STRERROR_R], [
109   :
110 ])