2 dnl Copyright (C) 2003, 2007-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 dnl Test whether the *printf family of functions supports the 'j', 'z', 't',
8 dnl 'L' size specifiers. (ISO C99, POSIX:2001)
9 dnl Result is gl_cv_func_printf_sizes_c99.
11 AC_DEFUN([gl_PRINTF_SIZES_C99],
13 AC_REQUIRE([AC_PROG_CC])
14 AC_REQUIRE([gl_AC_HEADER_STDINT_H])
15 AC_REQUIRE([gl_AC_HEADER_INTTYPES_H])
16 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17 AC_CACHE_CHECK([whether printf supports size specifiers as in C99],
18 [gl_cv_func_printf_sizes_c99],
25 #include <sys/types.h>
26 #if HAVE_STDINT_H_WITH_UINTMAX
29 #if HAVE_INTTYPES_H_WITH_UINTMAX
30 # include <inttypes.h>
36 #if HAVE_STDINT_H_WITH_UINTMAX || HAVE_INTTYPES_H_WITH_UINTMAX
38 if (sprintf (buf, "%ju %d", (uintmax_t) 12345671, 33, 44, 55) < 0
39 || strcmp (buf, "12345671 33") != 0)
45 if (sprintf (buf, "%zu %d", (size_t) 12345672, 33, 44, 55) < 0
46 || strcmp (buf, "12345672 33") != 0)
49 if (sprintf (buf, "%tu %d", (ptrdiff_t) 12345673, 33, 44, 55) < 0
50 || strcmp (buf, "12345673 33") != 0)
53 if (sprintf (buf, "%Lg %d", (long double) 1.5, 33, 44, 55) < 0
54 || strcmp (buf, "1.5 33") != 0)
58 [gl_cv_func_printf_sizes_c99=yes],
59 [gl_cv_func_printf_sizes_c99=no],
63 # Guess yes on glibc systems.
64 *-gnu* | gnu*) gl_cv_func_printf_sizes_c99="guessing yes";;
65 # Guess yes on musl systems.
66 *-musl*) gl_cv_func_printf_sizes_c99="guessing yes";;
67 # Guess yes on FreeBSD >= 5.
68 freebsd[1-4].*) gl_cv_func_printf_sizes_c99="guessing no";;
69 freebsd* | kfreebsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
70 # Guess yes on Mac OS X >= 10.3.
71 darwin[1-6].*) gl_cv_func_printf_sizes_c99="guessing no";;
72 darwin*) gl_cv_func_printf_sizes_c99="guessing yes";;
73 # Guess yes on OpenBSD >= 3.9.
74 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
75 gl_cv_func_printf_sizes_c99="guessing no";;
76 openbsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
77 # Guess yes on Solaris >= 2.10.
78 solaris2.[1-9][0-9]*) gl_cv_func_printf_sizes_c99="guessing yes";;
79 solaris*) gl_cv_func_printf_sizes_c99="guessing no";;
80 # Guess yes on NetBSD >= 3.
81 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
82 gl_cv_func_printf_sizes_c99="guessing no";;
83 netbsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
84 # Guess yes on Android.
85 linux*-android*) gl_cv_func_printf_sizes_c99="guessing yes";;
87 # Guess yes on MSVC, no on mingw.
88 mingw*) AC_EGREP_CPP([Known], [
93 [gl_cv_func_printf_sizes_c99="guessing yes"],
94 [gl_cv_func_printf_sizes_c99="guessing no"])
96 # If we don't know, obey --enable-cross-guesses.
97 *) gl_cv_func_printf_sizes_c99="$gl_cross_guess_normal";;
103 dnl Test whether the *printf family of functions supports 'long double'
104 dnl arguments together with the 'L' size specifier. (ISO C99, POSIX:2001)
105 dnl Result is gl_cv_func_printf_long_double.
107 AC_DEFUN([gl_PRINTF_LONG_DOUBLE],
109 AC_REQUIRE([AC_PROG_CC])
110 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
111 AC_CACHE_CHECK([whether printf supports 'long double' arguments],
112 [gl_cv_func_printf_long_double],
118 static char buf[10000];
123 if (sprintf (buf, "%Lf %d", 1.75L, 33, 44, 55) < 0
124 || strcmp (buf, "1.750000 33") != 0)
127 if (sprintf (buf, "%Le %d", 1.75L, 33, 44, 55) < 0
128 || strcmp (buf, "1.750000e+00 33") != 0)
131 if (sprintf (buf, "%Lg %d", 1.75L, 33, 44, 55) < 0
132 || strcmp (buf, "1.75 33") != 0)
136 [gl_cv_func_printf_long_double=yes],
137 [gl_cv_func_printf_long_double=no],
140 beos*) gl_cv_func_printf_long_double="guessing no";;
141 # Guess yes on Android.
142 linux*-android*) gl_cv_func_printf_long_double="guessing yes";;
143 # Guess yes on MSVC, no on mingw.
144 mingw*) AC_EGREP_CPP([Known], [
149 [gl_cv_func_printf_long_double="guessing yes"],
150 [gl_cv_func_printf_long_double="guessing no"])
152 *) gl_cv_func_printf_long_double="guessing yes";;
158 dnl Test whether the *printf family of functions supports infinite and NaN
159 dnl 'double' arguments and negative zero arguments in the %f, %e, %g
160 dnl directives. (ISO C99, POSIX:2001)
161 dnl Result is gl_cv_func_printf_infinite.
163 AC_DEFUN([gl_PRINTF_INFINITE],
165 AC_REQUIRE([AC_PROG_CC])
166 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
167 AC_CACHE_CHECK([whether printf supports infinite 'double' arguments],
168 [gl_cv_func_printf_infinite],
175 strisnan (const char *string, size_t start_index, size_t end_index)
177 if (start_index < end_index)
179 if (string[start_index] == '-')
181 if (start_index + 3 <= end_index
182 && memcmp (string + start_index, "nan", 3) == 0)
185 if (start_index == end_index
186 || (string[start_index] == '(' && string[end_index - 1] == ')'))
195 static double plus_zero = 0.0;
196 double minus_zero = - plus_zero;
197 return memcmp (&plus_zero, &minus_zero, sizeof (double)) != 0;
199 static char buf[10000];
200 static double zero = 0.0;
204 if (sprintf (buf, "%f", 1.0 / zero) < 0
205 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
207 if (sprintf (buf, "%f", -1.0 / zero) < 0
208 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
210 if (sprintf (buf, "%f", zero / zero) < 0
211 || !strisnan (buf, 0, strlen (buf)))
213 if (sprintf (buf, "%e", 1.0 / zero) < 0
214 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
216 if (sprintf (buf, "%e", -1.0 / zero) < 0
217 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
219 if (sprintf (buf, "%e", zero / zero) < 0
220 || !strisnan (buf, 0, strlen (buf)))
222 if (sprintf (buf, "%g", 1.0 / zero) < 0
223 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
225 if (sprintf (buf, "%g", -1.0 / zero) < 0
226 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
228 if (sprintf (buf, "%g", zero / zero) < 0
229 || !strisnan (buf, 0, strlen (buf)))
231 /* This test fails on HP-UX 10.20. */
232 if (have_minus_zero ())
233 if (sprintf (buf, "%g", - zero) < 0
234 || strcmp (buf, "-0") != 0)
238 [gl_cv_func_printf_infinite=yes],
239 [gl_cv_func_printf_infinite=no],
243 # Guess yes on glibc systems.
244 *-gnu* | gnu*) gl_cv_func_printf_infinite="guessing yes";;
245 # Guess yes on musl systems.
246 *-musl*) gl_cv_func_printf_infinite="guessing yes";;
247 # Guess yes on FreeBSD >= 6.
248 freebsd[1-5].*) gl_cv_func_printf_infinite="guessing no";;
249 freebsd* | kfreebsd*) gl_cv_func_printf_infinite="guessing yes";;
250 # Guess yes on Mac OS X >= 10.3.
251 darwin[1-6].*) gl_cv_func_printf_infinite="guessing no";;
252 darwin*) gl_cv_func_printf_infinite="guessing yes";;
253 # Guess yes on HP-UX >= 11.
254 hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite="guessing no";;
255 hpux*) gl_cv_func_printf_infinite="guessing yes";;
256 # Guess yes on NetBSD >= 3.
257 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
258 gl_cv_func_printf_infinite="guessing no";;
259 netbsd*) gl_cv_func_printf_infinite="guessing yes";;
261 beos*) gl_cv_func_printf_infinite="guessing yes";;
262 # Guess no on Android.
263 linux*-android*) gl_cv_func_printf_infinite="guessing no";;
265 # Guess yes on MSVC, no on mingw.
266 mingw*) AC_EGREP_CPP([Known], [
271 [gl_cv_func_printf_infinite="guessing yes"],
272 [gl_cv_func_printf_infinite="guessing no"])
274 # If we don't know, obey --enable-cross-guesses.
275 *) gl_cv_func_printf_infinite="$gl_cross_guess_normal";;
281 dnl Test whether the *printf family of functions supports infinite and NaN
282 dnl 'long double' arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001)
283 dnl Result is gl_cv_func_printf_infinite_long_double.
285 AC_DEFUN([gl_PRINTF_INFINITE_LONG_DOUBLE],
287 AC_REQUIRE([gl_PRINTF_LONG_DOUBLE])
288 AC_REQUIRE([AC_PROG_CC])
289 AC_REQUIRE([gl_BIGENDIAN])
290 AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
291 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
292 dnl The user can set or unset the variable gl_printf_safe to indicate
293 dnl that he wishes a safe handling of non-IEEE-754 'long double' values.
294 if test -n "$gl_printf_safe"; then
295 AC_DEFINE([CHECK_PRINTF_SAFE], [1],
296 [Define if you wish *printf() functions that have a safe handling of
297 non-IEEE-754 'long double' values.])
299 case "$gl_cv_func_printf_long_double" in
301 AC_CACHE_CHECK([whether printf supports infinite 'long double' arguments],
302 [gl_cv_func_printf_infinite_long_double],
311 strisnan (const char *string, size_t start_index, size_t end_index)
313 if (start_index < end_index)
315 if (string[start_index] == '-')
317 if (start_index + 3 <= end_index
318 && memcmp (string + start_index, "nan", 3) == 0)
321 if (start_index == end_index
322 || (string[start_index] == '(' && string[end_index - 1] == ')'))
328 static char buf[10000];
329 static long double zeroL = 0.0L;
334 if (sprintf (buf, "%Lf", 1.0L / zeroL) < 0
335 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
337 if (sprintf (buf, "%Lf", -1.0L / zeroL) < 0
338 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
340 if (sprintf (buf, "%Lf", zeroL / zeroL) < 0
341 || !strisnan (buf, 0, strlen (buf)))
343 if (sprintf (buf, "%Le", 1.0L / zeroL) < 0
344 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
346 if (sprintf (buf, "%Le", -1.0L / zeroL) < 0
347 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
349 if (sprintf (buf, "%Le", zeroL / zeroL) < 0
350 || !strisnan (buf, 0, strlen (buf)))
352 if (sprintf (buf, "%Lg", 1.0L / zeroL) < 0
353 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
355 if (sprintf (buf, "%Lg", -1.0L / zeroL) < 0
356 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
358 if (sprintf (buf, "%Lg", zeroL / zeroL) < 0
359 || !strisnan (buf, 0, strlen (buf)))
361 #if CHECK_PRINTF_SAFE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
362 /* Representation of an 80-bit 'long double' as an initializer for a sequence
363 of 'unsigned int' words. */
364 # ifdef WORDS_BIGENDIAN
365 # define LDBL80_WORDS(exponent,manthi,mantlo) \
366 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
367 ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
368 (unsigned int) (mantlo) << 16 \
371 # define LDBL80_WORDS(exponent,manthi,mantlo) \
372 { mantlo, manthi, exponent }
375 static union { unsigned int word[4]; long double value; } x =
376 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
377 if (sprintf (buf, "%Lf", x.value) < 0
378 || !strisnan (buf, 0, strlen (buf)))
380 if (sprintf (buf, "%Le", x.value) < 0
381 || !strisnan (buf, 0, strlen (buf)))
383 if (sprintf (buf, "%Lg", x.value) < 0
384 || !strisnan (buf, 0, strlen (buf)))
388 /* Signalling NaN. */
389 static union { unsigned int word[4]; long double value; } x =
390 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
391 if (sprintf (buf, "%Lf", x.value) < 0
392 || !strisnan (buf, 0, strlen (buf)))
394 if (sprintf (buf, "%Le", x.value) < 0
395 || !strisnan (buf, 0, strlen (buf)))
397 if (sprintf (buf, "%Lg", x.value) < 0
398 || !strisnan (buf, 0, strlen (buf)))
402 static union { unsigned int word[4]; long double value; } x =
403 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
404 if (sprintf (buf, "%Lf", x.value) <= 0)
406 if (sprintf (buf, "%Le", x.value) <= 0)
408 if (sprintf (buf, "%Lg", x.value) <= 0)
411 { /* Pseudo-Infinity. */
412 static union { unsigned int word[4]; long double value; } x =
413 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
414 if (sprintf (buf, "%Lf", x.value) <= 0)
416 if (sprintf (buf, "%Le", x.value) <= 0)
418 if (sprintf (buf, "%Lg", x.value) <= 0)
422 static union { unsigned int word[4]; long double value; } x =
423 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
424 if (sprintf (buf, "%Lf", x.value) <= 0)
426 if (sprintf (buf, "%Le", x.value) <= 0)
428 if (sprintf (buf, "%Lg", x.value) <= 0)
431 { /* Unnormalized number. */
432 static union { unsigned int word[4]; long double value; } x =
433 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
434 if (sprintf (buf, "%Lf", x.value) <= 0)
436 if (sprintf (buf, "%Le", x.value) <= 0)
438 if (sprintf (buf, "%Lg", x.value) <= 0)
441 { /* Pseudo-Denormal. */
442 static union { unsigned int word[4]; long double value; } x =
443 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
444 if (sprintf (buf, "%Lf", x.value) <= 0)
446 if (sprintf (buf, "%Le", x.value) <= 0)
448 if (sprintf (buf, "%Lg", x.value) <= 0)
454 [gl_cv_func_printf_infinite_long_double=yes],
455 [gl_cv_func_printf_infinite_long_double=no],
457 # Guess no on ia64, x86_64, i386.
458 ia64 | x86_64 | i*86) gl_cv_func_printf_infinite_long_double="guessing no";;
462 # Guess yes on glibc systems.
463 *-gnu* | gnu*) gl_cv_func_printf_infinite_long_double="guessing yes";;
464 # Guess yes on musl systems.
465 *-musl*) gl_cv_func_printf_infinite_long_double="guessing yes";;
466 # Guess yes on FreeBSD >= 6.
467 freebsd[1-5].*) gl_cv_func_printf_infinite_long_double="guessing no";;
468 freebsd* | kfreebsd*) gl_cv_func_printf_infinite_long_double="guessing yes";;
469 # Guess yes on HP-UX >= 11.
470 hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite_long_double="guessing no";;
471 hpux*) gl_cv_func_printf_infinite_long_double="guessing yes";;
472 # Guess no on Android.
473 linux*-android*) gl_cv_func_printf_infinite_long_double="guessing no";;
475 # Guess yes on MSVC, no on mingw.
476 mingw*) AC_EGREP_CPP([Known], [
481 [gl_cv_func_printf_infinite_long_double="guessing yes"],
482 [gl_cv_func_printf_infinite_long_double="guessing no"])
484 # If we don't know, obey --enable-cross-guesses.
485 *) gl_cv_func_printf_infinite_long_double="$gl_cross_guess_normal";;
493 gl_cv_func_printf_infinite_long_double="irrelevant"
498 dnl Test whether the *printf family of functions supports the 'a' and 'A'
499 dnl conversion specifier for hexadecimal output of floating-point numbers.
500 dnl (ISO C99, POSIX:2001)
501 dnl Result is gl_cv_func_printf_directive_a.
503 AC_DEFUN([gl_PRINTF_DIRECTIVE_A],
505 AC_REQUIRE([AC_PROG_CC])
506 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
507 AC_CACHE_CHECK([whether printf supports the 'a' and 'A' directives],
508 [gl_cv_func_printf_directive_a],
514 static char buf[100];
515 static double zero = 0.0;
519 if (sprintf (buf, "%a %d", 3.1416015625, 33, 44, 55) < 0
520 || (strcmp (buf, "0x1.922p+1 33") != 0
521 && strcmp (buf, "0x3.244p+0 33") != 0
522 && strcmp (buf, "0x6.488p-1 33") != 0
523 && strcmp (buf, "0xc.91p-2 33") != 0))
525 if (sprintf (buf, "%A %d", -3.1416015625, 33, 44, 55) < 0
526 || (strcmp (buf, "-0X1.922P+1 33") != 0
527 && strcmp (buf, "-0X3.244P+0 33") != 0
528 && strcmp (buf, "-0X6.488P-1 33") != 0
529 && strcmp (buf, "-0XC.91P-2 33") != 0))
531 /* This catches a FreeBSD 6.1 bug: it doesn't round. */
532 if (sprintf (buf, "%.2a %d", 1.51, 33, 44, 55) < 0
533 || (strcmp (buf, "0x1.83p+0 33") != 0
534 && strcmp (buf, "0x3.05p-1 33") != 0
535 && strcmp (buf, "0x6.0ap-2 33") != 0
536 && strcmp (buf, "0xc.14p-3 33") != 0))
538 /* This catches a Mac OS X 10.12.4 (Darwin 16.5) bug: it doesn't round. */
539 if (sprintf (buf, "%.0a %d", 1.51, 33, 44, 55) < 0
540 || (strcmp (buf, "0x2p+0 33") != 0
541 && strcmp (buf, "0x3p-1 33") != 0
542 && strcmp (buf, "0x6p-2 33") != 0
543 && strcmp (buf, "0xcp-3 33") != 0))
545 /* This catches a FreeBSD 6.1 bug. See
546 <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
547 if (sprintf (buf, "%010a %d", 1.0 / zero, 33, 44, 55) < 0
550 /* This catches a Mac OS X 10.3.9 (Darwin 7.9) bug. */
551 if (sprintf (buf, "%.1a", 1.999) < 0
552 || (strcmp (buf, "0x1.0p+1") != 0
553 && strcmp (buf, "0x2.0p+0") != 0
554 && strcmp (buf, "0x4.0p-1") != 0
555 && strcmp (buf, "0x8.0p-2") != 0))
557 /* This catches the same Mac OS X 10.3.9 (Darwin 7.9) bug and also a
558 glibc 2.4 bug <https://sourceware.org/bugzilla/show_bug.cgi?id=2908>. */
559 if (sprintf (buf, "%.1La", 1.999L) < 0
560 || (strcmp (buf, "0x1.0p+1") != 0
561 && strcmp (buf, "0x2.0p+0") != 0
562 && strcmp (buf, "0x4.0p-1") != 0
563 && strcmp (buf, "0x8.0p-2") != 0))
567 [gl_cv_func_printf_directive_a=yes],
568 [gl_cv_func_printf_directive_a=no],
571 # Guess yes on glibc >= 2.5 systems.
573 AC_EGREP_CPP([BZ2908], [
574 #include <features.h>
575 #ifdef __GNU_LIBRARY__
576 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)) && !defined __UCLIBC__
581 [gl_cv_func_printf_directive_a="guessing yes"],
582 [gl_cv_func_printf_directive_a="guessing no"])
584 # Guess yes on musl systems.
585 *-musl*) gl_cv_func_printf_directive_a="guessing yes";;
586 # Guess no on Android.
587 linux*-android*) gl_cv_func_printf_directive_a="guessing no";;
588 # Guess no on native Windows.
589 mingw*) gl_cv_func_printf_directive_a="guessing no";;
590 # If we don't know, obey --enable-cross-guesses.
591 *) gl_cv_func_printf_directive_a="$gl_cross_guess_normal";;
597 dnl Test whether the *printf family of functions supports the %F format
598 dnl directive. (ISO C99, POSIX:2001)
599 dnl Result is gl_cv_func_printf_directive_f.
601 AC_DEFUN([gl_PRINTF_DIRECTIVE_F],
603 AC_REQUIRE([AC_PROG_CC])
604 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
605 AC_CACHE_CHECK([whether printf supports the 'F' directive],
606 [gl_cv_func_printf_directive_f],
612 static char buf[100];
613 static double zero = 0.0;
617 if (sprintf (buf, "%F %d", 1234567.0, 33, 44, 55) < 0
618 || strcmp (buf, "1234567.000000 33") != 0)
620 if (sprintf (buf, "%F", 1.0 / zero) < 0
621 || (strcmp (buf, "INF") != 0 && strcmp (buf, "INFINITY") != 0))
623 /* This catches a Cygwin 1.5.x bug. */
624 if (sprintf (buf, "%.F", 1234.0) < 0
625 || strcmp (buf, "1234") != 0)
629 [gl_cv_func_printf_directive_f=yes],
630 [gl_cv_func_printf_directive_f=no],
634 # Guess yes on glibc systems.
635 *-gnu* | gnu*) gl_cv_func_printf_directive_f="guessing yes";;
636 # Guess yes on musl systems.
637 *-musl*) gl_cv_func_printf_directive_f="guessing yes";;
638 # Guess yes on FreeBSD >= 6.
639 freebsd[1-5].*) gl_cv_func_printf_directive_f="guessing no";;
640 freebsd* | kfreebsd*) gl_cv_func_printf_directive_f="guessing yes";;
641 # Guess yes on Mac OS X >= 10.3.
642 darwin[1-6].*) gl_cv_func_printf_directive_f="guessing no";;
643 darwin*) gl_cv_func_printf_directive_f="guessing yes";;
644 # Guess yes on Solaris >= 2.10.
645 solaris2.[1-9][0-9]*) gl_cv_func_printf_directive_f="guessing yes";;
646 solaris*) gl_cv_func_printf_directive_f="guessing no";;
647 # Guess no on Android.
648 linux*-android*) gl_cv_func_printf_directive_f="guessing no";;
650 # Guess yes on MSVC, no on mingw.
651 mingw*) AC_EGREP_CPP([Known], [
656 [gl_cv_func_printf_directive_f="guessing yes"],
657 [gl_cv_func_printf_directive_f="guessing no"])
659 # If we don't know, obey --enable-cross-guesses.
660 *) gl_cv_func_printf_directive_f="$gl_cross_guess_normal";;
666 dnl Test whether the *printf family of functions supports the %n format
667 dnl directive. (ISO C99, POSIX:2001)
668 dnl Result is gl_cv_func_printf_directive_n.
670 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
672 AC_REQUIRE([AC_PROG_CC])
673 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
674 AC_CACHE_CHECK([whether printf supports the 'n' directive],
675 [gl_cv_func_printf_directive_n],
683 #include <inttypes.h>
684 /* See page about "Parameter Validation" on msdn.microsoft.com.
685 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation>
686 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-invalid-parameter-handler-set-thread-local-invalid-parameter-handler> */
688 invalid_parameter_handler (const wchar_t *expression,
689 const wchar_t *function,
690 const wchar_t *file, unsigned int line,
696 static char fmtstring[10];
697 static char buf[100];
702 _set_invalid_parameter_handler (invalid_parameter_handler);
704 /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE=2)
705 support %n in format strings in read-only memory but not in writable
707 strcpy (fmtstring, "%d %n");
708 if (sprintf (buf, fmtstring, 123, &count, 33, 44, 55) < 0
709 || strcmp (buf, "123 ") != 0
714 [gl_cv_func_printf_directive_n=yes],
715 [gl_cv_func_printf_directive_n=no],
717 # Guess no on glibc when _FORTIFY_SOURCE >= 2.
718 *-gnu* | gnu*) AC_COMPILE_IFELSE(
720 [[#if _FORTIFY_SOURCE >= 2
724 [gl_cv_func_printf_directive_n="guessing yes"],
725 [gl_cv_func_printf_directive_n="guessing no"])
727 # Guess no on Android.
728 linux*-android*) gl_cv_func_printf_directive_n="guessing no";;
729 # Guess no on native Windows.
730 mingw*) gl_cv_func_printf_directive_n="guessing no";;
731 *) gl_cv_func_printf_directive_n="guessing yes";;
737 dnl Test whether the *printf family of functions supports the %ls format
738 dnl directive and in particular, when a precision is specified, whether
739 dnl the functions stop converting the wide string argument when the number
740 dnl of bytes that have been produced by this conversion equals or exceeds
742 dnl Result is gl_cv_func_printf_directive_ls.
744 AC_DEFUN([gl_PRINTF_DIRECTIVE_LS],
746 AC_REQUIRE([AC_PROG_CC])
747 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
748 AC_CACHE_CHECK([whether printf supports the 'ls' directive],
749 [gl_cv_func_printf_directive_ls],
753 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
755 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
756 included before <wchar.h>. */
766 /* Test whether %ls works at all.
767 This test fails on OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, but not on
770 static const wchar_t wstring[] = { 'a', 'b', 'c', 0 };
772 if (sprintf (buf, "%ls", wstring) < 0
773 || strcmp (buf, "abc") != 0)
776 /* This test fails on IRIX 6.5, Solaris 2.6, Cygwin 1.5, Haiku (with an
777 assertion failure inside libc), but not on OpenBSD 4.0. */
779 static const wchar_t wstring[] = { 'a', 0 };
781 if (sprintf (buf, "%ls", wstring) < 0
782 || strcmp (buf, "a") != 0)
785 /* Test whether precisions in %ls are supported as specified in ISO C 99
787 "If a precision is specified, no more than that many bytes are written
788 (including shift sequences, if any), and the array shall contain a
789 null wide character if, to equal the multibyte character sequence
790 length given by the precision, the function would need to access a
791 wide character one past the end of the array."
792 This test fails on Solaris 10. */
794 static const wchar_t wstring[] = { 'a', 'b', (wchar_t) 0xfdfdfdfd, 0 };
796 if (sprintf (buf, "%.2ls", wstring) < 0
797 || strcmp (buf, "ab") != 0)
802 [gl_cv_func_printf_directive_ls=yes],
803 [gl_cv_func_printf_directive_ls=no],
807 openbsd*) gl_cv_func_printf_directive_ls="guessing no";;
808 irix*) gl_cv_func_printf_directive_ls="guessing no";;
809 solaris*) gl_cv_func_printf_directive_ls="guessing no";;
810 cygwin*) gl_cv_func_printf_directive_ls="guessing no";;
811 beos* | haiku*) gl_cv_func_printf_directive_ls="guessing no";;
812 # Guess no on Android.
813 linux*-android*) gl_cv_func_printf_directive_ls="guessing no";;
814 # Guess yes on native Windows.
815 mingw*) gl_cv_func_printf_directive_ls="guessing yes";;
816 *) gl_cv_func_printf_directive_ls="guessing yes";;
823 dnl Test whether the *printf family of functions supports POSIX/XSI format
824 dnl strings with positions. (POSIX:2001)
825 dnl Result is gl_cv_func_printf_positions.
827 AC_DEFUN([gl_PRINTF_POSITIONS],
829 AC_REQUIRE([AC_PROG_CC])
830 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
831 AC_CACHE_CHECK([whether printf supports POSIX/XSI format strings with positions],
832 [gl_cv_func_printf_positions],
838 /* The string "%2$d %1$d", with dollar characters protected from the shell's
839 dollar expansion (possibly an autoconf bug). */
840 static char format[] = { '%', '2', '$', 'd', ' ', '%', '1', '$', 'd', '\0' };
841 static char buf[100];
844 sprintf (buf, format, 33, 55);
845 return (strcmp (buf, "55 33") != 0);
847 [gl_cv_func_printf_positions=yes],
848 [gl_cv_func_printf_positions=no],
852 netbsd[1-3]* | netbsdelf[1-3]* | netbsdaout[1-3]* | netbsdcoff[1-3]*)
853 gl_cv_func_printf_positions="guessing no";;
854 beos*) gl_cv_func_printf_positions="guessing no";;
855 # Guess yes on Android.
856 linux*-android*) gl_cv_func_printf_positions="guessing yes";;
857 # Guess no on native Windows.
858 mingw* | pw*) gl_cv_func_printf_positions="guessing no";;
859 *) gl_cv_func_printf_positions="guessing yes";;
866 dnl Test whether the *printf family of functions supports POSIX/XSI format
867 dnl strings with the ' flag for grouping of decimal digits. (POSIX:2001)
868 dnl Result is gl_cv_func_printf_flag_grouping.
870 AC_DEFUN([gl_PRINTF_FLAG_GROUPING],
872 AC_REQUIRE([AC_PROG_CC])
873 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
874 AC_CACHE_CHECK([whether printf supports the grouping flag],
875 [gl_cv_func_printf_flag_grouping],
881 static char buf[100];
884 if (sprintf (buf, "%'d %d", 1234567, 99) < 0
885 || buf[strlen (buf) - 1] != '9')
889 [gl_cv_func_printf_flag_grouping=yes],
890 [gl_cv_func_printf_flag_grouping=no],
894 cygwin*) gl_cv_func_printf_flag_grouping="guessing no";;
895 netbsd*) gl_cv_func_printf_flag_grouping="guessing no";;
896 # Guess no on Android.
897 linux*-android*) gl_cv_func_printf_flag_grouping="guessing no";;
898 # Guess no on native Windows.
899 mingw* | pw*) gl_cv_func_printf_flag_grouping="guessing no";;
900 *) gl_cv_func_printf_flag_grouping="guessing yes";;
907 dnl Test whether the *printf family of functions supports the - flag correctly.
909 dnl <https://lists.gnu.org/r/bug-coreutils/2008-02/msg00035.html>
910 dnl Result is gl_cv_func_printf_flag_leftadjust.
912 AC_DEFUN([gl_PRINTF_FLAG_LEFTADJUST],
914 AC_REQUIRE([AC_PROG_CC])
915 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
916 AC_CACHE_CHECK([whether printf supports the left-adjust flag correctly],
917 [gl_cv_func_printf_flag_leftadjust],
923 static char buf[100];
926 /* Check that a '-' flag is not annihilated by a negative width. */
927 if (sprintf (buf, "a%-*sc", -3, "b") < 0
928 || strcmp (buf, "ab c") != 0)
932 [gl_cv_func_printf_flag_leftadjust=yes],
933 [gl_cv_func_printf_flag_leftadjust=no],
937 # Guess yes on HP-UX 11.
938 hpux11*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
939 # Guess no on HP-UX 10 and older.
940 hpux*) gl_cv_func_printf_flag_leftadjust="guessing no";;
941 # Guess yes on Android.
942 linux*-android*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
943 # Guess yes on native Windows.
944 mingw*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
945 # Guess yes otherwise.
946 *) gl_cv_func_printf_flag_leftadjust="guessing yes";;
953 dnl Test whether the *printf family of functions supports padding of non-finite
954 dnl values with the 0 flag correctly. (ISO C99 + TC1 + TC2.) See
955 dnl <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html>
956 dnl Result is gl_cv_func_printf_flag_zero.
958 AC_DEFUN([gl_PRINTF_FLAG_ZERO],
960 AC_REQUIRE([AC_PROG_CC])
961 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
962 AC_CACHE_CHECK([whether printf supports the zero flag correctly],
963 [gl_cv_func_printf_flag_zero],
969 static char buf[100];
970 static double zero = 0.0;
973 if (sprintf (buf, "%010f", 1.0 / zero, 33, 44, 55) < 0
974 || (strcmp (buf, " inf") != 0
975 && strcmp (buf, " infinity") != 0))
979 [gl_cv_func_printf_flag_zero=yes],
980 [gl_cv_func_printf_flag_zero=no],
984 # Guess yes on glibc systems.
985 *-gnu* | gnu*) gl_cv_func_printf_flag_zero="guessing yes";;
986 # Guess yes on musl systems.
987 *-musl*) gl_cv_func_printf_flag_zero="guessing yes";;
989 beos*) gl_cv_func_printf_flag_zero="guessing yes";;
990 # Guess no on Android.
991 linux*-android*) gl_cv_func_printf_flag_zero="guessing no";;
992 # Guess no on native Windows.
993 mingw*) gl_cv_func_printf_flag_zero="guessing no";;
994 # If we don't know, obey --enable-cross-guesses.
995 *) gl_cv_func_printf_flag_zero="$gl_cross_guess_normal";;
1002 dnl Test whether the *printf family of functions supports large precisions.
1003 dnl On mingw, precisions larger than 512 are treated like 512, in integer,
1004 dnl floating-point or pointer output. On Solaris 10/x86, precisions larger
1005 dnl than 510 in floating-point output crash the program. On Solaris 10/SPARC,
1006 dnl precisions larger than 510 in floating-point output yield wrong results.
1007 dnl On AIX 7.1, precisions larger than 998 in floating-point output yield
1008 dnl wrong results. On BeOS, precisions larger than 1044 crash the program.
1009 dnl Result is gl_cv_func_printf_precision.
1011 AC_DEFUN([gl_PRINTF_PRECISION],
1013 AC_REQUIRE([AC_PROG_CC])
1014 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1015 AC_CACHE_CHECK([whether printf supports large precisions],
1016 [gl_cv_func_printf_precision],
1022 static char buf[5000];
1027 /* On BeOS, this would crash and show a dialog box. Avoid the crash. */
1030 if (sprintf (buf, "%.4000d %d", 1, 33, 44) < 4000 + 3)
1032 if (sprintf (buf, "%.4000f %d", 1.0, 33, 44) < 4000 + 5)
1034 if (sprintf (buf, "%.511f %d", 1.0, 33, 44) < 511 + 5
1037 if (sprintf (buf, "%.999f %d", 1.0, 33, 44) < 999 + 5
1042 [gl_cv_func_printf_precision=yes],
1043 [gl_cv_func_printf_precision=no],
1047 # Guess no only on Solaris, native Windows, and BeOS systems.
1048 solaris*) gl_cv_func_printf_precision="guessing no" ;;
1049 mingw* | pw*) gl_cv_func_printf_precision="guessing no" ;;
1050 beos*) gl_cv_func_printf_precision="guessing no" ;;
1051 # Guess yes on Android.
1052 linux*-android*) gl_cv_func_printf_precision="guessing yes" ;;
1053 *) gl_cv_func_printf_precision="guessing yes" ;;
1060 dnl Test whether the *printf family of functions recovers gracefully in case
1061 dnl of an out-of-memory condition, or whether it crashes the entire program.
1062 dnl Result is gl_cv_func_printf_enomem.
1064 AC_DEFUN([gl_PRINTF_ENOMEM],
1066 AC_REQUIRE([AC_PROG_CC])
1067 AC_REQUIRE([gl_MULTIARCH])
1068 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1069 AC_CACHE_CHECK([whether printf survives out-of-memory conditions],
1070 [gl_cv_func_printf_enomem],
1072 gl_cv_func_printf_enomem="guessing no"
1073 if test "$cross_compiling" = no; then
1074 if test $APPLE_UNIVERSAL_BUILD = 0; then
1075 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
1078 #include <sys/types.h>
1079 #include <sys/time.h>
1080 #include <sys/resource.h>
1084 struct rlimit limit;
1087 /* Some printf implementations allocate temporary space with malloc. */
1088 /* On BSD systems, malloc() is limited by RLIMIT_DATA. */
1090 if (getrlimit (RLIMIT_DATA, &limit) < 0)
1092 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1093 limit.rlim_max = 5000000;
1094 limit.rlim_cur = limit.rlim_max;
1095 if (setrlimit (RLIMIT_DATA, &limit) < 0)
1098 /* On Linux systems, malloc() is limited by RLIMIT_AS. */
1100 if (getrlimit (RLIMIT_AS, &limit) < 0)
1102 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1103 limit.rlim_max = 5000000;
1104 limit.rlim_cur = limit.rlim_max;
1105 if (setrlimit (RLIMIT_AS, &limit) < 0)
1108 /* Some printf implementations allocate temporary space on the stack. */
1110 if (getrlimit (RLIMIT_STACK, &limit) < 0)
1112 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1113 limit.rlim_max = 5000000;
1114 limit.rlim_cur = limit.rlim_max;
1115 if (setrlimit (RLIMIT_STACK, &limit) < 0)
1118 ret = printf ("%.5000000f", 1.0);
1119 return !(ret == 5000002 || (ret < 0 && errno == ENOMEM));
1122 if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
1123 (./conftest 2>&AS_MESSAGE_LOG_FD
1125 _AS_ECHO_LOG([\$? = $result])
1126 if test $result != 0 && test $result != 77; then result=1; fi
1128 ) >/dev/null 2>/dev/null
1130 0) gl_cv_func_printf_enomem="yes" ;;
1131 77) gl_cv_func_printf_enomem="guessing no" ;;
1132 *) gl_cv_func_printf_enomem="no" ;;
1135 gl_cv_func_printf_enomem="guessing no"
1139 dnl A universal build on Apple Mac OS X platforms.
1140 dnl The result would be 'no' in 32-bit mode and 'yes' in 64-bit mode.
1141 dnl But we need a configuration result that is valid in both modes.
1142 gl_cv_func_printf_enomem="guessing no"
1145 if test "$gl_cv_func_printf_enomem" = "guessing no"; then
1148 # Guess yes on glibc systems.
1149 *-gnu* | gnu*) gl_cv_func_printf_enomem="guessing yes";;
1150 # Guess yes on Solaris.
1151 solaris*) gl_cv_func_printf_enomem="guessing yes";;
1153 aix*) gl_cv_func_printf_enomem="guessing yes";;
1154 # Guess yes on HP-UX/hppa.
1155 hpux*) case "$host_cpu" in
1156 hppa*) gl_cv_func_printf_enomem="guessing yes";;
1157 *) gl_cv_func_printf_enomem="guessing no";;
1160 # Guess yes on IRIX.
1161 irix*) gl_cv_func_printf_enomem="guessing yes";;
1162 # Guess yes on OSF/1.
1163 osf*) gl_cv_func_printf_enomem="guessing yes";;
1164 # Guess yes on BeOS.
1165 beos*) gl_cv_func_printf_enomem="guessing yes";;
1166 # Guess yes on Haiku.
1167 haiku*) gl_cv_func_printf_enomem="guessing yes";;
1168 # Guess no on Android.
1169 linux*-android*) gl_cv_func_printf_enomem="guessing no";;
1170 # If we don't know, obey --enable-cross-guesses.
1171 *) gl_cv_func_printf_enomem="$gl_cross_guess_normal";;
1178 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
1179 dnl Result is ac_cv_func_snprintf.
1181 AC_DEFUN([gl_SNPRINTF_PRESENCE],
1183 AC_CHECK_FUNCS_ONCE([snprintf])
1186 dnl Test whether the string produced by the snprintf function is always NUL
1187 dnl terminated. (ISO C99, POSIX:2001)
1188 dnl Result is gl_cv_func_snprintf_truncation_c99.
1190 AC_DEFUN([gl_SNPRINTF_TRUNCATION_C99],
1192 AC_REQUIRE([AC_PROG_CC])
1193 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1194 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1195 AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
1196 [gl_cv_func_snprintf_truncation_c99],
1203 # define my_snprintf snprintf
1205 # include <stdarg.h>
1206 static int my_snprintf (char *buf, int size, const char *format, ...)
1210 va_start (args, format);
1211 ret = vsnprintf (buf, size, format, args);
1216 static char buf[100];
1219 strcpy (buf, "ABCDEF");
1220 my_snprintf (buf, 3, "%d %d", 4567, 89);
1221 if (memcmp (buf, "45\0DEF", 6) != 0)
1225 [gl_cv_func_snprintf_truncation_c99=yes],
1226 [gl_cv_func_snprintf_truncation_c99=no],
1230 # Guess yes on glibc systems.
1231 *-gnu* | gnu*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1232 # Guess yes on musl systems.
1233 *-musl*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1234 # Guess yes on FreeBSD >= 5.
1235 freebsd[1-4].*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1236 freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1237 # Guess yes on Mac OS X >= 10.3.
1238 darwin[1-6].*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1239 darwin*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1240 # Guess yes on OpenBSD >= 3.9.
1241 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
1242 gl_cv_func_snprintf_truncation_c99="guessing no";;
1243 openbsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1244 # Guess yes on Solaris >= 2.6.
1245 solaris2.[0-5] | solaris2.[0-5].*)
1246 gl_cv_func_snprintf_truncation_c99="guessing no";;
1247 solaris*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1248 # Guess yes on AIX >= 4.
1249 aix[1-3]*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1250 aix*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1251 # Guess yes on HP-UX >= 11.
1252 hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1253 hpux*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1254 # Guess yes on IRIX >= 6.5.
1255 irix6.5) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1256 # Guess yes on OSF/1 >= 5.
1257 osf[3-4]*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1258 osf*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1259 # Guess yes on NetBSD >= 3.
1260 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1261 gl_cv_func_snprintf_truncation_c99="guessing no";;
1262 netbsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1263 # Guess yes on BeOS.
1264 beos*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1265 # Guess yes on Android.
1266 linux*-android*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1267 # Guess no on native Windows.
1268 mingw*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1269 # If we don't know, obey --enable-cross-guesses.
1270 *) gl_cv_func_snprintf_truncation_c99="$gl_cross_guess_normal";;
1277 dnl Test whether the return value of the snprintf function is the number
1278 dnl of bytes (excluding the terminating NUL) that would have been produced
1279 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
1280 dnl For example, this test program fails on IRIX 6.5:
1281 dnl ---------------------------------------------------------------------
1282 dnl #include <stdio.h>
1285 dnl static char buf[8];
1286 dnl int retval = snprintf (buf, 3, "%d", 12345);
1287 dnl return retval >= 0 && retval < 3;
1289 dnl ---------------------------------------------------------------------
1290 dnl Result is gl_cv_func_snprintf_retval_c99.
1292 AC_DEFUN_ONCE([gl_SNPRINTF_RETVAL_C99],
1294 AC_REQUIRE([AC_PROG_CC])
1295 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1296 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1297 AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
1298 [gl_cv_func_snprintf_retval_c99],
1305 # define my_snprintf snprintf
1307 # include <stdarg.h>
1308 static int my_snprintf (char *buf, int size, const char *format, ...)
1312 va_start (args, format);
1313 ret = vsnprintf (buf, size, format, args);
1318 static char buf[100];
1321 strcpy (buf, "ABCDEF");
1322 if (my_snprintf (buf, 3, "%d %d", 4567, 89) != 7)
1324 if (my_snprintf (buf, 0, "%d %d", 4567, 89) != 7)
1326 if (my_snprintf (NULL, 0, "%d %d", 4567, 89) != 7)
1330 [gl_cv_func_snprintf_retval_c99=yes],
1331 [gl_cv_func_snprintf_retval_c99=no],
1334 # Guess yes on glibc systems.
1335 *-gnu* | gnu*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1336 # Guess yes on musl systems.
1337 *-musl*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1338 # Guess yes on FreeBSD >= 5.
1339 freebsd[1-4].*) gl_cv_func_snprintf_retval_c99="guessing no";;
1340 freebsd* | kfreebsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1341 # Guess yes on Mac OS X >= 10.3.
1342 darwin[1-6].*) gl_cv_func_snprintf_retval_c99="guessing no";;
1343 darwin*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1344 # Guess yes on OpenBSD >= 3.9.
1345 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
1346 gl_cv_func_snprintf_retval_c99="guessing no";;
1347 openbsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1348 # Guess yes on Solaris >= 2.10.
1349 solaris2.[1-9][0-9]*) gl_cv_func_printf_sizes_c99="guessing yes";;
1350 solaris*) gl_cv_func_printf_sizes_c99="guessing no";;
1351 # Guess yes on AIX >= 4.
1352 aix[1-3]*) gl_cv_func_snprintf_retval_c99="guessing no";;
1353 aix*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1354 # Guess yes on NetBSD >= 3.
1355 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1356 gl_cv_func_snprintf_retval_c99="guessing no";;
1357 netbsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1358 # Guess yes on BeOS.
1359 beos*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1360 # Guess yes on Android.
1361 linux*-android*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1363 # Guess yes on MSVC, no on mingw.
1364 mingw*) AC_EGREP_CPP([Known], [
1369 [gl_cv_func_snprintf_retval_c99="guessing yes"],
1370 [gl_cv_func_snprintf_retval_c99="guessing no"])
1372 # If we don't know, obey --enable-cross-guesses.
1373 *) gl_cv_func_snprintf_retval_c99="$gl_cross_guess_normal";;
1379 dnl Test whether the snprintf function supports the %n format directive
1380 dnl also in truncated portions of the format string. (ISO C99, POSIX:2001)
1381 dnl Result is gl_cv_func_snprintf_directive_n.
1383 AC_DEFUN([gl_SNPRINTF_DIRECTIVE_N],
1385 AC_REQUIRE([AC_PROG_CC])
1386 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1387 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1388 AC_CACHE_CHECK([whether snprintf fully supports the 'n' directive],
1389 [gl_cv_func_snprintf_directive_n],
1396 # define my_snprintf snprintf
1398 # include <stdarg.h>
1399 static int my_snprintf (char *buf, int size, const char *format, ...)
1403 va_start (args, format);
1404 ret = vsnprintf (buf, size, format, args);
1409 static char fmtstring[10];
1410 static char buf[100];
1414 /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE=2)
1415 support %n in format strings in read-only memory but not in writable
1417 strcpy (fmtstring, "%d %n");
1418 my_snprintf (buf, 4, fmtstring, 12345, &count, 33, 44, 55);
1423 [gl_cv_func_snprintf_directive_n=yes],
1424 [gl_cv_func_snprintf_directive_n=no],
1427 # Guess no on glibc when _FORTIFY_SOURCE >= 2.
1428 *-gnu* | gnu*) AC_COMPILE_IFELSE(
1430 [[#if _FORTIFY_SOURCE >= 2
1434 [gl_cv_func_snprintf_directive_n="guessing yes"],
1435 [gl_cv_func_snprintf_directive_n="guessing no"])
1438 # Guess yes on musl systems.
1439 *-musl*) gl_cv_func_snprintf_directive_n="guessing yes";;
1440 # Guess yes on FreeBSD >= 5.
1441 freebsd[1-4].*) gl_cv_func_snprintf_directive_n="guessing no";;
1442 freebsd* | kfreebsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1443 # Guess yes on Mac OS X >= 10.3.
1444 darwin[1-6].*) gl_cv_func_snprintf_directive_n="guessing no";;
1445 darwin*) gl_cv_func_snprintf_directive_n="guessing yes";;
1446 # Guess yes on Solaris >= 2.6.
1447 solaris2.[0-5] | solaris2.[0-5].*)
1448 gl_cv_func_snprintf_directive_n="guessing no";;
1449 solaris*) gl_cv_func_snprintf_directive_n="guessing yes";;
1450 # Guess yes on AIX >= 4.
1451 aix[1-3]*) gl_cv_func_snprintf_directive_n="guessing no";;
1452 aix*) gl_cv_func_snprintf_directive_n="guessing yes";;
1453 # Guess yes on IRIX >= 6.5.
1454 irix6.5) gl_cv_func_snprintf_directive_n="guessing yes";;
1455 # Guess yes on OSF/1 >= 5.
1456 osf[3-4]*) gl_cv_func_snprintf_directive_n="guessing no";;
1457 osf*) gl_cv_func_snprintf_directive_n="guessing yes";;
1458 # Guess yes on NetBSD >= 3.
1459 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1460 gl_cv_func_snprintf_directive_n="guessing no";;
1461 netbsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1462 # Guess yes on BeOS.
1463 beos*) gl_cv_func_snprintf_directive_n="guessing yes";;
1464 # Guess no on Android.
1465 linux*-android*) gl_cv_func_snprintf_directive_n="guessing no";;
1466 # Guess no on native Windows.
1467 mingw*) gl_cv_func_snprintf_directive_n="guessing no";;
1468 # If we don't know, obey --enable-cross-guesses.
1469 *) gl_cv_func_snprintf_directive_n="$gl_cross_guess_normal";;
1476 dnl Test whether the snprintf function, when passed a size = 1, writes any
1477 dnl output without bounds in this case, behaving like sprintf. This is the
1478 dnl case on Linux libc5.
1479 dnl Result is gl_cv_func_snprintf_size1.
1481 AC_DEFUN([gl_SNPRINTF_SIZE1],
1483 AC_REQUIRE([AC_PROG_CC])
1484 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1485 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1486 AC_CACHE_CHECK([whether snprintf respects a size of 1],
1487 [gl_cv_func_snprintf_size1],
1493 # define my_snprintf snprintf
1495 # include <stdarg.h>
1496 static int my_snprintf (char *buf, int size, const char *format, ...)
1500 va_start (args, format);
1501 ret = vsnprintf (buf, size, format, args);
1508 static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1509 my_snprintf (buf, 1, "%d", 12345);
1510 return buf[1] != 'E';
1512 [gl_cv_func_snprintf_size1=yes],
1513 [gl_cv_func_snprintf_size1=no],
1515 # Guess yes on Android.
1516 linux*-android*) gl_cv_func_snprintf_size1="guessing yes" ;;
1517 # Guess yes on native Windows.
1518 mingw*) gl_cv_func_snprintf_size1="guessing yes" ;;
1519 *) gl_cv_func_snprintf_size1="guessing yes" ;;
1525 dnl Test whether the vsnprintf function, when passed a zero size, produces no
1526 dnl output. (ISO C99, POSIX:2001)
1527 dnl For example, snprintf nevertheless writes a NUL byte in this case
1529 dnl ---------------------------------------------------------------------
1530 dnl #include <stdio.h>
1533 dnl static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1534 dnl snprintf (buf, 0, "%d", 12345);
1535 dnl return buf[0] != 'D';
1537 dnl ---------------------------------------------------------------------
1538 dnl And vsnprintf writes any output without bounds in this case, behaving like
1539 dnl vsprintf, on HP-UX 11 and OSF/1 5.1:
1540 dnl ---------------------------------------------------------------------
1541 dnl #include <stdarg.h>
1542 dnl #include <stdio.h>
1543 dnl static int my_snprintf (char *buf, int size, const char *format, ...)
1547 dnl va_start (args, format);
1548 dnl ret = vsnprintf (buf, size, format, args);
1554 dnl static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1555 dnl my_snprintf (buf, 0, "%d", 12345);
1556 dnl return buf[0] != 'D';
1558 dnl ---------------------------------------------------------------------
1559 dnl Result is gl_cv_func_vsnprintf_zerosize_c99.
1561 AC_DEFUN([gl_VSNPRINTF_ZEROSIZE_C99],
1563 AC_REQUIRE([AC_PROG_CC])
1564 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1565 AC_CACHE_CHECK([whether vsnprintf respects a zero size as in C99],
1566 [gl_cv_func_vsnprintf_zerosize_c99],
1572 static int my_snprintf (char *buf, int size, const char *format, ...)
1576 va_start (args, format);
1577 ret = vsnprintf (buf, size, format, args);
1583 static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1584 my_snprintf (buf, 0, "%d", 12345);
1585 return buf[0] != 'D';
1587 [gl_cv_func_vsnprintf_zerosize_c99=yes],
1588 [gl_cv_func_vsnprintf_zerosize_c99=no],
1592 # Guess yes on glibc systems.
1593 *-gnu* | gnu*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1594 # Guess yes on musl systems.
1595 *-musl*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1596 # Guess yes on FreeBSD >= 5.
1597 freebsd[1-4].*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1598 freebsd* | kfreebsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1599 # Guess yes on Mac OS X >= 10.3.
1600 darwin[1-6].*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1601 darwin*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1602 # Guess yes on Cygwin.
1603 cygwin*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1604 # Guess yes on Solaris >= 2.6.
1605 solaris2.[0-5] | solaris2.[0-5].*)
1606 gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1607 solaris*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1608 # Guess yes on AIX >= 4.
1609 aix[1-3]*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1610 aix*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1611 # Guess yes on IRIX >= 6.5.
1612 irix6.5) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1613 # Guess yes on NetBSD >= 3.
1614 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1615 gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1616 netbsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1617 # Guess yes on BeOS.
1618 beos*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1619 # Guess yes on Android.
1620 linux*-android*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1621 # Guess yes on native Windows.
1622 mingw* | pw*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1623 # If we don't know, obey --enable-cross-guesses.
1624 *) gl_cv_func_vsnprintf_zerosize_c99="$gl_cross_guess_normal";;
1631 dnl The results of these tests on various platforms are:
1633 dnl 1 = gl_PRINTF_SIZES_C99
1634 dnl 2 = gl_PRINTF_LONG_DOUBLE
1635 dnl 3 = gl_PRINTF_INFINITE
1636 dnl 4 = gl_PRINTF_INFINITE_LONG_DOUBLE
1637 dnl 5 = gl_PRINTF_DIRECTIVE_A
1638 dnl 6 = gl_PRINTF_DIRECTIVE_F
1639 dnl 7 = gl_PRINTF_DIRECTIVE_N
1640 dnl 8 = gl_PRINTF_DIRECTIVE_LS
1641 dnl 9 = gl_PRINTF_POSITIONS
1642 dnl 10 = gl_PRINTF_FLAG_GROUPING
1643 dnl 11 = gl_PRINTF_FLAG_LEFTADJUST
1644 dnl 12 = gl_PRINTF_FLAG_ZERO
1645 dnl 13 = gl_PRINTF_PRECISION
1646 dnl 14 = gl_PRINTF_ENOMEM
1647 dnl 15 = gl_SNPRINTF_PRESENCE
1648 dnl 16 = gl_SNPRINTF_TRUNCATION_C99
1649 dnl 17 = gl_SNPRINTF_RETVAL_C99
1650 dnl 18 = gl_SNPRINTF_DIRECTIVE_N
1651 dnl 19 = gl_SNPRINTF_SIZE1
1652 dnl 20 = gl_VSNPRINTF_ZEROSIZE_C99
1654 dnl 1 = checking whether printf supports size specifiers as in C99...
1655 dnl 2 = checking whether printf supports 'long double' arguments...
1656 dnl 3 = checking whether printf supports infinite 'double' arguments...
1657 dnl 4 = checking whether printf supports infinite 'long double' arguments...
1658 dnl 5 = checking whether printf supports the 'a' and 'A' directives...
1659 dnl 6 = checking whether printf supports the 'F' directive...
1660 dnl 7 = checking whether printf supports the 'n' directive...
1661 dnl 8 = checking whether printf supports the 'ls' directive...
1662 dnl 9 = checking whether printf supports POSIX/XSI format strings with positions...
1663 dnl 10 = checking whether printf supports the grouping flag...
1664 dnl 11 = checking whether printf supports the left-adjust flag correctly...
1665 dnl 12 = checking whether printf supports the zero flag correctly...
1666 dnl 13 = checking whether printf supports large precisions...
1667 dnl 14 = checking whether printf survives out-of-memory conditions...
1668 dnl 15 = checking for snprintf...
1669 dnl 16 = checking whether snprintf truncates the result as in C99...
1670 dnl 17 = checking whether snprintf returns a byte count as in C99...
1671 dnl 18 = checking whether snprintf fully supports the 'n' directive...
1672 dnl 19 = checking whether snprintf respects a size of 1...
1673 dnl 20 = checking whether vsnprintf respects a zero size as in C99...
1675 dnl . = yes, # = no.
1677 dnl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1678 dnl glibc 2.5 . . . . . . . . . . . . . . . . . . . .
1679 dnl glibc 2.3.6 . . . . # . . . . . . . . . . . . . . .
1680 dnl FreeBSD 5.4, 6.1 . . . . # . . . . . . # . # . . . . . .
1681 dnl Mac OS X 10.13.5 . . . # # . # . . . . . . . . . . # . .
1682 dnl Mac OS X 10.5.8 . . . # # . . . . . . # . . . . . . . .
1683 dnl Mac OS X 10.3.9 . . . . # . . . . . . # . # . . . . . .
1684 dnl OpenBSD 3.9, 4.0 . . # # # # . # . # . # . # . . . . . .
1685 dnl Cygwin 1.7.0 (2009) . . . # . . . ? . . . . . ? . . . . . .
1686 dnl Cygwin 1.5.25 (2008) . . . # # . . # . . . . . # . . . . . .
1687 dnl Cygwin 1.5.19 (2006) # . . # # # . # . # . # # # . . . . . .
1688 dnl Solaris 11.4 . . # # # . . # . . . # . . . . . . . .
1689 dnl Solaris 11.3 . . . . # . . # . . . . . . . . . . . .
1690 dnl Solaris 11.0 . . # # # . . # . . . # . . . . . . . .
1691 dnl Solaris 10 . . # # # . . # . . . # # . . . . . . .
1692 dnl Solaris 2.6 ... 9 # . # # # # . # . . . # # . . . # . . .
1693 dnl Solaris 2.5.1 # . # # # # . # . . . # . . # # # # # #
1694 dnl AIX 7.1 . . # # # . . . . . . # # . . . . . . .
1695 dnl AIX 5.2 . . # # # . . . . . . # . . . . . . . .
1696 dnl AIX 4.3.2, 5.1 # . # # # # . . . . . # . . . . # . . .
1697 dnl HP-UX 11.31 . . . . # . . . . . . # . . . . # # . .
1698 dnl HP-UX 11.{00,11,23} # . . . # # . . . . . # . . . . # # . #
1699 dnl HP-UX 10.20 # . # . # # . ? . . # # . . . . # # ? #
1700 dnl IRIX 6.5 # . # # # # . # . . . # . . . . # . . .
1701 dnl OSF/1 5.1 # . # # # # . . . . . # . . . . # . . #
1702 dnl OSF/1 4.0d # . # # # # . . . . . # . . # # # # # #
1703 dnl NetBSD 9.0 . . . . # . . . . . . . . . . . . . . .
1704 dnl NetBSD 5.0 . . . # # . . . . . . # . # . . . . . .
1705 dnl NetBSD 4.0 . ? ? ? ? ? . ? . ? ? ? ? ? . . . ? ? ?
1706 dnl NetBSD 3.0 . . . . # # . ? # # ? # . # . . . . . .
1707 dnl Haiku . . . # # # . # . . . . . ? . . ? . . .
1708 dnl BeOS # # . # # # . ? # . ? . # ? . . ? . . .
1709 dnl Android 4.3 . . # # # # # # . # . # . # . . . # . .
1710 dnl old mingw / msvcrt # # # # # # . . # # . # # ? . # # # . .
1711 dnl MSVC 9 # # # # # # # . # # . # # ? # # # # . .
1712 dnl mingw 2009-2011 . # . # . . . . # # . . . ? . . . . . .
1713 dnl mingw-w64 2011 # # # # # # . . # # . # # ? . # # # . .