Whitespace change.
[gnulib.git] / m4 / printf.m4
1 # printf.m4 serial 11
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 infinite 'double'
85 dnl arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001)
86 dnl Result is gl_cv_func_printf_infinite.
87
88 AC_DEFUN([gl_PRINTF_INFINITE],
89 [
90   AC_REQUIRE([AC_PROG_CC])
91   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
92   AC_CACHE_CHECK([whether printf supports infinite 'double' arguments],
93     [gl_cv_func_printf_infinite],
94     [
95       AC_TRY_RUN([
96 #include <stdio.h>
97 #include <string.h>
98 static char buf[100];
99 int main ()
100 {
101   if (sprintf (buf, "%f", 1.0 / 0.0) < 0
102       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
103     return 1;
104   if (sprintf (buf, "%f", -1.0 / 0.0) < 0
105       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
106     return 1;
107   if (sprintf (buf, "%e", 1.0 / 0.0) < 0
108       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
109     return 1;
110   if (sprintf (buf, "%e", -1.0 / 0.0) < 0
111       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
112     return 1;
113   if (sprintf (buf, "%g", 1.0 / 0.0) < 0
114       || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
115     return 1;
116   if (sprintf (buf, "%g", -1.0 / 0.0) < 0
117       || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
118     return 1;
119   return 0;
120 }], [gl_cv_func_printf_infinite=yes], [gl_cv_func_printf_infinite=no],
121       [
122 changequote(,)dnl
123        case "$host_os" in
124          mingw* | pw*) gl_cv_func_printf_infinite="guessing no";;
125          *)            gl_cv_func_printf_infinite="guessing yes";;
126        esac
127 changequote([,])dnl
128       ])
129     ])
130 ])
131
132 dnl Test whether the *printf family of functions supports 'long double'
133 dnl arguments together with the 'L' size specifier. (ISO C99, POSIX:2001)
134 dnl Result is gl_cv_func_printf_long_double.
135
136 AC_DEFUN([gl_PRINTF_LONG_DOUBLE],
137 [
138   AC_REQUIRE([AC_PROG_CC])
139   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
140   AC_CACHE_CHECK([whether printf supports 'long double' arguments],
141     [gl_cv_func_printf_long_double],
142     [
143       AC_TRY_RUN([
144 #include <stdio.h>
145 #include <string.h>
146 static char buf[100];
147 int main ()
148 {
149   buf[0] = '\0';
150   if (sprintf (buf, "%Lf %d", 1.75L, 33, 44, 55) < 0
151       || strcmp (buf, "1.750000 33") != 0)
152     return 1;
153   buf[0] = '\0';
154   if (sprintf (buf, "%Le %d", 1.75L, 33, 44, 55) < 0
155       || strcmp (buf, "1.750000e+00 33") != 0)
156     return 1;
157   buf[0] = '\0';
158   if (sprintf (buf, "%Lg %d", 1.75L, 33, 44, 55) < 0
159       || strcmp (buf, "1.75 33") != 0)
160     return 1;
161   return 0;
162 }], [gl_cv_func_printf_long_double=yes], [gl_cv_func_printf_long_double=no],
163       [
164 changequote(,)dnl
165        case "$host_os" in
166          beos*)        gl_cv_func_printf_long_double="guessing no";;
167          mingw* | pw*) gl_cv_func_printf_long_double="guessing no";;
168          *)            gl_cv_func_printf_long_double="guessing yes";;
169        esac
170 changequote([,])dnl
171       ])
172     ])
173 ])
174
175 dnl Test whether the *printf family of functions supports the 'a' and 'A'
176 dnl conversion specifier for hexadecimal output of floating-point numbers.
177 dnl (ISO C99, POSIX:2001)
178 dnl Result is gl_cv_func_printf_directive_a.
179
180 AC_DEFUN([gl_PRINTF_DIRECTIVE_A],
181 [
182   AC_REQUIRE([AC_PROG_CC])
183   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
184   AC_CACHE_CHECK([whether printf supports the 'a' and 'A' directives],
185     [gl_cv_func_printf_directive_a],
186     [
187       AC_TRY_RUN([
188 #include <stdio.h>
189 #include <string.h>
190 static char buf[100];
191 int main ()
192 {
193   if (sprintf (buf, "%a %d", 3.1416015625, 33, 44, 55) < 0
194       || (strcmp (buf, "0x1.922p+1 33") != 0
195           && strcmp (buf, "0x3.244p+0 33") != 0
196           && strcmp (buf, "0x6.488p-1 33") != 0
197           && strcmp (buf, "0xc.91p-2 33") != 0))
198     return 1;
199   if (sprintf (buf, "%A %d", -3.1416015625, 33, 44, 55) < 0
200       || (strcmp (buf, "-0X1.922P+1 33") != 0
201           && strcmp (buf, "-0X3.244P+0 33") != 0
202           && strcmp (buf, "-0X6.488P-1 33") != 0
203           && strcmp (buf, "-0XC.91P-2 33") != 0))
204     return 1;
205   /* This catches a FreeBSD 6.1 bug: it doesn't round.  */
206   if (sprintf (buf, "%.2a %d", 1.51, 33, 44, 55) < 0
207       || (strcmp (buf, "0x1.83p+0 33") != 0
208           && strcmp (buf, "0x3.05p-1 33") != 0
209           && strcmp (buf, "0x6.0ap-2 33") != 0
210           && strcmp (buf, "0xc.14p-3 33") != 0))
211     return 1;
212   /* This catches a FreeBSD 6.1 bug.  See
213      <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
214   if (sprintf (buf, "%010a %d", 1.0 / 0.0, 33, 44, 55) < 0
215       || buf[0] == '0')
216     return 1;
217   /* This catches a MacOS X 10.3.9 (Darwin 7.9) bug.  */
218   if (sprintf (buf, "%.1a", 1.999) < 0
219       || (strcmp (buf, "0x1.0p+1") != 0
220           && strcmp (buf, "0x2.0p+0") != 0
221           && strcmp (buf, "0x4.0p-1") != 0
222           && strcmp (buf, "0x8.0p-2") != 0))
223     return 1;
224   /* This catches the same MacOS X 10.3.9 (Darwin 7.9) bug and also a
225      glibc 2.4 bug <http://sourceware.org/bugzilla/show_bug.cgi?id=2908>.  */
226   if (sprintf (buf, "%.1La", 1.999L) < 0
227       || (strcmp (buf, "0x1.0p+1") != 0
228           && strcmp (buf, "0x2.0p+0") != 0
229           && strcmp (buf, "0x4.0p-1") != 0
230           && strcmp (buf, "0x8.0p-2") != 0))
231     return 1;
232   return 0;
233 }], [gl_cv_func_printf_directive_a=yes], [gl_cv_func_printf_directive_a=no],
234       [
235        case "$host_os" in
236                                # Guess yes on glibc >= 2.5 systems.
237          *-gnu*)
238            AC_EGREP_CPP([BZ2908], [
239              #include <features.h>
240              #ifdef __GNU_LIBRARY__
241               #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)
242                BZ2908
243               #endif
244              #endif
245              ],
246              [gl_cv_func_printf_directive_a="guessing yes"],
247              [gl_cv_func_printf_directive_a="guessing no"])
248            ;;
249                                # If we don't know, assume the worst.
250          *)                    gl_cv_func_printf_directive_a="guessing no";;
251        esac
252       ])
253     ])
254 ])
255
256 dnl Test whether the *printf family of functions supports the %F format
257 dnl directive. (ISO C99, POSIX:2001)
258 dnl Result is gl_cv_func_printf_directive_f.
259
260 AC_DEFUN([gl_PRINTF_DIRECTIVE_F],
261 [
262   AC_REQUIRE([AC_PROG_CC])
263   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
264   AC_CACHE_CHECK([whether printf supports the 'F' directive],
265     [gl_cv_func_printf_directive_f],
266     [
267       AC_TRY_RUN([
268 #include <stdio.h>
269 #include <string.h>
270 static char buf[100];
271 int main ()
272 {
273   if (sprintf (buf, "%F %d", 1234567.0, 33, 44, 55) < 0
274       || strcmp (buf, "1234567.000000 33") != 0)
275     return 1;
276   if (sprintf (buf, "%F", 1.0 / 0.0) < 0
277       || (strcmp (buf, "INF") != 0 && strcmp (buf, "INFINITY") != 0))
278     return 1;
279   /* This catches a Cygwin 2007 bug.  */
280   if (sprintf (buf, "%.F", 1234.0) < 0
281       || strcmp (buf, "1234") != 0)
282     return 1;
283   return 0;
284 }], [gl_cv_func_printf_directive_f=yes], [gl_cv_func_printf_directive_f=no],
285       [
286 changequote(,)dnl
287        case "$host_os" in
288                                # Guess yes on glibc systems.
289          *-gnu*)               gl_cv_func_printf_directive_f="guessing yes";;
290                                # Guess yes on FreeBSD >= 6.
291          freebsd[1-5]*)        gl_cv_func_printf_directive_f="guessing no";;
292          freebsd* | kfreebsd*) gl_cv_func_printf_directive_f="guessing yes";;
293                                # Guess yes on MacOS X >= 10.3.
294          darwin[1-6].*)        gl_cv_func_printf_directive_f="guessing no";;
295          darwin*)              gl_cv_func_printf_directive_f="guessing yes";;
296                                # Guess yes on Solaris >= 2.10.
297          solaris2.[0-9]*)      gl_cv_func_printf_directive_f="guessing no";;
298          solaris*)             gl_cv_func_printf_directive_f="guessing yes";;
299                                # If we don't know, assume the worst.
300          *)                    gl_cv_func_printf_directive_f="guessing no";;
301        esac
302 changequote([,])dnl
303       ])
304     ])
305 ])
306
307 dnl Test whether the *printf family of functions supports the %n format
308 dnl directive. (ISO C99, POSIX:2001)
309 dnl Result is gl_cv_func_printf_directive_n.
310
311 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
312 [
313   AC_REQUIRE([AC_PROG_CC])
314   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
315   AC_CACHE_CHECK([whether printf supports the 'n' directive],
316     [gl_cv_func_printf_directive_n],
317     [
318       AC_TRY_RUN([
319 #include <stdio.h>
320 #include <string.h>
321 static char buf[100];
322 int main ()
323 {
324   int count = -1;
325   if (sprintf (buf, "%d %n", 123, &count, 33, 44, 55) < 0
326       || strcmp (buf, "123 ") != 0
327       || count != 4)
328     return 1;
329   return 0;
330 }], [gl_cv_func_printf_directive_n=yes], [gl_cv_func_printf_directive_n=no],
331       [
332 changequote(,)dnl
333        case "$host_os" in
334          *)     gl_cv_func_printf_directive_n="guessing yes";;
335        esac
336 changequote([,])dnl
337       ])
338     ])
339 ])
340
341 dnl Test whether the *printf family of functions supports POSIX/XSI format
342 dnl strings with positions. (POSIX:2001)
343 dnl Result is gl_cv_func_printf_positions.
344
345 AC_DEFUN([gl_PRINTF_POSITIONS],
346 [
347   AC_REQUIRE([AC_PROG_CC])
348   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
349   AC_CACHE_CHECK([whether printf supports POSIX/XSI format strings with positions],
350     [gl_cv_func_printf_positions],
351     [
352       AC_TRY_RUN([
353 #include <stdio.h>
354 #include <string.h>
355 /* The string "%2$d %1$d", with dollar characters protected from the shell's
356    dollar expansion (possibly an autoconf bug).  */
357 static char format[] = { '%', '2', '$', 'd', ' ', '%', '1', '$', 'd', '\0' };
358 static char buf[100];
359 int main ()
360 {
361   sprintf (buf, format, 33, 55);
362   return (strcmp (buf, "55 33") != 0);
363 }], [gl_cv_func_printf_positions=yes], [gl_cv_func_printf_positions=no],
364       [
365 changequote(,)dnl
366        case "$host_os" in
367          netbsd[1-3]* | netbsdelf[1-3]* | netbsdaout[1-3]* | netbsdcoff[1-3]*)
368                        gl_cv_func_printf_positions="guessing no";;
369          beos*)        gl_cv_func_printf_positions="guessing no";;
370          mingw* | pw*) gl_cv_func_printf_positions="guessing no";;
371          *)            gl_cv_func_printf_positions="guessing yes";;
372        esac
373 changequote([,])dnl
374       ])
375     ])
376 ])
377
378 dnl Test whether the *printf family of functions supports POSIX/XSI format
379 dnl strings with the ' flag for grouping of decimal digits. (POSIX:2001)
380 dnl Result is gl_cv_func_printf_flag_grouping.
381
382 AC_DEFUN([gl_PRINTF_FLAG_GROUPING],
383 [
384   AC_REQUIRE([AC_PROG_CC])
385   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
386   AC_CACHE_CHECK([whether printf supports the grouping flag],
387     [gl_cv_func_printf_flag_grouping],
388     [
389       AC_TRY_RUN([
390 #include <stdio.h>
391 #include <string.h>
392 static char buf[100];
393 int main ()
394 {
395   if (sprintf (buf, "%'d %d", 1234567, 99) < 0
396       || buf[strlen (buf) - 1] != '9')
397     return 1;
398   return 0;
399 }], [gl_cv_func_printf_flag_grouping=yes], [gl_cv_func_printf_flag_grouping=no],
400       [
401 changequote(,)dnl
402        case "$host_os" in
403          cygwin*)      gl_cv_func_printf_flag_grouping="guessing no";;
404          netbsd*)      gl_cv_func_printf_flag_grouping="guessing no";;
405          mingw* | pw*) gl_cv_func_printf_flag_grouping="guessing no";;
406          *)            gl_cv_func_printf_flag_grouping="guessing yes";;
407        esac
408 changequote([,])dnl
409       ])
410     ])
411 ])
412
413 dnl Test whether the *printf family of functions supports padding of non-finite
414 dnl values with the 0 flag correctly. (ISO C99 + TC1 + TC2.) See
415 dnl <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html>
416 dnl Result is gl_cv_func_printf_flag_zero.
417
418 AC_DEFUN([gl_PRINTF_FLAG_ZERO],
419 [
420   AC_REQUIRE([AC_PROG_CC])
421   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
422   AC_CACHE_CHECK([whether printf supports the zero flag correctly],
423     [gl_cv_func_printf_flag_zero],
424     [
425       AC_TRY_RUN([
426 #include <stdio.h>
427 #include <string.h>
428 static char buf[100];
429 int main ()
430 {
431   if (sprintf (buf, "%010f", 1.0 / 0.0, 33, 44, 55) < 0
432       || (strcmp (buf, "       inf") != 0
433           && strcmp (buf, "  infinity") != 0))
434     return 1;
435   return 0;
436 }], [gl_cv_func_printf_flag_zero=yes], [gl_cv_func_printf_flag_zero=no],
437       [
438 changequote(,)dnl
439        case "$host_os" in
440                  # Guess yes on glibc systems.
441          *-gnu*) gl_cv_func_printf_flag_zero="guessing yes";;
442                  # Guess yes on BeOS.
443          beos*)  gl_cv_func_printf_flag_zero="guessing yes";;
444                  # If we don't know, assume the worst.
445          *)      gl_cv_func_printf_flag_zero="guessing no";;
446        esac
447 changequote([,])dnl
448       ])
449     ])
450 ])
451
452 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
453 dnl Result is ac_cv_func_snprintf.
454
455 AC_DEFUN([gl_SNPRINTF_PRESENCE],
456 [
457   AC_CHECK_FUNCS_ONCE([snprintf])
458 ])
459
460 dnl Test whether the string produced by the snprintf function is always NUL
461 dnl terminated. (ISO C99, POSIX:2001)
462 dnl Result is gl_cv_func_snprintf_truncation_c99.
463
464 AC_DEFUN([gl_SNPRINTF_TRUNCATION_C99],
465 [
466   AC_REQUIRE([AC_PROG_CC])
467   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
468   AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
469     [gl_cv_func_snprintf_truncation_c99],
470     [
471       AC_TRY_RUN([
472 #include <stdio.h>
473 #include <string.h>
474 static char buf[100];
475 int main ()
476 {
477   strcpy (buf, "ABCDEF");
478   snprintf (buf, 3, "%d %d", 4567, 89);
479   if (memcmp (buf, "45\0DEF", 6) != 0)
480     return 1;
481   return 0;
482 }], [gl_cv_func_snprintf_truncation_c99=yes], [gl_cv_func_snprintf_truncation_c99=no],
483       [
484 changequote(,)dnl
485        case "$host_os" in
486                                # Guess yes on glibc systems.
487          *-gnu*)               gl_cv_func_snprintf_truncation_c99="guessing yes";;
488                                # Guess yes on FreeBSD >= 5.
489          freebsd[1-4]*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
490          freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
491                                # Guess yes on MacOS X >= 10.3.
492          darwin[1-6].*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
493          darwin*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
494                                # Guess yes on OpenBSD >= 3.9.
495          openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
496                                gl_cv_func_snprintf_truncation_c99="guessing no";;
497          openbsd*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
498                                # Guess yes on Solaris >= 2.6.
499          solaris2.[0-5]*)      gl_cv_func_snprintf_truncation_c99="guessing no";;
500          solaris*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
501                                # Guess yes on AIX >= 4.
502          aix[1-3]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
503          aix*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
504                                # Guess yes on HP-UX >= 11.
505          hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
506          hpux*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
507                                # Guess yes on IRIX >= 6.5.
508          irix6.5)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
509                                # Guess yes on OSF/1 >= 5.
510          osf[3-4]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
511          osf*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
512                                # Guess yes on NetBSD >= 3.
513          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
514                                gl_cv_func_snprintf_truncation_c99="guessing no";;
515          netbsd*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
516                                # Guess yes on BeOS.
517          beos*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
518                                # If we don't know, assume the worst.
519          *)                    gl_cv_func_snprintf_truncation_c99="guessing no";;
520        esac
521 changequote([,])dnl
522       ])
523     ])
524 ])
525
526 dnl Test whether the return value of the snprintf function is the number
527 dnl of bytes (excluding the terminating NUL) that would have been produced
528 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
529 dnl For example, this test program fails on IRIX 6.5:
530 dnl     ---------------------------------------------------------------------
531 dnl     #include <stdio.h>
532 dnl     int main()
533 dnl     {
534 dnl       static char buf[8];
535 dnl       int retval = snprintf (buf, 3, "%d", 12345);
536 dnl       return retval >= 0 && retval < 3;
537 dnl     }
538 dnl     ---------------------------------------------------------------------
539 dnl Result is gl_cv_func_snprintf_retval_c99.
540
541 AC_DEFUN([gl_SNPRINTF_RETVAL_C99],
542 [
543   AC_REQUIRE([AC_PROG_CC])
544   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
545   AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
546     [gl_cv_func_snprintf_retval_c99],
547     [
548       AC_TRY_RUN([
549 #include <stdio.h>
550 #include <string.h>
551 static char buf[100];
552 int main ()
553 {
554   strcpy (buf, "ABCDEF");
555   if (snprintf (buf, 3, "%d %d", 4567, 89) != 7)
556     return 1;
557   return 0;
558 }], [gl_cv_func_snprintf_retval_c99=yes], [gl_cv_func_snprintf_retval_c99=no],
559       [
560 changequote(,)dnl
561        case "$host_os" in
562                                # Guess yes on glibc systems.
563          *-gnu*)               gl_cv_func_snprintf_retval_c99="guessing yes";;
564                                # Guess yes on FreeBSD >= 5.
565          freebsd[1-4]*)        gl_cv_func_snprintf_retval_c99="guessing no";;
566          freebsd* | kfreebsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
567                                # Guess yes on MacOS X >= 10.3.
568          darwin[1-6].*)        gl_cv_func_snprintf_retval_c99="guessing no";;
569          darwin*)              gl_cv_func_snprintf_retval_c99="guessing yes";;
570                                # Guess yes on OpenBSD >= 3.9.
571          openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
572                                gl_cv_func_snprintf_retval_c99="guessing no";;
573          openbsd*)             gl_cv_func_snprintf_retval_c99="guessing yes";;
574                                # Guess yes on Solaris >= 2.6.
575          solaris2.[0-5]*)      gl_cv_func_snprintf_retval_c99="guessing no";;
576          solaris*)             gl_cv_func_snprintf_retval_c99="guessing yes";;
577                                # Guess yes on AIX >= 4.
578          aix[1-3]*)            gl_cv_func_snprintf_retval_c99="guessing no";;
579          aix*)                 gl_cv_func_snprintf_retval_c99="guessing yes";;
580                                # Guess yes on NetBSD >= 3.
581          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
582                                gl_cv_func_snprintf_retval_c99="guessing no";;
583          netbsd*)              gl_cv_func_snprintf_retval_c99="guessing yes";;
584                                # Guess yes on BeOS.
585          beos*)                gl_cv_func_snprintf_retval_c99="guessing yes";;
586                                # If we don't know, assume the worst.
587          *)                    gl_cv_func_snprintf_retval_c99="guessing no";;
588        esac
589 changequote([,])dnl
590       ])
591     ])
592 ])
593
594 dnl Test whether the snprintf function supports the %n format directive
595 dnl also in truncated portions of the format string. (ISO C99, POSIX:2001)
596 dnl Result is gl_cv_func_snprintf_directive_n.
597
598 AC_DEFUN([gl_SNPRINTF_DIRECTIVE_N],
599 [
600   AC_REQUIRE([AC_PROG_CC])
601   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
602   AC_CACHE_CHECK([whether snprintf fully supports the 'n' directive],
603     [gl_cv_func_snprintf_directive_n],
604     [
605       AC_TRY_RUN([
606 #include <stdio.h>
607 #include <string.h>
608 static char buf[100];
609 int main ()
610 {
611   int count = -1;
612   snprintf (buf, 4, "%d %n", 12345, &count, 33, 44, 55);
613   if (count != 6)
614     return 1;
615   return 0;
616 }], [gl_cv_func_snprintf_directive_n=yes], [gl_cv_func_snprintf_directive_n=no],
617       [
618 changequote(,)dnl
619        case "$host_os" in
620                                # Guess yes on glibc systems.
621          *-gnu*)               gl_cv_func_snprintf_directive_n="guessing yes";;
622                                # Guess yes on FreeBSD >= 5.
623          freebsd[1-4]*)        gl_cv_func_snprintf_directive_n="guessing no";;
624          freebsd* | kfreebsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
625                                # Guess yes on MacOS X >= 10.3.
626          darwin[1-6].*)        gl_cv_func_snprintf_directive_n="guessing no";;
627          darwin*)              gl_cv_func_snprintf_directive_n="guessing yes";;
628                                # Guess yes on Solaris >= 2.6.
629          solaris2.[0-5]*)      gl_cv_func_snprintf_directive_n="guessing no";;
630          solaris*)             gl_cv_func_snprintf_directive_n="guessing yes";;
631                                # Guess yes on AIX >= 4.
632          aix[1-3]*)            gl_cv_func_snprintf_directive_n="guessing no";;
633          aix*)                 gl_cv_func_snprintf_directive_n="guessing yes";;
634                                # Guess yes on IRIX >= 6.5.
635          irix6.5)              gl_cv_func_snprintf_directive_n="guessing yes";;
636                                # Guess yes on OSF/1 >= 5.
637          osf[3-4]*)            gl_cv_func_snprintf_directive_n="guessing no";;
638          osf*)                 gl_cv_func_snprintf_directive_n="guessing yes";;
639                                # Guess yes on NetBSD >= 3.
640          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
641                                gl_cv_func_snprintf_directive_n="guessing no";;
642          netbsd*)              gl_cv_func_snprintf_directive_n="guessing yes";;
643                                # Guess yes on BeOS.
644          beos*)                gl_cv_func_snprintf_directive_n="guessing yes";;
645                                # If we don't know, assume the worst.
646          *)                    gl_cv_func_snprintf_directive_n="guessing no";;
647        esac
648 changequote([,])dnl
649       ])
650     ])
651 ])
652
653 dnl Test whether the vsnprintf function, when passed a zero size, produces no
654 dnl output. (ISO C99, POSIX:2001)
655 dnl For example, snprintf nevertheless writes a NUL byte in this case
656 dnl on OSF/1 5.1:
657 dnl     ---------------------------------------------------------------------
658 dnl     #include <stdio.h>
659 dnl     int main()
660 dnl     {
661 dnl       static char buf[8] = "DEADBEEF";
662 dnl       snprintf (buf, 0, "%d", 12345);
663 dnl       return buf[0] != 'D';
664 dnl     }
665 dnl     ---------------------------------------------------------------------
666 dnl And vsnprintf writes any output without bounds in this case, behaving like
667 dnl vsprintf, on HP-UX 11 and OSF/1 5.1:
668 dnl     ---------------------------------------------------------------------
669 dnl     #include <stdarg.h>
670 dnl     #include <stdio.h>
671 dnl     static int my_snprintf (char *buf, int size, const char *format, ...)
672 dnl     {
673 dnl       va_list args;
674 dnl       int ret;
675 dnl       va_start (args, format);
676 dnl       ret = vsnprintf (buf, size, format, args);
677 dnl       va_end (args);
678 dnl       return ret;
679 dnl     }
680 dnl     int main()
681 dnl     {
682 dnl       static char buf[8] = "DEADBEEF";
683 dnl       my_snprintf (buf, 0, "%d", 12345);
684 dnl       return buf[0] != 'D';
685 dnl     }
686 dnl     ---------------------------------------------------------------------
687 dnl Result is gl_cv_func_vsnprintf_zerosize_c99.
688
689 AC_DEFUN([gl_VSNPRINTF_ZEROSIZE_C99],
690 [
691   AC_REQUIRE([AC_PROG_CC])
692   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
693   AC_CACHE_CHECK([whether vsnprintf respects a zero size as in C99],
694     [gl_cv_func_vsnprintf_zerosize_c99],
695     [
696       AC_TRY_RUN([
697 #include <stdarg.h>
698 #include <stdio.h>
699 static int my_snprintf (char *buf, int size, const char *format, ...)
700 {
701   va_list args;
702   int ret;
703   va_start (args, format);
704   ret = vsnprintf (buf, size, format, args);
705   va_end (args);
706   return ret;
707 }
708 int main()
709 {
710   static char buf[8] = "DEADBEEF";
711   my_snprintf (buf, 0, "%d", 12345);
712   return buf[0] != 'D';
713 }],
714       [gl_cv_func_vsnprintf_zerosize_c99=yes],
715       [gl_cv_func_vsnprintf_zerosize_c99=no],
716       [
717 changequote(,)dnl
718        case "$host_os" in
719                                # Guess yes on glibc systems.
720          *-gnu*)               gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
721                                # Guess yes on FreeBSD >= 5.
722          freebsd[1-4]*)        gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
723          freebsd* | kfreebsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
724                                # Guess yes on MacOS X >= 10.3.
725          darwin[1-6].*)        gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
726          darwin*)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
727                                # Guess yes on Cygwin.
728          cygwin*)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
729                                # Guess yes on Solaris >= 2.6.
730          solaris2.[0-5]*)      gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
731          solaris*)             gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
732                                # Guess yes on AIX >= 4.
733          aix[1-3]*)            gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
734          aix*)                 gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
735                                # Guess yes on IRIX >= 6.5.
736          irix6.5)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
737                                # Guess yes on NetBSD >= 3.
738          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
739                                gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
740          netbsd*)              gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
741                                # Guess yes on BeOS.
742          beos*)                gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
743                                # Guess yes on mingw.
744          mingw* | pw*)         gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
745                                # If we don't know, assume the worst.
746          *)                    gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
747        esac
748 changequote([,])dnl
749       ])
750     ])
751 ])
752
753 dnl The results of these tests on various platforms are:
754 dnl
755 dnl 1 = gl_PRINTF_SIZES_C99
756 dnl 2 = gl_PRINTF_INFINITE
757 dnl 3 = gl_PRINTF_LONG_DOUBLE
758 dnl 4 = gl_PRINTF_DIRECTIVE_A
759 dnl 5 = gl_PRINTF_DIRECTIVE_F
760 dnl 6 = gl_PRINTF_DIRECTIVE_N
761 dnl 7 = gl_PRINTF_POSITIONS
762 dnl 8 = gl_PRINTF_FLAG_GROUPING
763 dnl 9 = gl_PRINTF_FLAG_ZERO
764 dnl 10 = gl_SNPRINTF_PRESENCE
765 dnl 11 = gl_SNPRINTF_TRUNCATION_C99
766 dnl 12 = gl_SNPRINTF_RETVAL_C99
767 dnl 13 = gl_SNPRINTF_DIRECTIVE_N
768 dnl 14 = gl_VSNPRINTF_ZEROSIZE_C99
769 dnl
770 dnl 1 = checking whether printf supports size specifiers as in C99...
771 dnl 2 = checking whether printf supports infinite 'double' arguments...
772 dnl 3 = checking whether printf supports 'long double' arguments...
773 dnl 4 = checking whether printf supports the 'a' and 'A' directives...
774 dnl 5 = checking whether printf supports the 'F' directive...
775 dnl 6 = checking whether printf supports the 'n' directive...
776 dnl 7 = checking whether printf supports POSIX/XSI format strings with positions...
777 dnl 8 = checking whether printf supports the grouping flag...
778 dnl 9 = checking whether printf supports the zero flag correctly...
779 dnl 10 = checking for snprintf...
780 dnl 11 = checking whether snprintf truncates the result as in C99...
781 dnl 12 = checking whether snprintf returns a byte count as in C99...
782 dnl 13 = checking whether snprintf fully supports the 'n' directive...
783 dnl 14 = checking whether vsnprintf respects a zero size as in C99...
784 dnl
785 dnl . = yes, # = no.
786 dnl
787 dnl                                     1  2  3  4  5  6  7  8  9 10 11 12 13 14
788 dnl   glibc 2.5                         .  .  .  .  .  .  .  .  .  .  .  .  .  .
789 dnl   glibc 2.3.6                       .  .  .  #  .  .  .  .  .  .  .  .  .  .
790 dnl   FreeBSD 5.4, 6.1                  .  .  ?  ?  .  .  .  .  #  .  .  .  .  .
791 dnl   MacOS X 10.3.9                    .  .  .  #  .  .  .  .  #  .  .  .  .  .
792 dnl   OpenBSD 3.9, 4.0                  .  ?  ?  #  ?  .  .  ?  ?  .  .  .  ?  ?
793 dnl   Cygwin 2007 (= Cygwin 1.5.24)     .  ?  ?  #  #  .  .  .  #  .  .  .  .  .
794 dnl   Cygwin 2006 (= Cygwin 1.5.19)     #  ?  ?  #  #  .  .  #  #  .  .  .  .  .
795 dnl   Solaris 10                        .  ?  .  #  .  .  .  .  #  .  .  .  .  .
796 dnl   Solaris 2.6 ... 9                 #  ?  .  #  #  .  .  .  #  .  .  .  .  .
797 dnl   Solaris 2.5.1                     #  ?  .  #  #  .  .  .  #  #  #  #  #  #
798 dnl   AIX 5.2                           .  ?  .  #  .  .  .  .  #  .  .  .  .  .
799 dnl   AIX 4.3.2, 5.1                    #  ?  .  #  #  .  .  .  #  .  .  .  .  .
800 dnl   HP-UX 11.31                       .  .  .  #  .  .  .  .  #  .  .  #  #  .
801 dnl   HP-UX 10.20, 11.{00,11,23}        #  .  .  #  #  .  .  .  #  .  .  #  #  #
802 dnl   IRIX 6.5                          #  ?  .  #  #  .  .  .  #  .  .  #  .  .
803 dnl   OSF/1 5.1                         #  ?  .  #  #  .  .  .  #  .  .  #  .  #
804 dnl   OSF/1 4.0d                        #  ?  .  #  #  .  .  .  #  #  #  #  #  #
805 dnl   NetBSD 4.0                        .  ?  ?  ?  ?  .  .  ?  ?  .  .  .  ?  ?
806 dnl   NetBSD 3.0                        .  ?  ?  #  #  .  #  #  #  .  .  .  .  .
807 dnl   BeOS                              #  ?  #  #  #  .  #  .  .  .  .  .  .  .
808 dnl   mingw                             #  #  #  #  #  .  #  #  #  .  #  #  #  .