7c97b95de05baa47d90e2a8492410f3df8706f45
[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([AC_CANONICAL_HOST]) dnl for cross-compiles
91   AC_CACHE_CHECK([whether printf supports the 'a' and 'A' directives],
92     [gl_cv_func_printf_directive_a], 
93     [
94       AC_TRY_RUN([
95 #include <stdio.h>
96 #include <string.h>
97 static char buf[100];
98 int main ()
99 {
100   if (sprintf (buf, "%a %d", 3.1416015625, 33, 44, 55) < 0
101       || strcmp (buf, "0x1.922p+1 33") != 0)
102     return 1;
103   if (sprintf (buf, "%A %d", -3.1416015625, 33, 44, 55) < 0
104       || strcmp (buf, "-0X1.922P+1 33") != 0)
105     return 1;
106   return 0;
107 }], [gl_cv_func_printf_directive_a=yes], [gl_cv_func_printf_directive_a=no],
108       [
109 changequote(,)dnl
110        case "$host_os" in
111                                dnl Guess yes on glibc systems.
112          *-gnu*)               gl_cv_func_printf_directive_a="guessing yes";;
113                                dnl Guess yes on FreeBSD >= 5.
114          freebsd[1-4]*)        gl_cv_func_printf_directive_a="guessing no";;
115          freebsd* | kfreebsd*) gl_cv_func_printf_directive_a="guessing yes";;
116                                dnl Gusss yes on MacOS X >= 10.3.
117          darwin[1-6].*)        gl_cv_func_printf_directive_a="guessing no";;
118          darwin*)              gl_cv_func_printf_directive_a="guessing yes";;
119                                dnl If we don't know, assume the worst.
120          *)                    gl_cv_func_printf_directive_a="guessing no";;
121        esac
122 changequote([,])dnl
123       ])
124     ])
125 ])
126
127 dnl Test whether the *printf family of functions supports the %n format
128 dnl directive. (ISO C99, POSIX:2001)
129 dnl Result is gl_cv_func_printf_directive_n.
130
131 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
132 [
133   AC_REQUIRE([AC_PROG_CC])
134   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
135   AC_CACHE_CHECK([whether printf supports the 'n' directive],
136     [gl_cv_func_printf_directive_n], 
137     [
138       AC_TRY_RUN([
139 #include <stdio.h>
140 #include <string.h>
141 static char buf[100];
142 int main ()
143 {
144   int count = -1;
145   if (sprintf (buf, "%d %n", 123, &count, 33, 44, 55) < 0
146       || strcmp (buf, "123 ") != 0
147       || count != 4)
148     return 1;
149   return 0;
150 }], [gl_cv_func_printf_directive_n=yes], [gl_cv_func_printf_directive_n=no],
151       [
152 changequote(,)dnl
153        case "$host_os" in
154          hpux*) gl_cv_func_printf_directive_n="guessing no";;
155          *)     gl_cv_func_printf_directive_n="guessing yes";;
156        esac
157 changequote([,])dnl
158       ])
159     ])
160 ])
161
162 dnl Test whether the *printf family of functions supports POSIX/XSI format
163 dnl strings with positions. (POSIX:2001)
164 dnl Result is gl_cv_func_printf_positions.
165
166 AC_DEFUN([gl_PRINTF_POSITIONS],
167 [
168   AC_REQUIRE([AC_PROG_CC])
169   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
170   AC_CACHE_CHECK([whether printf supports POSIX/XSI format strings with positions],
171     [gl_cv_func_printf_positions], 
172     [
173       AC_TRY_RUN([
174 #include <stdio.h>
175 #include <string.h>
176 /* The string "%2$d %1$d", with dollar characters protected from the shell's
177    dollar expansion (possibly an autoconf bug).  */
178 static char format[] = { '%', '2', '$', 'd', ' ', '%', '1', '$', 'd', '\0' };
179 static char buf[100];
180 int main ()
181 {
182   sprintf (buf, format, 33, 55);
183   return (strcmp (buf, "55 33") != 0);
184 }], [gl_cv_func_printf_positions=yes], [gl_cv_func_printf_positions=no],
185       [
186 changequote(,)dnl
187        case "$host_os" in
188          netbsd*)      gl_cv_func_printf_positions="guessing no";;
189          beos*)        gl_cv_func_printf_positions="guessing no";;
190          mingw* | pw*) gl_cv_func_printf_positions="guessing no";;
191          *)            gl_cv_func_printf_positions="guessing yes";;
192        esac
193 changequote([,])dnl
194       ])
195     ])
196 ])
197
198 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
199 dnl Result is ac_cv_func_snprintf.
200
201 AC_DEFUN([gl_SNPRINTF_PRESENCE],
202 [
203   AC_CHECK_FUNCS_ONCE([snprintf])
204 ])
205
206 dnl Test whether the string produced by the snprintf function is always NUL
207 dnl terminated. (ISO C99, POSIX:2001)
208 dnl Result is gl_cv_func_snprintf_truncation_c99.
209
210 AC_DEFUN([gl_SNPRINTF_TRUNCATION_C99],
211 [
212   AC_REQUIRE([AC_PROG_CC])
213   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
214   AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
215     [gl_cv_func_snprintf_truncation_c99], 
216     [
217       AC_TRY_RUN([
218 #include <stdio.h>
219 #include <string.h>
220 static char buf[100];
221 int main ()
222 {
223   strcpy (buf, "ABCDEF");
224   snprintf (buf, 3, "%d %d", 4567, 89);
225   if (memcmp (buf, "45\0DEF", 6) != 0)
226     return 1;
227   return 0;
228 }], [gl_cv_func_snprintf_truncation_c99=yes], [gl_cv_func_snprintf_truncation_c99=no],
229       [
230 changequote(,)dnl
231        case "$host_os" in
232                                dnl Guess yes on glibc systems.
233          *-gnu*)               gl_cv_func_snprintf_truncation_c99="guessing yes";;
234                                dnl Guess yes on FreeBSD >= 5.
235          freebsd[1-4]*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
236          freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
237                                dnl Gusss yes on MacOS X >= 10.3.
238          darwin[1-6].*)        gl_cv_func_snprintf_truncation_c99="guessing no";;
239          darwin*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
240                                dnl Guess yes on Solaris >= 2.6.
241          solaris2.[0-5]*)      gl_cv_func_snprintf_truncation_c99="guessing no";;
242          solaris*)             gl_cv_func_snprintf_truncation_c99="guessing yes";;
243                                dnl Guess yes on AIX >= 4.
244          aix[1-3]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
245          aix*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
246                                dnl Guess yes on HP-UX >= 11.
247          hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
248          hpux*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
249                                dnl Guess yes on IRIX >= 6.5.
250          irix6.5)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
251                                dnl Guess yes on OSF/1 >= 5.
252          osf[3-4]*)            gl_cv_func_snprintf_truncation_c99="guessing no";;
253          osf*)                 gl_cv_func_snprintf_truncation_c99="guessing yes";;
254                                dnl Guess yes on NetBSD >= 3.
255          netbsd[1-2]*)         gl_cv_func_snprintf_truncation_c99="guessing no";;
256          netbsd*)              gl_cv_func_snprintf_truncation_c99="guessing yes";;
257                                dnl Guess yes on BeOS.
258          beos*)                gl_cv_func_snprintf_truncation_c99="guessing yes";;
259                                dnl If we don't know, assume the worst.
260          *)                    gl_cv_func_snprintf_truncation_c99="guessing no";;
261        esac
262 changequote([,])dnl
263       ])
264     ])
265 ])
266
267 dnl Test whether the return value of the snprintf function is the number
268 dnl of bytes (excluding the terminating NUL) that would have been produced
269 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
270 dnl Result is gl_cv_func_printf_retval_c99.
271
272 AC_DEFUN([gl_SNPRINTF_RETVAL_C99],
273 [
274   AC_REQUIRE([AC_PROG_CC])
275   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
276   AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
277     [gl_cv_func_printf_retval_c99], 
278     [
279       AC_TRY_RUN([
280 #include <stdio.h>
281 #include <string.h>
282 static char buf[100];
283 int main ()
284 {
285   strcpy (buf, "ABCDEF");
286   if (snprintf (buf, 3, "%d %d", 4567, 89) != 7)
287     return 1;
288   return 0;
289 }], [gl_cv_func_printf_retval_c99=yes], [gl_cv_func_printf_retval_c99=no],
290       [
291 changequote(,)dnl
292        case "$host_os" in
293                                dnl Guess yes on glibc systems.
294          *-gnu*)               gl_cv_func_printf_retval_c99="guessing yes";;
295                                dnl Guess yes on FreeBSD >= 5.
296          freebsd[1-4]*)        gl_cv_func_printf_retval_c99="guessing no";;
297          freebsd* | kfreebsd*) gl_cv_func_printf_retval_c99="guessing yes";;
298                                dnl Gusss yes on MacOS X >= 10.3.
299          darwin[1-6].*)        gl_cv_func_printf_retval_c99="guessing no";;
300          darwin*)              gl_cv_func_printf_retval_c99="guessing yes";;
301                                dnl Guess yes on Solaris >= 2.6.
302          solaris2.[0-5]*)      gl_cv_func_printf_retval_c99="guessing no";;
303          solaris*)             gl_cv_func_printf_retval_c99="guessing yes";;
304                                dnl Guess yes on AIX >= 4.
305          aix[1-3]*)            gl_cv_func_printf_retval_c99="guessing no";;
306          aix*)                 gl_cv_func_printf_retval_c99="guessing yes";;
307                                dnl Guess yes on NetBSD >= 3.
308          netbsd[1-2]*)         gl_cv_func_printf_retval_c99="guessing no";;
309          netbsd*)              gl_cv_func_printf_retval_c99="guessing yes";;
310                                dnl Guess yes on BeOS.
311          beos*)                gl_cv_func_printf_retval_c99="guessing yes";;
312                                dnl If we don't know, assume the worst.
313          *)                    gl_cv_func_printf_retval_c99="guessing no";;
314        esac
315 changequote([,])dnl
316       ])
317     ])
318 ])
319
320 dnl The results of these tests on various platforms are:
321 dnl
322 dnl 1 = gl_PRINTF_SIZES_C99
323 dnl 2 = gl_PRINTF_DIRECTIVE_A
324 dnl 3 = gl_PRINTF_DIRECTIVE_N
325 dnl 4 = gl_PRINTF_POSITIONS
326 dnl 5 = gl_SNPRINTF_PRESENCE
327 dnl 6 = gl_SNPRINTF_TRUNCATION_C99
328 dnl 7 = gl_SNPRINTF_RETVAL_C99
329 dnl
330 dnl 1 = checking whether printf supports size specifiers as in C99...
331 dnl 2 = checking whether printf supports the 'a' and 'A' directives...
332 dnl 3 = checking whether printf supports the 'n' directive...
333 dnl 4 = checking whether printf supports POSIX/XSI format strings with positions...
334 dnl 5 = checking for snprintf...
335 dnl 6 = checking whether snprintf truncates the result as in C99...
336 dnl 7 = checking whether snprintf returns a byte count as in C99...
337 dnl
338 dnl . = yes, # = no.
339 dnl
340 dnl                                   1  2  3  4  5  6  7
341 dnl   glibc 2.3.6                     .  .  .  .  .  .  .
342 dnl   FreeBSD 5.4, 6.1                .  .  .  .  .  .  .
343 dnl   MacOS X 10.3.9                  .  .  .  .  .  .  .
344 dnl   Cygwin 2007                     .  #  .  .  .  .  .
345 dnl   Solaris 10                      .  #  .  .  .  .  .
346 dnl   Solaris 2.6 ... 9               #  #  .  .  .  .  .
347 dnl   Solaris 2.5.1                   #  #  .  .  #  #  #
348 dnl   AIX 4.3.2, 5.1                  #  #  .  .  .  .  .
349 dnl   HP-UX 11.31                     .  #  .  .  .  .  #
350 dnl   HP-UX 11.00, 11.11, 11.23       #  #  .  .  .  .  #
351 dnl   HP-UX 10.20                     #  #  #  ?  .  ?  #
352 dnl   IRIX 6.5                        #  #  .  .  .  .  #
353 dnl   OSF/1 5.1                       #  #  .  .  .  .  #
354 dnl   OSF/1 4.0d                      #  #  .  .  #  #  #
355 dnl   NetBSD 3.0                      .  #  .  #  .  .  .
356 dnl   BeOS                            #  #  .  #  .  .  .
357 dnl   mingw                           #  #  .  #  .  #  #