Imported Upstream version 3.8
[platform/upstream/diffutils.git] / m4 / mbsrtowcs.m4
1 # mbsrtowcs.m4 serial 14
2 dnl Copyright (C) 2008-2021 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_MBSRTOWCS],
8 [
9   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
10
11   AC_REQUIRE([AC_TYPE_MBSTATE_T])
12   gl_MBSTATE_T_BROKEN
13
14   AC_CHECK_FUNCS_ONCE([mbsrtowcs])
15   if test $ac_cv_func_mbsrtowcs = no; then
16     HAVE_MBSRTOWCS=0
17     AC_CHECK_DECLS([mbsrtowcs],,, [[
18       #include <wchar.h>
19     ]])
20     if test $ac_cv_have_decl_mbsrtowcs = yes; then
21       dnl On Minix 3.1.8, the system's <wchar.h> declares mbsrtowcs() although
22       dnl it does not have the function. Avoid a collision with gnulib's
23       dnl replacement.
24       REPLACE_MBSRTOWCS=1
25     fi
26   else
27     if test $REPLACE_MBSTATE_T = 1; then
28       REPLACE_MBSRTOWCS=1
29     else
30       gl_MBSRTOWCS_WORKS
31       case "$gl_cv_func_mbsrtowcs_works" in
32         *yes) ;;
33         *) REPLACE_MBSRTOWCS=1 ;;
34       esac
35     fi
36   fi
37 ])
38
39 dnl Test whether mbsrtowcs works.
40 dnl Result is gl_cv_func_mbsrtowcs_works.
41
42 AC_DEFUN([gl_MBSRTOWCS_WORKS],
43 [
44   AC_REQUIRE([AC_PROG_CC])
45   AC_REQUIRE([gt_LOCALE_FR])
46   AC_REQUIRE([gt_LOCALE_FR_UTF8])
47   AC_REQUIRE([gt_LOCALE_JA])
48   AC_REQUIRE([gt_LOCALE_ZH_CN])
49   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
50   AC_CACHE_CHECK([whether mbsrtowcs works],
51     [gl_cv_func_mbsrtowcs_works],
52     [
53       dnl Initial guess, used when cross-compiling or when no suitable locale
54       dnl is present.
55 changequote(,)dnl
56       case "$host_os" in
57                                    # Guess no on HP-UX, Solaris, mingw.
58         hpux* | solaris* | mingw*) gl_cv_func_mbsrtowcs_works="guessing no" ;;
59                                    # Guess yes otherwise.
60         *)                         gl_cv_func_mbsrtowcs_works="guessing yes" ;;
61       esac
62 changequote([,])dnl
63       if test $LOCALE_FR != none || test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none || test $LOCALE_ZH_CN != none; then
64         AC_RUN_IFELSE(
65           [AC_LANG_SOURCE([[
66 #include <locale.h>
67 #include <string.h>
68 #include <wchar.h>
69 int main ()
70 {
71   int result = 0;
72   /* Test whether the function supports a NULL destination argument.
73      This fails on native Windows.  */
74   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
75     {
76       const char input[] = "\337er";
77       const char *src = input;
78       mbstate_t state;
79
80       memset (&state, '\0', sizeof (mbstate_t));
81       if (mbsrtowcs (NULL, &src, 1, &state) != 3
82           || src != input)
83         result |= 1;
84     }
85   /* Test whether the function works when started with a conversion state
86      in non-initial state.  This fails on HP-UX 11.11 and Solaris 10.  */
87   if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
88     {
89       const char input[] = "B\303\274\303\237er";
90       mbstate_t state;
91
92       memset (&state, '\0', sizeof (mbstate_t));
93       if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
94         if (!mbsinit (&state))
95           {
96             const char *src = input + 2;
97             if (mbsrtowcs (NULL, &src, 10, &state) != 4)
98               result |= 2;
99           }
100     }
101   if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
102     {
103       const char input[] = "<\306\374\313\334\270\354>";
104       mbstate_t state;
105
106       memset (&state, '\0', sizeof (mbstate_t));
107       if (mbrtowc (NULL, input + 3, 1, &state) == (size_t)(-2))
108         if (!mbsinit (&state))
109           {
110             const char *src = input + 4;
111             if (mbsrtowcs (NULL, &src, 10, &state) != 3)
112               result |= 4;
113           }
114     }
115   if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
116     {
117       const char input[] = "B\250\271\201\060\211\070er";
118       mbstate_t state;
119
120       memset (&state, '\0', sizeof (mbstate_t));
121       if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
122         if (!mbsinit (&state))
123           {
124             const char *src = input + 2;
125             if (mbsrtowcs (NULL, &src, 10, &state) != 4)
126               result |= 8;
127           }
128     }
129   return result;
130 }]])],
131           [gl_cv_func_mbsrtowcs_works=yes],
132           [gl_cv_func_mbsrtowcs_works=no],
133           [:])
134       fi
135     ])
136 ])
137
138 # Prerequisites of lib/mbsrtowcs.c.
139 AC_DEFUN([gl_PREREQ_MBSRTOWCS], [
140   :
141 ])