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