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