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