Debian Codegraph @

m4/vasnprintf.m4

raw
1 # vasnprintf.m4
2 # serial 56
3 dnl Copyright (C) 2002-2004, 2006-2026 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN([gl_FUNC_VASNPRINTF],
10 [
11 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
12 AC_CHECK_FUNCS_ONCE([vasnprintf])
13 if test $ac_cv_func_vasnprintf = yes; then
14 dnl On Cygwin, in directives with a huge width, the width is ignored, and
15 dnl the function returns a wrong result.
16 AC_CACHE_CHECK([whether vasnprintf works],
17 [gl_cv_func_vasnprintf_works],
18 [AC_RUN_IFELSE(
19 [AC_LANG_SOURCE(
20 [[#include <stdio.h>
21 ]],
22 [[size_t len;
23 char *res = vasnprintf (NULL, &len, "x%03000000000dy\n", -17);
24 /* On Cygwin 3.4.6, res is "x-17y\n" and len == 6: wrong. */
25 return (res != NULL && len < 10);
26 ]])
27 ],
28 [gl_cv_func_vasnprintf_works=yes],
29 [gl_cv_func_vasnprintf_works=no],
30 [case "$host_os" in
31 # Guess no on Cygwin.
32 cygwin*) gl_cv_func_vasnprintf_works="guessing no";;
33 # If we don't know, obey --enable-cross-guesses.
34 *) gl_cv_func_vasnprintf_works="$gl_cross_guess_normal";;
35 esac
36 ])
37 ])
38 fi
39 if test $ac_cv_func_vasnprintf != yes \
40 || case "$gl_cv_func_vasnprintf_works" in
41 *yes) false;;
42 *) true;;
43 esac
44 then
45 gl_REPLACE_VASNPRINTF
46 fi
47 ])
48
49 AC_DEFUN([gl_REPLACE_VASNPRINTF],
50 [
51 AC_CHECK_FUNCS_ONCE([vasnprintf])
52 AC_LIBOBJ([vasnprintf])
53 AC_LIBOBJ([printf-args])
54 AC_LIBOBJ([printf-parse])
55 AC_LIBOBJ([asnprintf])
56 if test $ac_cv_func_vasnprintf = yes; then
57 AC_DEFINE([REPLACE_VASNPRINTF], [1],
58 [Define if vasnprintf exists but is overridden by gnulib.])
59 fi
60 gl_PREREQ_PRINTF_ARGS
61 gl_PREREQ_PRINTF_PARSE
62 gl_PREREQ_VASNPRINTF
63 gl_PREREQ_ASNPRINTF
64 ])
65
66 AC_DEFUN([gl_FUNC_VASNWPRINTF],
67 [
68 AC_LIBOBJ([printf-args])
69 gl_PREREQ_PRINTF_ARGS
70 gl_PREREQ_PRINTF_PARSE
71 gl_PREREQ_VASNWPRINTF
72 gl_PREREQ_ASNPRINTF
73 ])
74
75 # Prerequisites of lib/printf-args.h, lib/printf-args.c.
76 AC_DEFUN([gl_PREREQ_PRINTF_ARGS],
77 [
78 AC_REQUIRE([gt_TYPE_WINT_T])
79 ])
80
81 # Prerequisites of lib/printf-parse.h, lib/printf-parse.c.
82 # Prerequisites of lib/wprintf-parse.h, lib/wprintf-parse.c.
83 AC_DEFUN([gl_PREREQ_PRINTF_PARSE],
84 [
85 AC_REQUIRE([gl_FEATURES_H])
86 AC_REQUIRE([gt_TYPE_WINT_T])
87 AC_REQUIRE([AC_TYPE_SIZE_T])
88 AC_CHECK_TYPE([ptrdiff_t], ,
89 [AC_DEFINE([ptrdiff_t], [long],
90 [Define as the type of the result of subtracting two pointers, if the system doesn't define it.])
91 ])
92 AC_REQUIRE([gt_AC_TYPE_INTMAX_T])
93 ])
94
95 # Prerequisites of lib/vasnprintf.c if !WIDE_CHAR_VERSION.
96 AC_DEFUN_ONCE([gl_PREREQ_VASNPRINTF],
97 [
98 AC_CHECK_FUNCS([snprintf strnlen wcrtomb])
99 dnl Use the _snprintf function only if it is declared (because on NetBSD it
100 dnl is defined as a weak alias of snprintf; we prefer to use the latter).
101 AC_CHECK_DECLS([_snprintf], , , [[#include <stdio.h>]])
102 dnl We can avoid a lot of code by assuming that snprintf's return value
103 dnl conforms to ISO C99. So check that.
104 AC_REQUIRE([gl_SNPRINTF_RETVAL_C99])
105 case "$gl_cv_func_snprintf_retval_c99" in
106 *yes)
107 AC_DEFINE([HAVE_SNPRINTF_RETVAL_C99], [1],
108 [Define if the return value of the snprintf function is the number of
109 of bytes (excluding the terminating NUL) that would have been produced
110 if the buffer had been large enough.])
111 ;;
112 esac
113 dnl Additionally, the use of %n can be eliminated by assuming that snprintf
114 dnl always produces NUL-terminated strings (no truncation).
115 AC_REQUIRE([gl_SNPRINTF_TRUNCATION_C99])
116 case "$gl_cv_func_snprintf_truncation_c99" in
117 *yes)
118 AC_DEFINE([HAVE_SNPRINTF_TRUNCATION_C99], [1],
119 [Define if the string produced by the snprintf function is always NUL
120 terminated.])
121 ;;
122 esac
123 gl_PREREQ_VASNXPRINTF
124 ])
125
126 # Prerequisites of lib/vasnwprintf.c.
127 AC_DEFUN_ONCE([gl_PREREQ_VASNWPRINTF],
128 [
129 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
130 AC_CHECK_FUNCS_ONCE([swprintf wcsnlen mbrtowc])
131 AC_CHECK_DECLS([_snwprintf], , , [[#include <stdio.h>]])
132 AC_CHECK_DECLS([wcsnlen], , , [[#include <wchar.h>]])
133 gl_SWPRINTF_WORKS
134 case "$gl_cv_func_swprintf_works" in
135 *yes)
136 AC_DEFINE([HAVE_WORKING_SWPRINTF], [1],
137 [Define if the swprintf function works correctly when it produces output
138 that contains null wide characters.])
139 ;;
140 esac
141 gl_MBRTOWC_C_LOCALE
142 case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
143 *yes)
144 AC_CACHE_CHECK([whether swprintf in the C locale is free of encoding errors],
145 [gl_cv_func_swprintf_C_locale_sans_EILSEQ],
146 [
147 AC_RUN_IFELSE(
148 [AC_LANG_SOURCE([[
149 #ifndef __USE_MINGW_ANSI_STDIO
150 # define __USE_MINGW_ANSI_STDIO 1
151 #endif
152 #include <stdio.h>
153 #include <wchar.h>
154 int main()
155 {
156 int result = 0;
157 { /* This test fails on glibc 2.35, musl libc 1.2.4, FreeBSD 13.2, NetBSD 9.3,
158 OpenBSD 7.2, Cygwin 2.9.0.
159 Reported at <https://www.openwall.com/lists/musl/2023/06/12/2>. */
160 wchar_t buf[12];
161 int ret = swprintf (buf, 12, L"%c", '\377');
162 if (ret < 0)
163 result |= 1;
164 }
165 return result;
166 }]])],
167 [gl_cv_func_swprintf_C_locale_sans_EILSEQ=yes],
168 [gl_cv_func_swprintf_C_locale_sans_EILSEQ=no],
169 [case "$host_os" in
170 # Guess no on glibc systems.
171 *-gnu* | gnu*) gl_cv_func_swprintf_C_locale_sans_EILSEQ="guessing yes";;
172 # Guess no on musl systems.
173 *-musl* | midipix*) gl_cv_func_swprintf_C_locale_sans_EILSEQ="guessing no";;
174 # If we don't know, obey --enable-cross-guesses.
175 *) gl_cv_func_swprintf_C_locale_sans_EILSEQ="$gl_cross_guess_normal";;
176 esac
177 ])
178 ])
179 ;;
180 esac
181 if case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
182 *yes) false ;;
183 *) true ;;
184 esac \
185 || case "$gl_cv_func_swprintf_C_locale_sans_EILSEQ" in
186 *yes) false ;;
187 *) true ;;
188 esac; then
189 AC_DEFINE([NEED_WPRINTF_DIRECTIVE_C], [1],
190 [Define if the vasnwprintf implementation needs special code for
191 the 'c' directive.])
192 fi
193 gl_SWPRINTF_DIRECTIVE_LA
194 case "$gl_cv_func_swprintf_directive_la" in
195 *yes) ;;
196 *)
197 AC_DEFINE([NEED_WPRINTF_DIRECTIVE_LA], [1],
198 [Define if the vasnwprintf implementation needs special code for
199 the 'a' directive with 'long double' arguments.])
200 ;;
201 esac
202 gl_SWPRINTF_DIRECTIVE_LC
203 case "$gl_cv_func_swprintf_directive_lc" in
204 *yes) ;;
205 *)
206 AC_DEFINE([NEED_WPRINTF_DIRECTIVE_LC], [1],
207 [Define if the vasnwprintf implementation needs special code for
208 the 'lc' directive.])
209 ;;
210 esac
211 gl_MUSL_LIBC
212 gl_PREREQ_VASNXPRINTF
213 ])
214
215 # Common prerequisites of lib/vasnprintf.c and lib/vasnwprintf.c.
216 AC_DEFUN_ONCE([gl_PREREQ_VASNXPRINTF],
217 [
218 AC_REQUIRE([AC_FUNC_ALLOCA])
219 AC_REQUIRE([gt_TYPE_WINT_T])
220 AC_CHECK_FUNCS([wcslen])
221 dnl Knowing DBL_EXPBIT0_WORD and DBL_EXPBIT0_BIT enables an optimization
222 dnl in the code for NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE.
223 AC_REQUIRE([gl_DOUBLE_EXPONENT_LOCATION])
224 ])
225
226 # Extra prerequisites of lib/vasnprintf.c for supporting 'long double'
227 # arguments.
228 AC_DEFUN_ONCE([gl_PREREQ_VASNPRINTF_LONG_DOUBLE],
229 [
230 AC_REQUIRE([gl_PRINTF_LONG_DOUBLE])
231 case "$gl_cv_func_printf_long_double" in
232 *yes)
233 ;;
234 *)
235 AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
236 [Define if the vasnprintf implementation needs special code for
237 'long double' arguments.])
238 ;;
239 esac
240 ])
241
242 # Extra prerequisites of lib/vasnprintf.c for supporting infinite 'double'
243 # arguments.
244 AC_DEFUN([gl_PREREQ_VASNPRINTF_INFINITE_DOUBLE],
245 [
246 AC_REQUIRE([gl_PRINTF_INFINITE])
247 case "$gl_cv_func_printf_infinite" in
248 *yes)
249 ;;
250 *)
251 AC_DEFINE([NEED_PRINTF_INFINITE_DOUBLE], [1],
252 [Define if the vasnprintf implementation needs special code for
253 infinite 'double' arguments.])
254 ;;
255 esac
256 ])
257
258 # Extra prerequisites of lib/vasnprintf.c for supporting infinite 'long double'
259 # arguments.
260 AC_DEFUN([gl_PREREQ_VASNPRINTF_INFINITE_LONG_DOUBLE],
261 [
262 AC_REQUIRE([gl_PRINTF_INFINITE_LONG_DOUBLE])
263 dnl There is no need to set NEED_PRINTF_INFINITE_LONG_DOUBLE if
264 dnl NEED_PRINTF_LONG_DOUBLE is already set.
265 AC_REQUIRE([gl_PREREQ_VASNPRINTF_LONG_DOUBLE])
266 case "$gl_cv_func_printf_long_double" in
267 *yes)
268 case "$gl_cv_func_printf_infinite_long_double" in
269 *yes)
270 ;;
271 *)
272 AC_DEFINE([NEED_PRINTF_INFINITE_LONG_DOUBLE], [1],
273 [Define if the vasnprintf implementation needs special code for
274 infinite 'long double' arguments.])
275 ;;
276 esac
277 ;;
278 esac
279 ])
280
281 # Extra prerequisites of lib/vasnprintf.c for supporting the 'a' directive.
282 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_A],
283 [
284 AC_REQUIRE([gl_PRINTF_DIRECTIVE_A])
285 case "$gl_cv_func_printf_directive_a" in
286 *yes)
287 ;;
288 *)
289 AC_DEFINE([NEED_PRINTF_DIRECTIVE_A], [1],
290 [Define if the vasnprintf implementation needs special code for
291 the 'a' and 'A' directives.])
292 gl_CHECK_FUNCS_ANDROID([nl_langinfo], [[#include <langinfo.h>]])
293 ;;
294 esac
295 ])
296
297 # Extra prerequisites of lib/vasnprintf.c for supporting the 'b' directive.
298 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_B],
299 [
300 AC_REQUIRE([gl_PRINTF_DIRECTIVE_B])
301 case "$gl_cv_func_printf_directive_b" in
302 *yes)
303 ;;
304 *)
305 AC_DEFINE([NEED_PRINTF_DIRECTIVE_B], [1],
306 [Define if the vasnprintf implementation needs special code for
307 the 'b' directive.])
308 ;;
309 esac
310 ])
311
312 # Extra prerequisites of lib/vasnprintf.c for supporting the 'F' directive.
313 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_F],
314 [
315 AC_REQUIRE([gl_PRINTF_DIRECTIVE_F])
316 case "$gl_cv_func_printf_directive_f" in
317 *yes)
318 ;;
319 *)
320 AC_DEFINE([NEED_PRINTF_DIRECTIVE_F], [1],
321 [Define if the vasnprintf implementation needs special code for
322 the 'F' directive.])
323 ;;
324 esac
325 ])
326
327 # Extra prerequisites of lib/vasnprintf.c for supporting the 'ls' directive.
328 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_LS],
329 [
330 AC_REQUIRE([gl_PRINTF_DIRECTIVE_LS])
331 case "$gl_cv_func_printf_directive_ls" in
332 *yes)
333 ;;
334 *)
335 AC_DEFINE([NEED_PRINTF_DIRECTIVE_LS], [1],
336 [Define if the vasnprintf implementation needs special code for
337 the 'ls' directive.])
338 ;;
339 esac
340 ])
341
342 # Extra prerequisites of lib/vasnprintf.c for supporting the 'lc' directive.
343 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_LC],
344 [
345 AC_REQUIRE([gl_PRINTF_DIRECTIVE_LC])
346 case "$gl_cv_func_printf_directive_lc" in
347 *yes)
348 ;;
349 *)
350 AC_DEFINE([NEED_PRINTF_DIRECTIVE_LC], [1],
351 [Define if the vasnprintf implementation needs special code for
352 the 'lc' directive.])
353 ;;
354 esac
355 ])
356
357 # Extra prerequisites of lib/vasnprintf.c for supporting the ' flag.
358 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_GROUPING],
359 [
360 AC_REQUIRE([AC_CANONICAL_HOST])
361 AC_REQUIRE([gl_PRINTF_FLAG_GROUPING])
362 AC_REQUIRE([gl_PRINTF_FLAG_GROUPING_INT_PRECISION])
363 AC_REQUIRE([gl_PRINTF_FLAG_GROUPING_MULTIBYTE])
364 case "$host_os" in
365 mingw* | windows*)
366 dnl MSVC does not support the ' flag at all.
367 dnl mingw does not support it, unless __USE_MINGW_ANSI_STDIO is defined.
368 dnl mingw also has other bugs regarding the ' flag.
369 AC_DEFINE([NEED_PRINTF_FLAG_GROUPING], [1],
370 [Define if the vasnprintf implementation needs special code for the
371 ' flag.])
372 ;;
373 *)
374 case "$gl_cv_func_printf_flag_grouping,$gl_cv_func_printf_flag_grouping_multibyte" in
375 *yes,*yes)
376 case "$gl_cv_func_printf_flag_grouping_int_precision" in
377 *yes)
378 ;;
379 *)
380 AC_DEFINE([NEED_PRINTF_FLAG_GROUPING_INT], [1],
381 [Define if the vasnprintf implementation needs special code for the
382 ' flag, for integer directives only.])
383 ;;
384 esac
385 ;;
386 *)
387 AC_DEFINE([NEED_PRINTF_FLAG_GROUPING], [1],
388 [Define if the vasnprintf implementation needs special code for the
389 ' flag.])
390 ;;
391 esac
392 ;;
393 esac
394 ])
395
396 # Extra prerequisites of lib/vasnprintf.c for supporting the '-' flag.
397 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_LEFTADJUST],
398 [
399 AC_REQUIRE([gl_PRINTF_FLAG_LEFTADJUST])
400 case "$gl_cv_func_printf_flag_leftadjust" in
401 *yes)
402 ;;
403 *)
404 AC_DEFINE([NEED_PRINTF_FLAG_LEFTADJUST], [1],
405 [Define if the vasnprintf implementation needs special code for the
406 '-' flag.])
407 ;;
408 esac
409 ])
410
411 # Extra prerequisites of lib/vasnprintf.c for supporting the 0 flag.
412 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_ZERO],
413 [
414 AC_REQUIRE([gl_PRINTF_FLAG_ZERO])
415 case "$gl_cv_func_printf_flag_zero" in
416 *yes)
417 ;;
418 *)
419 AC_DEFINE([NEED_PRINTF_FLAG_ZERO], [1],
420 [Define if the vasnprintf implementation needs special code for the
421 0 flag.])
422 ;;
423 esac
424 ])
425
426 # Extra prerequisites of lib/vasnprintf.c for supporting the # flag with a
427 # zero precision and a zero value in the 'x' and 'X' directives.
428 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_ALT_PRECISION_ZERO],
429 [
430 AC_REQUIRE([gl_PRINTF_FLAG_ALT_PRECISION_ZERO])
431 case "$gl_cv_func_printf_flag_alt_precision_zero" in
432 *yes)
433 ;;
434 *)
435 AC_DEFINE([NEED_PRINTF_FLAG_ALT_PRECISION_ZERO], [1],
436 [Define if the vasnprintf implementation needs special code for the
437 # flag with a zero precision and a zero value in the 'x' and 'X' directives.])
438 ;;
439 esac
440 ])
441
442 # Extra prerequisites of lib/vasnprintf.c for supporting large precisions.
443 AC_DEFUN([gl_PREREQ_VASNPRINTF_PRECISION],
444 [
445 AC_REQUIRE([gl_PRINTF_PRECISION])
446 case "$gl_cv_func_printf_precision" in
447 *yes)
448 ;;
449 *)
450 AC_DEFINE([NEED_PRINTF_UNBOUNDED_PRECISION], [1],
451 [Define if the vasnprintf implementation needs special code for
452 supporting large precisions without arbitrary bounds.])
453 AC_DEFINE([NEED_PRINTF_DOUBLE], [1],
454 [Define if the vasnprintf implementation needs special code for
455 'double' arguments.])
456 AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
457 [Define if the vasnprintf implementation needs special code for
458 'long double' arguments.])
459 ;;
460 esac
461 ])
462
463 # Extra prerequisites of lib/vasnprintf.c for surviving out-of-memory
464 # conditions.
465 AC_DEFUN([gl_PREREQ_VASNPRINTF_ENOMEM],
466 [
467 AC_REQUIRE([gl_PRINTF_ENOMEM])
468 case "$gl_cv_func_printf_enomem" in
469 *yes)
470 ;;
471 *)
472 AC_DEFINE([NEED_PRINTF_ENOMEM], [1],
473 [Define if the vasnprintf implementation needs special code for
474 surviving out-of-memory conditions.])
475 AC_DEFINE([NEED_PRINTF_DOUBLE], [1],
476 [Define if the vasnprintf implementation needs special code for
477 'double' arguments.])
478 AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
479 [Define if the vasnprintf implementation needs special code for
480 'long double' arguments.])
481 ;;
482 esac
483 ])
484
485 # Prerequisites of lib/vasnprintf.c including all extras for POSIX compliance.
486 AC_DEFUN([gl_PREREQ_VASNPRINTF_WITH_POSIX_EXTRAS],
487 [
488 AC_REQUIRE([gl_PREREQ_VASNPRINTF])
489 gl_PREREQ_VASNPRINTF_LONG_DOUBLE
490 gl_PREREQ_VASNPRINTF_INFINITE_DOUBLE
491 gl_PREREQ_VASNPRINTF_INFINITE_LONG_DOUBLE
492 gl_PREREQ_VASNPRINTF_DIRECTIVE_A
493 gl_PREREQ_VASNPRINTF_DIRECTIVE_B
494 gl_PREREQ_VASNPRINTF_DIRECTIVE_F
495 gl_PREREQ_VASNPRINTF_DIRECTIVE_LS
496 gl_PREREQ_VASNPRINTF_DIRECTIVE_LC
497 gl_PREREQ_VASNPRINTF_FLAG_GROUPING
498 gl_PREREQ_VASNPRINTF_FLAG_LEFTADJUST
499 gl_PREREQ_VASNPRINTF_FLAG_ZERO
500 gl_PREREQ_VASNPRINTF_FLAG_ALT_PRECISION_ZERO
501 gl_PREREQ_VASNPRINTF_PRECISION
502 gl_PREREQ_VASNPRINTF_ENOMEM
503 ])
504
505 # Extra prerequisites of lib/vasnprintf.c for supporting the 'B' directive.
506 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_UPPERCASE_B],
507 [
508 AC_REQUIRE([gl_PRINTF_DIRECTIVE_UPPERCASE_B])
509 case "$gl_cv_func_printf_directive_uppercase_b" in
510 *yes)
511 ;;
512 *)
513 AC_DEFINE([NEED_PRINTF_DIRECTIVE_UPPERCASE_B], [1],
514 [Define if the vasnprintf implementation needs special code for
515 the 'B' directive.])
516 ;;
517 esac
518 ])
519
520 # Prerequisites of lib/vasnprintf.c including all extras for POSIX compliance
521 # and GNU compatibility.
522 AC_DEFUN([gl_PREREQ_VASNPRINTF_WITH_GNU_EXTRAS],
523 [
524 gl_PREREQ_VASNPRINTF_WITH_POSIX_EXTRAS
525 AC_DEFINE([SUPPORT_GNU_PRINTF_DIRECTIVES], [1],
526 [Define if the vasnprintf implementation should support GNU compatible
527 printf directives.])
528 gl_PREREQ_VASNPRINTF_DIRECTIVE_UPPERCASE_B
529 ])
530
531 # Prerequisites of lib/asnprintf.c.
532 # Prerequisites of lib/asnwprintf.c.
533 AC_DEFUN([gl_PREREQ_ASNPRINTF],
534 [
535 ])