strerror_r-posix: Tweaks.
[gnulib.git] / m4 / strerror_r.m4
1 # strerror_r.m4 serial 10
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_FUNC_STRERROR_R_WORKS])
11
12   dnl Persuade Solaris <string.h> to declare strerror_r().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14
15   dnl Some systems don't declare strerror_r() if _THREAD_SAFE and _REENTRANT
16   dnl are not defined.
17   AC_CHECK_DECLS_ONCE([strerror_r])
18   if test $ac_cv_have_decl_strerror_r = no; then
19     HAVE_DECL_STRERROR_R=0
20   fi
21
22   if test $ac_cv_func_strerror_r = yes; then
23     if test -z "$ERRNO_H"; then
24       if test $gl_cv_func_strerror_r_posix_signature = yes; then
25         case "$gl_cv_func_strerror_r_works" in
26           dnl The system's strerror_r has bugs.  Replace it.
27           *no) REPLACE_STRERROR_R=1 ;;
28         esac
29       else
30         dnl The system's strerror_r() has a wrong signature. Replace it.
31         REPLACE_STRERROR_R=1
32       fi
33     else
34       dnl The system's strerror_r() cannot know about the new errno values we
35       dnl add to <errno.h>. Replace it.
36       REPLACE_STRERROR_R=1
37     fi
38   fi
39 ])
40
41 # Prerequisites of lib/strerror_r.c.
42 AC_DEFUN([gl_PREREQ_STRERROR_R], [
43   dnl glibc >= 2.3.4 and cygwin 1.7.9 have a function __xpg_strerror_r.
44   AC_CHECK_FUNCS([__xpg_strerror_r])
45   AC_CHECK_FUNCS_ONCE([catgets])
46 ])
47
48 # Detect if strerror_r works, but without affecting whether a replacement
49 # strerror_r will be used.
50 AC_DEFUN([gl_FUNC_STRERROR_R_WORKS],
51 [
52   AC_REQUIRE([gl_HEADER_ERRNO_H])
53   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
54
55   AC_CHECK_FUNCS_ONCE([strerror_r])
56   if test $ac_cv_func_strerror_r = yes; then
57     if test -z "$ERRNO_H"; then
58       dnl The POSIX prototype is:  int strerror_r (int, char *, size_t);
59       dnl glibc, Cygwin:           char *strerror_r (int, char *, size_t);
60       dnl AIX 5.1, OSF/1 5.1:      int strerror_r (int, char *, int);
61       AC_CACHE_CHECK([for strerror_r with POSIX signature],
62         [gl_cv_func_strerror_r_posix_signature],
63         [AC_COMPILE_IFELSE(
64            [AC_LANG_PROGRAM(
65               [[#include <string.h>
66                 int strerror_r (int, char *, size_t);
67               ]],
68               [])],
69            [gl_cv_func_strerror_r_posix_signature=yes],
70            [gl_cv_func_strerror_r_posix_signature=no])
71         ])
72       if test $gl_cv_func_strerror_r_posix_signature = yes; then
73         dnl AIX 6.1 strerror_r fails by returning -1, not an error number.
74         dnl HP-UX 11.31 strerror_r always fails when the buffer length argument
75         dnl is less than 80.
76         dnl FreeBSD 8.s strerror_r claims failure on 0
77         dnl Solaris 10 strerror_r corrupts errno on failure
78         AC_CACHE_CHECK([whether strerror_r works],
79           [gl_cv_func_strerror_r_works],
80           [AC_RUN_IFELSE(
81              [AC_LANG_PROGRAM(
82                 [[#include <errno.h>
83                   #include <string.h>
84                 ]],
85                 [[int result = 0;
86                   char buf[79];
87                   if (strerror_r (EACCES, buf, 0) < 0)
88                     result |= 1;
89                   errno = 0;
90                   if (strerror_r (EACCES, buf, sizeof buf) != 0)
91                     result |= 2;
92                   if (strerror_r (0, buf, sizeof buf) != 0)
93                     result |= 4;
94                   if (errno)
95                     result |= 8;
96                   errno = 0;
97                   if (strerror_r (-3, buf, sizeof buf) != 0)
98                     result |= 0x10;
99                   if (errno)
100                     result |= 0x20;
101                   return result;
102                 ]])],
103              [gl_cv_func_strerror_r_works=yes],
104              [gl_cv_func_strerror_r_works=no],
105              [
106 changequote(,)dnl
107               case "$host_os" in
108                        # Guess no on AIX.
109                 aix*)  gl_cv_func_strerror_r_works="guessing no";;
110                        # Guess no on HP-UX.
111                 hpux*) gl_cv_func_strerror_r_works="guessing no";;
112                        # Guess no on FreeBSD.
113                 freebsd*)  gl_cv_func_strerror_r_works="guessing no";;
114                        # Guess yes otherwise.
115                 *)     gl_cv_func_strerror_r_works="guessing yes";;
116               esac
117 changequote([,])dnl
118              ])
119           ])
120       else
121         dnl The system's strerror() has a wrong signature.
122         dnl glibc >= 2.3.4 and cygwin 1.7.9 have a function __xpg_strerror_r.
123         AC_CHECK_FUNCS([__xpg_strerror_r])
124         dnl In glibc < 2.14, __xpg_strerror_r does not populate buf on failure.
125         dnl In cygwin < 1.7.10, __xpg_strerror_r clobbers strerror's buffer.
126         if test $ac_cv_func___xpg_strerror_r = yes; then
127           AC_CACHE_CHECK([whether __xpg_strerror_r works],
128             [gl_cv_func_strerror_r_works],
129             [AC_RUN_IFELSE(
130                [AC_LANG_PROGRAM(
131                   [[#include <errno.h>
132                     #include <string.h>
133                     extern int __xpg_strerror_r(int, char *, size_t);
134                   ]],
135                   [[int result = 0;
136                     char buf[256] = "^";
137                     char copy[256];
138                     char *str = strerror (-1);
139                     strcpy (copy, str);
140                     if (__xpg_strerror_r (-2, buf, 1) == 0)
141                       result |= 1;
142                     if (*buf)
143                       result |= 2;
144                     __xpg_strerror_r (-2, buf, 256);
145                     if (strcmp (str, copy))
146                       result |= 4;
147                     return result;
148                   ]])],
149                [gl_cv_func_strerror_r_works=yes],
150                [gl_cv_func_strerror_r_works=no],
151                [dnl Guess no on all platforms that have __xpg_strerror_r,
152                 dnl at least until fixed glibc and cygwin are more common.
153                 gl_cv_func_strerror_r_works="guessing no"
154                ])
155             ])
156         fi
157       fi
158     fi
159   fi
160 ])