Assume 'long double' exists.
[gnulib.git] / m4 / printf.m4
1 # printf.m4 serial 4
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 (result, "0x1.83p+0 33") != 0
117           && strcmp (result, "0x3.05p-1 33") != 0
118           && strcmp (result, "0x6.0ap-2 33") != 0
119           && strcmp (result, "0xc.14p-3 33") != 0))
120     return 1;
121   /* This catches a MacOS X 10.3.9 (Darwin 7.9) bug.  */
122   if (sprintf (buf, "%.1a", 1.999) < 0
123       || (strcmp (buf, "0x1.0p+1") != 0
124           && strcmp (buf, "0x2.0p+0") != 0
125           && strcmp (buf, "0x4.0p-1") != 0
126           && strcmp (buf, "0x8.0p-2") != 0))
127     return 1;
128   /* This catches the same MacOS X 10.3.9 (Darwin 7.9) bug and also a
129      glibc 2.4 bug <http://sourceware.org/bugzilla/show_bug.cgi?id=2908>.  */
130   if (sprintf (buf, "%.1La", 1.999L) < 0
131       || (strcmp (buf, "0x1.0p+1") != 0
132           && strcmp (buf, "0x2.0p+0") != 0
133           && strcmp (buf, "0x4.0p-1") != 0
134           && strcmp (buf, "0x8.0p-2") != 0))
135     return 1;
136   return 0;
137 }], [gl_cv_func_printf_directive_a=yes], [gl_cv_func_printf_directive_a=no],
138       [
139        case "$host_os" in
140                                # Guess yes on glibc >= 2.5 systems.
141          *-gnu*)
142            AC_EGREP_CPP([BZ2908], [
143              #include <features.h>
144              #ifdef __GNU_LIBRARY__
145               #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)
146                BZ2908
147               #endif
148              #endif
149              ],
150              [gl_cv_func_printf_directive_a="guessing yes"],
151              [gl_cv_func_printf_directive_a="guessing no"])
152            ;;
153                                # If we don't know, assume the worst.
154          *)                    gl_cv_func_printf_directive_a="guessing no";;
155        esac
156       ])
157     ])
158 ])
159
160 dnl Test whether the *printf family of functions supports the %n format
161 dnl directive. (ISO C99, POSIX:2001)
162 dnl Result is gl_cv_func_printf_directive_n.
163
164 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
165 [
166   AC_REQUIRE([AC_PROG_CC])
167   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
168   AC_CACHE_CHECK([whether printf supports the 'n' directive],
169     [gl_cv_func_printf_directive_n], 
170     [
171       AC_TRY_RUN([
172 #include <stdio.h>
173 #include <string.h>
174 static char buf[100];
175 int main ()
176 {
177   int count = -1;
178   if (sprintf (buf, "%d %n", 123, &count, 33, 44, 55) < 0
179       || strcmp (buf, "123 ") != 0
180       || count != 4)
181     return 1;
182   return 0;
183 }], [gl_cv_func_printf_directive_n=yes], [gl_cv_func_printf_directive_n=no],
184       [
185 changequote(,)dnl
186        case "$host_os" in
187          *)     gl_cv_func_printf_directive_n="guessing yes";;
188        esac
189 changequote([,])dnl
190       ])
191     ])
192 ])
193
194 dnl Test whether the *printf family of functions supports POSIX/XSI format
195 dnl strings with positions. (POSIX:2001)
196 dnl Result is gl_cv_func_printf_positions.
197
198 AC_DEFUN([gl_PRINTF_POSITIONS],
199 [
200   AC_REQUIRE([AC_PROG_CC])
201   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
202   AC_CACHE_CHECK([whether printf supports POSIX/XSI format strings with positions],
203     [gl_cv_func_printf_positions], 
204     [
205       AC_TRY_RUN([
206 #include <stdio.h>
207 #include <string.h>
208 /* The string "%2$d %1$d", with dollar characters protected from the shell's
209    dollar expansion (possibly an autoconf bug).  */
210 static char format[] = { '%', '2', '$', 'd', ' ', '%', '1', '$', 'd', '\0' };
211 static char buf[100];
212 int main ()
213 {
214   sprintf (buf, format, 33, 55);
215   return (strcmp (buf, "55 33") != 0);
216 }], [gl_cv_func_printf_positions=yes], [gl_cv_func_printf_positions=no],
217       [
218 changequote(,)dnl
219        case "$host_os" in
220          netbsd[1-3]* | netbsdelf[1-3]* | netbsdaout[1-3]* | netbsdcoff[1-3]*)
221                        gl_cv_func_printf_positions="guessing no";;
222          beos*)        gl_cv_func_printf_positions="guessing no";;
223          mingw* | pw*) gl_cv_func_printf_positions="guessing no";;
224          *)            gl_cv_func_printf_positions="guessing yes";;
225        esac
226 changequote([,])dnl
227       ])
228     ])
229 ])
230
231 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
232 dnl Result is ac_cv_func_snprintf.
233
234 AC_DEFUN([gl_SNPRINTF_PRESENCE],
235 [
236   AC_CHECK_FUNCS_ONCE([snprintf])
237 ])
238
239 dnl Test whether the string produced by the snprintf function is always NUL
240 dnl terminated. (ISO C99, POSIX:2001)
241 dnl Result is gl_cv_func_snprintf_truncation_c99.
242
243 AC_DEFUN([gl_SNPRINTF_TRUNCATION_C99],
244 [
245   AC_REQUIRE([AC_PROG_CC])
246   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
247   AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
248     [gl_cv_func_snprintf_truncation_c99], 
249     [
250       AC_TRY_RUN([
251 #include <stdio.h>
252 #include <string.h>
253 static char buf[100];
254 int main ()
255 {
256   strcpy (buf, "ABCDEF");
257   snprintf (buf, 3, "%d %d", 4567, 89);
258   if (memcmp (buf, "45\0DEF", 6) != 0)
259     return 1;
260   return 0;
261 }], [gl_cv_func_snprintf_truncation_c99=yes], [gl_cv_func_snprintf_truncation_c99=no],
262       [
263 changequote(,)dnl
264        case "$host_os" in
265                                # Guess yes on glibc systems.
266          *-gnu*)               gl_cv_func_snprintf_truncation_c99="guessing yes";;
267                                # Guess yes on FreeBSD >= 5.
268          freebsd[1-4]*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
269          freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
270                                # Guess yes on MacOS X >= 10.3.
271          darwin[1-6].*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
272          darwin*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
273                                # Guess yes on OpenBSD >= 3.9.
274          openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
275                                gl_cv_func_snprintf_truncation_c99="guessing no";;
276          openbsd*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
277                                # Guess yes on Solaris >= 2.6.
278          solaris2.[0-5]*)      gl_cv_func_snprintf_truncation_c99="guessing no";;
279          solaris*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
280                                # Guess yes on AIX >= 4.
281          aix[1-3]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
282          aix*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
283                                # Guess yes on HP-UX >= 11.
284          hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
285          hpux*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
286                                # Guess yes on IRIX >= 6.5.
287          irix6.5)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
288                                # Guess yes on OSF/1 >= 5.
289          osf[3-4]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
290          osf*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
291                                # Guess yes on NetBSD >= 3.
292          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
293                                gl_cv_func_snprintf_truncation_c99="guessing no";;
294          netbsd*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
295                                # Guess yes on BeOS.
296          beos*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
297                                # If we don't know, assume the worst.
298          *)                    gl_cv_func_snprintf_truncation_c99="guessing no";;
299        esac
300 changequote([,])dnl
301       ])
302     ])
303 ])
304
305 dnl Test whether the return value of the snprintf function is the number
306 dnl of bytes (excluding the terminating NUL) that would have been produced
307 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
308 dnl Result is gl_cv_func_snprintf_retval_c99.
309
310 AC_DEFUN([gl_SNPRINTF_RETVAL_C99],
311 [
312   AC_REQUIRE([AC_PROG_CC])
313   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
314   AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
315     [gl_cv_func_snprintf_retval_c99], 
316     [
317       AC_TRY_RUN([
318 #include <stdio.h>
319 #include <string.h>
320 static char buf[100];
321 int main ()
322 {
323   strcpy (buf, "ABCDEF");
324   if (snprintf (buf, 3, "%d %d", 4567, 89) != 7)
325     return 1;
326   return 0;
327 }], [gl_cv_func_snprintf_retval_c99=yes], [gl_cv_func_snprintf_retval_c99=no],
328       [
329 changequote(,)dnl
330        case "$host_os" in
331                                # Guess yes on glibc systems.
332          *-gnu*)               gl_cv_func_snprintf_retval_c99="guessing yes";;
333                                # Guess yes on FreeBSD >= 5.
334          freebsd[1-4]*)        gl_cv_func_snprintf_retval_c99="guessing no";;
335          freebsd* | kfreebsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
336                                # Guess yes on MacOS X >= 10.3.
337          darwin[1-6].*)        gl_cv_func_snprintf_retval_c99="guessing no";;
338          darwin*)              gl_cv_func_snprintf_retval_c99="guessing yes";;
339                                # Guess yes on OpenBSD >= 3.9.
340          openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
341                                gl_cv_func_snprintf_retval_c99="guessing no";;
342          openbsd*)             gl_cv_func_snprintf_retval_c99="guessing yes";;
343                                # Guess yes on Solaris >= 2.6.
344          solaris2.[0-5]*)      gl_cv_func_snprintf_retval_c99="guessing no";;
345          solaris*)             gl_cv_func_snprintf_retval_c99="guessing yes";;
346                                # Guess yes on AIX >= 4.
347          aix[1-3]*)            gl_cv_func_snprintf_retval_c99="guessing no";;
348          aix*)                 gl_cv_func_snprintf_retval_c99="guessing yes";;
349                                # Guess yes on NetBSD >= 3.
350          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
351                                gl_cv_func_snprintf_retval_c99="guessing no";;
352          netbsd*)              gl_cv_func_snprintf_retval_c99="guessing yes";;
353                                # Guess yes on BeOS.
354          beos*)                gl_cv_func_snprintf_retval_c99="guessing yes";;
355                                # If we don't know, assume the worst.
356          *)                    gl_cv_func_snprintf_retval_c99="guessing no";;
357        esac
358 changequote([,])dnl
359       ])
360     ])
361 ])
362
363 dnl Test whether the snprintf function supports the %n format directive
364 dnl also in truncated portions of the format string. (ISO C99, POSIX:2001)
365 dnl Result is gl_cv_func_snprintf_directive_n.
366
367 AC_DEFUN([gl_SNPRINTF_DIRECTIVE_N],
368 [
369   AC_REQUIRE([AC_PROG_CC])
370   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
371   AC_CACHE_CHECK([whether snprintf fully supports the 'n' directive],
372     [gl_cv_func_snprintf_directive_n], 
373     [
374       AC_TRY_RUN([
375 #include <stdio.h>
376 #include <string.h>
377 static char buf[100];
378 int main ()
379 {
380   int count = -1;
381   snprintf (buf, 4, "%d %n", 12345, &count, 33, 44, 55);
382   if (count != 6)
383     return 1;
384   return 0;
385 }], [gl_cv_func_snprintf_directive_n=yes], [gl_cv_func_snprintf_directive_n=no],
386       [
387 changequote(,)dnl
388        case "$host_os" in
389                                # Guess yes on glibc systems.
390          *-gnu*)               gl_cv_func_snprintf_directive_n="guessing yes";;
391                                # Guess yes on FreeBSD >= 5.
392          freebsd[1-4]*)        gl_cv_func_snprintf_directive_n="guessing no";;
393          freebsd* | kfreebsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
394                                # Guess yes on MacOS X >= 10.3.
395          darwin[1-6].*)        gl_cv_func_snprintf_directive_n="guessing no";;
396          darwin*)              gl_cv_func_snprintf_directive_n="guessing yes";;
397                                # Guess yes on Solaris >= 2.6.
398          solaris2.[0-5]*)      gl_cv_func_snprintf_directive_n="guessing no";;
399          solaris*)             gl_cv_func_snprintf_directive_n="guessing yes";;
400                                # Guess yes on AIX >= 4.
401          aix[1-3]*)            gl_cv_func_snprintf_directive_n="guessing no";;
402          aix*)                 gl_cv_func_snprintf_directive_n="guessing yes";;
403                                # Guess yes on IRIX >= 6.5.
404          irix6.5)              gl_cv_func_snprintf_directive_n="guessing yes";;
405                                # Guess yes on OSF/1 >= 5.
406          osf[3-4]*)            gl_cv_func_snprintf_directive_n="guessing no";;
407          osf*)                 gl_cv_func_snprintf_directive_n="guessing yes";;
408                                # Guess yes on NetBSD >= 3.
409          netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
410                                gl_cv_func_snprintf_directive_n="guessing no";;
411          netbsd*)              gl_cv_func_snprintf_directive_n="guessing yes";;
412                                # Guess yes on BeOS.
413          beos*)                gl_cv_func_snprintf_directive_n="guessing yes";;
414                                # If we don't know, assume the worst.
415          *)                    gl_cv_func_snprintf_directive_n="guessing no";;
416        esac
417 changequote([,])dnl
418       ])
419     ])
420 ])
421
422 dnl The results of these tests on various platforms are:
423 dnl
424 dnl 1 = gl_PRINTF_SIZES_C99
425 dnl 2 = gl_PRINTF_DIRECTIVE_A
426 dnl 3 = gl_PRINTF_DIRECTIVE_N
427 dnl 4 = gl_PRINTF_POSITIONS
428 dnl 5 = gl_SNPRINTF_PRESENCE
429 dnl 6 = gl_SNPRINTF_TRUNCATION_C99
430 dnl 7 = gl_SNPRINTF_RETVAL_C99
431 dnl 8 = gl_SNPRINTF_DIRECTIVE_N
432 dnl
433 dnl 1 = checking whether printf supports size specifiers as in C99...
434 dnl 2 = checking whether printf supports the 'a' and 'A' directives...
435 dnl 3 = checking whether printf supports the 'n' directive...
436 dnl 4 = checking whether printf supports POSIX/XSI format strings with positions...
437 dnl 5 = checking for snprintf...
438 dnl 6 = checking whether snprintf truncates the result as in C99...
439 dnl 7 = checking whether snprintf returns a byte count as in C99...
440 dnl 8 = checking whether snprintf fully supports the 'n' directive...
441 dnl
442 dnl . = yes, # = no.
443 dnl
444 dnl                                        1  2  3  4  5  6  7  8
445 dnl   glibc 2.5                            .  .  .  .  .  .  .  .
446 dnl   glibc 2.3.6                          .  #  .  .  .  .  .  .
447 dnl   FreeBSD 5.4, 6.1                     .  ?  .  .  .  .  .  .
448 dnl   MacOS X 10.3.9                       .  #  .  .  .  .  .  .
449 dnl   OpenBSD 3.9, 4.0                     .  #  .  .  .  .  .  ?
450 dnl   Cygwin 2007                          .  #  .  .  .  .  .  ?
451 dnl   Cygwin 2006                          #  #  .  .  .  .  .  ?
452 dnl   Solaris 10                           .  #  .  .  .  .  .  .
453 dnl   Solaris 2.6 ... 9                    #  #  .  .  .  .  .  .
454 dnl   Solaris 2.5.1                        #  #  .  .  #  #  #  #
455 dnl   AIX 4.3.2, 5.1                       #  #  .  .  .  .  .  .
456 dnl   HP-UX 11.31                          .  #  .  .  .  .  #  #
457 dnl   HP-UX 10.20, 11.00, 11.11, 11.23     #  #  .  .  .  .  #  #
458 dnl   IRIX 6.5                             #  #  .  .  .  .  #  .
459 dnl   OSF/1 5.1                            #  #  .  .  .  .  #  .
460 dnl   OSF/1 4.0d                           #  #  .  .  #  #  #  #
461 dnl   NetBSD 4.0                           .  ?  .  .  .  .  .  ?
462 dnl   NetBSD 3.0                           .  #  .  #  .  .  .  .
463 dnl   BeOS                                 #  #  .  #  .  .  .  .
464 dnl   mingw                                #  #  .  #  .  #  #  #