Debian Codegraph @

m4/btowc.m4

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