a35c83378193c75e3cc729db7e487a6f12fd54c4
[gnulib.git] / m4 / printf.m4
1 # printf.m4 serial 19
2 dnl Copyright (C) 2003, 2007 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.
6
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.
10
11 AC_DEFUN([gl_PRINTF_SIZES_C99],
12 [
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_TRY_RUN([
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #if HAVE_STDINT_H_WITH_UINTMAX
26 # include <stdint.h>
27 #endif
28 #if HAVE_INTTYPES_H_WITH_UINTMAX
29 # include <inttypes.h>
30 #endif
31 static char buf[100];
32 int main ()
33 {
34 #if HAVE_STDINT_H_WITH_UINTMAX || HAVE_INTTYPES_H_WITH_UINTMAX
35   buf[0] = '\0';
36   if (sprintf (buf, "%ju %d", (uintmax_t) 12345671, 33, 44, 55) < 0
37       || strcmp (buf, "12345671 33") != 0)
38     return 1;
39 #endif
40   buf[0] = '\0';
41   if (sprintf (buf, "%zu %d", (size_t) 12345672, 33, 44, 55) < 0
42       || strcmp (buf, "12345672 33") != 0)
43     return 1;
44   buf[0] = '\0';
45   if (sprintf (buf, "%tu %d", (ptrdiff_t) 12345673, 33, 44, 55) < 0
46       || strcmp (buf, "12345673 33") != 0)
47     return 1;
48   buf[0] = '\0';
49   if (sprintf (buf, "%Lg %d", (long double) 1.5, 33, 44, 55) < 0
50       || strcmp (buf, "1.5 33") != 0)
51     return 1;
52   return 0;
53 }], [gl_cv_func_printf_sizes_c99=yes], [gl_cv_func_printf_sizes_c99=no],
54       [
55 changequote(,)dnl
56        case "$host_os" in
57                                # Guess yes on glibc systems.
58          *-gnu*)               gl_cv_func_printf_sizes_c99="guessing yes";;
59                                # Guess yes on FreeBSD >= 5.
60          freebsd[1-4]*)        gl_cv_func_printf_sizes_c99="guessing no";;
61          freebsd* | kfreebsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
62                                # Guess yes on MacOS X >= 10.3.
63          darwin[1-6].*)        gl_cv_func_printf_sizes_c99="guessing no";;
64          darwin*)              gl_cv_func_printf_sizes_c99="guessing yes";;
65                                # Guess yes on OpenBSD >= 3.9.
66          openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
67                                gl_cv_func_printf_sizes_c99="guessing no";;
68          openbsd*)             gl_cv_func_printf_sizes_c99="guessing yes";;
69                                # Guess yes on Solaris >= 2.10.
70          solaris2.[0-9]*)      gl_cv_func_printf_sizes_c99="guessing no";;
71          solaris*)             gl_cv_func_printf_sizes_c99="guessing yes";;
72                                # Guess yes on NetBSD >= 3.
73          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
74                                gl_cv_func_printf_sizes_c99="guessing no";;
75          netbsd*)              gl_cv_func_printf_sizes_c99="guessing yes";;
76                                # If we don't know, assume the worst.
77          *)                    gl_cv_func_printf_sizes_c99="guessing no";;
78        esac
79 changequote([,])dnl
80       ])
81     ])
82 ])
83
84 dnl Test whether the *printf family of functions supports 'long double'
85 dnl arguments together with the 'L' size specifier. (ISO C99, POSIX:2001)
86 dnl Result is gl_cv_func_printf_long_double.
87
88 AC_DEFUN([gl_PRINTF_LONG_DOUBLE],
89 [
90   AC_REQUIRE([AC_PROG_CC])
91   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
92   AC_CACHE_CHECK([whether printf supports 'long double' arguments],
93     [gl_cv_func_printf_long_double],
94     [
95       AC_TRY_RUN([
96 #include <stdio.h>
97 #include <string.h>
98 static char buf[10000];
99 int main ()
100 {
101   buf[0] = '\0';
102   if (sprintf (buf, "%Lf %d", 1.75L, 33, 44, 55) < 0
103       || strcmp (buf, "1.750000 33") != 0)
104     return 1;
105   buf[0] = '\0';
106   if (sprintf (buf, "%Le %d", 1.75L, 33, 44, 55) < 0
107       || strcmp (buf, "1.750000e+00 33") != 0)
108     return 1;
109   buf[0] = '\0';
110   if (sprintf (buf, "%Lg %d", 1.75L, 33, 44, 55) < 0
111       || strcmp (buf, "1.75 33") != 0)
112     return 1;
113   return 0;
114 }], [gl_cv_func_printf_long_double=yes], [gl_cv_func_printf_long_double=no],
115       [
116 changequote(,)dnl
117        case "$host_os" in
118          beos*)        gl_cv_func_printf_long_double="guessing no";;
119          mingw* | pw*) gl_cv_func_printf_long_double="guessing no";;
120          *)            gl_cv_func_printf_long_double="guessing yes";;
121        esac
122 changequote([,])dnl
123       ])
124     ])
125 ])
126
127 dnl Test whether the *printf family of functions supports infinite and NaN
128 dnl 'double' arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001)
129 dnl Result is gl_cv_func_printf_infinite.
130
131 AC_DEFUN([gl_PRINTF_INFINITE],
132 [
133   AC_REQUIRE([AC_PROG_CC])
134   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
135   AC_CACHE_CHECK([whether printf supports infinite 'double' arguments],
136     [gl_cv_func_printf_infinite],
137     [
138       AC_TRY_RUN([
139 #include <stdio.h>
140 #include <string.h>
141 static int
142 strisnan (const char *string, size_t start_index, size_t end_index)
143 {
144   if (start_index < end_index)
145     {
146       if (string[start_index] == '-')
147         start_index++;
148       if (start_index + 3 <= end_index
149           && memcmp (string + start_index, "nan", 3) == 0)
150         {
151           start_index += 3;
152           if (start_index == end_index
153               || (string[start_index] == '(' && string[end_index - 1] == ')'))
154             return 1;
155         }
156     }
157   return 0;
158 }
159 static char buf[10000];
160 static double zero = 0.0;
161 int main ()
162 {
163   if (sprintf (buf, "%f", 1.0 / 0.0) < 0
164       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
165     return 1;
166   if (sprintf (buf, "%f", -1.0 / 0.0) < 0
167       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
168     return 1;
169   if (sprintf (buf, "%f", zero / zero) < 0
170       || !strisnan (buf, 0, strlen (buf)))
171     return 1;
172   if (sprintf (buf, "%e", 1.0 / 0.0) < 0
173       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
174     return 1;
175   if (sprintf (buf, "%e", -1.0 / 0.0) < 0
176       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
177     return 1;
178   if (sprintf (buf, "%e", zero / zero) < 0
179       || !strisnan (buf, 0, strlen (buf)))
180     return 1;
181   if (sprintf (buf, "%g", 1.0 / 0.0) < 0
182       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
183     return 1;
184   if (sprintf (buf, "%g", -1.0 / 0.0) < 0
185       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
186     return 1;
187   if (sprintf (buf, "%g", zero / zero) < 0
188       || !strisnan (buf, 0, strlen (buf)))
189     return 1;
190   return 0;
191 }], [gl_cv_func_printf_infinite=yes], [gl_cv_func_printf_infinite=no],
192       [
193 changequote(,)dnl
194        case "$host_os" in
195                                # Guess yes on glibc systems.
196          *-gnu*)               gl_cv_func_printf_infinite="guessing yes";;
197                                # Guess yes on FreeBSD >= 6.
198          freebsd[1-5]*)        gl_cv_func_printf_infinite="guessing no";;
199          freebsd* | kfreebsd*) gl_cv_func_printf_infinite="guessing yes";;
200                                # Guess yes on MacOS X >= 10.3.
201          darwin[1-6].*)        gl_cv_func_printf_infinite="guessing no";;
202          darwin*)              gl_cv_func_printf_infinite="guessing yes";;
203                                # Guess yes on HP-UX >= 11.
204          hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite="guessing no";;
205          hpux*)                gl_cv_func_printf_infinite="guessing yes";;
206                                # Guess yes on NetBSD >= 3.
207          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
208                                gl_cv_func_printf_infinite="guessing no";;
209          netbsd*)              gl_cv_func_printf_infinite="guessing yes";;
210                                # Guess yes on BeOS.
211          beos*)                gl_cv_func_printf_infinite="guessing yes";;
212                                # If we don't know, assume the worst.
213          *)                    gl_cv_func_printf_infinite="guessing no";;
214        esac
215 changequote([,])dnl
216       ])
217     ])
218 ])
219
220 dnl Test whether the *printf family of functions supports infinite and NaN
221 dnl 'long double' arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001)
222 dnl Result is gl_cv_func_printf_infinite_long_double.
223
224 AC_DEFUN([gl_PRINTF_INFINITE_LONG_DOUBLE],
225 [
226   AC_REQUIRE([gl_PRINTF_LONG_DOUBLE])
227   AC_REQUIRE([AC_PROG_CC])
228   AC_REQUIRE([AC_C_BIGENDIAN])
229   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
230   dnl The user can set or unset the variable gl_printf_safe to indicate
231   dnl that he wishes a safe handling of non-IEEE-754 'long double' values.
232   if test -n "$gl_printf_safe"; then
233     AC_DEFINE([CHECK_PRINTF_SAFE], 1,
234       [Define if you wish *printf() functions that have a safe handling of
235        non-IEEE-754 'long double' values.])
236   fi
237   case "$gl_cv_func_printf_long_double" in
238     *yes)
239       AC_CACHE_CHECK([whether printf supports infinite 'long double' arguments],
240         [gl_cv_func_printf_infinite_long_double],
241         [
242           AC_TRY_RUN([
243 #include <float.h>
244 #include <stdio.h>
245 #include <string.h>
246 static int
247 strisnan (const char *string, size_t start_index, size_t end_index)
248 {
249   if (start_index < end_index)
250     {
251       if (string[start_index] == '-')
252         start_index++;
253       if (start_index + 3 <= end_index
254           && memcmp (string + start_index, "nan", 3) == 0)
255         {
256           start_index += 3;
257           if (start_index == end_index
258               || (string[start_index] == '(' && string[end_index - 1] == ')'))
259             return 1;
260         }
261     }
262   return 0;
263 }
264 static char buf[10000];
265 static long double zeroL = 0.0L;
266 int main ()
267 {
268   if (sprintf (buf, "%Lf", 1.0L / 0.0L) < 0
269       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
270     return 1;
271   if (sprintf (buf, "%Lf", -1.0L / 0.0L) < 0
272       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
273     return 1;
274   if (sprintf (buf, "%Lf", zeroL / zeroL) < 0
275       || !strisnan (buf, 0, strlen (buf)))
276     return 1;
277   if (sprintf (buf, "%Le", 1.0L / 0.0L) < 0
278       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
279     return 1;
280   if (sprintf (buf, "%Le", -1.0L / 0.0L) < 0
281       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
282     return 1;
283   if (sprintf (buf, "%Le", zeroL / zeroL) < 0
284       || !strisnan (buf, 0, strlen (buf)))
285     return 1;
286   if (sprintf (buf, "%Lg", 1.0L / 0.0L) < 0
287       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
288     return 1;
289   if (sprintf (buf, "%Lg", -1.0L / 0.0L) < 0
290       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
291     return 1;
292   if (sprintf (buf, "%Lg", zeroL / zeroL) < 0
293       || !strisnan (buf, 0, strlen (buf)))
294     return 1;
295 #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_))
296 /* Representation of an 80-bit 'long double' as an initializer for a sequence
297    of 'unsigned int' words.  */
298 # ifdef WORDS_BIGENDIAN
299 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
300      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
301        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
302        (unsigned int) (mantlo) << 16                                        \
303      }
304 # else
305 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
306      { mantlo, manthi, exponent }
307 # endif
308   { /* Quiet NaN.  */
309     static union { unsigned int word[4]; long double value; } x =
310       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
311     if (sprintf (buf, "%Lf", x.value) < 0
312         || !strisnan (buf, 0, strlen (buf)))
313       return 1;
314     if (sprintf (buf, "%Le", x.value) < 0
315         || !strisnan (buf, 0, strlen (buf)))
316       return 1;
317     if (sprintf (buf, "%Lg", x.value) < 0
318         || !strisnan (buf, 0, strlen (buf)))
319       return 1;
320   }
321   {
322     /* Signalling NaN.  */
323     static union { unsigned int word[4]; long double value; } x =
324       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
325     if (sprintf (buf, "%Lf", x.value) < 0
326         || !strisnan (buf, 0, strlen (buf)))
327       return 1;
328     if (sprintf (buf, "%Le", x.value) < 0
329         || !strisnan (buf, 0, strlen (buf)))
330       return 1;
331     if (sprintf (buf, "%Lg", x.value) < 0
332         || !strisnan (buf, 0, strlen (buf)))
333       return 1;
334   }
335   { /* Pseudo-NaN.  */
336     static union { unsigned int word[4]; long double value; } x =
337       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
338     if (sprintf (buf, "%Lf", x.value) < 0
339         || !strisnan (buf, 0, strlen (buf)))
340       return 1;
341     if (sprintf (buf, "%Le", x.value) < 0
342         || !strisnan (buf, 0, strlen (buf)))
343       return 1;
344     if (sprintf (buf, "%Lg", x.value) < 0
345         || !strisnan (buf, 0, strlen (buf)))
346       return 1;
347   }
348   { /* Pseudo-Infinity.  */
349     static union { unsigned int word[4]; long double value; } x =
350       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
351     if (sprintf (buf, "%Lf", x.value) < 0
352         || !strisnan (buf, 0, strlen (buf)))
353       return 1;
354     if (sprintf (buf, "%Le", x.value) < 0
355         || !strisnan (buf, 0, strlen (buf)))
356       return 1;
357     if (sprintf (buf, "%Lg", x.value) < 0
358         || !strisnan (buf, 0, strlen (buf)))
359       return 1;
360   }
361   { /* Pseudo-Zero.  */
362     static union { unsigned int word[4]; long double value; } x =
363       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
364     if (sprintf (buf, "%Lf", x.value) < 0
365         || !strisnan (buf, 0, strlen (buf)))
366       return 1;
367     if (sprintf (buf, "%Le", x.value) < 0
368         || !strisnan (buf, 0, strlen (buf)))
369       return 1;
370     if (sprintf (buf, "%Lg", x.value) < 0
371         || !strisnan (buf, 0, strlen (buf)))
372       return 1;
373   }
374   { /* Unnormalized number.  */
375     static union { unsigned int word[4]; long double value; } x =
376       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
377     if (sprintf (buf, "%Lf", x.value) < 0
378         || !strisnan (buf, 0, strlen (buf)))
379       return 1;
380     if (sprintf (buf, "%Le", x.value) < 0
381         || !strisnan (buf, 0, strlen (buf)))
382       return 1;
383     if (sprintf (buf, "%Lg", x.value) < 0
384         || !strisnan (buf, 0, strlen (buf)))
385       return 1;
386   }
387   { /* Pseudo-Denormal.  */
388     static union { unsigned int word[4]; long double value; } x =
389       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
390     if (sprintf (buf, "%Lf", x.value) < 0
391         || !strisnan (buf, 0, strlen (buf)))
392       return 1;
393     if (sprintf (buf, "%Le", x.value) < 0
394         || !strisnan (buf, 0, strlen (buf)))
395       return 1;
396     if (sprintf (buf, "%Lg", x.value) < 0
397         || !strisnan (buf, 0, strlen (buf)))
398       return 1;
399   }
400 #endif
401   return 0;
402 }],
403           [gl_cv_func_printf_infinite_long_double=yes],
404           [gl_cv_func_printf_infinite_long_double=no],
405           [
406 changequote(,)dnl
407            case "$host_cpu" in
408                                    # Guess no on ia64, x86_64, i386.
409              ia64 | x86_64 | i*86) gl_cv_func_printf_infinite_long_double="guessing no";;
410              *)
411                case "$host_os" in
412                                        # Guess yes on glibc systems.
413                  *-gnu*)               gl_cv_func_printf_infinite_long_double="guessing yes";;
414                                        # Guess yes on FreeBSD >= 6.
415                  freebsd[1-5]*)        gl_cv_func_printf_infinite_long_double="guessing no";;
416                  freebsd* | kfreebsd*) gl_cv_func_printf_infinite_long_double="guessing yes";;
417                                        # Guess yes on MacOS X >= 10.3.
418                  darwin[1-6].*)        gl_cv_func_printf_infinite_long_double="guessing no";;
419                  darwin*)              gl_cv_func_printf_infinite_long_double="guessing yes";;
420                                        # Guess yes on HP-UX >= 11.
421                  hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite_long_double="guessing no";;
422                  hpux*)                gl_cv_func_printf_infinite_long_double="guessing yes";;
423                                        # Guess yes on NetBSD >= 3.
424                  netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
425                                        gl_cv_func_printf_infinite_long_double="guessing no";;
426                  netbsd*)              gl_cv_func_printf_infinite_long_double="guessing yes";;
427                                        # If we don't know, assume the worst.
428                  *)                    gl_cv_func_printf_infinite_long_double="guessing no";;
429                esac
430                ;;
431            esac
432 changequote([,])dnl
433           ])
434         ])
435       ;;
436     *)
437       gl_cv_func_printf_infinite_long_double="irrelevant"
438       ;;
439   esac
440 ])
441
442 dnl Test whether the *printf family of functions supports the 'a' and 'A'
443 dnl conversion specifier for hexadecimal output of floating-point numbers.
444 dnl (ISO C99, POSIX:2001)
445 dnl Result is gl_cv_func_printf_directive_a.
446
447 AC_DEFUN([gl_PRINTF_DIRECTIVE_A],
448 [
449   AC_REQUIRE([AC_PROG_CC])
450   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
451   AC_CACHE_CHECK([whether printf supports the 'a' and 'A' directives],
452     [gl_cv_func_printf_directive_a],
453     [
454       AC_TRY_RUN([
455 #include <stdio.h>
456 #include <string.h>
457 static char buf[100];
458 int main ()
459 {
460   if (sprintf (buf, "%a %d", 3.1416015625, 33, 44, 55) < 0
461       || (strcmp (buf, "0x1.922p+1 33") != 0
462           && strcmp (buf, "0x3.244p+0 33") != 0
463           && strcmp (buf, "0x6.488p-1 33") != 0
464           && strcmp (buf, "0xc.91p-2 33") != 0))
465     return 1;
466   if (sprintf (buf, "%A %d", -3.1416015625, 33, 44, 55) < 0
467       || (strcmp (buf, "-0X1.922P+1 33") != 0
468           && strcmp (buf, "-0X3.244P+0 33") != 0
469           && strcmp (buf, "-0X6.488P-1 33") != 0
470           && strcmp (buf, "-0XC.91P-2 33") != 0))
471     return 1;
472   /* This catches a FreeBSD 6.1 bug: it doesn't round.  */
473   if (sprintf (buf, "%.2a %d", 1.51, 33, 44, 55) < 0
474       || (strcmp (buf, "0x1.83p+0 33") != 0
475           && strcmp (buf, "0x3.05p-1 33") != 0
476           && strcmp (buf, "0x6.0ap-2 33") != 0
477           && strcmp (buf, "0xc.14p-3 33") != 0))
478     return 1;
479   /* This catches a FreeBSD 6.1 bug.  See
480      <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
481   if (sprintf (buf, "%010a %d", 1.0 / 0.0, 33, 44, 55) < 0
482       || buf[0] == '0')
483     return 1;
484   /* This catches a MacOS X 10.3.9 (Darwin 7.9) bug.  */
485   if (sprintf (buf, "%.1a", 1.999) < 0
486       || (strcmp (buf, "0x1.0p+1") != 0
487           && strcmp (buf, "0x2.0p+0") != 0
488           && strcmp (buf, "0x4.0p-1") != 0
489           && strcmp (buf, "0x8.0p-2") != 0))
490     return 1;
491   /* This catches the same MacOS X 10.3.9 (Darwin 7.9) bug and also a
492      glibc 2.4 bug <http://sourceware.org/bugzilla/show_bug.cgi?id=2908>.  */
493   if (sprintf (buf, "%.1La", 1.999L) < 0
494       || (strcmp (buf, "0x1.0p+1") != 0
495           && strcmp (buf, "0x2.0p+0") != 0
496           && strcmp (buf, "0x4.0p-1") != 0
497           && strcmp (buf, "0x8.0p-2") != 0))
498     return 1;
499   return 0;
500 }], [gl_cv_func_printf_directive_a=yes], [gl_cv_func_printf_directive_a=no],
501       [
502        case "$host_os" in
503                                # Guess yes on glibc >= 2.5 systems.
504          *-gnu*)
505            AC_EGREP_CPP([BZ2908], [
506              #include <features.h>
507              #ifdef __GNU_LIBRARY__
508               #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)
509                BZ2908
510               #endif
511              #endif
512              ],
513              [gl_cv_func_printf_directive_a="guessing yes"],
514              [gl_cv_func_printf_directive_a="guessing no"])
515            ;;
516                                # If we don't know, assume the worst.
517          *)                    gl_cv_func_printf_directive_a="guessing no";;
518        esac
519       ])
520     ])
521 ])
522
523 dnl Test whether the *printf family of functions supports the %F format
524 dnl directive. (ISO C99, POSIX:2001)
525 dnl Result is gl_cv_func_printf_directive_f.
526
527 AC_DEFUN([gl_PRINTF_DIRECTIVE_F],
528 [
529   AC_REQUIRE([AC_PROG_CC])
530   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
531   AC_CACHE_CHECK([whether printf supports the 'F' directive],
532     [gl_cv_func_printf_directive_f],
533     [
534       AC_TRY_RUN([
535 #include <stdio.h>
536 #include <string.h>
537 static char buf[100];
538 int main ()
539 {
540   if (sprintf (buf, "%F %d", 1234567.0, 33, 44, 55) < 0
541       || strcmp (buf, "1234567.000000 33") != 0)
542     return 1;
543   if (sprintf (buf, "%F", 1.0 / 0.0) < 0
544       || (strcmp (buf, "INF") != 0 && strcmp (buf, "INFINITY") != 0))
545     return 1;
546   /* This catches a Cygwin 2007 bug.  */
547   if (sprintf (buf, "%.F", 1234.0) < 0
548       || strcmp (buf, "1234") != 0)
549     return 1;
550   return 0;
551 }], [gl_cv_func_printf_directive_f=yes], [gl_cv_func_printf_directive_f=no],
552       [
553 changequote(,)dnl
554        case "$host_os" in
555                                # Guess yes on glibc systems.
556          *-gnu*)               gl_cv_func_printf_directive_f="guessing yes";;
557                                # Guess yes on FreeBSD >= 6.
558          freebsd[1-5]*)        gl_cv_func_printf_directive_f="guessing no";;
559          freebsd* | kfreebsd*) gl_cv_func_printf_directive_f="guessing yes";;
560                                # Guess yes on MacOS X >= 10.3.
561          darwin[1-6].*)        gl_cv_func_printf_directive_f="guessing no";;
562          darwin*)              gl_cv_func_printf_directive_f="guessing yes";;
563                                # Guess yes on Solaris >= 2.10.
564          solaris2.[0-9]*)      gl_cv_func_printf_directive_f="guessing no";;
565          solaris*)             gl_cv_func_printf_directive_f="guessing yes";;
566                                # If we don't know, assume the worst.
567          *)                    gl_cv_func_printf_directive_f="guessing no";;
568        esac
569 changequote([,])dnl
570       ])
571     ])
572 ])
573
574 dnl Test whether the *printf family of functions supports the %n format
575 dnl directive. (ISO C99, POSIX:2001)
576 dnl Result is gl_cv_func_printf_directive_n.
577
578 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
579 [
580   AC_REQUIRE([AC_PROG_CC])
581   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
582   AC_CACHE_CHECK([whether printf supports the 'n' directive],
583     [gl_cv_func_printf_directive_n],
584     [
585       AC_TRY_RUN([
586 #include <stdio.h>
587 #include <string.h>
588 static char fmtstring[10];
589 static char buf[100];
590 int main ()
591 {
592   int count = -1;
593   /* Copy the format string.  Some systems (glibc with _FORTIFY_SOURCE=2)
594      support %n in format strings in read-only memory but not in writable
595      memory.  */
596   strcpy (fmtstring, "%d %n");
597   if (sprintf (buf, fmtstring, 123, &count, 33, 44, 55) < 0
598       || strcmp (buf, "123 ") != 0
599       || count != 4)
600     return 1;
601   return 0;
602 }], [gl_cv_func_printf_directive_n=yes], [gl_cv_func_printf_directive_n=no],
603       [
604 changequote(,)dnl
605        case "$host_os" in
606          *)     gl_cv_func_printf_directive_n="guessing yes";;
607        esac
608 changequote([,])dnl
609       ])
610     ])
611 ])
612
613 dnl Test whether the *printf family of functions supports POSIX/XSI format
614 dnl strings with positions. (POSIX:2001)
615 dnl Result is gl_cv_func_printf_positions.
616
617 AC_DEFUN([gl_PRINTF_POSITIONS],
618 [
619   AC_REQUIRE([AC_PROG_CC])
620   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
621   AC_CACHE_CHECK([whether printf supports POSIX/XSI format strings with positions],
622     [gl_cv_func_printf_positions],
623     [
624       AC_TRY_RUN([
625 #include <stdio.h>
626 #include <string.h>
627 /* The string "%2$d %1$d", with dollar characters protected from the shell's
628    dollar expansion (possibly an autoconf bug).  */
629 static char format[] = { '%', '2', '$', 'd', ' ', '%', '1', '$', 'd', '\0' };
630 static char buf[100];
631 int main ()
632 {
633   sprintf (buf, format, 33, 55);
634   return (strcmp (buf, "55 33") != 0);
635 }], [gl_cv_func_printf_positions=yes], [gl_cv_func_printf_positions=no],
636       [
637 changequote(,)dnl
638        case "$host_os" in
639          netbsd[1-3]* | netbsdelf[1-3]* | netbsdaout[1-3]* | netbsdcoff[1-3]*)
640                        gl_cv_func_printf_positions="guessing no";;
641          beos*)        gl_cv_func_printf_positions="guessing no";;
642          mingw* | pw*) gl_cv_func_printf_positions="guessing no";;
643          *)            gl_cv_func_printf_positions="guessing yes";;
644        esac
645 changequote([,])dnl
646       ])
647     ])
648 ])
649
650 dnl Test whether the *printf family of functions supports POSIX/XSI format
651 dnl strings with the ' flag for grouping of decimal digits. (POSIX:2001)
652 dnl Result is gl_cv_func_printf_flag_grouping.
653
654 AC_DEFUN([gl_PRINTF_FLAG_GROUPING],
655 [
656   AC_REQUIRE([AC_PROG_CC])
657   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
658   AC_CACHE_CHECK([whether printf supports the grouping flag],
659     [gl_cv_func_printf_flag_grouping],
660     [
661       AC_TRY_RUN([
662 #include <stdio.h>
663 #include <string.h>
664 static char buf[100];
665 int main ()
666 {
667   if (sprintf (buf, "%'d %d", 1234567, 99) < 0
668       || buf[strlen (buf) - 1] != '9')
669     return 1;
670   return 0;
671 }], [gl_cv_func_printf_flag_grouping=yes], [gl_cv_func_printf_flag_grouping=no],
672       [
673 changequote(,)dnl
674        case "$host_os" in
675          cygwin*)      gl_cv_func_printf_flag_grouping="guessing no";;
676          netbsd*)      gl_cv_func_printf_flag_grouping="guessing no";;
677          mingw* | pw*) gl_cv_func_printf_flag_grouping="guessing no";;
678          *)            gl_cv_func_printf_flag_grouping="guessing yes";;
679        esac
680 changequote([,])dnl
681       ])
682     ])
683 ])
684
685 dnl Test whether the *printf family of functions supports padding of non-finite
686 dnl values with the 0 flag correctly. (ISO C99 + TC1 + TC2.) See
687 dnl <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html>
688 dnl Result is gl_cv_func_printf_flag_zero.
689
690 AC_DEFUN([gl_PRINTF_FLAG_ZERO],
691 [
692   AC_REQUIRE([AC_PROG_CC])
693   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
694   AC_CACHE_CHECK([whether printf supports the zero flag correctly],
695     [gl_cv_func_printf_flag_zero],
696     [
697       AC_TRY_RUN([
698 #include <stdio.h>
699 #include <string.h>
700 static char buf[100];
701 int main ()
702 {
703   if (sprintf (buf, "%010f", 1.0 / 0.0, 33, 44, 55) < 0
704       || (strcmp (buf, "       inf") != 0
705           && strcmp (buf, "  infinity") != 0))
706     return 1;
707   return 0;
708 }], [gl_cv_func_printf_flag_zero=yes], [gl_cv_func_printf_flag_zero=no],
709       [
710 changequote(,)dnl
711        case "$host_os" in
712                  # Guess yes on glibc systems.
713          *-gnu*) gl_cv_func_printf_flag_zero="guessing yes";;
714                  # Guess yes on BeOS.
715          beos*)  gl_cv_func_printf_flag_zero="guessing yes";;
716                  # If we don't know, assume the worst.
717          *)      gl_cv_func_printf_flag_zero="guessing no";;
718        esac
719 changequote([,])dnl
720       ])
721     ])
722 ])
723
724 dnl Test whether the *printf family of functions supports large precisions.
725 dnl On mingw, precisions larger than 512 are treated like 512, in integer,
726 dnl floating-point or pointer output. On BeOS, precisions larger than 1044
727 dnl crash the program.
728 dnl Result is gl_cv_func_printf_precision.
729
730 AC_DEFUN([gl_PRINTF_PRECISION],
731 [
732   AC_REQUIRE([AC_PROG_CC])
733   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
734   AC_CACHE_CHECK([whether printf supports large precisions],
735     [gl_cv_func_printf_precision],
736     [
737       AC_TRY_RUN([
738 #include <stdio.h>
739 #include <string.h>
740 static char buf[5000];
741 int main ()
742 {
743 #ifdef __BEOS__
744   /* On BeOS, this would crash and show a dialog box.  Avoid the crash.  */
745   return 1;
746 #endif
747   if (sprintf (buf, "%.4000d %d", 1, 33, 44) < 4000 + 3)
748     return 1;
749   return 0;
750 }], [gl_cv_func_printf_precision=yes], [gl_cv_func_printf_precision=no],
751       [
752 changequote(,)dnl
753        case "$host_os" in
754          # Guess no only on native Win32 and BeOS systems.
755          mingw* | pw*) gl_cv_func_printf_precision="guessing no" ;;
756          beos*)        gl_cv_func_printf_precision="guessing no" ;;
757          *)            gl_cv_func_printf_precision="guessing yes" ;;
758        esac
759 changequote([,])dnl
760       ])
761     ])
762 ])
763
764 dnl Test whether the *printf family of functions recovers gracefully in case
765 dnl of an out-of-memory condition, or whether it crashes the entire program.
766 dnl Result is gl_cv_func_printf_enomem.
767
768 AC_DEFUN([gl_PRINTF_ENOMEM],
769 [
770   AC_REQUIRE([AC_PROG_CC])
771   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
772   AC_CACHE_CHECK([whether printf survives out-of-memory conditions],
773     [gl_cv_func_printf_enomem],
774     [
775       if test "$cross_compiling" = no; then
776         AC_LANG_CONFTEST([AC_LANG_SOURCE([
777 changequote(,)dnl
778 #include <stdio.h>
779 #include <sys/types.h>
780 #include <sys/time.h>
781 #include <sys/resource.h>
782 #include <errno.h>
783 int main()
784 {
785   struct rlimit limit;
786   int ret;
787   /* Some printf implementations allocate temporary space with malloc.  */
788   /* On BSD systems, malloc() is limited by RLIMIT_DATA.  */
789 #ifdef RLIMIT_DATA
790   if (getrlimit (RLIMIT_DATA, &limit) < 0)
791     return 77;
792   if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
793     limit.rlim_max = 5000000;
794   limit.rlim_cur = limit.rlim_max;
795   if (setrlimit (RLIMIT_DATA, &limit) < 0)
796     return 77;
797 #endif
798   /* On Linux systems, malloc() is limited by RLIMIT_AS.  */
799 #ifdef RLIMIT_AS
800   if (getrlimit (RLIMIT_AS, &limit) < 0)
801     return 77;
802   if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
803     limit.rlim_max = 5000000;
804   limit.rlim_cur = limit.rlim_max;
805   if (setrlimit (RLIMIT_AS, &limit) < 0)
806     return 77;
807 #endif
808   /* Some printf implementations allocate temporary space on the stack.  */
809 #ifdef RLIMIT_STACK
810   if (getrlimit (RLIMIT_STACK, &limit) < 0)
811     return 77;
812   if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
813     limit.rlim_max = 5000000;
814   limit.rlim_cur = limit.rlim_max;
815   if (setrlimit (RLIMIT_STACK, &limit) < 0)
816     return 77;
817 #endif
818   ret = printf ("%.5000000f", 1.0);
819   return !(ret == 5000002 || (ret < 0 && errno == ENOMEM));
820 }
821 changequote([,])dnl
822           ])])
823         if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
824           (./conftest
825            result=$?
826            if test $result != 0 && test $result != 77; then result=1; fi
827            exit $result
828           ) >/dev/null 2>/dev/null
829           case $? in
830             0) gl_cv_func_printf_enomem="yes" ;;
831             77) gl_cv_func_printf_enomem="guessing no" ;;
832             *) gl_cv_func_printf_enomem="no" ;;
833           esac
834         else
835           gl_cv_func_printf_enomem="guessing no"
836         fi
837         rm -fr conftest*
838       else
839 changequote(,)dnl
840         case "$host_os" in
841                     # Guess yes on glibc systems.
842           *-gnu*)   gl_cv_func_printf_enomem="guessing yes";;
843                     # Guess yes on Solaris.
844           solaris*) gl_cv_func_printf_enomem="guessing yes";;
845                     # Guess yes on AIX.
846           aix*)     gl_cv_func_printf_enomem="guessing yes";;
847                     # Guess yes on HP-UX/hppa.
848           hpux*)    case "$host_cpu" in
849                       hppa*) gl_cv_func_printf_enomem="guessing yes";;
850                       *)     gl_cv_func_printf_enomem="guessing no";;
851                     esac
852                     ;;
853                     # Guess yes on IRIX.
854           irix*)    gl_cv_func_printf_enomem="guessing yes";;
855                     # Guess yes on OSF/1.
856           osf*)     gl_cv_func_printf_enomem="guessing yes";;
857                     # Guess yes on BeOS.
858           beos*)    gl_cv_func_printf_enomem="guessing yes";;
859                     # If we don't know, assume the worst.
860           *)        gl_cv_func_printf_enomem="guessing no";;
861         esac
862 changequote([,])dnl
863       fi
864     ])
865 ])
866
867 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
868 dnl Result is ac_cv_func_snprintf.
869
870 AC_DEFUN([gl_SNPRINTF_PRESENCE],
871 [
872   AC_CHECK_FUNCS_ONCE([snprintf])
873 ])
874
875 dnl Test whether the string produced by the snprintf function is always NUL
876 dnl terminated. (ISO C99, POSIX:2001)
877 dnl Result is gl_cv_func_snprintf_truncation_c99.
878
879 AC_DEFUN([gl_SNPRINTF_TRUNCATION_C99],
880 [
881   AC_REQUIRE([AC_PROG_CC])
882   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
883   AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
884     [gl_cv_func_snprintf_truncation_c99],
885     [
886       AC_TRY_RUN([
887 #include <stdio.h>
888 #include <string.h>
889 static char buf[100];
890 int main ()
891 {
892   strcpy (buf, "ABCDEF");
893   snprintf (buf, 3, "%d %d", 4567, 89);
894   if (memcmp (buf, "45\0DEF", 6) != 0)
895     return 1;
896   return 0;
897 }], [gl_cv_func_snprintf_truncation_c99=yes], [gl_cv_func_snprintf_truncation_c99=no],
898       [
899 changequote(,)dnl
900        case "$host_os" in
901                                # Guess yes on glibc systems.
902          *-gnu*)               gl_cv_func_snprintf_truncation_c99="guessing yes";;
903                                # Guess yes on FreeBSD >= 5.
904          freebsd[1-4]*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
905          freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
906                                # Guess yes on MacOS X >= 10.3.
907          darwin[1-6].*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
908          darwin*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
909                                # Guess yes on OpenBSD >= 3.9.
910          openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
911                                gl_cv_func_snprintf_truncation_c99="guessing no";;
912          openbsd*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
913                                # Guess yes on Solaris >= 2.6.
914          solaris2.[0-5]*)      gl_cv_func_snprintf_truncation_c99="guessing no";;
915          solaris*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
916                                # Guess yes on AIX >= 4.
917          aix[1-3]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
918          aix*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
919                                # Guess yes on HP-UX >= 11.
920          hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
921          hpux*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
922                                # Guess yes on IRIX >= 6.5.
923          irix6.5)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
924                                # Guess yes on OSF/1 >= 5.
925          osf[3-4]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
926          osf*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
927                                # Guess yes on NetBSD >= 3.
928          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
929                                gl_cv_func_snprintf_truncation_c99="guessing no";;
930          netbsd*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
931                                # Guess yes on BeOS.
932          beos*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
933                                # If we don't know, assume the worst.
934          *)                    gl_cv_func_snprintf_truncation_c99="guessing no";;
935        esac
936 changequote([,])dnl
937       ])
938     ])
939 ])
940
941 dnl Test whether the return value of the snprintf function is the number
942 dnl of bytes (excluding the terminating NUL) that would have been produced
943 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
944 dnl For example, this test program fails on IRIX 6.5:
945 dnl     ---------------------------------------------------------------------
946 dnl     #include <stdio.h>
947 dnl     int main()
948 dnl     {
949 dnl       static char buf[8];
950 dnl       int retval = snprintf (buf, 3, "%d", 12345);
951 dnl       return retval >= 0 && retval < 3;
952 dnl     }
953 dnl     ---------------------------------------------------------------------
954 dnl Result is gl_cv_func_snprintf_retval_c99.
955
956 AC_DEFUN([gl_SNPRINTF_RETVAL_C99],
957 [
958   AC_REQUIRE([AC_PROG_CC])
959   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
960   AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
961     [gl_cv_func_snprintf_retval_c99],
962     [
963       AC_TRY_RUN([
964 #include <stdio.h>
965 #include <string.h>
966 static char buf[100];
967 int main ()
968 {
969   strcpy (buf, "ABCDEF");
970   if (snprintf (buf, 3, "%d %d", 4567, 89) != 7)
971     return 1;
972   return 0;
973 }], [gl_cv_func_snprintf_retval_c99=yes], [gl_cv_func_snprintf_retval_c99=no],
974       [
975 changequote(,)dnl
976        case "$host_os" in
977                                # Guess yes on glibc systems.
978          *-gnu*)               gl_cv_func_snprintf_retval_c99="guessing yes";;
979                                # Guess yes on FreeBSD >= 5.
980          freebsd[1-4]*)        gl_cv_func_snprintf_retval_c99="guessing no";;
981          freebsd* | kfreebsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
982                                # Guess yes on MacOS X >= 10.3.
983          darwin[1-6].*)        gl_cv_func_snprintf_retval_c99="guessing no";;
984          darwin*)              gl_cv_func_snprintf_retval_c99="guessing yes";;
985                                # Guess yes on OpenBSD >= 3.9.
986          openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
987                                gl_cv_func_snprintf_retval_c99="guessing no";;
988          openbsd*)             gl_cv_func_snprintf_retval_c99="guessing yes";;
989                                # Guess yes on Solaris >= 2.6.
990          solaris2.[0-5]*)      gl_cv_func_snprintf_retval_c99="guessing no";;
991          solaris*)             gl_cv_func_snprintf_retval_c99="guessing yes";;
992                                # Guess yes on AIX >= 4.
993          aix[1-3]*)            gl_cv_func_snprintf_retval_c99="guessing no";;
994          aix*)                 gl_cv_func_snprintf_retval_c99="guessing yes";;
995                                # Guess yes on NetBSD >= 3.
996          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
997                                gl_cv_func_snprintf_retval_c99="guessing no";;
998          netbsd*)              gl_cv_func_snprintf_retval_c99="guessing yes";;
999                                # Guess yes on BeOS.
1000          beos*)                gl_cv_func_snprintf_retval_c99="guessing yes";;
1001                                # If we don't know, assume the worst.
1002          *)                    gl_cv_func_snprintf_retval_c99="guessing no";;
1003        esac
1004 changequote([,])dnl
1005       ])
1006     ])
1007 ])
1008
1009 dnl Test whether the snprintf function supports the %n format directive
1010 dnl also in truncated portions of the format string. (ISO C99, POSIX:2001)
1011 dnl Result is gl_cv_func_snprintf_directive_n.
1012
1013 AC_DEFUN([gl_SNPRINTF_DIRECTIVE_N],
1014 [
1015   AC_REQUIRE([AC_PROG_CC])
1016   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1017   AC_CACHE_CHECK([whether snprintf fully supports the 'n' directive],
1018     [gl_cv_func_snprintf_directive_n],
1019     [
1020       AC_TRY_RUN([
1021 #include <stdio.h>
1022 #include <string.h>
1023 static char fmtstring[10];
1024 static char buf[100];
1025 int main ()
1026 {
1027   int count = -1;
1028   /* Copy the format string.  Some systems (glibc with _FORTIFY_SOURCE=2)
1029      support %n in format strings in read-only memory but not in writable
1030      memory.  */
1031   strcpy (fmtstring, "%d %n");
1032   snprintf (buf, 4, fmtstring, 12345, &count, 33, 44, 55);
1033   if (count != 6)
1034     return 1;
1035   return 0;
1036 }], [gl_cv_func_snprintf_directive_n=yes], [gl_cv_func_snprintf_directive_n=no],
1037       [
1038 changequote(,)dnl
1039        case "$host_os" in
1040                                # Guess yes on glibc systems.
1041          *-gnu*)               gl_cv_func_snprintf_directive_n="guessing yes";;
1042                                # Guess yes on FreeBSD >= 5.
1043          freebsd[1-4]*)        gl_cv_func_snprintf_directive_n="guessing no";;
1044          freebsd* | kfreebsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1045                                # Guess yes on MacOS X >= 10.3.
1046          darwin[1-6].*)        gl_cv_func_snprintf_directive_n="guessing no";;
1047          darwin*)              gl_cv_func_snprintf_directive_n="guessing yes";;
1048                                # Guess yes on Solaris >= 2.6.
1049          solaris2.[0-5]*)      gl_cv_func_snprintf_directive_n="guessing no";;
1050          solaris*)             gl_cv_func_snprintf_directive_n="guessing yes";;
1051                                # Guess yes on AIX >= 4.
1052          aix[1-3]*)            gl_cv_func_snprintf_directive_n="guessing no";;
1053          aix*)                 gl_cv_func_snprintf_directive_n="guessing yes";;
1054                                # Guess yes on IRIX >= 6.5.
1055          irix6.5)              gl_cv_func_snprintf_directive_n="guessing yes";;
1056                                # Guess yes on OSF/1 >= 5.
1057          osf[3-4]*)            gl_cv_func_snprintf_directive_n="guessing no";;
1058          osf*)                 gl_cv_func_snprintf_directive_n="guessing yes";;
1059                                # Guess yes on NetBSD >= 3.
1060          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1061                                gl_cv_func_snprintf_directive_n="guessing no";;
1062          netbsd*)              gl_cv_func_snprintf_directive_n="guessing yes";;
1063                                # Guess yes on BeOS.
1064          beos*)                gl_cv_func_snprintf_directive_n="guessing yes";;
1065                                # If we don't know, assume the worst.
1066          *)                    gl_cv_func_snprintf_directive_n="guessing no";;
1067        esac
1068 changequote([,])dnl
1069       ])
1070     ])
1071 ])
1072
1073 dnl Test whether the vsnprintf function, when passed a zero size, produces no
1074 dnl output. (ISO C99, POSIX:2001)
1075 dnl For example, snprintf nevertheless writes a NUL byte in this case
1076 dnl on OSF/1 5.1:
1077 dnl     ---------------------------------------------------------------------
1078 dnl     #include <stdio.h>
1079 dnl     int main()
1080 dnl     {
1081 dnl       static char buf[8] = "DEADBEEF";
1082 dnl       snprintf (buf, 0, "%d", 12345);
1083 dnl       return buf[0] != 'D';
1084 dnl     }
1085 dnl     ---------------------------------------------------------------------
1086 dnl And vsnprintf writes any output without bounds in this case, behaving like
1087 dnl vsprintf, on HP-UX 11 and OSF/1 5.1:
1088 dnl     ---------------------------------------------------------------------
1089 dnl     #include <stdarg.h>
1090 dnl     #include <stdio.h>
1091 dnl     static int my_snprintf (char *buf, int size, const char *format, ...)
1092 dnl     {
1093 dnl       va_list args;
1094 dnl       int ret;
1095 dnl       va_start (args, format);
1096 dnl       ret = vsnprintf (buf, size, format, args);
1097 dnl       va_end (args);
1098 dnl       return ret;
1099 dnl     }
1100 dnl     int main()
1101 dnl     {
1102 dnl       static char buf[8] = "DEADBEEF";
1103 dnl       my_snprintf (buf, 0, "%d", 12345);
1104 dnl       return buf[0] != 'D';
1105 dnl     }
1106 dnl     ---------------------------------------------------------------------
1107 dnl Result is gl_cv_func_vsnprintf_zerosize_c99.
1108
1109 AC_DEFUN([gl_VSNPRINTF_ZEROSIZE_C99],
1110 [
1111   AC_REQUIRE([AC_PROG_CC])
1112   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1113   AC_CACHE_CHECK([whether vsnprintf respects a zero size as in C99],
1114     [gl_cv_func_vsnprintf_zerosize_c99],
1115     [
1116       AC_TRY_RUN([
1117 #include <stdarg.h>
1118 #include <stdio.h>
1119 static int my_snprintf (char *buf, int size, const char *format, ...)
1120 {
1121   va_list args;
1122   int ret;
1123   va_start (args, format);
1124   ret = vsnprintf (buf, size, format, args);
1125   va_end (args);
1126   return ret;
1127 }
1128 int main()
1129 {
1130   static char buf[8] = "DEADBEEF";
1131   my_snprintf (buf, 0, "%d", 12345);
1132   return buf[0] != 'D';
1133 }],
1134       [gl_cv_func_vsnprintf_zerosize_c99=yes],
1135       [gl_cv_func_vsnprintf_zerosize_c99=no],
1136       [
1137 changequote(,)dnl
1138        case "$host_os" in
1139                                # Guess yes on glibc systems.
1140          *-gnu*)               gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1141                                # Guess yes on FreeBSD >= 5.
1142          freebsd[1-4]*)        gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1143          freebsd* | kfreebsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1144                                # Guess yes on MacOS X >= 10.3.
1145          darwin[1-6].*)        gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1146          darwin*)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1147                                # Guess yes on Cygwin.
1148          cygwin*)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1149                                # Guess yes on Solaris >= 2.6.
1150          solaris2.[0-5]*)      gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1151          solaris*)             gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1152                                # Guess yes on AIX >= 4.
1153          aix[1-3]*)            gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1154          aix*)                 gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1155                                # Guess yes on IRIX >= 6.5.
1156          irix6.5)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1157                                # Guess yes on NetBSD >= 3.
1158          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1159                                gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1160          netbsd*)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1161                                # Guess yes on BeOS.
1162          beos*)                gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1163                                # Guess yes on mingw.
1164          mingw* | pw*)         gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
1165                                # If we don't know, assume the worst.
1166          *)                    gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
1167        esac
1168 changequote([,])dnl
1169       ])
1170     ])
1171 ])
1172
1173 dnl The results of these tests on various platforms are:
1174 dnl
1175 dnl 1 = gl_PRINTF_SIZES_C99
1176 dnl 2 = gl_PRINTF_LONG_DOUBLE
1177 dnl 3 = gl_PRINTF_INFINITE
1178 dnl 4 = gl_PRINTF_INFINITE_LONG_DOUBLE
1179 dnl 5 = gl_PRINTF_DIRECTIVE_A
1180 dnl 6 = gl_PRINTF_DIRECTIVE_F
1181 dnl 7 = gl_PRINTF_DIRECTIVE_N
1182 dnl 8 = gl_PRINTF_POSITIONS
1183 dnl 9 = gl_PRINTF_FLAG_GROUPING
1184 dnl 10 = gl_PRINTF_FLAG_ZERO
1185 dnl 11 = gl_PRINTF_PRECISION
1186 dnl 12 = gl_PRINTF_ENOMEM
1187 dnl 13 = gl_SNPRINTF_PRESENCE
1188 dnl 14 = gl_SNPRINTF_TRUNCATION_C99
1189 dnl 15 = gl_SNPRINTF_RETVAL_C99
1190 dnl 16 = gl_SNPRINTF_DIRECTIVE_N
1191 dnl 17 = gl_VSNPRINTF_ZEROSIZE_C99
1192 dnl
1193 dnl 1 = checking whether printf supports size specifiers as in C99...
1194 dnl 2 = checking whether printf supports 'long double' arguments...
1195 dnl 3 = checking whether printf supports infinite 'double' arguments...
1196 dnl 4 = checking whether printf supports infinite 'long double' arguments...
1197 dnl 5 = checking whether printf supports the 'a' and 'A' directives...
1198 dnl 6 = checking whether printf supports the 'F' directive...
1199 dnl 7 = checking whether printf supports the 'n' directive...
1200 dnl 8 = checking whether printf supports POSIX/XSI format strings with positions...
1201 dnl 9 = checking whether printf supports the grouping flag...
1202 dnl 10 = checking whether printf supports the zero flag correctly...
1203 dnl 11 = checking whether printf supports large precisions...
1204 dnl 12 = checking whether printf survives out-of-memory conditions...
1205 dnl 13 = checking for snprintf...
1206 dnl 14 = checking whether snprintf truncates the result as in C99...
1207 dnl 15 = checking whether snprintf returns a byte count as in C99...
1208 dnl 16 = checking whether snprintf fully supports the 'n' directive...
1209 dnl 17 = checking whether vsnprintf respects a zero size as in C99...
1210 dnl
1211 dnl . = yes, # = no.
1212 dnl
1213 dnl                                  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
1214 dnl   glibc 2.5                      .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
1215 dnl   glibc 2.3.6                    .  .  .  .  #  .  .  .  .  .  .  .  .  .  .  .  .
1216 dnl   FreeBSD 5.4, 6.1               .  .  .  .  #  .  .  .  .  #  .  #  .  .  .  .  .
1217 dnl   MacOS X 10.3.9                 .  .  .  .  #  .  .  .  .  #  .  #  .  .  .  .  .
1218 dnl   OpenBSD 3.9, 4.0               .  ?  ?  ?  #  ?  .  .  ?  ?  ?  ?  .  .  .  ?  ?
1219 dnl   Cygwin 2007 (= Cygwin 1.5.24)  .  .  .  .  #  #  .  .  .  #  ?  ?  .  .  .  .  .
1220 dnl   Cygwin 2006 (= Cygwin 1.5.19)  #  .  .  .  #  #  .  .  #  #  ?  ?  .  .  .  .  .
1221 dnl   Solaris 10                     .  .  #  #  #  .  .  .  .  #  .  .  .  .  .  .  .
1222 dnl   Solaris 2.6 ... 9              #  .  #  #  #  #  .  .  .  #  .  .  .  .  .  .  .
1223 dnl   Solaris 2.5.1                  #  .  #  #  #  #  .  .  .  #  .  .  #  #  #  #  #
1224 dnl   AIX 5.2                        .  .  #  #  #  .  .  .  .  #  .  .  .  .  .  .  .
1225 dnl   AIX 4.3.2, 5.1                 #  .  #  #  #  #  .  .  .  #  .  .  .  .  .  .  .
1226 dnl   HP-UX 11.31                    .  .  .  .  #  .  .  .  .  #  .  .  .  .  #  #  .
1227 dnl   HP-UX 10.20, 11.{00,11,23}     #  .  .  .  #  #  .  .  .  #  .  .  .  .  #  #  #
1228 dnl   IRIX 6.5                       #  .  #  #  #  #  .  .  .  #  .  .  .  .  #  .  .
1229 dnl   OSF/1 5.1                      #  .  #  #  #  #  .  .  .  #  .  .  .  .  #  .  #
1230 dnl   OSF/1 4.0d                     #  .  #  #  #  #  .  .  .  #  .  .  #  #  #  #  #
1231 dnl   NetBSD 4.0                     .  ?  ?  ?  ?  ?  .  .  ?  ?  ?  ?  .  .  .  ?  ?
1232 dnl   NetBSD 3.0                     .  .  .  .  #  #  .  #  #  #  .  #  .  .  .  .  .
1233 dnl   BeOS                           #  #  .  #  #  #  .  #  .  .  #  ?  .  .  .  .  .
1234 dnl   mingw                          #  #  #  #  #  #  .  #  #  #  #  ?  .  #  #  #  .