Add info about some older Cygwin.
[gnulib.git] / m4 / printf.m4
1 # printf.m4 serial 1
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([gt_TYPE_LONGDOUBLE])
17   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
18   AC_CACHE_CHECK([whether printf supports size specifiers as in C99],
19     [gl_cv_func_printf_sizes_c99], 
20     [
21       AC_TRY_RUN([
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 #if HAVE_STDINT_H_WITH_UINTMAX || HAVE_INTTYPES_H_WITH_UINTMAX
36   buf[0] = '\0';
37   if (sprintf (buf, "%ju %d", (uintmax_t) 12345671, 33, 44, 55) < 0
38       || strcmp (buf, "12345671 33") != 0)
39     return 1;
40 #endif
41   buf[0] = '\0';
42   if (sprintf (buf, "%zu %d", (size_t) 12345672, 33, 44, 55) < 0
43       || strcmp (buf, "12345672 33") != 0)
44     return 1;
45   buf[0] = '\0';
46   if (sprintf (buf, "%tu %d", (ptrdiff_t) 12345673, 33, 44, 55) < 0
47       || strcmp (buf, "12345673 33") != 0)
48     return 1;
49 #if HAVE_LONG_DOUBLE
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     return 1;
54 #endif
55   return 0;
56 }], [gl_cv_func_printf_sizes_c99=yes], [gl_cv_func_printf_sizes_c99=no],
57       [
58 changequote(,)dnl
59        case "$host_os" in
60                                dnl Guess yes on glibc systems.
61          *-gnu*)               gl_cv_func_printf_sizes_c99="guessing yes";;
62                                dnl Guess yes on FreeBSD >= 5.
63          freebsd[1-4]*)        gl_cv_func_printf_sizes_c99="guessing no";;
64          freebsd* | kfreebsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
65                                dnl Gusss yes on MacOS X >= 10.3.
66          darwin[1-6].*)        gl_cv_func_printf_sizes_c99="guessing no";;
67          darwin*)              gl_cv_func_printf_sizes_c99="guessing yes";;
68                                dnl Guess yes on Solaris >= 2.10.
69          solaris2.[0-9]*)      gl_cv_func_printf_sizes_c99="guessing no";;
70          solaris*)             gl_cv_func_printf_sizes_c99="guessing yes";;
71                                dnl Guess yes on NetBSD >= 3.
72          netbsd[1-2]*)         gl_cv_func_printf_sizes_c99="guessing no";;
73          netbsd*)              gl_cv_func_printf_sizes_c99="guessing yes";;
74                                dnl If we don't know, assume the worst.
75          *)                    gl_cv_func_printf_sizes_c99="guessing no";;
76        esac
77 changequote([,])dnl
78       ])
79     ])
80 ])
81
82 dnl Test whether the *printf family of functions supports the 'a' and 'A'
83 dnl conversion specifier for hexadecimal output of floating-point numbers.
84 dnl (ISO C99, POSIX:2001)
85 dnl Result is gl_cv_func_printf_directive_a.
86
87 AC_DEFUN([gl_PRINTF_DIRECTIVE_A],
88 [
89   AC_REQUIRE([AC_PROG_CC])
90   AC_REQUIRE([gt_TYPE_LONGDOUBLE])
91   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
92   AC_CACHE_CHECK([whether printf supports the 'a' and 'A' directives],
93     [gl_cv_func_printf_directive_a], 
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, "%a %d", 3.1416015625, 33, 44, 55) < 0
102       || (strcmp (buf, "0x1.922p+1 33") != 0
103           && strcmp (buf, "0x3.244p+0 33") != 0
104           && strcmp (buf, "0x6.488p-1 33") != 0
105           && strcmp (buf, "0xc.91p-2 33") != 0))
106     return 1;
107   if (sprintf (buf, "%A %d", -3.1416015625, 33, 44, 55) < 0
108       || (strcmp (buf, "-0X1.922P+1 33") != 0
109           && strcmp (buf, "-0X3.244P+0 33") != 0
110           && strcmp (buf, "-0X6.488P-1 33") != 0
111           && strcmp (buf, "-0XC.91P-2 33") != 0))
112     return 1;
113   /* This catches a MacOS X 10.3.9 (Darwin 7.9) bug.  */
114   if (sprintf (buf, "%.1a", 1.999) < 0
115       || (strcmp (buf, "0x1.0p+1") != 0
116           && strcmp (buf, "0x2.0p+0") != 0
117           && strcmp (buf, "0x4.0p-1") != 0
118           && strcmp (buf, "0x8.0p-2") != 0))
119     return 1;
120 #if HAVE_LONG_DOUBLE
121   /* This catches the same MacOS X 10.3.9 (Darwin 7.9) bug and also a
122      glibc 2.4 bug <http://sourceware.org/bugzilla/show_bug.cgi?id=2908>.  */
123   if (sprintf (buf, "%.1La", 1.999L) < 0
124       || (strcmp (buf, "0x1.0p+1") != 0
125           && strcmp (buf, "0x2.0p+0") != 0
126           && strcmp (buf, "0x4.0p-1") != 0
127           && strcmp (buf, "0x8.0p-2") != 0))
128     return 1;
129 #endif
130   return 0;
131 }], [gl_cv_func_printf_directive_a=yes], [gl_cv_func_printf_directive_a=no],
132       [
133        case "$host_os" in
134                                dnl Guess yes on glibc >= 2.5 systems.
135          *-gnu*)
136            AC_EGREP_CPP([BZ2908], [
137              #include <features.h>
138              #ifdef __GNU_LIBRARY__
139               #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)
140                BZ2908
141               #endif
142              #endif
143              ],
144              [gl_cv_func_printf_directive_a="guessing yes"],
145              [gl_cv_func_printf_directive_a="guessing no"])
146            ;;
147 changequote(,)dnl
148                                dnl Guess yes on FreeBSD >= 5.
149          freebsd[1-4]*)        gl_cv_func_printf_directive_a="guessing no";;
150          freebsd* | kfreebsd*) gl_cv_func_printf_directive_a="guessing yes";;
151                                dnl If we don't know, assume the worst.
152          *)                    gl_cv_func_printf_directive_a="guessing no";;
153        esac
154 changequote([,])dnl
155       ])
156     ])
157 ])
158
159 dnl Test whether the *printf family of functions supports the %n format
160 dnl directive. (ISO C99, POSIX:2001)
161 dnl Result is gl_cv_func_printf_directive_n.
162
163 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
164 [
165   AC_REQUIRE([AC_PROG_CC])
166   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
167   AC_CACHE_CHECK([whether printf supports the 'n' directive],
168     [gl_cv_func_printf_directive_n], 
169     [
170       AC_TRY_RUN([
171 #include <stdio.h>
172 #include <string.h>
173 static char buf[100];
174 int main ()
175 {
176   int count = -1;
177   if (sprintf (buf, "%d %n", 123, &count, 33, 44, 55) < 0
178       || strcmp (buf, "123 ") != 0
179       || count != 4)
180     return 1;
181   return 0;
182 }], [gl_cv_func_printf_directive_n=yes], [gl_cv_func_printf_directive_n=no],
183       [
184 changequote(,)dnl
185        case "$host_os" in
186          hpux*) gl_cv_func_printf_directive_n="guessing no";;
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*)      gl_cv_func_printf_positions="guessing no";;
221          beos*)        gl_cv_func_printf_positions="guessing no";;
222          mingw* | pw*) gl_cv_func_printf_positions="guessing no";;
223          *)            gl_cv_func_printf_positions="guessing yes";;
224        esac
225 changequote([,])dnl
226       ])
227     ])
228 ])
229
230 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
231 dnl Result is ac_cv_func_snprintf.
232
233 AC_DEFUN([gl_SNPRINTF_PRESENCE],
234 [
235   AC_CHECK_FUNCS_ONCE([snprintf])
236 ])
237
238 dnl Test whether the string produced by the snprintf function is always NUL
239 dnl terminated. (ISO C99, POSIX:2001)
240 dnl Result is gl_cv_func_snprintf_truncation_c99.
241
242 AC_DEFUN([gl_SNPRINTF_TRUNCATION_C99],
243 [
244   AC_REQUIRE([AC_PROG_CC])
245   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
246   AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
247     [gl_cv_func_snprintf_truncation_c99], 
248     [
249       AC_TRY_RUN([
250 #include <stdio.h>
251 #include <string.h>
252 static char buf[100];
253 int main ()
254 {
255   strcpy (buf, "ABCDEF");
256   snprintf (buf, 3, "%d %d", 4567, 89);
257   if (memcmp (buf, "45\0DEF", 6) != 0)
258     return 1;
259   return 0;
260 }], [gl_cv_func_snprintf_truncation_c99=yes], [gl_cv_func_snprintf_truncation_c99=no],
261       [
262 changequote(,)dnl
263        case "$host_os" in
264                                dnl Guess yes on glibc systems.
265          *-gnu*)               gl_cv_func_snprintf_truncation_c99="guessing yes";;
266                                dnl Guess yes on FreeBSD >= 5.
267          freebsd[1-4]*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
268          freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
269                                dnl Gusss yes on MacOS X >= 10.3.
270          darwin[1-6].*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
271          darwin*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
272                                dnl Guess yes on Solaris >= 2.6.
273          solaris2.[0-5]*)      gl_cv_func_snprintf_truncation_c99="guessing no";;
274          solaris*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
275                                dnl Guess yes on AIX >= 4.
276          aix[1-3]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
277          aix*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
278                                dnl Guess yes on HP-UX >= 11.
279          hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
280          hpux*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
281                                dnl Guess yes on IRIX >= 6.5.
282          irix6.5)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
283                                dnl Guess yes on OSF/1 >= 5.
284          osf[3-4]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
285          osf*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
286                                dnl Guess yes on NetBSD >= 3.
287          netbsd[1-2]*)         gl_cv_func_snprintf_truncation_c99="guessing no";;
288          netbsd*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
289                                dnl Guess yes on BeOS.
290          beos*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
291                                dnl If we don't know, assume the worst.
292          *)                    gl_cv_func_snprintf_truncation_c99="guessing no";;
293        esac
294 changequote([,])dnl
295       ])
296     ])
297 ])
298
299 dnl Test whether the return value of the snprintf function is the number
300 dnl of bytes (excluding the terminating NUL) that would have been produced
301 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
302 dnl Result is gl_cv_func_printf_retval_c99.
303
304 AC_DEFUN([gl_SNPRINTF_RETVAL_C99],
305 [
306   AC_REQUIRE([AC_PROG_CC])
307   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
308   AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
309     [gl_cv_func_printf_retval_c99], 
310     [
311       AC_TRY_RUN([
312 #include <stdio.h>
313 #include <string.h>
314 static char buf[100];
315 int main ()
316 {
317   strcpy (buf, "ABCDEF");
318   if (snprintf (buf, 3, "%d %d", 4567, 89) != 7)
319     return 1;
320   return 0;
321 }], [gl_cv_func_printf_retval_c99=yes], [gl_cv_func_printf_retval_c99=no],
322       [
323 changequote(,)dnl
324        case "$host_os" in
325                                dnl Guess yes on glibc systems.
326          *-gnu*)               gl_cv_func_printf_retval_c99="guessing yes";;
327                                dnl Guess yes on FreeBSD >= 5.
328          freebsd[1-4]*)        gl_cv_func_printf_retval_c99="guessing no";;
329          freebsd* | kfreebsd*) gl_cv_func_printf_retval_c99="guessing yes";;
330                                dnl Gusss yes on MacOS X >= 10.3.
331          darwin[1-6].*)        gl_cv_func_printf_retval_c99="guessing no";;
332          darwin*)              gl_cv_func_printf_retval_c99="guessing yes";;
333                                dnl Guess yes on Solaris >= 2.6.
334          solaris2.[0-5]*)      gl_cv_func_printf_retval_c99="guessing no";;
335          solaris*)             gl_cv_func_printf_retval_c99="guessing yes";;
336                                dnl Guess yes on AIX >= 4.
337          aix[1-3]*)            gl_cv_func_printf_retval_c99="guessing no";;
338          aix*)                 gl_cv_func_printf_retval_c99="guessing yes";;
339                                dnl Guess yes on NetBSD >= 3.
340          netbsd[1-2]*)         gl_cv_func_printf_retval_c99="guessing no";;
341          netbsd*)              gl_cv_func_printf_retval_c99="guessing yes";;
342                                dnl Guess yes on BeOS.
343          beos*)                gl_cv_func_printf_retval_c99="guessing yes";;
344                                dnl If we don't know, assume the worst.
345          *)                    gl_cv_func_printf_retval_c99="guessing no";;
346        esac
347 changequote([,])dnl
348       ])
349     ])
350 ])
351
352 dnl The results of these tests on various platforms are:
353 dnl
354 dnl 1 = gl_PRINTF_SIZES_C99
355 dnl 2 = gl_PRINTF_DIRECTIVE_A
356 dnl 3 = gl_PRINTF_DIRECTIVE_N
357 dnl 4 = gl_PRINTF_POSITIONS
358 dnl 5 = gl_SNPRINTF_PRESENCE
359 dnl 6 = gl_SNPRINTF_TRUNCATION_C99
360 dnl 7 = gl_SNPRINTF_RETVAL_C99
361 dnl
362 dnl 1 = checking whether printf supports size specifiers as in C99...
363 dnl 2 = checking whether printf supports the 'a' and 'A' directives...
364 dnl 3 = checking whether printf supports the 'n' directive...
365 dnl 4 = checking whether printf supports POSIX/XSI format strings with positions...
366 dnl 5 = checking for snprintf...
367 dnl 6 = checking whether snprintf truncates the result as in C99...
368 dnl 7 = checking whether snprintf returns a byte count as in C99...
369 dnl
370 dnl . = yes, # = no.
371 dnl
372 dnl                                   1  2  3  4  5  6  7
373 dnl   glibc 2.3.6                     .  #  .  .  .  .  .
374 dnl   FreeBSD 5.4, 6.1                .  .  .  .  .  .  .
375 dnl   MacOS X 10.3.9                  .  #  .  .  .  .  .
376 dnl   Cygwin 2007                     .  #  .  .  .  .  .
377 dnl   Cygwin 2006                     #  #  .  .  .  .  .
378 dnl   Solaris 10                      .  #  .  .  .  .  .
379 dnl   Solaris 2.6 ... 9               #  #  .  .  .  .  .
380 dnl   Solaris 2.5.1                   #  #  .  .  #  #  #
381 dnl   AIX 4.3.2, 5.1                  #  #  .  .  .  .  .
382 dnl   HP-UX 11.31                     .  #  .  .  .  .  #
383 dnl   HP-UX 11.00, 11.11, 11.23       #  #  .  .  .  .  #
384 dnl   HP-UX 10.20                     #  #  #  ?  .  ?  #
385 dnl   IRIX 6.5                        #  #  .  .  .  .  #
386 dnl   OSF/1 5.1                       #  #  .  .  .  .  #
387 dnl   OSF/1 4.0d                      #  #  .  .  #  #  #
388 dnl   NetBSD 3.0                      .  #  .  #  .  .  .
389 dnl   BeOS                            #  #  .  #  .  .  .
390 dnl   mingw                           #  #  .  #  .  #  #