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
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],
35 if (btowc ('\0') != 0)
39 [gl_cv_func_btowc_nul=yes],
40 [gl_cv_func_btowc_nul=no],
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" ;;
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],
59 dnl Initial guess, used when cross-compiling or when no suitable locale
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" ;;
71 if test $LOCALE_FR != none; then
79 if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
81 if (btowc (EOF) != WEOF)
86 [gl_cv_func_btowc_eof=yes],
87 [gl_cv_func_btowc_eof=no],
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],
107 wint_t wc1 = btowc (0x80);
108 wchar_t wc2 = (wchar_t) 0xbadface;
109 char buf[1] = { 0x80 };
111 memset (&state, 0, sizeof (mbstate_t));
112 if (mbrtowc (&wc2, buf, 1, &state) != 1 || wc1 != wc2)
117 [gl_cv_func_btowc_consistent=yes],
118 [gl_cv_func_btowc_consistent=no],
122 AC_EGREP_CPP([Problem], [
127 [gl_cv_func_btowc_consistent="guessing no"],
128 [gl_cv_func_btowc_consistent="guessing yes"])
130 # Guess yes otherwise.
131 *) gl_cv_func_btowc_consistent="guessing yes" ;;
136 case "$gl_cv_func_btowc_nul" in
138 *) REPLACE_BTOWC=1 ;;
140 case "$gl_cv_func_btowc_eof" in
142 *) REPLACE_BTOWC=1 ;;
144 case "$gl_cv_func_btowc_consistent" in
146 *) REPLACE_BTOWC=1 ;;
148 if test $REPLACE_BTOWC = 0; then
150 case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
152 *) REPLACE_BTOWC=1 ;;
158 # Prerequisites of lib/btowc.c.
159 AC_DEFUN([gl_PREREQ_BTOWC], [
161 AC_CHECK_FUNCS_ONCE([mbrtowc])