| 1 |
# strerror_r.m4 |
| 2 |
# serial 28 |
| 3 |
dnl Copyright (C) 2002, 2007-2024 Free Software Foundation, Inc. |
| 4 |
dnl This file is free software; the Free Software Foundation |
| 5 |
dnl gives unlimited permission to copy and/or distribute it, |
| 6 |
dnl with or without modifications, as long as this notice is preserved. |
| 7 |
dnl This file is offered as-is, without any warranty. |
| 8 |
|
| 9 |
AC_DEFUN([gl_FUNC_STRERROR_R], |
| 10 |
[ |
| 11 |
AC_REQUIRE([gl_STRING_H_DEFAULTS]) |
| 12 |
AC_REQUIRE([gl_FUNC_STRERROR_R_WORKS]) |
| 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 "$GL_GENERATE_ERRNO_H:$REPLACE_STRERROR_0" = false:0; 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_r() 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>, or any fix for strerror(0). 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_REQUIRE([AC_FUNC_STRERROR_R]) |
| 43 |
dnl glibc >= 2.3.4 and cygwin 1.7.9 have a function __xpg_strerror_r. |
| 44 |
AC_CHECK_FUNCS_ONCE([__xpg_strerror_r]) |
| 45 |
gl_CHECK_FUNCS_ANDROID([catgets], [[#include <nl_types.h>]]) |
| 46 |
AC_CHECK_FUNCS_ONCE([snprintf]) |
| 47 |
]) |
| 48 |
|
| 49 |
# Detect if strerror_r works, but without affecting whether a replacement |
| 50 |
# strerror_r will be used. |
| 51 |
AC_DEFUN([gl_FUNC_STRERROR_R_WORKS], |
| 52 |
[ |
| 53 |
AC_REQUIRE([gl_HEADER_ERRNO_H]) |
| 54 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 55 |
|
| 56 |
dnl Persuade Android <string.h> to use the GNU strerror_r API, |
| 57 |
dnl and Solaris <string.h> to declare strerror_r. |
| 58 |
AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) |
| 59 |
|
| 60 |
AC_REQUIRE([gl_FUNC_STRERROR_0]) |
| 61 |
|
| 62 |
gl_CHECK_FUNCS_ANDROID([strerror_r], [[#include <string.h>]]) |
| 63 |
if test $ac_cv_func_strerror_r = yes; then |
| 64 |
if test "$GL_GENERATE_ERRNO_H:$REPLACE_STRERROR_0" = false:0; then |
| 65 |
dnl The POSIX prototype is: int strerror_r (int, char *, size_t); |
| 66 |
dnl glibc, Cygwin: char *strerror_r (int, char *, size_t); |
| 67 |
dnl AIX 5.1, OSF/1 5.1: int strerror_r (int, char *, int); |
| 68 |
AC_CACHE_CHECK([for strerror_r with POSIX signature], |
| 69 |
[gl_cv_func_strerror_r_posix_signature], |
| 70 |
[AC_COMPILE_IFELSE( |
| 71 |
[AC_LANG_PROGRAM( |
| 72 |
[[#include <string.h> |
| 73 |
int strerror_r (int, char *, size_t); |
| 74 |
]], |
| 75 |
[])], |
| 76 |
[gl_cv_func_strerror_r_posix_signature=yes], |
| 77 |
[gl_cv_func_strerror_r_posix_signature=no]) |
| 78 |
]) |
| 79 |
if test $gl_cv_func_strerror_r_posix_signature = yes; then |
| 80 |
dnl AIX 6.1 strerror_r fails by returning -1, not an error number. |
| 81 |
dnl HP-UX 11.31 strerror_r always fails when the buffer length argument |
| 82 |
dnl is less than 80. |
| 83 |
dnl FreeBSD 8.s strerror_r claims failure on 0 |
| 84 |
dnl Mac OS X 10.5 strerror_r treats 0 like -1 |
| 85 |
dnl Solaris 10 strerror_r corrupts errno on failure |
| 86 |
AC_CACHE_CHECK([whether strerror_r works], |
| 87 |
[gl_cv_func_strerror_r_works], |
| 88 |
[AC_RUN_IFELSE( |
| 89 |
[AC_LANG_PROGRAM( |
| 90 |
[[#include <errno.h> |
| 91 |
#include <string.h> |
| 92 |
]], |
| 93 |
[[int result = 0; |
| 94 |
char buf[79]; |
| 95 |
if (strerror_r (EACCES, buf, 0) < 0) |
| 96 |
result |= 1; |
| 97 |
errno = 0; |
| 98 |
if (strerror_r (EACCES, buf, sizeof buf) != 0) |
| 99 |
result |= 2; |
| 100 |
strcpy (buf, "Unknown"); |
| 101 |
if (strerror_r (0, buf, sizeof buf) != 0) |
| 102 |
result |= 4; |
| 103 |
if (errno) |
| 104 |
result |= 8; |
| 105 |
if (strstr (buf, "nknown") || strstr (buf, "ndefined")) |
| 106 |
result |= 0x10; |
| 107 |
errno = 0; |
| 108 |
*buf = 0; |
| 109 |
if (strerror_r (-3, buf, sizeof buf) < 0) |
| 110 |
result |= 0x20; |
| 111 |
if (errno) |
| 112 |
result |= 0x40; |
| 113 |
if (!*buf) |
| 114 |
result |= 0x80; |
| 115 |
return result; |
| 116 |
]])], |
| 117 |
[gl_cv_func_strerror_r_works=yes], |
| 118 |
[gl_cv_func_strerror_r_works=no], |
| 119 |
[ |
| 120 |
changequote(,)dnl |
| 121 |
case "$host_os" in |
| 122 |
# Guess no on AIX. |
| 123 |
aix*) gl_cv_func_strerror_r_works="guessing no";; |
| 124 |
# Guess no on HP-UX. |
| 125 |
hpux*) gl_cv_func_strerror_r_works="guessing no";; |
| 126 |
# Guess no on BSD variants. |
| 127 |
*bsd*) gl_cv_func_strerror_r_works="guessing no";; |
| 128 |
# Guess yes otherwise. |
| 129 |
*) gl_cv_func_strerror_r_works="guessing yes";; |
| 130 |
esac |
| 131 |
changequote([,])dnl |
| 132 |
]) |
| 133 |
]) |
| 134 |
else |
| 135 |
dnl The system's strerror() has a wrong signature. |
| 136 |
dnl glibc >= 2.3.4 and cygwin 1.7.9 have a function __xpg_strerror_r. |
| 137 |
AC_CHECK_FUNCS_ONCE([__xpg_strerror_r]) |
| 138 |
dnl In glibc < 2.14, __xpg_strerror_r does not populate buf on failure. |
| 139 |
dnl In cygwin < 1.7.10, __xpg_strerror_r clobbers strerror's buffer. |
| 140 |
if test $ac_cv_func___xpg_strerror_r = yes; then |
| 141 |
AC_CACHE_CHECK([whether __xpg_strerror_r works], |
| 142 |
[gl_cv_func_strerror_r_works], |
| 143 |
[AC_RUN_IFELSE( |
| 144 |
[AC_LANG_PROGRAM( |
| 145 |
[[#include <errno.h> |
| 146 |
#include <string.h> |
| 147 |
extern |
| 148 |
#ifdef __cplusplus |
| 149 |
"C" |
| 150 |
#endif |
| 151 |
int __xpg_strerror_r(int, char *, size_t); |
| 152 |
]], |
| 153 |
[[int result = 0; |
| 154 |
char buf[256] = "^"; |
| 155 |
char copy[256]; |
| 156 |
char *str = strerror (-1); |
| 157 |
strcpy (copy, str); |
| 158 |
if (__xpg_strerror_r (-2, buf, 1) == 0) |
| 159 |
result |= 1; |
| 160 |
if (*buf) |
| 161 |
result |= 2; |
| 162 |
__xpg_strerror_r (-2, buf, 256); |
| 163 |
if (strcmp (str, copy)) |
| 164 |
result |= 4; |
| 165 |
return result; |
| 166 |
]])], |
| 167 |
[gl_cv_func_strerror_r_works=yes], |
| 168 |
[gl_cv_func_strerror_r_works=no], |
| 169 |
[dnl Guess no on all platforms that have __xpg_strerror_r, |
| 170 |
dnl at least until fixed glibc and cygwin are more common. |
| 171 |
gl_cv_func_strerror_r_works="$gl_cross_guess_normal" |
| 172 |
]) |
| 173 |
]) |
| 174 |
fi |
| 175 |
fi |
| 176 |
fi |
| 177 |
else |
| 178 |
case "$gl_cv_onwards_func_strerror_r" in |
| 179 |
future*) REPLACE_STRERROR_R=1 ;; |
| 180 |
esac |
| 181 |
fi |
| 182 |
]) |