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