maint: update copyright
[gnulib.git] / m4 / wcsrtombs.m4
1 # wcsrtombs.m4 serial 11
2 dnl Copyright (C) 2008-2014 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_WCSRTOMBS],
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([wcsrtombs])
15   if test $ac_cv_func_wcsrtombs = no; then
16     HAVE_WCSRTOMBS=0
17     AC_CHECK_DECLS([wcsrtombs],,, [[
18 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
19    <wchar.h>.
20    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
21    included before <wchar.h>.  */
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <time.h>
25 #include <wchar.h>
26 ]])
27     if test $ac_cv_have_decl_wcsrtombs = yes; then
28       dnl On Minix 3.1.8, the system's <wchar.h> declares wcsrtombs() although
29       dnl it does not have the function. Avoid a collision with gnulib's
30       dnl replacement.
31       REPLACE_WCSRTOMBS=1
32     fi
33   else
34     if test $REPLACE_MBSTATE_T = 1; then
35       REPLACE_WCSRTOMBS=1
36     else
37       gl_WCSRTOMBS_TERMINATION
38       gl_WCSRTOMBS_NULL
39       case "$gl_cv_func_wcsrtombs_termination" in
40         *yes) ;;
41         *) AC_DEFINE([WCSRTOMBS_TERMINATION_BUG], [1],
42              [Define if the wcsrtombs function may set the source pointer to NULL without NUL-terminating the destination.])
43            REPLACE_WCSRTOMBS=1 ;;
44       esac
45       case "$gl_cv_func_wcsrtombs_null" in
46         *yes) ;;
47         *) AC_DEFINE([WCSRTOMBS_NULL_ARG_BUG], [1],
48              [Define if the wcsrtombs function has the NULL destination argument bug.])
49            REPLACE_WCSRTOMBS=1 ;;
50       esac
51     fi
52   fi
53 ])
54
55 dnl Test whether the wcsrtombs implementation stores a non-NULL source pointer
56 dnl as long as it has not written the final NUL byte to the destination string.
57 dnl The OSF/1 5.1 implementation stores a NULL pointer already after storing
58 dnl the last non-NUL character, even if there is no more room for the
59 dnl terminating NUL character.
60 dnl Result is gl_cv_func_wcsrtombs_termination.
61
62 AC_DEFUN([gl_WCSRTOMBS_TERMINATION],
63 [
64   AC_REQUIRE([AC_PROG_CC])
65   AC_REQUIRE([gt_LOCALE_FR])
66   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
67   AC_CACHE_CHECK([whether wcsrtombs updates the source pointer correctly],
68     [gl_cv_func_wcsrtombs_termination],
69     [
70       dnl Initial guess, used when cross-compiling or when no suitable locale
71       dnl is present.
72 changequote(,)dnl
73       case "$host_os" in
74               # Guess no on OSF/1.
75         osf*) gl_cv_func_wcsrtombs_termination="guessing no" ;;
76               # Guess yes otherwise.
77         *)    gl_cv_func_wcsrtombs_termination="guessing yes" ;;
78       esac
79 changequote([,])dnl
80       if test $LOCALE_FR != none; then
81         AC_RUN_IFELSE(
82           [AC_LANG_SOURCE([[
83 #include <locale.h>
84 #include <stdlib.h>
85 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
86    <wchar.h>.
87    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
88    included before <wchar.h>.  */
89 #include <stddef.h>
90 #include <stdio.h>
91 #include <time.h>
92 #include <wchar.h>
93 int main ()
94 {
95   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
96     {
97       const char original[] = "B\374\337er";
98       wchar_t input[10];
99
100       if (mbstowcs (input, original, 10) == 5)
101         {
102           const wchar_t *src = input;
103           char output[5];
104           wcsrtombs (output, &src, 5, NULL);
105           if (src != input + 5)
106             return 1;
107         }
108     }
109   return 0;
110 }]])],
111           [gl_cv_func_wcsrtombs_termination=yes],
112           [gl_cv_func_wcsrtombs_termination=no],
113           [:])
114       fi
115     ])
116 ])
117
118 dnl Test whether the wcsrtombs implementation supports a NULL argument
119 dnl correctly. This is not the case on HP-UX 11.11 and OSF/1 5.1: These
120 dnl implementations updates the source pointer also if the destination argument
121 dnl is NULL.
122 dnl Result is gl_cv_func_wcsrtombs_null.
123
124 AC_DEFUN([gl_WCSRTOMBS_NULL],
125 [
126   AC_REQUIRE([AC_PROG_CC])
127   AC_REQUIRE([gt_LOCALE_FR])
128   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
129   AC_CACHE_CHECK([whether wcsrtombs supports a NULL argument correctly],
130     [gl_cv_func_wcsrtombs_null],
131     [
132       dnl Initial guess, used when cross-compiling or when no suitable locale
133       dnl is present.
134 changequote(,)dnl
135       case "$host_os" in
136                                # Guess no on HP-UX, OSF/1, mingw.
137         hpux* | osf* | mingw*) gl_cv_func_wcsrtombs_null="guessing no" ;;
138                                # Guess yes otherwise.
139         *)                     gl_cv_func_wcsrtombs_null="guessing yes" ;;
140       esac
141 changequote([,])dnl
142       if test $LOCALE_FR != none; then
143         AC_RUN_IFELSE(
144           [AC_LANG_SOURCE([[
145 #include <locale.h>
146 #include <stdlib.h>
147 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
148    <wchar.h>.
149    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
150    included before <wchar.h>.  */
151 #include <stddef.h>
152 #include <stdio.h>
153 #include <time.h>
154 #include <wchar.h>
155 int main ()
156 {
157   int result = 0;
158   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
159     {
160       const char original[] = "B\374\337er";
161       wchar_t input[10];
162
163       if (mbstowcs (input, original, 10) == 5)
164         {
165           const wchar_t *src = input;
166           size_t ret = wcsrtombs (NULL, &src, 3, NULL);
167           if (ret != 5)
168             result |= 1;
169           if (src != input)
170             result |= 2;
171         }
172     }
173   return result;
174 }]])],
175           [gl_cv_func_wcsrtombs_null=yes],
176           [gl_cv_func_wcsrtombs_null=no],
177           [:])
178       fi
179     ])
180 ])
181
182 # Prerequisites of lib/wcsrtombs.c.
183 AC_DEFUN([gl_PREREQ_WCSRTOMBS], [
184   :
185 ])