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