immutable: Add tests.
[gnulib.git] / m4 / printf.m4
blobc8e74e22cc722eabd298dd6ef935d4883cd5a6e8
1 # printf.m4 serial 71
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],
19     [
20       AC_RUN_IFELSE(
21         [AC_LANG_SOURCE([[
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #if HAVE_STDINT_H_WITH_UINTMAX
27 # include <stdint.h>
28 #endif
29 #if HAVE_INTTYPES_H_WITH_UINTMAX
30 # include <inttypes.h>
31 #endif
32 static char buf[100];
33 int main ()
35   int result = 0;
36 #if HAVE_STDINT_H_WITH_UINTMAX || HAVE_INTTYPES_H_WITH_UINTMAX
37   buf[0] = '\0';
38   if (sprintf (buf, "%ju %d", (uintmax_t) 12345671, 33, 44, 55) < 0
39       || strcmp (buf, "12345671 33") != 0)
40     result |= 1;
41 #else
42   result |= 1;
43 #endif
44   buf[0] = '\0';
45   if (sprintf (buf, "%zu %d", (size_t) 12345672, 33, 44, 55) < 0
46       || strcmp (buf, "12345672 33") != 0)
47     result |= 2;
48   buf[0] = '\0';
49   if (sprintf (buf, "%tu %d", (ptrdiff_t) 12345673, 33, 44, 55) < 0
50       || strcmp (buf, "12345673 33") != 0)
51     result |= 4;
52   buf[0] = '\0';
53   if (sprintf (buf, "%Lg %d", (long double) 1.5, 33, 44, 55) < 0
54       || strcmp (buf, "1.5 33") != 0)
55     result |= 8;
56   return result;
57 }]])],
58         [gl_cv_func_printf_sizes_c99=yes],
59         [gl_cv_func_printf_sizes_c99=no],
60         [
61          case "$host_os" in
62 changequote(,)dnl
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";;
86 changequote([,])dnl
87                                  # Guess yes on MSVC, no on mingw.
88            mingw*)               AC_EGREP_CPP([Known], [
89 #ifdef _MSC_VER
90  Known
91 #endif
92                                    ],
93                                    [gl_cv_func_printf_sizes_c99="guessing yes"],
94                                    [gl_cv_func_printf_sizes_c99="guessing no"])
95                                  ;;
96                                  # If we don't know, obey --enable-cross-guesses.
97            *)                    gl_cv_func_printf_sizes_c99="$gl_cross_guess_normal";;
98          esac
99         ])
100     ])
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],
113     [
114       AC_RUN_IFELSE(
115         [AC_LANG_SOURCE([[
116 #include <stdio.h>
117 #include <string.h>
118 static char buf[10000];
119 int main ()
121   int result = 0;
122   buf[0] = '\0';
123   if (sprintf (buf, "%Lf %d", 1.75L, 33, 44, 55) < 0
124       || strcmp (buf, "1.750000 33") != 0)
125     result |= 1;
126   buf[0] = '\0';
127   if (sprintf (buf, "%Le %d", 1.75L, 33, 44, 55) < 0
128       || strcmp (buf, "1.750000e+00 33") != 0)
129     result |= 2;
130   buf[0] = '\0';
131   if (sprintf (buf, "%Lg %d", 1.75L, 33, 44, 55) < 0
132       || strcmp (buf, "1.75 33") != 0)
133     result |= 4;
134   return result;
135 }]])],
136         [gl_cv_func_printf_long_double=yes],
137         [gl_cv_func_printf_long_double=no],
138         [case "$host_os" in
139                             # Guess no on BeOS.
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], [
145 #ifdef _MSC_VER
146  Known
147 #endif
148                               ],
149                               [gl_cv_func_printf_long_double="guessing yes"],
150                               [gl_cv_func_printf_long_double="guessing no"])
151                             ;;
152            *)               gl_cv_func_printf_long_double="guessing yes";;
153          esac
154         ])
155     ])
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],
169     [
170       AC_RUN_IFELSE(
171         [AC_LANG_SOURCE([[
172 #include <stdio.h>
173 #include <string.h>
174 static int
175 strisnan (const char *string, size_t start_index, size_t end_index)
177   if (start_index < end_index)
178     {
179       if (string[start_index] == '-')
180         start_index++;
181       if (start_index + 3 <= end_index
182           && memcmp (string + start_index, "nan", 3) == 0)
183         {
184           start_index += 3;
185           if (start_index == end_index
186               || (string[start_index] == '(' && string[end_index - 1] == ')'))
187             return 1;
188         }
189     }
190   return 0;
192 static int
193 have_minus_zero ()
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;
201 int main ()
203   int result = 0;
204   if (sprintf (buf, "%f", 1.0 / zero) < 0
205       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
206     result |= 1;
207   if (sprintf (buf, "%f", -1.0 / zero) < 0
208       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
209     result |= 1;
210   if (sprintf (buf, "%f", zero / zero) < 0
211       || !strisnan (buf, 0, strlen (buf)))
212     result |= 2;
213   if (sprintf (buf, "%e", 1.0 / zero) < 0
214       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
215     result |= 4;
216   if (sprintf (buf, "%e", -1.0 / zero) < 0
217       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
218     result |= 4;
219   if (sprintf (buf, "%e", zero / zero) < 0
220       || !strisnan (buf, 0, strlen (buf)))
221     result |= 8;
222   if (sprintf (buf, "%g", 1.0 / zero) < 0
223       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
224     result |= 16;
225   if (sprintf (buf, "%g", -1.0 / zero) < 0
226       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
227     result |= 16;
228   if (sprintf (buf, "%g", zero / zero) < 0
229       || !strisnan (buf, 0, strlen (buf)))
230     result |= 32;
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)
235     result |= 64;
236   return result;
237 }]])],
238         [gl_cv_func_printf_infinite=yes],
239         [gl_cv_func_printf_infinite=no],
240         [
241          case "$host_os" in
242 changequote(,)dnl
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";;
263                                  # Guess yes on BeOS.
264            beos*)                gl_cv_func_printf_infinite="guessing yes";;
265                                  # Guess no on Android.
266            linux*-android*)      gl_cv_func_printf_infinite="guessing no";;
267 changequote([,])dnl
268                                  # Guess yes on MSVC, no on mingw.
269            mingw*)               AC_EGREP_CPP([Known], [
270 #ifdef _MSC_VER
271  Known
272 #endif
273                                    ],
274                                    [gl_cv_func_printf_infinite="guessing yes"],
275                                    [gl_cv_func_printf_infinite="guessing no"])
276                                  ;;
277                                  # If we don't know, obey --enable-cross-guesses.
278            *)                    gl_cv_func_printf_infinite="$gl_cross_guess_normal";;
279          esac
280         ])
281     ])
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.])
301   fi
302   case "$gl_cv_func_printf_long_double" in
303     *yes)
304       AC_CACHE_CHECK([whether printf supports infinite 'long double' arguments],
305         [gl_cv_func_printf_infinite_long_double],
306         [
307           AC_RUN_IFELSE(
308             [AC_LANG_SOURCE([[
309 ]GL_NOCRASH[
310 #include <float.h>
311 #include <stdio.h>
312 #include <string.h>
313 static int
314 strisnan (const char *string, size_t start_index, size_t end_index)
316   if (start_index < end_index)
317     {
318       if (string[start_index] == '-')
319         start_index++;
320       if (start_index + 3 <= end_index
321           && memcmp (string + start_index, "nan", 3) == 0)
322         {
323           start_index += 3;
324           if (start_index == end_index
325               || (string[start_index] == '(' && string[end_index - 1] == ')'))
326             return 1;
327         }
328     }
329   return 0;
331 static char buf[10000];
332 static long double zeroL = 0.0L;
333 int main ()
335   int result = 0;
336   nocrash_init();
337   if (sprintf (buf, "%Lf", 1.0L / zeroL) < 0
338       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
339     result |= 1;
340   if (sprintf (buf, "%Lf", -1.0L / zeroL) < 0
341       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
342     result |= 1;
343   if (sprintf (buf, "%Lf", zeroL / zeroL) < 0
344       || !strisnan (buf, 0, strlen (buf)))
345     result |= 1;
346   if (sprintf (buf, "%Le", 1.0L / zeroL) < 0
347       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
348     result |= 1;
349   if (sprintf (buf, "%Le", -1.0L / zeroL) < 0
350       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
351     result |= 1;
352   if (sprintf (buf, "%Le", zeroL / zeroL) < 0
353       || !strisnan (buf, 0, strlen (buf)))
354     result |= 1;
355   if (sprintf (buf, "%Lg", 1.0L / zeroL) < 0
356       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
357     result |= 1;
358   if (sprintf (buf, "%Lg", -1.0L / zeroL) < 0
359       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
360     result |= 1;
361   if (sprintf (buf, "%Lg", zeroL / zeroL) < 0
362       || !strisnan (buf, 0, strlen (buf)))
363     result |= 1;
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                                        \
372      }
373 # else
374 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
375      { mantlo, manthi, exponent }
376 # endif
377   { /* Quiet NaN.  */
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)))
382       result |= 2;
383     if (sprintf (buf, "%Le", x.value) < 0
384         || !strisnan (buf, 0, strlen (buf)))
385       result |= 2;
386     if (sprintf (buf, "%Lg", x.value) < 0
387         || !strisnan (buf, 0, strlen (buf)))
388       result |= 2;
389   }
390   {
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)))
396       result |= 2;
397     if (sprintf (buf, "%Le", x.value) < 0
398         || !strisnan (buf, 0, strlen (buf)))
399       result |= 2;
400     if (sprintf (buf, "%Lg", x.value) < 0
401         || !strisnan (buf, 0, strlen (buf)))
402       result |= 2;
403   }
404   { /* Pseudo-NaN.  */
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)
408       result |= 4;
409     if (sprintf (buf, "%Le", x.value) <= 0)
410       result |= 4;
411     if (sprintf (buf, "%Lg", x.value) <= 0)
412       result |= 4;
413   }
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)
418       result |= 8;
419     if (sprintf (buf, "%Le", x.value) <= 0)
420       result |= 8;
421     if (sprintf (buf, "%Lg", x.value) <= 0)
422       result |= 8;
423   }
424   { /* Pseudo-Zero.  */
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)
428       result |= 16;
429     if (sprintf (buf, "%Le", x.value) <= 0)
430       result |= 16;
431     if (sprintf (buf, "%Lg", x.value) <= 0)
432       result |= 16;
433   }
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)
438       result |= 32;
439     if (sprintf (buf, "%Le", x.value) <= 0)
440       result |= 32;
441     if (sprintf (buf, "%Lg", x.value) <= 0)
442       result |= 32;
443   }
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)
448       result |= 64;
449     if (sprintf (buf, "%Le", x.value) <= 0)
450       result |= 64;
451     if (sprintf (buf, "%Lg", x.value) <= 0)
452       result |= 64;
453   }
454 #endif
455   return result;
456 }]])],
457             [gl_cv_func_printf_infinite_long_double=yes],
458             [gl_cv_func_printf_infinite_long_double=no],
459             [case "$host_cpu" in
460                                      # Guess no on ia64, x86_64, i386.
461                ia64 | x86_64 | i*86) gl_cv_func_printf_infinite_long_double="guessing no";;
462                *)
463                  case "$host_os" in
464 changequote(,)dnl
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";;
480 changequote([,])dnl
481                                          # Guess yes on MSVC, no on mingw.
482                    mingw*)               AC_EGREP_CPP([Known], [
483 #ifdef _MSC_VER
484  Known
485 #endif
486                                            ],
487                                            [gl_cv_func_printf_infinite_long_double="guessing yes"],
488                                            [gl_cv_func_printf_infinite_long_double="guessing no"])
489                                          ;;
490                                          # If we don't know, obey --enable-cross-guesses.
491                    *)                    gl_cv_func_printf_infinite_long_double="$gl_cross_guess_normal";;
492                  esac
493                  ;;
494              esac
495             ])
496         ])
497       ;;
498     *)
499       gl_cv_func_printf_infinite_long_double="irrelevant"
500       ;;
501   esac
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],
515     [
516       AC_RUN_IFELSE(
517         [AC_LANG_SOURCE([[
518 #include <stdio.h>
519 #include <string.h>
520 static char buf[100];
521 static double zero = 0.0;
522 int main ()
524   int result = 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))
530     result |= 1;
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))
536     result |= 2;
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))
543     result |= 4;
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))
550     result |= 4;
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
554       || buf[0] == '0')
555     result |= 8;
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))
562     result |= 16;
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))
570     result |= 32;
571   return result;
572 }]])],
573         [gl_cv_func_printf_directive_a=yes],
574         [gl_cv_func_printf_directive_a=no],
575         [
576          case "$host_os" in
577                                  # Guess yes on glibc >= 2.5 systems.
578            *-gnu* | gnu*)
579              AC_EGREP_CPP([BZ2908], [
580                #include <features.h>
581                #ifdef __GNU_LIBRARY__
582                 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)) && !defined __UCLIBC__
583                  BZ2908
584                 #endif
585                #endif
586                ],
587                [gl_cv_func_printf_directive_a="guessing yes"],
588                [gl_cv_func_printf_directive_a="guessing no"])
589              ;;
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";;
598          esac
599         ])
600     ])
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],
613     [
614       AC_RUN_IFELSE(
615         [AC_LANG_SOURCE([[
616 #include <stdio.h>
617 #include <string.h>
618 static char buf[100];
619 static double zero = 0.0;
620 int main ()
622   int result = 0;
623   if (sprintf (buf, "%F %d", 1234567.0, 33, 44, 55) < 0
624       || strcmp (buf, "1234567.000000 33") != 0)
625     result |= 1;
626   if (sprintf (buf, "%F", 1.0 / zero) < 0
627       || (strcmp (buf, "INF") != 0 && strcmp (buf, "INFINITY") != 0))
628     result |= 2;
629   /* This catches a Cygwin 1.5.x bug.  */
630   if (sprintf (buf, "%.F", 1234.0) < 0
631       || strcmp (buf, "1234") != 0)
632     result |= 4;
633   return result;
634 }]])],
635         [gl_cv_func_printf_directive_f=yes],
636         [gl_cv_func_printf_directive_f=no],
637         [
638          case "$host_os" in
639 changequote(,)dnl
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";;
658 changequote([,])dnl
659                                  # Guess yes on MSVC, no on mingw.
660            mingw*)               AC_EGREP_CPP([Known], [
661 #ifdef _MSC_VER
662  Known
663 #endif
664                                    ],
665                                    [gl_cv_func_printf_directive_f="guessing yes"],
666                                    [gl_cv_func_printf_directive_f="guessing no"])
667                                  ;;
668                                  # If we don't know, obey --enable-cross-guesses.
669            *)                    gl_cv_func_printf_directive_f="$gl_cross_guess_normal";;
670          esac
671         ])
672     ])
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],
685     [
686       AC_RUN_IFELSE(
687         [AC_LANG_SOURCE([[
688 #include <stdio.h>
689 #include <stdlib.h>
690 #include <string.h>
691 #ifdef _MSC_VER
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>  */
696 static void cdecl
697 invalid_parameter_handler (const wchar_t *expression,
698                            const wchar_t *function,
699                            const wchar_t *file, unsigned int line,
700                            uintptr_t dummy)
702   exit (1);
704 #endif
705 static char fmtstring[10];
706 static char buf[100];
707 int main ()
709   int count = -1;
710 #ifdef _MSC_VER
711   _set_invalid_parameter_handler (invalid_parameter_handler);
712 #endif
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
715      memory.  */
716   strcpy (fmtstring, "%d %n");
717   if (sprintf (buf, fmtstring, 123, &count, 33, 44, 55) < 0
718       || strcmp (buf, "123 ") != 0
719       || count != 4)
720     return 1;
721   return 0;
722 }]])],
723         [gl_cv_func_printf_directive_n=yes],
724         [gl_cv_func_printf_directive_n=no],
725         [case "$host_os" in
726                             # Guess no on glibc when _FORTIFY_SOURCE >= 2.
727            *-gnu* | gnu*)   AC_COMPILE_IFELSE(
728                               [AC_LANG_SOURCE(
729                                  [[#if _FORTIFY_SOURCE >= 2
730                                     error fail
731                                    #endif
732                                  ]])],
733                               [gl_cv_func_printf_directive_n="guessing yes"],
734                               [gl_cv_func_printf_directive_n="guessing no"])
735                             ;;
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";;
741          esac
742         ])
743     ])
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
750 dnl the precision.
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],
759     [
760       AC_RUN_IFELSE(
761         [AC_LANG_SOURCE([[
762 #include <stdio.h>
763 #include <wchar.h>
764 #include <string.h>
765 int main ()
767   int result = 0;
768   char buf[100];
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
771      Cygwin 1.5.  */
772   {
773     static const wchar_t wstring[] = { 'a', 'b', 'c', 0 };
774     buf[0] = '\0';
775     if (sprintf (buf, "%ls", wstring) < 0
776         || strcmp (buf, "abc") != 0)
777       result |= 1;
778   }
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.  */
781   {
782     static const wchar_t wstring[] = { 'a', 0 };
783     buf[0] = '\0';
784     if (sprintf (buf, "%ls", wstring) < 0
785         || strcmp (buf, "a") != 0)
786       result |= 2;
787   }
788   /* Test whether precisions in %ls are supported as specified in ISO C 99
789      section 7.19.6.1:
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.  */
796   {
797     static const wchar_t wstring[] = { 'a', 'b', (wchar_t) 0xfdfdfdfd, 0 };
798     buf[0] = '\0';
799     if (sprintf (buf, "%.2ls", wstring) < 0
800         || strcmp (buf, "ab") != 0)
801       result |= 8;
802   }
803   return result;
804 }]])],
805         [gl_cv_func_printf_directive_ls=yes],
806         [gl_cv_func_printf_directive_ls=no],
807         [
808 changequote(,)dnl
809          case "$host_os" in
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";;
822          esac
823 changequote([,])dnl
824         ])
825     ])
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],
838     [
839       AC_RUN_IFELSE(
840         [AC_LANG_SOURCE([[
841 #include <stdio.h>
842 #include <string.h>
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];
847 int main ()
849   sprintf (buf, format, 33, 55);
850   return (strcmp (buf, "55 33") != 0);
851 }]])],
852         [gl_cv_func_printf_positions=yes],
853         [gl_cv_func_printf_positions=no],
854         [
855 changequote(,)dnl
856          case "$host_os" in
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";;
865          esac
866 changequote([,])dnl
867         ])
868     ])
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],
881     [
882       AC_RUN_IFELSE(
883         [AC_LANG_SOURCE([[
884 #include <stdio.h>
885 #include <string.h>
886 static char buf[100];
887 int main ()
889   if (sprintf (buf, "%'d %d", 1234567, 99) < 0
890       || buf[strlen (buf) - 1] != '9')
891     return 1;
892   return 0;
893 }]])],
894         [gl_cv_func_printf_flag_grouping=yes],
895         [gl_cv_func_printf_flag_grouping=no],
896         [
897 changequote(,)dnl
898          case "$host_os" in
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";;
906          esac
907 changequote([,])dnl
908         ])
909     ])
912 dnl Test whether the *printf family of functions supports the - flag correctly.
913 dnl (ISO C99.) See
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],
923     [
924       AC_RUN_IFELSE(
925         [AC_LANG_SOURCE([[
926 #include <stdio.h>
927 #include <string.h>
928 static char buf[100];
929 int main ()
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)
934     return 1;
935   return 0;
936 }]])],
937         [gl_cv_func_printf_flag_leftadjust=yes],
938         [gl_cv_func_printf_flag_leftadjust=no],
939         [
940 changequote(,)dnl
941          case "$host_os" in
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";;
952          esac
953 changequote([,])dnl
954         ])
955     ])
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],
969     [
970       AC_RUN_IFELSE(
971         [AC_LANG_SOURCE([[
972 #include <stdio.h>
973 #include <string.h>
974 static char buf[100];
975 static double zero = 0.0;
976 int main ()
978   if (sprintf (buf, "%010f", 1.0 / zero, 33, 44, 55) < 0
979       || (strcmp (buf, "       inf") != 0
980           && strcmp (buf, "  infinity") != 0))
981     return 1;
982   return 0;
983 }]])],
984         [gl_cv_func_printf_flag_zero=yes],
985         [gl_cv_func_printf_flag_zero=no],
986         [
987 changequote(,)dnl
988          case "$host_os" in
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";;
993                             # Guess yes on BeOS.
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";;
1001          esac
1002 changequote([,])dnl
1003         ])
1004     ])
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],
1022     [
1023       AC_RUN_IFELSE(
1024         [AC_LANG_SOURCE([[
1025 #include <stdio.h>
1026 #include <string.h>
1027 static char buf[5000];
1028 int main ()
1030   int result = 0;
1031 #ifdef __BEOS__
1032   /* On BeOS, this would crash and show a dialog box.  Avoid the crash.  */
1033   return 1;
1034 #endif
1035   if (sprintf (buf, "%.4000d %d", 1, 33, 44) < 4000 + 3)
1036     result |= 1;
1037   if (sprintf (buf, "%.4000f %d", 1.0, 33, 44) < 4000 + 5)
1038     result |= 2;
1039   if (sprintf (buf, "%.511f %d", 1.0, 33, 44) < 511 + 5
1040       || buf[0] != '1')
1041     result |= 4;
1042   if (sprintf (buf, "%.999f %d", 1.0, 33, 44) < 999 + 5
1043       || buf[0] != '1')
1044     result |= 4;
1045   return result;
1046 }]])],
1047         [gl_cv_func_printf_precision=yes],
1048         [gl_cv_func_printf_precision=no],
1049         [
1050 changequote(,)dnl
1051          case "$host_os" in
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" ;;
1059          esac
1060 changequote([,])dnl
1061         ])
1062     ])
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],
1076     [
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([[
1081 ]GL_NOCRASH[
1082 #include <stdio.h>
1083 #include <sys/types.h>
1084 #include <sys/time.h>
1085 #include <sys/resource.h>
1086 #include <errno.h>
1087 int main()
1089   struct rlimit limit;
1090   int ret;
1091   nocrash_init ();
1092   /* Some printf implementations allocate temporary space with malloc.  */
1093   /* On BSD systems, malloc() is limited by RLIMIT_DATA.  */
1094 #ifdef RLIMIT_DATA
1095   if (getrlimit (RLIMIT_DATA, &limit) < 0)
1096     return 77;
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)
1101     return 77;
1102 #endif
1103   /* On Linux systems, malloc() is limited by RLIMIT_AS.  */
1104 #ifdef RLIMIT_AS
1105   if (getrlimit (RLIMIT_AS, &limit) < 0)
1106     return 77;
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)
1111     return 77;
1112 #endif
1113   /* Some printf implementations allocate temporary space on the stack.  */
1114 #ifdef RLIMIT_STACK
1115   if (getrlimit (RLIMIT_STACK, &limit) < 0)
1116     return 77;
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)
1121     return 77;
1122 #endif
1123   ret = printf ("%.5000000f", 1.0);
1124   return !(ret == 5000002 || (ret < 0 && errno == ENOMEM));
1126           ]])])
1127           if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
1128             (./conftest 2>&AS_MESSAGE_LOG_FD
1129              result=$?
1130              _AS_ECHO_LOG([\$? = $result])
1131              if test $result != 0 && test $result != 77; then result=1; fi
1132              exit $result
1133             ) >/dev/null 2>/dev/null
1134             case $? in
1135               0) gl_cv_func_printf_enomem="yes" ;;
1136               77) gl_cv_func_printf_enomem="guessing no" ;;
1137               *) gl_cv_func_printf_enomem="no" ;;
1138             esac
1139           else
1140             gl_cv_func_printf_enomem="guessing no"
1141           fi
1142           rm -fr conftest*
1143         else
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"
1148         fi
1149       fi
1150       if test "$gl_cv_func_printf_enomem" = "guessing no"; then
1151 changequote(,)dnl
1152         case "$host_os" in
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";;
1157                            # Guess yes on AIX.
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";;
1163                            esac
1164                            ;;
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";;
1177         esac
1178 changequote([,])dnl
1179       fi
1180     ])
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],
1202     [
1203       AC_RUN_IFELSE(
1204         [AC_LANG_SOURCE([[
1205 #include <stdio.h>
1206 #include <string.h>
1207 #if HAVE_SNPRINTF
1208 # define my_snprintf snprintf
1209 #else
1210 # include <stdarg.h>
1211 static int my_snprintf (char *buf, int size, const char *format, ...)
1213   va_list args;
1214   int ret;
1215   va_start (args, format);
1216   ret = vsnprintf (buf, size, format, args);
1217   va_end (args);
1218   return ret;
1220 #endif
1221 static char buf[100];
1222 int main ()
1224   strcpy (buf, "ABCDEF");
1225   my_snprintf (buf, 3, "%d %d", 4567, 89);
1226   if (memcmp (buf, "45\0DEF", 6) != 0)
1227     return 1;
1228   return 0;
1229 }]])],
1230         [gl_cv_func_snprintf_truncation_c99=yes],
1231         [gl_cv_func_snprintf_truncation_c99=no],
1232         [
1233 changequote(,)dnl
1234          case "$host_os" in
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";;
1276          esac
1277 changequote([,])dnl
1278         ])
1279     ])
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>
1288 dnl     int main()
1289 dnl     {
1290 dnl       static char buf[8];
1291 dnl       int retval = snprintf (buf, 3, "%d", 12345);
1292 dnl       return retval >= 0 && retval < 3;
1293 dnl     }
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],
1304     [
1305       AC_RUN_IFELSE(
1306         [AC_LANG_SOURCE([[
1307 #include <stdio.h>
1308 #include <string.h>
1309 #if HAVE_SNPRINTF
1310 # define my_snprintf snprintf
1311 #else
1312 # include <stdarg.h>
1313 static int my_snprintf (char *buf, int size, const char *format, ...)
1315   va_list args;
1316   int ret;
1317   va_start (args, format);
1318   ret = vsnprintf (buf, size, format, args);
1319   va_end (args);
1320   return ret;
1322 #endif
1323 static char buf[100];
1324 int main ()
1326   strcpy (buf, "ABCDEF");
1327   if (my_snprintf (buf, 3, "%d %d", 4567, 89) != 7)
1328     return 1;
1329   if (my_snprintf (buf, 0, "%d %d", 4567, 89) != 7)
1330     return 2;
1331   if (my_snprintf (NULL, 0, "%d %d", 4567, 89) != 7)
1332     return 3;
1333   return 0;
1334 }]])],
1335         [gl_cv_func_snprintf_retval_c99=yes],
1336         [gl_cv_func_snprintf_retval_c99=no],
1337         [case "$host_os" in
1338 changequote(,)dnl
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";;
1367 changequote([,])dnl
1368                                  # Guess yes on MSVC, no on mingw.
1369            mingw*)               AC_EGREP_CPP([Known], [
1370 #ifdef _MSC_VER
1371  Known
1372 #endif
1373                                    ],
1374                                    [gl_cv_func_snprintf_retval_c99="guessing yes"],
1375                                    [gl_cv_func_snprintf_retval_c99="guessing no"])
1376                                  ;;
1377                                  # If we don't know, obey --enable-cross-guesses.
1378            *)                    gl_cv_func_snprintf_retval_c99="$gl_cross_guess_normal";;
1379          esac
1380         ])
1381     ])
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],
1395     [
1396       AC_RUN_IFELSE(
1397         [AC_LANG_SOURCE([[
1398 #include <stdio.h>
1399 #include <string.h>
1400 #if HAVE_SNPRINTF
1401 # define my_snprintf snprintf
1402 #else
1403 # include <stdarg.h>
1404 static int my_snprintf (char *buf, int size, const char *format, ...)
1406   va_list args;
1407   int ret;
1408   va_start (args, format);
1409   ret = vsnprintf (buf, size, format, args);
1410   va_end (args);
1411   return ret;
1413 #endif
1414 static char fmtstring[10];
1415 static char buf[100];
1416 int main ()
1418   int count = -1;
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
1421      memory.  */
1422   strcpy (fmtstring, "%d %n");
1423   my_snprintf (buf, 4, fmtstring, 12345, &count, 33, 44, 55);
1424   if (count != 6)
1425     return 1;
1426   return 0;
1427 }]])],
1428         [gl_cv_func_snprintf_directive_n=yes],
1429         [gl_cv_func_snprintf_directive_n=no],
1430         [
1431          case "$host_os" in
1432                                  # Guess no on glibc when _FORTIFY_SOURCE >= 2.
1433            *-gnu* | gnu*)        AC_COMPILE_IFELSE(
1434                                    [AC_LANG_SOURCE(
1435                                       [[#if _FORTIFY_SOURCE >= 2
1436                                          error fail
1437                                         #endif
1438                                       ]])],
1439                                    [gl_cv_func_snprintf_directive_n="guessing yes"],
1440                                    [gl_cv_func_snprintf_directive_n="guessing no"])
1441                                  ;;
1442 changequote(,)dnl
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";;
1475 changequote([,])dnl
1476          esac
1477         ])
1478     ])
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],
1493     [
1494       AC_RUN_IFELSE(
1495         [AC_LANG_SOURCE([[
1496 #include <stdio.h>
1497 #if HAVE_SNPRINTF
1498 # define my_snprintf snprintf
1499 #else
1500 # include <stdarg.h>
1501 static int my_snprintf (char *buf, int size, const char *format, ...)
1503   va_list args;
1504   int ret;
1505   va_start (args, format);
1506   ret = vsnprintf (buf, size, format, args);
1507   va_end (args);
1508   return ret;
1510 #endif
1511 int main()
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';
1516 }]])],
1517         [gl_cv_func_snprintf_size1=yes],
1518         [gl_cv_func_snprintf_size1=no],
1519         [case "$host_os" in
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" ;;
1525          esac
1526         ])
1527     ])
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
1533 dnl on OSF/1 5.1:
1534 dnl     ---------------------------------------------------------------------
1535 dnl     #include <stdio.h>
1536 dnl     int main()
1537 dnl     {
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';
1541 dnl     }
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, ...)
1549 dnl     {
1550 dnl       va_list args;
1551 dnl       int ret;
1552 dnl       va_start (args, format);
1553 dnl       ret = vsnprintf (buf, size, format, args);
1554 dnl       va_end (args);
1555 dnl       return ret;
1556 dnl     }
1557 dnl     int main()
1558 dnl     {
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';
1562 dnl     }
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],
1572     [
1573       AC_RUN_IFELSE(
1574         [AC_LANG_SOURCE([[
1575 #include <stdarg.h>
1576 #include <stdio.h>
1577 static int my_snprintf (char *buf, int size, const char *format, ...)
1579   va_list args;
1580   int ret;
1581   va_start (args, format);
1582   ret = vsnprintf (buf, size, format, args);
1583   va_end (args);
1584   return ret;
1586 int main()
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';
1591 }]])],
1592         [gl_cv_func_vsnprintf_zerosize_c99=yes],
1593         [gl_cv_func_vsnprintf_zerosize_c99=no],
1594         [
1595 changequote(,)dnl
1596          case "$host_os" in
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";;
1630          esac
1631 changequote([,])dnl
1632         ])
1633     ])
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                 #  #  #  #  #  #  .  .  #  #  .  #  #  ?  .  #  #  #  .  .