findprog, relocatable-prog: Ignore directories during PATH search.
[gnulib.git] / m4 / wcsrtombs.m4
blobb94e54ebcab861962c2eb3b15d07701f28c4f03f
1 # wcsrtombs.m4 serial 12
2 dnl Copyright (C) 2008-2020 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 /* 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
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.
62 AC_DEFUN([gl_WCSRTOMBS_TERMINATION],
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 on native Windows.
77         mingw*) gl_cv_func_wcsrtombs_termination="guessing yes" ;;
78                 # Guess yes otherwise.
79         *)      gl_cv_func_wcsrtombs_termination="guessing yes" ;;
80       esac
81 changequote([,])dnl
82       if test $LOCALE_FR != none; then
83         AC_RUN_IFELSE(
84           [AC_LANG_SOURCE([[
85 #include <locale.h>
86 #include <stdlib.h>
87 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
88    <wchar.h>.
89    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
90    included before <wchar.h>.  */
91 #include <stddef.h>
92 #include <stdio.h>
93 #include <time.h>
94 #include <wchar.h>
95 int main ()
97   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
98     {
99       const char original[] = "B\374\337er";
100       wchar_t input[10];
102       if (mbstowcs (input, original, 10) == 5)
103         {
104           const wchar_t *src = input;
105           char output[5];
106           wcsrtombs (output, &src, 5, NULL);
107           if (src != input + 5)
108             return 1;
109         }
110     }
111   return 0;
112 }]])],
113           [gl_cv_func_wcsrtombs_termination=yes],
114           [gl_cv_func_wcsrtombs_termination=no],
115           [:])
116       fi
117     ])
120 dnl Test whether the wcsrtombs implementation supports a NULL argument
121 dnl correctly. This is not the case on HP-UX 11.11 and OSF/1 5.1: These
122 dnl implementations updates the source pointer also if the destination argument
123 dnl is NULL.
124 dnl Result is gl_cv_func_wcsrtombs_null.
126 AC_DEFUN([gl_WCSRTOMBS_NULL],
128   AC_REQUIRE([AC_PROG_CC])
129   AC_REQUIRE([gt_LOCALE_FR])
130   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
131   AC_CACHE_CHECK([whether wcsrtombs supports a NULL argument correctly],
132     [gl_cv_func_wcsrtombs_null],
133     [
134       dnl Initial guess, used when cross-compiling or when no suitable locale
135       dnl is present.
136 changequote(,)dnl
137       case "$host_os" in
138                                # Guess no on HP-UX, OSF/1, mingw.
139         hpux* | osf* | mingw*) gl_cv_func_wcsrtombs_null="guessing no" ;;
140                                # Guess yes on native Windows.
141         mingw*)                gl_cv_func_wcsrtombs_null="guessing yes" ;;
142                                # Guess yes otherwise.
143         *)                     gl_cv_func_wcsrtombs_null="guessing yes" ;;
144       esac
145 changequote([,])dnl
146       if test $LOCALE_FR != none; then
147         AC_RUN_IFELSE(
148           [AC_LANG_SOURCE([[
149 #include <locale.h>
150 #include <stdlib.h>
151 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
152    <wchar.h>.
153    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
154    included before <wchar.h>.  */
155 #include <stddef.h>
156 #include <stdio.h>
157 #include <time.h>
158 #include <wchar.h>
159 int main ()
161   int result = 0;
162   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
163     {
164       const char original[] = "B\374\337er";
165       wchar_t input[10];
167       if (mbstowcs (input, original, 10) == 5)
168         {
169           const wchar_t *src = input;
170           size_t ret = wcsrtombs (NULL, &src, 3, NULL);
171           if (ret != 5)
172             result |= 1;
173           if (src != input)
174             result |= 2;
175         }
176     }
177   return result;
178 }]])],
179           [gl_cv_func_wcsrtombs_null=yes],
180           [gl_cv_func_wcsrtombs_null=no],
181           [:])
182       fi
183     ])
186 # Prerequisites of lib/wcsrtombs.c.
187 AC_DEFUN([gl_PREREQ_WCSRTOMBS], [
188   :