copysignl tests: Avoid failure on Solaris 11.4.
[gnulib.git] / m4 / btowc.m4
blobd9dd7036a229cd56dbbe385d33fc80f9d2473386
1 # btowc.m4
2 # serial 14
3 dnl Copyright (C) 2008-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.
8 AC_DEFUN([gl_FUNC_BTOWC],
10   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
12   dnl Check whether <wchar.h> is usable at all, first. Otherwise the test
13   dnl program below may lead to an endless loop. See
14   dnl <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42440>.
15   AC_REQUIRE([gl_WCHAR_H_INLINE_OK])
17   AC_CHECK_FUNCS_ONCE([btowc])
18   if test $ac_cv_func_btowc = no; then
19     HAVE_BTOWC=0
20   else
22     AC_REQUIRE([AC_PROG_CC])
23     AC_REQUIRE([gt_LOCALE_FR])
24     AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
26     dnl Cygwin 1.7.2 btowc('\0') is WEOF, not 0.
27     AC_CACHE_CHECK([whether btowc(0) is correct],
28       [gl_cv_func_btowc_nul],
29       [
30         AC_RUN_IFELSE(
31           [AC_LANG_SOURCE([[
32 #include <wchar.h>
33 int main ()
35   if (btowc ('\0') != 0)
36     return 1;
37   return 0;
38 }]])],
39           [gl_cv_func_btowc_nul=yes],
40           [gl_cv_func_btowc_nul=no],
41           [
42 changequote(,)dnl
43            case "$host_os" in
44                                 # Guess no on Cygwin.
45              cygwin*)           gl_cv_func_btowc_nul="guessing no" ;;
46                                 # Guess yes on native Windows.
47              mingw* | windows*) gl_cv_func_btowc_nul="guessing yes" ;;
48                                 # Guess yes otherwise.
49              *)                 gl_cv_func_btowc_nul="guessing yes" ;;
50            esac
51 changequote([,])dnl
52           ])
53       ])
55     dnl IRIX 6.5 btowc(EOF) is 0xFF, not WEOF.
56     AC_CACHE_CHECK([whether btowc(EOF) is correct],
57       [gl_cv_func_btowc_eof],
58       [
59         dnl Initial guess, used when cross-compiling or when no suitable locale
60         dnl is present.
61 changequote(,)dnl
62         case "$host_os" in
63                              # Guess no on IRIX.
64           irix*)             gl_cv_func_btowc_eof="guessing no" ;;
65                              # Guess yes on native Windows.
66           mingw* | windows*) gl_cv_func_btowc_eof="guessing yes" ;;
67                              # Guess yes otherwise.
68           *)                 gl_cv_func_btowc_eof="guessing yes" ;;
69         esac
70 changequote([,])dnl
71         if test $LOCALE_FR != none; then
72           AC_RUN_IFELSE(
73             [AC_LANG_SOURCE([[
74 #include <locale.h>
75 #include <stdio.h>
76 #include <wchar.h>
77 int main ()
79   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
80     {
81       if (btowc (EOF) != WEOF)
82         return 1;
83     }
84   return 0;
85 }]])],
86             [gl_cv_func_btowc_eof=yes],
87             [gl_cv_func_btowc_eof=no],
88             [:])
89         fi
90       ])
92     dnl On mingw, in the C locale, btowc is inconsistent with mbrtowc:
93     dnl mbrtowc avoids calling MultiByteToWideChar when MB_CUR_MAX is 1 and
94     dnl ___lc_codepage_func() is 0, but btowc is lacking this special case.
95     AC_CHECK_FUNCS_ONCE([mbrtowc])
96     AC_CACHE_CHECK([whether btowc is consistent with mbrtowc in the C locale],
97       [gl_cv_func_btowc_consistent],
98       [
99         AC_RUN_IFELSE(
100           [AC_LANG_SOURCE([[
101 #include <stdlib.h>
102 #include <string.h>
103 #include <wchar.h>
104 int main ()
106 #if HAVE_MBRTOWC
107   wint_t wc1 = btowc (0x80);
108   wchar_t wc2 = (wchar_t) 0xbadface;
109   char buf[1] = { 0x80 };
110   mbstate_t state;
111   memset (&state, 0, sizeof (mbstate_t));
112   if (mbrtowc (&wc2, buf, 1, &state) != 1 || wc1 != wc2)
113     return 1;
114 #endif
115   return 0;
116 }]])],
117           [gl_cv_func_btowc_consistent=yes],
118           [gl_cv_func_btowc_consistent=no],
119           [case "$host_os" in
120                # Guess no on mingw.
121              mingw* | windows*)
122                AC_EGREP_CPP([Problem], [
123 #ifdef __MINGW32__
124  Problem
125 #endif
126                  ],
127                  [gl_cv_func_btowc_consistent="guessing no"],
128                  [gl_cv_func_btowc_consistent="guessing yes"])
129                ;;
130                # Guess yes otherwise.
131              *) gl_cv_func_btowc_consistent="guessing yes" ;;
132            esac
133           ])
134       ])
136     case "$gl_cv_func_btowc_nul" in
137       *yes) ;;
138       *) REPLACE_BTOWC=1 ;;
139     esac
140     case "$gl_cv_func_btowc_eof" in
141       *yes) ;;
142       *) REPLACE_BTOWC=1 ;;
143     esac
144     case "$gl_cv_func_btowc_consistent" in
145       *yes) ;;
146       *) REPLACE_BTOWC=1 ;;
147     esac
148     if test $REPLACE_BTOWC = 0; then
149       gl_MBRTOWC_C_LOCALE
150       case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
151         *yes) ;;
152         *) REPLACE_BTOWC=1 ;;
153       esac
154     fi
155   fi
158 # Prerequisites of lib/btowc.c.
159 AC_DEFUN([gl_PREREQ_BTOWC], [
160   :
161   AC_CHECK_FUNCS_ONCE([mbrtowc])