2 dnl Copyright (C) 2003, 2007-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 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";;
260 # Guess yes on OpenBSD >= 6.0.
261 openbsd[1-5].*) gl_cv_func_printf_infinite="guessing no";;
262 openbsd*) gl_cv_func_printf_infinite="guessing yes";;
264 beos*) gl_cv_func_printf_infinite="guessing yes";;
265 # Guess no on Android.
266 linux*-android*) gl_cv_func_printf_infinite="guessing no";;
268 # Guess yes on MSVC, no on mingw.
269 mingw*) AC_EGREP_CPP([Known], [
274 [gl_cv_func_printf_infinite="guessing yes"],
275 [gl_cv_func_printf_infinite="guessing no"])
277 # If we don't know, obey --enable-cross-guesses.
278 *) gl_cv_func_printf_infinite="$gl_cross_guess_normal";;
284 dnl Test whether the *printf family of functions supports infinite and NaN
285 dnl 'long double' arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001)
286 dnl Result is gl_cv_func_printf_infinite_long_double.
288 AC_DEFUN([gl_PRINTF_INFINITE_LONG_DOUBLE],
290 AC_REQUIRE([gl_PRINTF_LONG_DOUBLE])
291 AC_REQUIRE([AC_PROG_CC])
292 AC_REQUIRE([gl_BIGENDIAN])
293 AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
294 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
295 dnl The user can set or unset the variable gl_printf_safe to indicate
296 dnl that he wishes a safe handling of non-IEEE-754 'long double' values.
297 if test -n "$gl_printf_safe"; then
298 AC_DEFINE([CHECK_PRINTF_SAFE], [1],
299 [Define if you wish *printf() functions that have a safe handling of
300 non-IEEE-754 'long double' values.])
302 case "$gl_cv_func_printf_long_double" in
304 AC_CACHE_CHECK([whether printf supports infinite 'long double' arguments],
305 [gl_cv_func_printf_infinite_long_double],
314 strisnan (const char *string, size_t start_index, size_t end_index)
316 if (start_index < end_index)
318 if (string[start_index] == '-')
320 if (start_index + 3 <= end_index
321 && memcmp (string + start_index, "nan", 3) == 0)
324 if (start_index == end_index
325 || (string[start_index] == '(' && string[end_index - 1] == ')'))
331 static char buf[10000];
332 static long double zeroL = 0.0L;
337 if (sprintf (buf, "%Lf", 1.0L / zeroL) < 0
338 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
340 if (sprintf (buf, "%Lf", -1.0L / zeroL) < 0
341 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
343 if (sprintf (buf, "%Lf", zeroL / zeroL) < 0
344 || !strisnan (buf, 0, strlen (buf)))
346 if (sprintf (buf, "%Le", 1.0L / zeroL) < 0
347 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
349 if (sprintf (buf, "%Le", -1.0L / zeroL) < 0
350 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
352 if (sprintf (buf, "%Le", zeroL / zeroL) < 0
353 || !strisnan (buf, 0, strlen (buf)))
355 if (sprintf (buf, "%Lg", 1.0L / zeroL) < 0
356 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
358 if (sprintf (buf, "%Lg", -1.0L / zeroL) < 0
359 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
361 if (sprintf (buf, "%Lg", zeroL / zeroL) < 0
362 || !strisnan (buf, 0, strlen (buf)))
364 #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
365 /* Representation of an 80-bit 'long double' as an initializer for a sequence
366 of 'unsigned int' words. */
367 # ifdef WORDS_BIGENDIAN
368 # define LDBL80_WORDS(exponent,manthi,mantlo) \
369 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
370 ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
371 (unsigned int) (mantlo) << 16 \
374 # define LDBL80_WORDS(exponent,manthi,mantlo) \
375 { mantlo, manthi, exponent }
378 static union { unsigned int word[4]; long double value; } x =
379 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
380 if (sprintf (buf, "%Lf", x.value) < 0
381 || !strisnan (buf, 0, strlen (buf)))
383 if (sprintf (buf, "%Le", x.value) < 0
384 || !strisnan (buf, 0, strlen (buf)))
386 if (sprintf (buf, "%Lg", x.value) < 0
387 || !strisnan (buf, 0, strlen (buf)))
391 /* Signalling NaN. */
392 static union { unsigned int word[4]; long double value; } x =
393 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
394 if (sprintf (buf, "%Lf", x.value) < 0
395 || !strisnan (buf, 0, strlen (buf)))
397 if (sprintf (buf, "%Le", x.value) < 0
398 || !strisnan (buf, 0, strlen (buf)))
400 if (sprintf (buf, "%Lg", x.value) < 0
401 || !strisnan (buf, 0, strlen (buf)))
405 static union { unsigned int word[4]; long double value; } x =
406 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
407 if (sprintf (buf, "%Lf", x.value) <= 0)
409 if (sprintf (buf, "%Le", x.value) <= 0)
411 if (sprintf (buf, "%Lg", x.value) <= 0)
414 { /* Pseudo-Infinity. */
415 static union { unsigned int word[4]; long double value; } x =
416 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
417 if (sprintf (buf, "%Lf", x.value) <= 0)
419 if (sprintf (buf, "%Le", x.value) <= 0)
421 if (sprintf (buf, "%Lg", x.value) <= 0)
425 static union { unsigned int word[4]; long double value; } x =
426 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
427 if (sprintf (buf, "%Lf", x.value) <= 0)
429 if (sprintf (buf, "%Le", x.value) <= 0)
431 if (sprintf (buf, "%Lg", x.value) <= 0)
434 { /* Unnormalized number. */
435 static union { unsigned int word[4]; long double value; } x =
436 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
437 if (sprintf (buf, "%Lf", x.value) <= 0)
439 if (sprintf (buf, "%Le", x.value) <= 0)
441 if (sprintf (buf, "%Lg", x.value) <= 0)
444 { /* Pseudo-Denormal. */
445 static union { unsigned int word[4]; long double value; } x =
446 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
447 if (sprintf (buf, "%Lf", x.value) <= 0)
449 if (sprintf (buf, "%Le", x.value) <= 0)
451 if (sprintf (buf, "%Lg", x.value) <= 0)
457 [gl_cv_func_printf_infinite_long_double=yes],
458 [gl_cv_func_printf_infinite_long_double=no],
460 # Guess no on ia64, x86_64, i386.
461 ia64 | x86_64 | i*86) gl_cv_func_printf_infinite_long_double="guessing no";;
465 # Guess yes on glibc systems.
466 *-gnu* | gnu*) gl_cv_func_printf_infinite_long_double="guessing yes";;
467 # Guess yes on musl systems.
468 *-musl*) gl_cv_func_printf_infinite_long_double="guessing yes";;
469 # Guess yes on FreeBSD >= 6.
470 freebsd[1-5].*) gl_cv_func_printf_infinite_long_double="guessing no";;
471 freebsd* | kfreebsd*) gl_cv_func_printf_infinite_long_double="guessing yes";;
472 # Guess yes on HP-UX >= 11.
473 hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite_long_double="guessing no";;
474 hpux*) gl_cv_func_printf_infinite_long_double="guessing yes";;
475 # Guess yes on OpenBSD >= 6.0.
476 openbsd[1-5].*) gl_cv_func_printf_infinite_long_double="guessing no";;
477 openbsd*) gl_cv_func_printf_infinite_long_double="guessing yes";;
478 # Guess no on Android.
479 linux*-android*) gl_cv_func_printf_infinite_long_double="guessing no";;
481 # Guess yes on MSVC, no on mingw.
482 mingw*) AC_EGREP_CPP([Known], [
487 [gl_cv_func_printf_infinite_long_double="guessing yes"],
488 [gl_cv_func_printf_infinite_long_double="guessing no"])
490 # If we don't know, obey --enable-cross-guesses.
491 *) gl_cv_func_printf_infinite_long_double="$gl_cross_guess_normal";;
499 gl_cv_func_printf_infinite_long_double="irrelevant"
504 dnl Test whether the *printf family of functions supports the 'a' and 'A'
505 dnl conversion specifier for hexadecimal output of floating-point numbers.
506 dnl (ISO C99, POSIX:2001)
507 dnl Result is gl_cv_func_printf_directive_a.
509 AC_DEFUN([gl_PRINTF_DIRECTIVE_A],
511 AC_REQUIRE([AC_PROG_CC])
512 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
513 AC_CACHE_CHECK([whether printf supports the 'a' and 'A' directives],
514 [gl_cv_func_printf_directive_a],
520 static char buf[100];
521 static double zero = 0.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 if (sprintf (buf, "%A %d", -3.1416015625, 33, 44, 55) < 0
532 || (strcmp (buf, "-0X1.922P+1 33") != 0
533 && strcmp (buf, "-0X3.244P+0 33") != 0
534 && strcmp (buf, "-0X6.488P-1 33") != 0
535 && strcmp (buf, "-0XC.91P-2 33") != 0))
537 /* This catches a FreeBSD 6.1 bug: it doesn't round. */
538 if (sprintf (buf, "%.2a %d", 1.51, 33, 44, 55) < 0
539 || (strcmp (buf, "0x1.83p+0 33") != 0
540 && strcmp (buf, "0x3.05p-1 33") != 0
541 && strcmp (buf, "0x6.0ap-2 33") != 0
542 && strcmp (buf, "0xc.14p-3 33") != 0))
544 /* This catches a Mac OS X 10.12.4 (Darwin 16.5) bug: it doesn't round. */
545 if (sprintf (buf, "%.0a %d", 1.51, 33, 44, 55) < 0
546 || (strcmp (buf, "0x2p+0 33") != 0
547 && strcmp (buf, "0x3p-1 33") != 0
548 && strcmp (buf, "0x6p-2 33") != 0
549 && strcmp (buf, "0xcp-3 33") != 0))
551 /* This catches a FreeBSD 6.1 bug. See
552 <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
553 if (sprintf (buf, "%010a %d", 1.0 / zero, 33, 44, 55) < 0
556 /* This catches a Mac OS X 10.3.9 (Darwin 7.9) bug. */
557 if (sprintf (buf, "%.1a", 1.999) < 0
558 || (strcmp (buf, "0x1.0p+1") != 0
559 && strcmp (buf, "0x2.0p+0") != 0
560 && strcmp (buf, "0x4.0p-1") != 0
561 && strcmp (buf, "0x8.0p-2") != 0))
563 /* This catches the same Mac OS X 10.3.9 (Darwin 7.9) bug and also a
564 glibc 2.4 bug <https://sourceware.org/bugzilla/show_bug.cgi?id=2908>. */
565 if (sprintf (buf, "%.1La", 1.999L) < 0
566 || (strcmp (buf, "0x1.0p+1") != 0
567 && strcmp (buf, "0x2.0p+0") != 0
568 && strcmp (buf, "0x4.0p-1") != 0
569 && strcmp (buf, "0x8.0p-2") != 0))
573 [gl_cv_func_printf_directive_a=yes],
574 [gl_cv_func_printf_directive_a=no],
577 # Guess yes on glibc >= 2.5 systems.
579 AC_EGREP_CPP([BZ2908], [
580 #include <features.h>
581 #ifdef __GNU_LIBRARY__
582 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)) && !defined __UCLIBC__
587 [gl_cv_func_printf_directive_a="guessing yes"],
588 [gl_cv_func_printf_directive_a="guessing no"])
590 # Guess yes on musl systems.
591 *-musl*) gl_cv_func_printf_directive_a="guessing yes";;
592 # Guess no on Android.
593 linux*-android*) gl_cv_func_printf_directive_a="guessing no";;
594 # Guess no on native Windows.
595 mingw*) gl_cv_func_printf_directive_a="guessing no";;
596 # If we don't know, obey --enable-cross-guesses.
597 *) gl_cv_func_printf_directive_a="$gl_cross_guess_normal";;
603 dnl Test whether the *printf family of functions supports the %F format
604 dnl directive. (ISO C99, POSIX:2001)
605 dnl Result is gl_cv_func_printf_directive_f.
607 AC_DEFUN([gl_PRINTF_DIRECTIVE_F],
609 AC_REQUIRE([AC_PROG_CC])
610 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
611 AC_CACHE_CHECK([whether printf supports the 'F' directive],
612 [gl_cv_func_printf_directive_f],
618 static char buf[100];
619 static double zero = 0.0;
623 if (sprintf (buf, "%F %d", 1234567.0, 33, 44, 55) < 0
624 || strcmp (buf, "1234567.000000 33") != 0)
626 if (sprintf (buf, "%F", 1.0 / zero) < 0
627 || (strcmp (buf, "INF") != 0 && strcmp (buf, "INFINITY") != 0))
629 /* This catches a Cygwin 1.5.x bug. */
630 if (sprintf (buf, "%.F", 1234.0) < 0
631 || strcmp (buf, "1234") != 0)
635 [gl_cv_func_printf_directive_f=yes],
636 [gl_cv_func_printf_directive_f=no],
640 # Guess yes on glibc systems.
641 *-gnu* | gnu*) gl_cv_func_printf_directive_f="guessing yes";;
642 # Guess yes on musl systems.
643 *-musl*) gl_cv_func_printf_directive_f="guessing yes";;
644 # Guess yes on FreeBSD >= 6.
645 freebsd[1-5].*) gl_cv_func_printf_directive_f="guessing no";;
646 freebsd* | kfreebsd*) gl_cv_func_printf_directive_f="guessing yes";;
647 # Guess yes on Mac OS X >= 10.3.
648 darwin[1-6].*) gl_cv_func_printf_directive_f="guessing no";;
649 darwin*) gl_cv_func_printf_directive_f="guessing yes";;
650 # Guess yes on OpenBSD >= 6.0.
651 openbsd[1-5].*) gl_cv_func_printf_directive_f="guessing no";;
652 openbsd*) gl_cv_func_printf_directive_f="guessing yes";;
653 # Guess yes on Solaris >= 2.10.
654 solaris2.[1-9][0-9]*) gl_cv_func_printf_directive_f="guessing yes";;
655 solaris*) gl_cv_func_printf_directive_f="guessing no";;
656 # Guess no on Android.
657 linux*-android*) gl_cv_func_printf_directive_f="guessing no";;
659 # Guess yes on MSVC, no on mingw.
660 mingw*) AC_EGREP_CPP([Known], [
665 [gl_cv_func_printf_directive_f="guessing yes"],
666 [gl_cv_func_printf_directive_f="guessing no"])
668 # If we don't know, obey --enable-cross-guesses.
669 *) gl_cv_func_printf_directive_f="$gl_cross_guess_normal";;
675 dnl Test whether the *printf family of functions supports the %n format
676 dnl directive. (ISO C99, POSIX:2001)
677 dnl Result is gl_cv_func_printf_directive_n.
679 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
681 AC_REQUIRE([AC_PROG_CC])
682 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
683 AC_CACHE_CHECK([whether printf supports the 'n' directive],
684 [gl_cv_func_printf_directive_n],
692 #include <inttypes.h>
693 /* See page about "Parameter Validation" on msdn.microsoft.com.
694 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation>
695 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-invalid-parameter-handler-set-thread-local-invalid-parameter-handler> */
697 invalid_parameter_handler (const wchar_t *expression,
698 const wchar_t *function,
699 const wchar_t *file, unsigned int line,
705 static char fmtstring[10];
706 static char buf[100];
711 _set_invalid_parameter_handler (invalid_parameter_handler);
713 /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE=2)
714 support %n in format strings in read-only memory but not in writable
716 strcpy (fmtstring, "%d %n");
717 if (sprintf (buf, fmtstring, 123, &count, 33, 44, 55) < 0
718 || strcmp (buf, "123 ") != 0
723 [gl_cv_func_printf_directive_n=yes],
724 [gl_cv_func_printf_directive_n=no],
726 # Guess no on glibc when _FORTIFY_SOURCE >= 2.
727 *-gnu* | gnu*) AC_COMPILE_IFELSE(
729 [[#if _FORTIFY_SOURCE >= 2
733 [gl_cv_func_printf_directive_n="guessing yes"],
734 [gl_cv_func_printf_directive_n="guessing no"])
736 # Guess no on Android.
737 linux*-android*) gl_cv_func_printf_directive_n="guessing no";;
738 # Guess no on native Windows.
739 mingw*) gl_cv_func_printf_directive_n="guessing no";;
740 *) gl_cv_func_printf_directive_n="guessing yes";;
746 dnl Test whether the *printf family of functions supports the %ls format
747 dnl directive and in particular, when a precision is specified, whether
748 dnl the functions stop converting the wide string argument when the number
749 dnl of bytes that have been produced by this conversion equals or exceeds
751 dnl Result is gl_cv_func_printf_directive_ls.
753 AC_DEFUN([gl_PRINTF_DIRECTIVE_LS],
755 AC_REQUIRE([AC_PROG_CC])
756 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
757 AC_CACHE_CHECK([whether printf supports the 'ls' directive],
758 [gl_cv_func_printf_directive_ls],
769 /* Test whether %ls works at all.
770 This test fails on OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, but not on
773 static const wchar_t wstring[] = { 'a', 'b', 'c', 0 };
775 if (sprintf (buf, "%ls", wstring) < 0
776 || strcmp (buf, "abc") != 0)
779 /* This test fails on IRIX 6.5, Solaris 2.6, Cygwin 1.5, Haiku (with an
780 assertion failure inside libc), but not on OpenBSD 4.0. */
782 static const wchar_t wstring[] = { 'a', 0 };
784 if (sprintf (buf, "%ls", wstring) < 0
785 || strcmp (buf, "a") != 0)
788 /* Test whether precisions in %ls are supported as specified in ISO C 99
790 "If a precision is specified, no more than that many bytes are written
791 (including shift sequences, if any), and the array shall contain a
792 null wide character if, to equal the multibyte character sequence
793 length given by the precision, the function would need to access a
794 wide character one past the end of the array."
795 This test fails on Solaris 10. */
797 static const wchar_t wstring[] = { 'a', 'b', (wchar_t) 0xfdfdfdfd, 0 };
799 if (sprintf (buf, "%.2ls", wstring) < 0
800 || strcmp (buf, "ab") != 0)
805 [gl_cv_func_printf_directive_ls=yes],
806 [gl_cv_func_printf_directive_ls=no],
810 # Guess yes on OpenBSD >= 6.0.
811 openbsd[1-5].*) gl_cv_func_printf_directive_ls="guessing no";;
812 openbsd*) gl_cv_func_printf_directive_ls="guessing yes";;
813 irix*) gl_cv_func_printf_directive_ls="guessing no";;
814 solaris*) gl_cv_func_printf_directive_ls="guessing no";;
815 cygwin*) gl_cv_func_printf_directive_ls="guessing no";;
816 beos* | haiku*) gl_cv_func_printf_directive_ls="guessing no";;
817 # Guess no on Android.
818 linux*-android*) gl_cv_func_printf_directive_ls="guessing no";;
819 # Guess yes on native Windows.
820 mingw*) gl_cv_func_printf_directive_ls="guessing yes";;
821 *) gl_cv_func_printf_directive_ls="guessing yes";;
828 dnl Test whether the *printf family of functions supports POSIX/XSI format
829 dnl strings with positions. (POSIX:2001)
830 dnl Result is gl_cv_func_printf_positions.
832 AC_DEFUN([gl_PRINTF_POSITIONS],
834 AC_REQUIRE([AC_PROG_CC])
835 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
836 AC_CACHE_CHECK([whether printf supports POSIX/XSI format strings with positions],
837 [gl_cv_func_printf_positions],
843 /* The string "%2$d %1$d", with dollar characters protected from the shell's
844 dollar expansion (possibly an autoconf bug). */
845 static char format[] = { '%', '2', '$', 'd', ' ', '%', '1', '$', 'd', '\0' };
846 static char buf[100];
849 sprintf (buf, format, 33, 55);
850 return (strcmp (buf, "55 33") != 0);
852 [gl_cv_func_printf_positions=yes],
853 [gl_cv_func_printf_positions=no],
857 netbsd[1-3]* | netbsdelf[1-3]* | netbsdaout[1-3]* | netbsdcoff[1-3]*)
858 gl_cv_func_printf_positions="guessing no";;
859 beos*) gl_cv_func_printf_positions="guessing no";;
860 # Guess yes on Android.
861 linux*-android*) gl_cv_func_printf_positions="guessing yes";;
862 # Guess no on native Windows.
863 mingw* | pw*) gl_cv_func_printf_positions="guessing no";;
864 *) gl_cv_func_printf_positions="guessing yes";;
871 dnl Test whether the *printf family of functions supports POSIX/XSI format
872 dnl strings with the ' flag for grouping of decimal digits. (POSIX:2001)
873 dnl Result is gl_cv_func_printf_flag_grouping.
875 AC_DEFUN([gl_PRINTF_FLAG_GROUPING],
877 AC_REQUIRE([AC_PROG_CC])
878 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
879 AC_CACHE_CHECK([whether printf supports the grouping flag],
880 [gl_cv_func_printf_flag_grouping],
886 static char buf[100];
889 if (sprintf (buf, "%'d %d", 1234567, 99) < 0
890 || buf[strlen (buf) - 1] != '9')
894 [gl_cv_func_printf_flag_grouping=yes],
895 [gl_cv_func_printf_flag_grouping=no],
899 cygwin*) gl_cv_func_printf_flag_grouping="guessing no";;
900 netbsd*) gl_cv_func_printf_flag_grouping="guessing no";;
901 # Guess no on Android.
902 linux*-android*) gl_cv_func_printf_flag_grouping="guessing no";;
903 # Guess no on native Windows.
904 mingw* | pw*) gl_cv_func_printf_flag_grouping="guessing no";;
905 *) gl_cv_func_printf_flag_grouping="guessing yes";;
912 dnl Test whether the *printf family of functions supports the - flag correctly.
914 dnl <https://lists.gnu.org/r/bug-coreutils/2008-02/msg00035.html>
915 dnl Result is gl_cv_func_printf_flag_leftadjust.
917 AC_DEFUN([gl_PRINTF_FLAG_LEFTADJUST],
919 AC_REQUIRE([AC_PROG_CC])
920 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
921 AC_CACHE_CHECK([whether printf supports the left-adjust flag correctly],
922 [gl_cv_func_printf_flag_leftadjust],
928 static char buf[100];
931 /* Check that a '-' flag is not annihilated by a negative width. */
932 if (sprintf (buf, "a%-*sc", -3, "b") < 0
933 || strcmp (buf, "ab c") != 0)
937 [gl_cv_func_printf_flag_leftadjust=yes],
938 [gl_cv_func_printf_flag_leftadjust=no],
942 # Guess yes on HP-UX 11.
943 hpux11*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
944 # Guess no on HP-UX 10 and older.
945 hpux*) gl_cv_func_printf_flag_leftadjust="guessing no";;
946 # Guess yes on Android.
947 linux*-android*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
948 # Guess yes on native Windows.
949 mingw*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
950 # Guess yes otherwise.
951 *) gl_cv_func_printf_flag_leftadjust="guessing yes";;
958 dnl Test whether the *printf family of functions supports padding of non-finite
959 dnl values with the 0 flag correctly. (ISO C99 + TC1 + TC2.) See
960 dnl <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html>
961 dnl Result is gl_cv_func_printf_flag_zero.
963 AC_DEFUN([gl_PRINTF_FLAG_ZERO],
965 AC_REQUIRE([AC_PROG_CC])
966 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
967 AC_CACHE_CHECK([whether printf supports the zero flag correctly],
968 [gl_cv_func_printf_flag_zero],
974 static char buf[100];
975 static double zero = 0.0;
978 if (sprintf (buf, "%010f", 1.0 / zero, 33, 44, 55) < 0
979 || (strcmp (buf, " inf") != 0
980 && strcmp (buf, " infinity") != 0))
984 [gl_cv_func_printf_flag_zero=yes],
985 [gl_cv_func_printf_flag_zero=no],
989 # Guess yes on glibc systems.
990 *-gnu* | gnu*) gl_cv_func_printf_flag_zero="guessing yes";;
991 # Guess yes on musl systems.
992 *-musl*) gl_cv_func_printf_flag_zero="guessing yes";;
994 beos*) gl_cv_func_printf_flag_zero="guessing yes";;
995 # Guess no on Android.
996 linux*-android*) gl_cv_func_printf_flag_zero="guessing no";;
997 # Guess no on native Windows.
998 mingw*) gl_cv_func_printf_flag_zero="guessing no";;
999 # If we don't know, obey --enable-cross-guesses.
1000 *) gl_cv_func_printf_flag_zero="$gl_cross_guess_normal";;
1007 dnl Test whether the *printf family of functions supports large precisions.
1008 dnl On mingw, precisions larger than 512 are treated like 512, in integer,
1009 dnl floating-point or pointer output. On Solaris 10/x86, precisions larger
1010 dnl than 510 in floating-point output crash the program. On Solaris 10/SPARC,
1011 dnl precisions larger than 510 in floating-point output yield wrong results.
1012 dnl On AIX 7.1, precisions larger than 998 in floating-point output yield
1013 dnl wrong results. On BeOS, precisions larger than 1044 crash the program.
1014 dnl Result is gl_cv_func_printf_precision.
1016 AC_DEFUN([gl_PRINTF_PRECISION],
1018 AC_REQUIRE([AC_PROG_CC])
1019 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1020 AC_CACHE_CHECK([whether printf supports large precisions],
1021 [gl_cv_func_printf_precision],
1027 static char buf[5000];
1032 /* On BeOS, this would crash and show a dialog box. Avoid the crash. */
1035 if (sprintf (buf, "%.4000d %d", 1, 33, 44) < 4000 + 3)
1037 if (sprintf (buf, "%.4000f %d", 1.0, 33, 44) < 4000 + 5)
1039 if (sprintf (buf, "%.511f %d", 1.0, 33, 44) < 511 + 5
1042 if (sprintf (buf, "%.999f %d", 1.0, 33, 44) < 999 + 5
1047 [gl_cv_func_printf_precision=yes],
1048 [gl_cv_func_printf_precision=no],
1052 # Guess no only on Solaris, native Windows, and BeOS systems.
1053 solaris*) gl_cv_func_printf_precision="guessing no" ;;
1054 mingw* | pw*) gl_cv_func_printf_precision="guessing no" ;;
1055 beos*) gl_cv_func_printf_precision="guessing no" ;;
1056 # Guess yes on Android.
1057 linux*-android*) gl_cv_func_printf_precision="guessing yes" ;;
1058 *) gl_cv_func_printf_precision="guessing yes" ;;
1065 dnl Test whether the *printf family of functions recovers gracefully in case
1066 dnl of an out-of-memory condition, or whether it crashes the entire program.
1067 dnl Result is gl_cv_func_printf_enomem.
1069 AC_DEFUN([gl_PRINTF_ENOMEM],
1071 AC_REQUIRE([AC_PROG_CC])
1072 AC_REQUIRE([gl_MULTIARCH])
1073 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1074 AC_CACHE_CHECK([whether printf survives out-of-memory conditions],
1075 [gl_cv_func_printf_enomem],
1077 gl_cv_func_printf_enomem="guessing no"
1078 if test "$cross_compiling" = no; then
1079 if test $APPLE_UNIVERSAL_BUILD = 0; then
1080 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
1083 #include <sys/types.h>
1084 #include <sys/time.h>
1085 #include <sys/resource.h>
1089 struct rlimit limit;
1092 /* Some printf implementations allocate temporary space with malloc. */
1093 /* On BSD systems, malloc() is limited by RLIMIT_DATA. */
1095 if (getrlimit (RLIMIT_DATA, &limit) < 0)
1097 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1098 limit.rlim_max = 5000000;
1099 limit.rlim_cur = limit.rlim_max;
1100 if (setrlimit (RLIMIT_DATA, &limit) < 0)
1103 /* On Linux systems, malloc() is limited by RLIMIT_AS. */
1105 if (getrlimit (RLIMIT_AS, &limit) < 0)
1107 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1108 limit.rlim_max = 5000000;
1109 limit.rlim_cur = limit.rlim_max;
1110 if (setrlimit (RLIMIT_AS, &limit) < 0)
1113 /* Some printf implementations allocate temporary space on the stack. */
1115 if (getrlimit (RLIMIT_STACK, &limit) < 0)
1117 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1118 limit.rlim_max = 5000000;
1119 limit.rlim_cur = limit.rlim_max;
1120 if (setrlimit (RLIMIT_STACK, &limit) < 0)
1123 ret = printf ("%.5000000f", 1.0);
1124 return !(ret == 5000002 || (ret < 0 && errno == ENOMEM));
1127 if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
1128 (./conftest 2>&AS_MESSAGE_LOG_FD
1130 _AS_ECHO_LOG([\$? = $result])
1131 if test $result != 0 && test $result != 77; then result=1; fi
1133 ) >/dev/null 2>/dev/null
1135 0) gl_cv_func_printf_enomem="yes" ;;
1136 77) gl_cv_func_printf_enomem="guessing no" ;;
1137 *) gl_cv_func_printf_enomem="no" ;;
1140 gl_cv_func_printf_enomem="guessing no"
1144 dnl A universal build on Apple Mac OS X platforms.
1145 dnl The result would be 'no' in 32-bit mode and 'yes' in 64-bit mode.
1146 dnl But we need a configuration result that is valid in both modes.
1147 gl_cv_func_printf_enomem="guessing no"
1150 if test "$gl_cv_func_printf_enomem" = "guessing no"; then
1153 # Guess yes on glibc systems.
1154 *-gnu* | gnu*) gl_cv_func_printf_enomem="guessing yes";;
1155 # Guess yes on Solaris.
1156 solaris*) gl_cv_func_printf_enomem="guessing yes";;
1158 aix*) gl_cv_func_printf_enomem="guessing yes";;
1159 # Guess yes on HP-UX/hppa.
1160 hpux*) case "$host_cpu" in
1161 hppa*) gl_cv_func_printf_enomem="guessing yes";;
1162 *) gl_cv_func_printf_enomem="guessing no";;
1165 # Guess yes on IRIX.
1166 irix*) gl_cv_func_printf_enomem="guessing yes";;
1167 # Guess yes on OSF/1.
1168 osf*) gl_cv_func_printf_enomem="guessing yes";;
1169 # Guess yes on BeOS.
1170 beos*) gl_cv_func_printf_enomem="guessing yes";;
1171 # Guess yes on Haiku.
1172 haiku*) gl_cv_func_printf_enomem="guessing yes";;
1173 # Guess no on Android.
1174 linux*-android*) gl_cv_func_printf_enomem="guessing no";;
1175 # If we don't know, obey --enable-cross-guesses.
1176 *) gl_cv_func_printf_enomem="$gl_cross_guess_normal";;
1183 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
1184 dnl Result is ac_cv_func_snprintf.
1186 AC_DEFUN([gl_SNPRINTF_PRESENCE],
1188 AC_CHECK_FUNCS_ONCE([snprintf])
1191 dnl Test whether the string produced by the snprintf function is always NUL
1192 dnl terminated. (ISO C99, POSIX:2001)
1193 dnl Result is gl_cv_func_snprintf_truncation_c99.
1195 AC_DEFUN_ONCE([gl_SNPRINTF_TRUNCATION_C99],
1197 AC_REQUIRE([AC_PROG_CC])
1198 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1199 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1200 AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
1201 [gl_cv_func_snprintf_truncation_c99],
1208 # define my_snprintf snprintf
1210 # include <stdarg.h>
1211 static int my_snprintf (char *buf, int size, const char *format, ...)
1215 va_start (args, format);
1216 ret = vsnprintf (buf, size, format, args);
1221 static char buf[100];
1224 strcpy (buf, "ABCDEF");
1225 my_snprintf (buf, 3, "%d %d", 4567, 89);
1226 if (memcmp (buf, "45\0DEF", 6) != 0)
1230 [gl_cv_func_snprintf_truncation_c99=yes],
1231 [gl_cv_func_snprintf_truncation_c99=no],
1235 # Guess yes on glibc systems.
1236 *-gnu* | gnu*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1237 # Guess yes on musl systems.
1238 *-musl*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1239 # Guess yes on FreeBSD >= 5.
1240 freebsd[1-4].*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1241 freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1242 # Guess yes on Mac OS X >= 10.3.
1243 darwin[1-6].*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1244 darwin*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1245 # Guess yes on OpenBSD >= 3.9.
1246 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
1247 gl_cv_func_snprintf_truncation_c99="guessing no";;
1248 openbsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1249 # Guess yes on Solaris >= 2.6.
1250 solaris2.[0-5] | solaris2.[0-5].*)
1251 gl_cv_func_snprintf_truncation_c99="guessing no";;
1252 solaris*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1253 # Guess yes on AIX >= 4.
1254 aix[1-3]*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1255 aix*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1256 # Guess yes on HP-UX >= 11.
1257 hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1258 hpux*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1259 # Guess yes on IRIX >= 6.5.
1260 irix6.5) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1261 # Guess yes on OSF/1 >= 5.
1262 osf[3-4]*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1263 osf*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1264 # Guess yes on NetBSD >= 3.
1265 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1266 gl_cv_func_snprintf_truncation_c99="guessing no";;
1267 netbsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1268 # Guess yes on BeOS.
1269 beos*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1270 # Guess yes on Android.
1271 linux*-android*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1272 # Guess no on native Windows.
1273 mingw*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1274 # If we don't know, obey --enable-cross-guesses.
1275 *) gl_cv_func_snprintf_truncation_c99="$gl_cross_guess_normal";;
1282 dnl Test whether the return value of the snprintf function is the number
1283 dnl of bytes (excluding the terminating NUL) that would have been produced
1284 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
1285 dnl For example, this test program fails on IRIX 6.5:
1286 dnl ---------------------------------------------------------------------
1287 dnl #include <stdio.h>
1290 dnl static char buf[8];
1291 dnl int retval = snprintf (buf, 3, "%d", 12345);
1292 dnl return retval >= 0 && retval < 3;
1294 dnl ---------------------------------------------------------------------
1295 dnl Result is gl_cv_func_snprintf_retval_c99.
1297 AC_DEFUN_ONCE([gl_SNPRINTF_RETVAL_C99],
1299 AC_REQUIRE([AC_PROG_CC])
1300 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1301 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1302 AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
1303 [gl_cv_func_snprintf_retval_c99],
1310 # define my_snprintf snprintf
1312 # include <stdarg.h>
1313 static int my_snprintf (char *buf, int size, const char *format, ...)
1317 va_start (args, format);
1318 ret = vsnprintf (buf, size, format, args);
1323 static char buf[100];
1326 strcpy (buf, "ABCDEF");
1327 if (my_snprintf (buf, 3, "%d %d", 4567, 89) != 7)
1329 if (my_snprintf (buf, 0, "%d %d", 4567, 89) != 7)
1331 if (my_snprintf (NULL, 0, "%d %d", 4567, 89) != 7)
1335 [gl_cv_func_snprintf_retval_c99=yes],
1336 [gl_cv_func_snprintf_retval_c99=no],
1339 # Guess yes on glibc systems.
1340 *-gnu* | gnu*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1341 # Guess yes on musl systems.
1342 *-musl*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1343 # Guess yes on FreeBSD >= 5.
1344 freebsd[1-4].*) gl_cv_func_snprintf_retval_c99="guessing no";;
1345 freebsd* | kfreebsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1346 # Guess yes on Mac OS X >= 10.3.
1347 darwin[1-6].*) gl_cv_func_snprintf_retval_c99="guessing no";;
1348 darwin*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1349 # Guess yes on OpenBSD >= 3.9.
1350 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
1351 gl_cv_func_snprintf_retval_c99="guessing no";;
1352 openbsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1353 # Guess yes on Solaris >= 2.10.
1354 solaris2.[1-9][0-9]*) gl_cv_func_printf_sizes_c99="guessing yes";;
1355 solaris*) gl_cv_func_printf_sizes_c99="guessing no";;
1356 # Guess yes on AIX >= 4.
1357 aix[1-3]*) gl_cv_func_snprintf_retval_c99="guessing no";;
1358 aix*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1359 # Guess yes on NetBSD >= 3.
1360 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1361 gl_cv_func_snprintf_retval_c99="guessing no";;
1362 netbsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1363 # Guess yes on BeOS.
1364 beos*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1365 # Guess yes on Android.
1366 linux*-android*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1368 # Guess yes on MSVC, no on mingw.
1369 mingw*) AC_EGREP_CPP([Known], [
1374 [gl_cv_func_snprintf_retval_c99="guessing yes"],
1375 [gl_cv_func_snprintf_retval_c99="guessing no"])
1377 # If we don't know, obey --enable-cross-guesses.
1378 *) gl_cv_func_snprintf_retval_c99="$gl_cross_guess_normal";;
1384 dnl Test whether the snprintf function supports the %n format directive
1385 dnl also in truncated portions of the format string. (ISO C99, POSIX:2001)
1386 dnl Result is gl_cv_func_snprintf_directive_n.
1388 AC_DEFUN([gl_SNPRINTF_DIRECTIVE_N],
1390 AC_REQUIRE([AC_PROG_CC])
1391 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1392 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1393 AC_CACHE_CHECK([whether snprintf fully supports the 'n' directive],
1394 [gl_cv_func_snprintf_directive_n],
1401 # define my_snprintf snprintf
1403 # include <stdarg.h>
1404 static int my_snprintf (char *buf, int size, const char *format, ...)
1408 va_start (args, format);
1409 ret = vsnprintf (buf, size, format, args);
1414 static char fmtstring[10];
1415 static char buf[100];
1419 /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE=2)
1420 support %n in format strings in read-only memory but not in writable
1422 strcpy (fmtstring, "%d %n");
1423 my_snprintf (buf, 4, fmtstring, 12345, &count, 33, 44, 55);
1428 [gl_cv_func_snprintf_directive_n=yes],
1429 [gl_cv_func_snprintf_directive_n=no],
1432 # Guess no on glibc when _FORTIFY_SOURCE >= 2.
1433 *-gnu* | gnu*) AC_COMPILE_IFELSE(
1435 [[#if _FORTIFY_SOURCE >= 2
1439 [gl_cv_func_snprintf_directive_n="guessing yes"],
1440 [gl_cv_func_snprintf_directive_n="guessing no"])
1443 # Guess yes on musl systems.
1444 *-musl*) gl_cv_func_snprintf_directive_n="guessing yes";;
1445 # Guess yes on FreeBSD >= 5.
1446 freebsd[1-4].*) gl_cv_func_snprintf_directive_n="guessing no";;
1447 freebsd* | kfreebsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1448 # Guess yes on Mac OS X >= 10.3.
1449 darwin[1-6].*) gl_cv_func_snprintf_directive_n="guessing no";;
1450 darwin*) gl_cv_func_snprintf_directive_n="guessing yes";;
1451 # Guess yes on Solaris >= 2.6.
1452 solaris2.[0-5] | solaris2.[0-5].*)
1453 gl_cv_func_snprintf_directive_n="guessing no";;
1454 solaris*) gl_cv_func_snprintf_directive_n="guessing yes";;
1455 # Guess yes on AIX >= 4.
1456 aix[1-3]*) gl_cv_func_snprintf_directive_n="guessing no";;
1457 aix*) gl_cv_func_snprintf_directive_n="guessing yes";;
1458 # Guess yes on IRIX >= 6.5.
1459 irix6.5) gl_cv_func_snprintf_directive_n="guessing yes";;
1460 # Guess yes on OSF/1 >= 5.
1461 osf[3-4]*) gl_cv_func_snprintf_directive_n="guessing no";;
1462 osf*) gl_cv_func_snprintf_directive_n="guessing yes";;
1463 # Guess yes on NetBSD >= 3.
1464 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1465 gl_cv_func_snprintf_directive_n="guessing no";;
1466 netbsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1467 # Guess yes on BeOS.
1468 beos*) gl_cv_func_snprintf_directive_n="guessing yes";;
1469 # Guess no on Android.
1470 linux*-android*) gl_cv_func_snprintf_directive_n="guessing no";;
1471 # Guess no on native Windows.
1472 mingw*) gl_cv_func_snprintf_directive_n="guessing no";;
1473 # If we don't know, obey --enable-cross-guesses.
1474 *) gl_cv_func_snprintf_directive_n="$gl_cross_guess_normal";;
1481 dnl Test whether the snprintf function, when passed a size = 1, writes any
1482 dnl output without bounds in this case, behaving like sprintf. This is the
1483 dnl case on Linux libc5.
1484 dnl Result is gl_cv_func_snprintf_size1.
1486 AC_DEFUN([gl_SNPRINTF_SIZE1],
1488 AC_REQUIRE([AC_PROG_CC])
1489 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1490 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1491 AC_CACHE_CHECK([whether snprintf respects a size of 1],
1492 [gl_cv_func_snprintf_size1],
1498 # define my_snprintf snprintf
1500 # include <stdarg.h>
1501 static int my_snprintf (char *buf, int size, const char *format, ...)
1505 va_start (args, format);
1506 ret = vsnprintf (buf, size, format, args);
1513 static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1514 my_snprintf (buf, 1, "%d", 12345);
1515 return buf[1] != 'E';
1517 [gl_cv_func_snprintf_size1=yes],
1518 [gl_cv_func_snprintf_size1=no],
1520 # Guess yes on Android.
1521 linux*-android*) gl_cv_func_snprintf_size1="guessing yes" ;;
1522 # Guess yes on native Windows.
1523 mingw*) gl_cv_func_snprintf_size1="guessing yes" ;;
1524 *) gl_cv_func_snprintf_size1="guessing yes" ;;
1530 dnl Test whether the vsnprintf function, when passed a zero size, produces no
1531 dnl output. (ISO C99, POSIX:2001)
1532 dnl For example, snprintf nevertheless writes a NUL byte in this case
1534 dnl ---------------------------------------------------------------------
1535 dnl #include <stdio.h>
1538 dnl static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1539 dnl snprintf (buf, 0, "%d", 12345);
1540 dnl return buf[0] != 'D';
1542 dnl ---------------------------------------------------------------------
1543 dnl And vsnprintf writes any output without bounds in this case, behaving like
1544 dnl vsprintf, on HP-UX 11 and OSF/1 5.1:
1545 dnl ---------------------------------------------------------------------
1546 dnl #include <stdarg.h>
1547 dnl #include <stdio.h>
1548 dnl static int my_snprintf (char *buf, int size, const char *format, ...)
1552 dnl va_start (args, format);
1553 dnl ret = vsnprintf (buf, size, format, args);
1559 dnl static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1560 dnl my_snprintf (buf, 0, "%d", 12345);
1561 dnl return buf[0] != 'D';
1563 dnl ---------------------------------------------------------------------
1564 dnl Result is gl_cv_func_vsnprintf_zerosize_c99.
1566 AC_DEFUN([gl_VSNPRINTF_ZEROSIZE_C99],
1568 AC_REQUIRE([AC_PROG_CC])
1569 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1570 AC_CACHE_CHECK([whether vsnprintf respects a zero size as in C99],
1571 [gl_cv_func_vsnprintf_zerosize_c99],
1577 static int my_snprintf (char *buf, int size, const char *format, ...)
1581 va_start (args, format);
1582 ret = vsnprintf (buf, size, format, args);
1588 static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1589 my_snprintf (buf, 0, "%d", 12345);
1590 return buf[0] != 'D';
1592 [gl_cv_func_vsnprintf_zerosize_c99=yes],
1593 [gl_cv_func_vsnprintf_zerosize_c99=no],
1597 # Guess yes on glibc systems.
1598 *-gnu* | gnu*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1599 # Guess yes on musl systems.
1600 *-musl*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1601 # Guess yes on FreeBSD >= 5.
1602 freebsd[1-4].*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1603 freebsd* | kfreebsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1604 # Guess yes on Mac OS X >= 10.3.
1605 darwin[1-6].*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1606 darwin*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1607 # Guess yes on Cygwin.
1608 cygwin*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1609 # Guess yes on Solaris >= 2.6.
1610 solaris2.[0-5] | solaris2.[0-5].*)
1611 gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1612 solaris*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1613 # Guess yes on AIX >= 4.
1614 aix[1-3]*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1615 aix*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1616 # Guess yes on IRIX >= 6.5.
1617 irix6.5) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1618 # Guess yes on NetBSD >= 3.
1619 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1620 gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1621 netbsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1622 # Guess yes on BeOS.
1623 beos*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1624 # Guess yes on Android.
1625 linux*-android*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1626 # Guess yes on native Windows.
1627 mingw* | pw*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1628 # If we don't know, obey --enable-cross-guesses.
1629 *) gl_cv_func_vsnprintf_zerosize_c99="$gl_cross_guess_normal";;
1636 dnl The results of these tests on various platforms are:
1638 dnl 1 = gl_PRINTF_SIZES_C99
1639 dnl 2 = gl_PRINTF_LONG_DOUBLE
1640 dnl 3 = gl_PRINTF_INFINITE
1641 dnl 4 = gl_PRINTF_INFINITE_LONG_DOUBLE
1642 dnl 5 = gl_PRINTF_DIRECTIVE_A
1643 dnl 6 = gl_PRINTF_DIRECTIVE_F
1644 dnl 7 = gl_PRINTF_DIRECTIVE_N
1645 dnl 8 = gl_PRINTF_DIRECTIVE_LS
1646 dnl 9 = gl_PRINTF_POSITIONS
1647 dnl 10 = gl_PRINTF_FLAG_GROUPING
1648 dnl 11 = gl_PRINTF_FLAG_LEFTADJUST
1649 dnl 12 = gl_PRINTF_FLAG_ZERO
1650 dnl 13 = gl_PRINTF_PRECISION
1651 dnl 14 = gl_PRINTF_ENOMEM
1652 dnl 15 = gl_SNPRINTF_PRESENCE
1653 dnl 16 = gl_SNPRINTF_TRUNCATION_C99
1654 dnl 17 = gl_SNPRINTF_RETVAL_C99
1655 dnl 18 = gl_SNPRINTF_DIRECTIVE_N
1656 dnl 19 = gl_SNPRINTF_SIZE1
1657 dnl 20 = gl_VSNPRINTF_ZEROSIZE_C99
1659 dnl 1 = checking whether printf supports size specifiers as in C99...
1660 dnl 2 = checking whether printf supports 'long double' arguments...
1661 dnl 3 = checking whether printf supports infinite 'double' arguments...
1662 dnl 4 = checking whether printf supports infinite 'long double' arguments...
1663 dnl 5 = checking whether printf supports the 'a' and 'A' directives...
1664 dnl 6 = checking whether printf supports the 'F' directive...
1665 dnl 7 = checking whether printf supports the 'n' directive...
1666 dnl 8 = checking whether printf supports the 'ls' directive...
1667 dnl 9 = checking whether printf supports POSIX/XSI format strings with positions...
1668 dnl 10 = checking whether printf supports the grouping flag...
1669 dnl 11 = checking whether printf supports the left-adjust flag correctly...
1670 dnl 12 = checking whether printf supports the zero flag correctly...
1671 dnl 13 = checking whether printf supports large precisions...
1672 dnl 14 = checking whether printf survives out-of-memory conditions...
1673 dnl 15 = checking for snprintf...
1674 dnl 16 = checking whether snprintf truncates the result as in C99...
1675 dnl 17 = checking whether snprintf returns a byte count as in C99...
1676 dnl 18 = checking whether snprintf fully supports the 'n' directive...
1677 dnl 19 = checking whether snprintf respects a size of 1...
1678 dnl 20 = checking whether vsnprintf respects a zero size as in C99...
1680 dnl . = yes, # = no.
1682 dnl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1683 dnl glibc 2.5 . . . . . . . . . . . . . . . . . . . .
1684 dnl glibc 2.3.6 . . . . # . . . . . . . . . . . . . . .
1685 dnl FreeBSD 5.4, 6.1 . . . . # . . . . . . # . # . . . . . .
1686 dnl Mac OS X 10.13.5 . . . # # . # . . . . . . . . . . # . .
1687 dnl Mac OS X 10.5.8 . . . # # . . . . . . # . . . . . . . .
1688 dnl Mac OS X 10.3.9 . . . . # . . . . . . # . # . . . . . .
1689 dnl OpenBSD 6.0, 6.7 . . . . # . . . . . . . . # . . . . . .
1690 dnl OpenBSD 3.9, 4.0 . . # # # # . # . # . # . # . . . . . .
1691 dnl Cygwin 1.7.0 (2009) . . . # . . . ? . . . . . ? . . . . . .
1692 dnl Cygwin 1.5.25 (2008) . . . # # . . # . . . . . # . . . . . .
1693 dnl Cygwin 1.5.19 (2006) # . . # # # . # . # . # # # . . . . . .
1694 dnl Solaris 11.4 . . # # # . . # . . . # . . . . . . . .
1695 dnl Solaris 11.3 . . . . # . . # . . . . . . . . . . . .
1696 dnl Solaris 11.0 . . # # # . . # . . . # . . . . . . . .
1697 dnl Solaris 10 . . # # # . . # . . . # # . . . . . . .
1698 dnl Solaris 2.6 ... 9 # . # # # # . # . . . # # . . . # . . .
1699 dnl Solaris 2.5.1 # . # # # # . # . . . # . . # # # # # #
1700 dnl AIX 7.1 . . # # # . . . . . . # # . . . . . . .
1701 dnl AIX 5.2 . . # # # . . . . . . # . . . . . . . .
1702 dnl AIX 4.3.2, 5.1 # . # # # # . . . . . # . . . . # . . .
1703 dnl HP-UX 11.31 . . . . # . . . . . . # . . . . # # . .
1704 dnl HP-UX 11.{00,11,23} # . . . # # . . . . . # . . . . # # . #
1705 dnl HP-UX 10.20 # . # . # # . ? . . # # . . . . # # ? #
1706 dnl IRIX 6.5 # . # # # # . # . . . # . . . . # . . .
1707 dnl OSF/1 5.1 # . # # # # . . . . . # . . . . # . . #
1708 dnl OSF/1 4.0d # . # # # # . . . . . # . . # # # # # #
1709 dnl NetBSD 9.0 . . . . # . . . . . . . . . . . . . . .
1710 dnl NetBSD 5.0 . . . # # . . . . . . # . # . . . . . .
1711 dnl NetBSD 4.0 . ? ? ? ? ? . ? . ? ? ? ? ? . . . ? ? ?
1712 dnl NetBSD 3.0 . . . . # # . ? # # ? # . # . . . . . .
1713 dnl Haiku . . . # # # . # . . . . . ? . . ? . . .
1714 dnl BeOS # # . # # # . ? # . ? . # ? . . ? . . .
1715 dnl Android 4.3 . . # # # # # # . # . # . # . . . # . .
1716 dnl old mingw / msvcrt # # # # # # . . # # . # # ? . # # # . .
1717 dnl MSVC 9 # # # # # # # . # # . # # ? # # # # . .
1718 dnl mingw 2009-2011 . # . # . . . . # # . . . ? . . . . . .
1719 dnl mingw-w64 2011 # # # # # # . . # # . # # ? . # # # . .