immutable: Add tests.
[gnulib.git] / m4 / wcsrtombs.m4
blob903bdd34354344d731734c23048cc09a352e76d4
1 # wcsrtombs.m4 serial 13
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.
7 AC_DEFUN([gl_FUNC_WCSRTOMBS],
9   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
11   AC_REQUIRE([AC_TYPE_MBSTATE_T])
12   gl_MBSTATE_T_BROKEN
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       #include <wchar.h>
19     ]])
20     if test $ac_cv_have_decl_wcsrtombs = yes; then
21       dnl On Minix 3.1.8, the system's <wchar.h> declares wcsrtombs() although
22       dnl it does not have the function. Avoid a collision with gnulib's
23       dnl replacement.
24       REPLACE_WCSRTOMBS=1
25     fi
26   else
27     if test $REPLACE_MBSTATE_T = 1; then
28       REPLACE_WCSRTOMBS=1
29     else
30       gl_WCSRTOMBS_TERMINATION
31       gl_WCSRTOMBS_NULL
32       case "$gl_cv_func_wcsrtombs_termination" in
33         *yes) ;;
34         *) AC_DEFINE([WCSRTOMBS_TERMINATION_BUG], [1],
35              [Define if the wcsrtombs function may set the source pointer to NULL without NUL-terminating the destination.])
36            REPLACE_WCSRTOMBS=1 ;;
37       esac
38       case "$gl_cv_func_wcsrtombs_null" in
39         *yes) ;;
40         *) AC_DEFINE([WCSRTOMBS_NULL_ARG_BUG], [1],
41              [Define if the wcsrtombs function has the NULL destination argument bug.])
42            REPLACE_WCSRTOMBS=1 ;;
43       esac
44     fi
45   fi
48 dnl Test whether the wcsrtombs implementation stores a non-NULL source pointer
49 dnl as long as it has not written the final NUL byte to the destination string.
50 dnl The OSF/1 5.1 implementation stores a NULL pointer already after storing
51 dnl the last non-NUL character, even if there is no more room for the
52 dnl terminating NUL character.
53 dnl Result is gl_cv_func_wcsrtombs_termination.
55 AC_DEFUN([gl_WCSRTOMBS_TERMINATION],
57   AC_REQUIRE([AC_PROG_CC])
58   AC_REQUIRE([gt_LOCALE_FR])
59   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
60   AC_CACHE_CHECK([whether wcsrtombs updates the source pointer correctly],
61     [gl_cv_func_wcsrtombs_termination],
62     [
63       dnl Initial guess, used when cross-compiling or when no suitable locale
64       dnl is present.
65 changequote(,)dnl
66       case "$host_os" in
67                 # Guess no on OSF/1.
68         osf*)   gl_cv_func_wcsrtombs_termination="guessing no" ;;
69                 # Guess yes on native Windows.
70         mingw*) gl_cv_func_wcsrtombs_termination="guessing yes" ;;
71                 # Guess yes otherwise.
72         *)      gl_cv_func_wcsrtombs_termination="guessing yes" ;;
73       esac
74 changequote([,])dnl
75       if test $LOCALE_FR != none; then
76         AC_RUN_IFELSE(
77           [AC_LANG_SOURCE([[
78 #include <locale.h>
79 #include <stdlib.h>
80 #include <wchar.h>
81 int main ()
83   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
84     {
85       const char original[] = "B\374\337er";
86       wchar_t input[10];
88       if (mbstowcs (input, original, 10) == 5)
89         {
90           const wchar_t *src = input;
91           char output[5];
92           wcsrtombs (output, &src, 5, NULL);
93           if (src != input + 5)
94             return 1;
95         }
96     }
97   return 0;
98 }]])],
99           [gl_cv_func_wcsrtombs_termination=yes],
100           [gl_cv_func_wcsrtombs_termination=no],
101           [:])
102       fi
103     ])
106 dnl Test whether the wcsrtombs implementation supports a NULL argument
107 dnl correctly. This is not the case on HP-UX 11.11 and OSF/1 5.1: These
108 dnl implementations updates the source pointer also if the destination argument
109 dnl is NULL.
110 dnl Result is gl_cv_func_wcsrtombs_null.
112 AC_DEFUN([gl_WCSRTOMBS_NULL],
114   AC_REQUIRE([AC_PROG_CC])
115   AC_REQUIRE([gt_LOCALE_FR])
116   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
117   AC_CACHE_CHECK([whether wcsrtombs supports a NULL argument correctly],
118     [gl_cv_func_wcsrtombs_null],
119     [
120       dnl Initial guess, used when cross-compiling or when no suitable locale
121       dnl is present.
122 changequote(,)dnl
123       case "$host_os" in
124                                # Guess no on HP-UX, OSF/1, mingw.
125         hpux* | osf* | mingw*) gl_cv_func_wcsrtombs_null="guessing no" ;;
126                                # Guess yes on native Windows.
127         mingw*)                gl_cv_func_wcsrtombs_null="guessing yes" ;;
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 #include <wchar.h>
138 int main ()
140   int result = 0;
141   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
142     {
143       const char original[] = "B\374\337er";
144       wchar_t input[10];
146       if (mbstowcs (input, original, 10) == 5)
147         {
148           const wchar_t *src = input;
149           size_t ret = wcsrtombs (NULL, &src, 3, NULL);
150           if (ret != 5)
151             result |= 1;
152           if (src != input)
153             result |= 2;
154         }
155     }
156   return result;
157 }]])],
158           [gl_cv_func_wcsrtombs_null=yes],
159           [gl_cv_func_wcsrtombs_null=no],
160           [:])
161       fi
162     ])
165 # Prerequisites of lib/wcsrtombs.c.
166 AC_DEFUN([gl_PREREQ_WCSRTOMBS], [
167   :