Debian Codegraph @

m4/printf.m4

raw
1 # printf.m4
2 # serial 98
3 dnl Copyright (C) 2003, 2007-2026 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 dnl Test whether the *printf family of functions supports the 'j', 'z', 't',
10 dnl 'L' size specifiers. (ISO C99, POSIX:2001)
11 dnl Result is gl_cv_func_printf_sizes_c99.
12
13 AC_DEFUN([gl_PRINTF_SIZES_C99],
14 [
15 AC_REQUIRE([AC_PROG_CC])
16 AC_REQUIRE([gl_AC_HEADER_STDINT_H])
17 AC_REQUIRE([gl_AC_HEADER_INTTYPES_H])
18 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
19 AC_CACHE_CHECK([whether printf supports size specifiers as in C99],
20 [gl_cv_func_printf_sizes_c99],
21 [
22 AC_RUN_IFELSE(
23 [AC_LANG_SOURCE([[
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #if HAVE_STDINT_H_WITH_UINTMAX
29 # include <stdint.h>
30 #endif
31 #if HAVE_INTTYPES_H_WITH_UINTMAX
32 # include <inttypes.h>
33 #endif
34 static char buf[100];
35 int main ()
36 {
37 int result = 0;
38 #if HAVE_STDINT_H_WITH_UINTMAX || HAVE_INTTYPES_H_WITH_UINTMAX
39 buf[0] = '\0';
40 if (sprintf (buf, "%ju %d", (uintmax_t) 12345671, 33, 44, 55) < 0
41 || strcmp (buf, "12345671 33") != 0)
42 result |= 1;
43 #else
44 result |= 1;
45 #endif
46 buf[0] = '\0';
47 if (sprintf (buf, "%zu %d", (size_t) 12345672, 33, 44, 55) < 0
48 || strcmp (buf, "12345672 33") != 0)
49 result |= 2;
50 buf[0] = '\0';
51 if (sprintf (buf, "%tu %d", (ptrdiff_t) 12345673, 33, 44, 55) < 0
52 || strcmp (buf, "12345673 33") != 0)
53 result |= 4;
54 buf[0] = '\0';
55 if (sprintf (buf, "%Lg %d", (long double) 1.5, 33, 44, 55) < 0
56 || strcmp (buf, "1.5 33") != 0)
57 result |= 8;
58 return result;
59 }]])],
60 [gl_cv_func_printf_sizes_c99=yes],
61 [gl_cv_func_printf_sizes_c99=no],
62 [
63 case "$host_os" in
64 changequote(,)dnl
65 # Guess yes on glibc systems.
66 *-gnu* | gnu*) gl_cv_func_printf_sizes_c99="guessing yes";;
67 # Guess yes on musl systems.
68 *-musl* | midipix*) gl_cv_func_printf_sizes_c99="guessing yes";;
69 # Guess yes on FreeBSD >= 5.
70 freebsd[1-4].*) gl_cv_func_printf_sizes_c99="guessing no";;
71 freebsd* | kfreebsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
72 midnightbsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
73 # Guess yes on Mac OS X >= 10.3.
74 darwin[1-6].*) gl_cv_func_printf_sizes_c99="guessing no";;
75 darwin*) gl_cv_func_printf_sizes_c99="guessing yes";;
76 # Guess yes on OpenBSD >= 3.9.
77 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
78 gl_cv_func_printf_sizes_c99="guessing no";;
79 openbsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
80 # Guess yes on Solaris >= 2.10.
81 solaris2.[1-9][0-9]*) gl_cv_func_printf_sizes_c99="guessing yes";;
82 solaris*) gl_cv_func_printf_sizes_c99="guessing no";;
83 # Guess yes on NetBSD >= 3.
84 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
85 gl_cv_func_printf_sizes_c99="guessing no";;
86 netbsd*) gl_cv_func_printf_sizes_c99="guessing yes";;
87 # Guess yes on Android.
88 linux*-android*) gl_cv_func_printf_sizes_c99="guessing yes";;
89 changequote([,])dnl
90 # Guess yes on MSVC, no on mingw.
91 windows*-msvc*) gl_cv_func_printf_sizes_c99="guessing yes" ;;
92 mingw* | windows*) AC_EGREP_CPP([Known], [
93 #ifdef _MSC_VER
94 Known
95 #endif
96 ],
97 [gl_cv_func_printf_sizes_c99="guessing yes"],
98 [gl_cv_func_printf_sizes_c99="guessing no"])
99 ;;
100 # If we don't know, obey --enable-cross-guesses.
101 *) gl_cv_func_printf_sizes_c99="$gl_cross_guess_normal";;
102 esac
103 ])
104 ])
105 ])
106
107 dnl Test whether the *printf family of functions supports the 'w8', 'w16',
108 dnl 'w32', 'w64', 'wf8', 'wf16', 'wf32', 'wf64' size specifiers. (ISO C23)
109 dnl Result is gl_cv_func_printf_sizes_c23.
110
111 AC_DEFUN([gl_PRINTF_SIZES_C23],
112 [
113 AC_REQUIRE([AC_PROG_CC])
114 AC_REQUIRE([gl_AC_HEADER_STDINT_H])
115 AC_REQUIRE([gl_AC_HEADER_INTTYPES_H])
116 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
117 AC_CACHE_CHECK([whether printf supports size specifiers as in C23],
118 [gl_cv_func_printf_sizes_c23],
119 [
120 AC_RUN_IFELSE(
121 [AC_LANG_SOURCE([[
122 #include <stddef.h>
123 #include <stdio.h>
124 #include <string.h>
125 #include <sys/types.h>
126 #if HAVE_STDINT_H_WITH_UINTMAX
127 # include <stdint.h>
128 #endif
129 #if HAVE_INTTYPES_H_WITH_UINTMAX
130 # include <inttypes.h>
131 #endif
132 static char buf[100];
133 int main ()
134 {
135 int result = 0;
136 buf[0] = '\0';
137 if (sprintf (buf, "%w8u %d", (uint8_t) 123, 33, 44, 55) < 0
138 || strcmp (buf, "123 33") != 0)
139 result |= 1;
140 buf[0] = '\0';
141 if (sprintf (buf, "%wf8u %d", (uint_fast8_t) 123, 33, 44, 55) < 0
142 || strcmp (buf, "123 33") != 0)
143 result |= 1;
144 buf[0] = '\0';
145 if (sprintf (buf, "%w16u %d", (uint16_t) 12345, 33, 44, 55) < 0
146 || strcmp (buf, "12345 33") != 0)
147 result |= 2;
148 buf[0] = '\0';
149 if (sprintf (buf, "%wf16u %d", (uint_fast16_t) 12345, 33, 44, 55) < 0
150 || strcmp (buf, "12345 33") != 0)
151 result |= 2;
152 buf[0] = '\0';
153 if (sprintf (buf, "%w32u %d", (uint32_t) 12345671, 33, 44, 55) < 0
154 || strcmp (buf, "12345671 33") != 0)
155 result |= 4;
156 buf[0] = '\0';
157 if (sprintf (buf, "%wf32u %d", (uint_fast32_t) 12345671, 33, 44, 55) < 0
158 || strcmp (buf, "12345671 33") != 0)
159 result |= 4;
160 #if HAVE_STDINT_H_WITH_UINTMAX || HAVE_INTTYPES_H_WITH_UINTMAX
161 buf[0] = '\0';
162 if (sprintf (buf, "%w64u %d", (uint64_t) 12345671, 33, 44, 55) < 0
163 || strcmp (buf, "12345671 33") != 0)
164 result |= 8;
165 buf[0] = '\0';
166 if (sprintf (buf, "%wf64u %d", (uint_fast64_t) 12345671, 33, 44, 55) < 0
167 || strcmp (buf, "12345671 33") != 0)
168 result |= 8;
169 #else
170 result |= 8;
171 #endif
172 return result;
173 }]])],
174 [gl_cv_func_printf_sizes_c23=yes],
175 [gl_cv_func_printf_sizes_c23=no],
176 [
177 case "$host_os" in
178 # Guess no on glibc systems.
179 *-gnu* | gnu*) gl_cv_func_printf_sizes_c23="guessing no";;
180 # Guess no on musl systems.
181 *-musl* | midipix*) gl_cv_func_printf_sizes_c23="guessing no";;
182 # Guess no on Android.
183 linux*-android*) gl_cv_func_printf_sizes_c23="guessing no";;
184 # Guess no on native Windows.
185 mingw* | windows*) gl_cv_func_printf_sizes_c23="guessing no";;
186 # If we don't know, obey --enable-cross-guesses.
187 *) gl_cv_func_printf_sizes_c23="$gl_cross_guess_normal";;
188 esac
189 ])
190 ])
191 ])
192
193 dnl Test whether the *printf family of functions supports 'long double'
194 dnl arguments together with the 'L' size specifier. (ISO C99, POSIX:2001)
195 dnl Result is gl_cv_func_printf_long_double.
196
197 AC_DEFUN([gl_PRINTF_LONG_DOUBLE],
198 [
199 AC_REQUIRE([AC_PROG_CC])
200 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
201 AC_CACHE_CHECK([whether printf supports 'long double' arguments],
202 [gl_cv_func_printf_long_double],
203 [
204 AC_RUN_IFELSE(
205 [AC_LANG_SOURCE([[
206 #include <stdio.h>
207 #include <string.h>
208 static char buf[10000];
209 int main ()
210 {
211 int result = 0;
212 buf[0] = '\0';
213 if (sprintf (buf, "%Lf %d", 1.75L, 33, 44, 55) < 0
214 || strcmp (buf, "1.750000 33") != 0)
215 result |= 1;
216 buf[0] = '\0';
217 if (sprintf (buf, "%Le %d", 1.75L, 33, 44, 55) < 0
218 || strcmp (buf, "1.750000e+00 33") != 0)
219 result |= 2;
220 buf[0] = '\0';
221 if (sprintf (buf, "%Lg %d", 1.75L, 33, 44, 55) < 0
222 || strcmp (buf, "1.75 33") != 0)
223 result |= 4;
224 return result;
225 }]])],
226 [gl_cv_func_printf_long_double=yes],
227 [gl_cv_func_printf_long_double=no],
228 [case "$host_os" in
229 # Guess no on BeOS.
230 beos*) gl_cv_func_printf_long_double="guessing no";;
231 # Guess yes on Android.
232 linux*-android*) gl_cv_func_printf_long_double="guessing yes";;
233 # Guess yes on MSVC, no on mingw.
234 windows*-msvc*) gl_cv_func_printf_long_double="guessing yes" ;;
235 mingw* | windows*) AC_EGREP_CPP([Known], [
236 #ifdef _MSC_VER
237 Known
238 #endif
239 ],
240 [gl_cv_func_printf_long_double="guessing yes"],
241 [gl_cv_func_printf_long_double="guessing no"])
242 ;;
243 *) gl_cv_func_printf_long_double="guessing yes";;
244 esac
245 ])
246 ])
247 ])
248
249 dnl Test whether the *printf family of functions supports infinite and NaN
250 dnl 'double' arguments and negative zero arguments in the %f, %e, %g
251 dnl directives. (ISO C99, POSIX:2001)
252 dnl Result is gl_cv_func_printf_infinite.
253
254 AC_DEFUN([gl_PRINTF_INFINITE],
255 [
256 AC_REQUIRE([AC_PROG_CC])
257 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
258 AC_CACHE_CHECK([whether printf supports infinite 'double' arguments],
259 [gl_cv_func_printf_infinite],
260 [
261 AC_RUN_IFELSE(
262 [AC_LANG_SOURCE([[
263 #include <stdio.h>
264 #include <string.h>
265 static int
266 strisnan (const char *string, size_t start_index, size_t end_index)
267 {
268 if (start_index < end_index)
269 {
270 if (string[start_index] == '-')
271 start_index++;
272 if (start_index + 3 <= end_index
273 && memcmp (string + start_index, "nan", 3) == 0)
274 {
275 start_index += 3;
276 if (start_index == end_index
277 || (string[start_index] == '(' && string[end_index - 1] == ')'))
278 return 1;
279 }
280 }
281 return 0;
282 }
283 static int
284 have_minus_zero ()
285 {
286 static double plus_zero = 0.0;
287 double minus_zero = - plus_zero;
288 return memcmp (&plus_zero, &minus_zero, sizeof (double)) != 0;
289 }
290 static char buf[10000];
291 static double zero = 0.0;
292 int main ()
293 {
294 int result = 0;
295 if (sprintf (buf, "%f", 1.0 / zero) < 0
296 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
297 result |= 1;
298 if (sprintf (buf, "%f", -1.0 / zero) < 0
299 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
300 result |= 1;
301 if (sprintf (buf, "%f", zero / zero) < 0
302 || !strisnan (buf, 0, strlen (buf)))
303 result |= 2;
304 if (sprintf (buf, "%e", 1.0 / zero) < 0
305 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
306 result |= 4;
307 if (sprintf (buf, "%e", -1.0 / zero) < 0
308 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
309 result |= 4;
310 if (sprintf (buf, "%e", zero / zero) < 0
311 || !strisnan (buf, 0, strlen (buf)))
312 result |= 8;
313 if (sprintf (buf, "%g", 1.0 / zero) < 0
314 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
315 result |= 16;
316 if (sprintf (buf, "%g", -1.0 / zero) < 0
317 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
318 result |= 16;
319 if (sprintf (buf, "%g", zero / zero) < 0
320 || !strisnan (buf, 0, strlen (buf)))
321 result |= 32;
322 /* This test fails on HP-UX 10.20. */
323 if (have_minus_zero ())
324 if (sprintf (buf, "%g", - zero) < 0
325 || strcmp (buf, "-0") != 0)
326 result |= 64;
327 return result;
328 }]])],
329 [gl_cv_func_printf_infinite=yes],
330 [gl_cv_func_printf_infinite=no],
331 [
332 case "$host_os" in
333 changequote(,)dnl
334 # Guess yes on glibc systems.
335 *-gnu* | gnu*) gl_cv_func_printf_infinite="guessing yes";;
336 # Guess yes on musl systems.
337 *-musl* | midipix*) gl_cv_func_printf_infinite="guessing yes";;
338 # Guess yes on FreeBSD >= 6.
339 freebsd[1-5].*) gl_cv_func_printf_infinite="guessing no";;
340 freebsd* | kfreebsd*) gl_cv_func_printf_infinite="guessing yes";;
341 midnightbsd*) gl_cv_func_printf_infinite="guessing yes";;
342 # Guess yes on Mac OS X >= 10.3.
343 darwin[1-6].*) gl_cv_func_printf_infinite="guessing no";;
344 darwin*) gl_cv_func_printf_infinite="guessing yes";;
345 # Guess yes on HP-UX >= 11.
346 hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite="guessing no";;
347 hpux*) gl_cv_func_printf_infinite="guessing yes";;
348 # Guess yes on NetBSD >= 3.
349 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
350 gl_cv_func_printf_infinite="guessing no";;
351 netbsd*) gl_cv_func_printf_infinite="guessing yes";;
352 # Guess yes on OpenBSD >= 6.0.
353 openbsd[1-5].*) gl_cv_func_printf_infinite="guessing no";;
354 openbsd*) gl_cv_func_printf_infinite="guessing yes";;
355 # Guess yes on BeOS.
356 beos*) gl_cv_func_printf_infinite="guessing yes";;
357 # Guess no on Android.
358 linux*-android*) gl_cv_func_printf_infinite="guessing no";;
359 changequote([,])dnl
360 # Guess yes on MSVC, no on mingw.
361 windows*-msvc*) gl_cv_func_printf_infinite="guessing yes" ;;
362 mingw* | windows*) AC_EGREP_CPP([Known], [
363 #ifdef _MSC_VER
364 Known
365 #endif
366 ],
367 [gl_cv_func_printf_infinite="guessing yes"],
368 [gl_cv_func_printf_infinite="guessing no"])
369 ;;
370 # If we don't know, obey --enable-cross-guesses.
371 *) gl_cv_func_printf_infinite="$gl_cross_guess_normal";;
372 esac
373 ])
374 ])
375 ])
376
377 dnl Test whether the *printf family of functions supports infinite and NaN
378 dnl 'long double' arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001)
379 dnl Result is gl_cv_func_printf_infinite_long_double.
380
381 AC_DEFUN([gl_PRINTF_INFINITE_LONG_DOUBLE],
382 [
383 AC_REQUIRE([gl_PRINTF_LONG_DOUBLE])
384 AC_REQUIRE([AC_PROG_CC])
385 AC_REQUIRE([gl_BIGENDIAN])
386 AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
387 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
388 dnl The user can set or unset the variable gl_printf_safe to indicate
389 dnl that he wishes a safe handling of non-IEEE-754 'long double' values.
390 if test -n "$gl_printf_safe"; then
391 AC_DEFINE([CHECK_PRINTF_SAFE], [1],
392 [Define if you wish *printf() functions that have a safe handling of
393 non-IEEE-754 'long double' values.])
394 fi
395 case "$gl_cv_func_printf_long_double" in
396 *yes)
397 AC_CACHE_CHECK([whether printf supports infinite 'long double' arguments],
398 [gl_cv_func_printf_infinite_long_double],
399 [
400 AC_RUN_IFELSE(
401 [AC_LANG_SOURCE([[
402 ]GL_NOCRASH[
403 #include <float.h>
404 #include <stdio.h>
405 #include <string.h>
406 static int
407 strisnan (const char *string, size_t start_index, size_t end_index)
408 {
409 if (start_index < end_index)
410 {
411 if (string[start_index] == '-')
412 start_index++;
413 if (start_index + 3 <= end_index
414 && memcmp (string + start_index, "nan", 3) == 0)
415 {
416 start_index += 3;
417 if (start_index == end_index
418 || (string[start_index] == '(' && string[end_index - 1] == ')'))
419 return 1;
420 }
421 }
422 return 0;
423 }
424 static char buf[10000];
425 static long double zeroL = 0.0L;
426 int main ()
427 {
428 int result = 0;
429 nocrash_init();
430 if (sprintf (buf, "%Lf", 1.0L / zeroL) < 0
431 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
432 result |= 1;
433 if (sprintf (buf, "%Lf", -1.0L / zeroL) < 0
434 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
435 result |= 1;
436 if (sprintf (buf, "%Lf", zeroL / zeroL) < 0
437 || !strisnan (buf, 0, strlen (buf)))
438 result |= 1;
439 if (sprintf (buf, "%Le", 1.0L / zeroL) < 0
440 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
441 result |= 1;
442 if (sprintf (buf, "%Le", -1.0L / zeroL) < 0
443 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
444 result |= 1;
445 if (sprintf (buf, "%Le", zeroL / zeroL) < 0
446 || !strisnan (buf, 0, strlen (buf)))
447 result |= 1;
448 if (sprintf (buf, "%Lg", 1.0L / zeroL) < 0
449 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0))
450 result |= 1;
451 if (sprintf (buf, "%Lg", -1.0L / zeroL) < 0
452 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0))
453 result |= 1;
454 if (sprintf (buf, "%Lg", zeroL / zeroL) < 0
455 || !strisnan (buf, 0, strlen (buf)))
456 result |= 1;
457 #if CHECK_PRINTF_SAFE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
458 /* Representation of an 80-bit 'long double' as an initializer for a sequence
459 of 'unsigned int' words. */
460 # ifdef WORDS_BIGENDIAN
461 # define LDBL80_WORDS(exponent,manthi,mantlo) \
462 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
463 ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
464 (unsigned int) (mantlo) << 16 \
465 }
466 # else
467 # define LDBL80_WORDS(exponent,manthi,mantlo) \
468 { mantlo, manthi, exponent }
469 # endif
470 { /* Quiet NaN. */
471 static union { unsigned int word[4]; long double value; } x =
472 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
473 if (sprintf (buf, "%Lf", x.value) < 0
474 || !strisnan (buf, 0, strlen (buf)))
475 result |= 2;
476 if (sprintf (buf, "%Le", x.value) < 0
477 || !strisnan (buf, 0, strlen (buf)))
478 result |= 2;
479 if (sprintf (buf, "%Lg", x.value) < 0
480 || !strisnan (buf, 0, strlen (buf)))
481 result |= 2;
482 }
483 {
484 /* Signalling NaN. */
485 static union { unsigned int word[4]; long double value; } x =
486 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
487 if (sprintf (buf, "%Lf", x.value) < 0
488 || !strisnan (buf, 0, strlen (buf)))
489 result |= 2;
490 if (sprintf (buf, "%Le", x.value) < 0
491 || !strisnan (buf, 0, strlen (buf)))
492 result |= 2;
493 if (sprintf (buf, "%Lg", x.value) < 0
494 || !strisnan (buf, 0, strlen (buf)))
495 result |= 2;
496 }
497 { /* Pseudo-NaN. */
498 static union { unsigned int word[4]; long double value; } x =
499 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
500 if (sprintf (buf, "%Lf", x.value) <= 0)
501 result |= 4;
502 if (sprintf (buf, "%Le", x.value) <= 0)
503 result |= 4;
504 if (sprintf (buf, "%Lg", x.value) <= 0)
505 result |= 4;
506 }
507 { /* Pseudo-Infinity. */
508 static union { unsigned int word[4]; long double value; } x =
509 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
510 if (sprintf (buf, "%Lf", x.value) <= 0)
511 result |= 8;
512 if (sprintf (buf, "%Le", x.value) <= 0)
513 result |= 8;
514 if (sprintf (buf, "%Lg", x.value) <= 0)
515 result |= 8;
516 }
517 { /* Pseudo-Zero. */
518 static union { unsigned int word[4]; long double value; } x =
519 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
520 if (sprintf (buf, "%Lf", x.value) <= 0)
521 result |= 16;
522 if (sprintf (buf, "%Le", x.value) <= 0)
523 result |= 16;
524 if (sprintf (buf, "%Lg", x.value) <= 0)
525 result |= 16;
526 }
527 { /* Unnormalized number. */
528 static union { unsigned int word[4]; long double value; } x =
529 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
530 if (sprintf (buf, "%Lf", x.value) <= 0)
531 result |= 32;
532 if (sprintf (buf, "%Le", x.value) <= 0)
533 result |= 32;
534 if (sprintf (buf, "%Lg", x.value) <= 0)
535 result |= 32;
536 }
537 { /* Pseudo-Denormal. */
538 static union { unsigned int word[4]; long double value; } x =
539 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
540 if (sprintf (buf, "%Lf", x.value) <= 0)
541 result |= 64;
542 if (sprintf (buf, "%Le", x.value) <= 0)
543 result |= 64;
544 if (sprintf (buf, "%Lg", x.value) <= 0)
545 result |= 64;
546 }
547 #endif
548 return result;
549 }]])],
550 [gl_cv_func_printf_infinite_long_double=yes],
551 [gl_cv_func_printf_infinite_long_double=no],
552 [case "$host_cpu" in
553 # Guess no on ia64, x86_64, i386.
554 ia64 | x86_64 | i*86) gl_cv_func_printf_infinite_long_double="guessing no";;
555 *)
556 case "$host_os" in
557 changequote(,)dnl
558 # Guess yes on glibc systems.
559 *-gnu* | gnu*) gl_cv_func_printf_infinite_long_double="guessing yes";;
560 # Guess yes on musl systems.
561 *-musl* | midipix*) gl_cv_func_printf_infinite_long_double="guessing yes";;
562 # Guess yes on FreeBSD >= 6.
563 freebsd[1-5].*) gl_cv_func_printf_infinite_long_double="guessing no";;
564 freebsd* | kfreebsd*) gl_cv_func_printf_infinite_long_double="guessing yes";;
565 midnightbsd*) gl_cv_func_printf_infinite_long_double="guessing yes";;
566 # Guess yes on HP-UX >= 11.
567 hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite_long_double="guessing no";;
568 hpux*) gl_cv_func_printf_infinite_long_double="guessing yes";;
569 # Guess yes on OpenBSD >= 6.0.
570 openbsd[1-5].*) gl_cv_func_printf_infinite_long_double="guessing no";;
571 openbsd*) gl_cv_func_printf_infinite_long_double="guessing yes";;
572 # Guess no on Android.
573 linux*-android*) gl_cv_func_printf_infinite_long_double="guessing no";;
574 changequote([,])dnl
575 # Guess yes on MSVC, no on mingw.
576 windows*-msvc*) gl_cv_func_printf_infinite_long_double="guessing yes" ;;
577 mingw* | windows*) AC_EGREP_CPP([Known], [
578 #ifdef _MSC_VER
579 Known
580 #endif
581 ],
582 [gl_cv_func_printf_infinite_long_double="guessing yes"],
583 [gl_cv_func_printf_infinite_long_double="guessing no"])
584 ;;
585 # If we don't know, obey --enable-cross-guesses.
586 *) gl_cv_func_printf_infinite_long_double="$gl_cross_guess_normal";;
587 esac
588 ;;
589 esac
590 ])
591 ])
592 ;;
593 *)
594 gl_cv_func_printf_infinite_long_double="irrelevant"
595 ;;
596 esac
597 ])
598
599 dnl Test whether the *printf family of functions supports the 'a' and 'A'
600 dnl conversion specifier for hexadecimal output of floating-point numbers.
601 dnl (ISO C99, POSIX:2001)
602 dnl Result is gl_cv_func_printf_directive_a.
603
604 AC_DEFUN([gl_PRINTF_DIRECTIVE_A],
605 [
606 AC_REQUIRE([AC_PROG_CC])
607 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
608 AC_CACHE_CHECK([whether printf supports the 'a' and 'A' directives],
609 [gl_cv_func_printf_directive_a],
610 [
611 AC_RUN_IFELSE(
612 [AC_LANG_SOURCE([[
613 #include <stdio.h>
614 #include <string.h>
615 static char buf[100];
616 static double zero = 0.0;
617 int main ()
618 {
619 int result = 0;
620 /* This fails on FreeBSD 5.2.1, Solaris 11.4. */
621 if (sprintf (buf, "%a %d", 3.1416015625, 33, 44, 55) < 0
622 || (strcmp (buf, "0x1.922p+1 33") != 0
623 && strcmp (buf, "0x3.244p+0 33") != 0
624 && strcmp (buf, "0x6.488p-1 33") != 0
625 && strcmp (buf, "0xc.91p-2 33") != 0))
626 result |= 1;
627 if (sprintf (buf, "%A %d", -3.1416015625, 33, 44, 55) < 0
628 || (strcmp (buf, "-0X1.922P+1 33") != 0
629 && strcmp (buf, "-0X3.244P+0 33") != 0
630 && strcmp (buf, "-0X6.488P-1 33") != 0
631 && strcmp (buf, "-0XC.91P-2 33") != 0))
632 result |= 1;
633 /* This catches a Mac OS X 10.5, FreeBSD 6.4, NetBSD 10.0 bug:
634 it doesn't round. */
635 if (sprintf (buf, "%.2a %d", 1.51, 33, 44, 55) < 0
636 || (strcmp (buf, "0x1.83p+0 33") != 0
637 && strcmp (buf, "0x3.05p-1 33") != 0
638 && strcmp (buf, "0x6.0ap-2 33") != 0
639 && strcmp (buf, "0xc.14p-3 33") != 0))
640 result |= 2;
641 /* This catches a macOS 14 (Darwin 23), FreeBSD 14.0, OpenBSD 7.5, AIX 7.3,
642 Solaris 11.4 bug: it doesn't round. */
643 if (sprintf (buf, "%.0a %d", 1.51, 33, 44, 55) < 0
644 || (strcmp (buf, "0x2p+0 33") != 0
645 && strcmp (buf, "0x3p-1 33") != 0
646 && strcmp (buf, "0x6p-2 33") != 0
647 && strcmp (buf, "0xcp-3 33") != 0))
648 result |= 4;
649 /* This catches a Mac OS X 10.5, FreeBSD 6.4 bug. See
650 <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
651 if (sprintf (buf, "%010a %d", 1.0 / zero, 33, 44, 55) < 0
652 || buf[0] == '0')
653 result |= 8;
654 /* This catches a Mac OS X 10.3.9 (Darwin 7.9), FreeBSD 6.4 bug. */
655 if (sprintf (buf, "%.1a", 1.999) < 0
656 || (strcmp (buf, "0x1.0p+1") != 0
657 && strcmp (buf, "0x2.0p+0") != 0
658 && strcmp (buf, "0x4.0p-1") != 0
659 && strcmp (buf, "0x8.0p-2") != 0))
660 result |= 16;
661 /* This catches the same Mac OS X 10.3.9 (Darwin 7.9) bug and also a
662 glibc 2.4 bug <https://sourceware.org/PR2908>
663 and a FreeBSD 6.4, NetBSD 10.0 bug. */
664 if (sprintf (buf, "%.1La", 1.999L) < 0
665 || (strcmp (buf, "0x1.0p+1") != 0
666 && strcmp (buf, "0x2.0p+0") != 0
667 && strcmp (buf, "0x4.0p-1") != 0
668 && strcmp (buf, "0x8.0p-2") != 0))
669 result |= 32;
670 return result;
671 }]])],
672 [gl_cv_func_printf_directive_a=yes],
673 [gl_cv_func_printf_directive_a=no],
674 [
675 case "$host_os" in
676 # Guess yes on glibc >= 2.5 systems.
677 *-gnu* | gnu*)
678 AC_EGREP_CPP([BZ2908], [
679 #include <features.h>
680 #ifdef __GNU_LIBRARY__
681 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 5) || (__GLIBC__ > 2)) && !defined __UCLIBC__
682 BZ2908
683 #endif
684 #endif
685 ],
686 [gl_cv_func_printf_directive_a="guessing yes"],
687 [gl_cv_func_printf_directive_a="guessing no"])
688 ;;
689 # Guess yes on musl systems.
690 *-musl* | midipix*) gl_cv_func_printf_directive_a="guessing yes";;
691 # Guess no on Android.
692 linux*-android*) gl_cv_func_printf_directive_a="guessing no";;
693 # Guess no on native Windows.
694 mingw* | windows*) gl_cv_func_printf_directive_a="guessing no";;
695 # If we don't know, obey --enable-cross-guesses.
696 *) gl_cv_func_printf_directive_a="$gl_cross_guess_normal";;
697 esac
698 ])
699 ])
700 ])
701
702 dnl Test whether the *printf family of functions supports the 'b' conversion
703 dnl specifier for binary output of integers.
704 dnl (ISO C23)
705 dnl Result is gl_cv_func_printf_directive_b.
706
707 AC_DEFUN([gl_PRINTF_DIRECTIVE_B],
708 [
709 AC_REQUIRE([AC_PROG_CC])
710 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
711 AC_CACHE_CHECK([whether printf supports the 'b' directive],
712 [gl_cv_func_printf_directive_b],
713 [
714 AC_RUN_IFELSE(
715 [AC_LANG_SOURCE([[
716 #include <stdio.h>
717 #include <string.h>
718 static char buf[100];
719 int main ()
720 {
721 int result = 0;
722 if (sprintf (buf, "%b %d", 12345, 33, 44, 55) < 0
723 || strcmp (buf, "11000000111001 33") != 0)
724 result |= 1;
725 return result;
726 }]])],
727 [gl_cv_func_printf_directive_b=yes],
728 [gl_cv_func_printf_directive_b=no],
729 [
730 case "$host_os" in
731 # Guess yes on glibc >= 2.35 systems.
732 *-gnu* | gnu*)
733 AC_EGREP_CPP([Lucky], [
734 #include <features.h>
735 #ifdef __GNU_LIBRARY__
736 #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 35) || (__GLIBC__ > 2)
737 Lucky user
738 #endif
739 #endif
740 ],
741 [gl_cv_func_printf_directive_uppercase_b="guessing yes"],
742 [gl_cv_func_printf_directive_uppercase_b="guessing no"])
743 ;;
744 # Guess no on musl systems.
745 *-musl* | midipix*) gl_cv_func_printf_directive_b="guessing no";;
746 # Guess no on Android.
747 linux*-android*) gl_cv_func_printf_directive_b="guessing no";;
748 # Guess no on native Windows.
749 mingw* | windows*) gl_cv_func_printf_directive_b="guessing no";;
750 # If we don't know, obey --enable-cross-guesses.
751 *) gl_cv_func_printf_directive_b="$gl_cross_guess_normal";;
752 esac
753 ])
754 ])
755 ])
756
757 dnl Test whether the *printf family of functions supports the 'B' conversion
758 dnl specifier for binary output of integers.
759 dnl (GNU, encouraged by ISO C23 § 7.23.6.1)
760 dnl Result is gl_cv_func_printf_directive_uppercase_b.
761
762 AC_DEFUN([gl_PRINTF_DIRECTIVE_UPPERCASE_B],
763 [
764 AC_REQUIRE([AC_PROG_CC])
765 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
766 AC_CACHE_CHECK([whether printf supports the 'B' directive],
767 [gl_cv_func_printf_directive_uppercase_b],
768 [
769 AC_RUN_IFELSE(
770 [AC_LANG_SOURCE([[
771 #include <stdio.h>
772 #include <string.h>
773 static char buf[100];
774 int main ()
775 {
776 int result = 0;
777 if (sprintf (buf, "%#B %d", 12345, 33, 44, 55) < 0
778 || strcmp (buf, "0B11000000111001 33") != 0)
779 result |= 1;
780 return result;
781 }]])],
782 [gl_cv_func_printf_directive_uppercase_b=yes],
783 [gl_cv_func_printf_directive_uppercase_b=no],
784 [
785 case "$host_os" in
786 # Guess yes on glibc >= 2.35 systems.
787 *-gnu* | gnu*)
788 AC_EGREP_CPP([Lucky], [
789 #include <features.h>
790 #ifdef __GNU_LIBRARY__
791 #if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 35) || (__GLIBC__ > 2)
792 Lucky user
793 #endif
794 #endif
795 ],
796 [gl_cv_func_printf_directive_uppercase_b="guessing yes"],
797 [gl_cv_func_printf_directive_uppercase_b="guessing no"])
798 ;;
799 # Guess no on musl systems.
800 *-musl* | midipix*) gl_cv_func_printf_directive_uppercase_b="guessing no";;
801 # Guess no on Android.
802 linux*-android*) gl_cv_func_printf_directive_uppercase_b="guessing no";;
803 # Guess no on native Windows.
804 mingw* | windows*) gl_cv_func_printf_directive_uppercase_b="guessing no";;
805 # If we don't know, obey --enable-cross-guesses.
806 *) gl_cv_func_printf_directive_uppercase_b="$gl_cross_guess_normal";;
807 esac
808 ])
809 ])
810 ])
811
812 dnl Test whether the *printf family of functions supports the %F format
813 dnl directive. (ISO C99, POSIX:2001)
814 dnl Result is gl_cv_func_printf_directive_f.
815
816 AC_DEFUN([gl_PRINTF_DIRECTIVE_F],
817 [
818 AC_REQUIRE([AC_PROG_CC])
819 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
820 AC_CACHE_CHECK([whether printf supports the 'F' directive],
821 [gl_cv_func_printf_directive_f],
822 [
823 AC_RUN_IFELSE(
824 [AC_LANG_SOURCE([[
825 #include <stdio.h>
826 #include <string.h>
827 static char buf[100];
828 static double zero = 0.0;
829 int main ()
830 {
831 int result = 0;
832 if (sprintf (buf, "%F %d", 1234567.0, 33, 44, 55) < 0
833 || strcmp (buf, "1234567.000000 33") != 0)
834 result |= 1;
835 if (sprintf (buf, "%F", 1.0 / zero) < 0
836 || (strcmp (buf, "INF") != 0 && strcmp (buf, "INFINITY") != 0))
837 result |= 2;
838 /* This catches a Cygwin 1.5.x bug. */
839 if (sprintf (buf, "%.F", 1234.0) < 0
840 || strcmp (buf, "1234") != 0)
841 result |= 4;
842 return result;
843 }]])],
844 [gl_cv_func_printf_directive_f=yes],
845 [gl_cv_func_printf_directive_f=no],
846 [
847 case "$host_os" in
848 changequote(,)dnl
849 # Guess yes on glibc systems.
850 *-gnu* | gnu*) gl_cv_func_printf_directive_f="guessing yes";;
851 # Guess yes on musl systems.
852 *-musl* | midipix*) gl_cv_func_printf_directive_f="guessing yes";;
853 # Guess yes on FreeBSD >= 6.
854 freebsd[1-5].*) gl_cv_func_printf_directive_f="guessing no";;
855 freebsd* | kfreebsd*) gl_cv_func_printf_directive_f="guessing yes";;
856 midnightbsd*) gl_cv_func_printf_directive_f="guessing yes";;
857 # Guess yes on Mac OS X >= 10.3.
858 darwin[1-6].*) gl_cv_func_printf_directive_f="guessing no";;
859 darwin*) gl_cv_func_printf_directive_f="guessing yes";;
860 # Guess yes on OpenBSD >= 6.0.
861 openbsd[1-5].*) gl_cv_func_printf_directive_f="guessing no";;
862 openbsd*) gl_cv_func_printf_directive_f="guessing yes";;
863 # Guess yes on Solaris >= 2.10.
864 solaris2.[1-9][0-9]*) gl_cv_func_printf_directive_f="guessing yes";;
865 solaris*) gl_cv_func_printf_directive_f="guessing no";;
866 # Guess no on Android.
867 linux*-android*) gl_cv_func_printf_directive_f="guessing no";;
868 changequote([,])dnl
869 # Guess yes on MSVC, no on mingw.
870 windows*-msvc*) gl_cv_func_printf_directive_f="guessing yes" ;;
871 mingw* | windows*) AC_EGREP_CPP([Known], [
872 #ifdef _MSC_VER
873 Known
874 #endif
875 ],
876 [gl_cv_func_printf_directive_f="guessing yes"],
877 [gl_cv_func_printf_directive_f="guessing no"])
878 ;;
879 # If we don't know, obey --enable-cross-guesses.
880 *) gl_cv_func_printf_directive_f="$gl_cross_guess_normal";;
881 esac
882 ])
883 ])
884 ])
885
886 dnl Test whether the *printf family of functions supports the %n format
887 dnl directive. (ISO C99, POSIX:2001)
888 dnl Result is gl_cv_func_printf_directive_n.
889
890 AC_DEFUN([gl_PRINTF_DIRECTIVE_N],
891 [
892 AC_REQUIRE([AC_PROG_CC])
893 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
894 AC_CACHE_CHECK([whether printf supports the 'n' directive],
895 [gl_cv_func_printf_directive_n],
896 [
897 AC_RUN_IFELSE(
898 [AC_LANG_SOURCE([[
899 #include <signal.h>
900 #include <stdio.h>
901 #include <string.h>
902 #if defined _WIN32 && !defined __CYGWIN__
903 # include <stdlib.h>
904 #else
905 # include <unistd.h>
906 #endif
907 #ifdef _MSC_VER
908 #include <crtdbg.h>
909 #include <inttypes.h>
910 /* See page about "Parameter Validation" on msdn.microsoft.com.
911 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation>
912 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-invalid-parameter-handler-set-thread-local-invalid-parameter-handler> */
913 static void cdecl
914 invalid_parameter_handler (const wchar_t *expression,
915 const wchar_t *function,
916 const wchar_t *file, unsigned int line,
917 uintptr_t dummy)
918 {
919 exit (1);
920 }
921 #endif
922 static void
923 abort_handler (int sig)
924 {
925 (void) sig;
926 _exit (1);
927 }
928 static char fmtstring[10];
929 static char buf[100];
930 int main ()
931 {
932 int count = -1;
933 #ifdef _MSC_VER
934 _set_invalid_parameter_handler (invalid_parameter_handler);
935 /* Also avoid an Abort/Retry/Ignore dialog in debug builds.
936 <https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetreportmode> */
937 _CrtSetReportMode (_CRT_ASSERT, 0);
938 #endif
939 signal (SIGABRT, abort_handler);
940 /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE >= 2)
941 support %n in format strings in read-only memory but not in writable
942 memory. */
943 strcpy (fmtstring, "%d %n");
944 if (sprintf (buf, fmtstring, 123, &count, 33, 44, 55) < 0
945 || strcmp (buf, "123 ") != 0
946 || count != 4)
947 return 1;
948 return 0;
949 }]])],
950 [gl_cv_func_printf_directive_n=yes],
951 [gl_cv_func_printf_directive_n=no],
952 [case "$host_os" in
953 # Guess no on glibc when _FORTIFY_SOURCE >= 2.
954 *-gnu* | gnu*) AC_COMPILE_IFELSE(
955 [AC_LANG_SOURCE(
956 [[#if _FORTIFY_SOURCE >= 2
957 error fail
958 #endif
959 ]])],
960 [gl_cv_func_printf_directive_n="guessing yes"],
961 [gl_cv_func_printf_directive_n="guessing no"])
962 ;;
963 # Guess no on Android.
964 linux*-android*) gl_cv_func_printf_directive_n="guessing no";;
965 # Guess no on native Windows.
966 mingw* | windows*) gl_cv_func_printf_directive_n="guessing no";;
967 *) gl_cv_func_printf_directive_n="guessing yes";;
968 esac
969 ])
970 ])
971 ])
972
973 dnl Test whether the *printf family of functions supports the %ls format
974 dnl directive and in particular, when a precision is specified, whether
975 dnl the functions stop converting the wide string argument when the number
976 dnl of bytes that have been produced by this conversion equals or exceeds
977 dnl the precision.
978 dnl Result is gl_cv_func_printf_directive_ls.
979
980 AC_DEFUN([gl_PRINTF_DIRECTIVE_LS],
981 [
982 AC_REQUIRE([AC_PROG_CC])
983 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
984 AC_CACHE_CHECK([whether printf supports the 'ls' directive],
985 [gl_cv_func_printf_directive_ls],
986 [
987 AC_RUN_IFELSE(
988 [AC_LANG_SOURCE([[
989 #include <stdio.h>
990 #include <wchar.h>
991 #include <string.h>
992 int main ()
993 {
994 int result = 0;
995 char buf[100];
996 /* Test whether %ls works at all.
997 This test fails on OpenBSD 4.0, Solaris 2.6, Haiku, but not on
998 Cygwin 1.5. */
999 {
1000 static const wchar_t wstring[] = { 'a', 'b', 'c', 0 };
1001 buf[0] = '\0';
1002 if (sprintf (buf, "%ls", wstring) < 0
1003 || strcmp (buf, "abc") != 0)
1004 result |= 1;
1005 }
1006 /* This test fails on Solaris 2.6, Cygwin 1.5, Haiku (with an assertion
1007 failure inside libc), but not on OpenBSD 4.0. */
1008 {
1009 static const wchar_t wstring[] = { 'a', 0 };
1010 buf[0] = '\0';
1011 if (sprintf (buf, "%ls", wstring) < 0
1012 || strcmp (buf, "a") != 0)
1013 result |= 2;
1014 }
1015 /* Test whether precisions in %ls are supported as specified in ISO C 99
1016 section 7.19.6.1:
1017 "If a precision is specified, no more than that many bytes are written
1018 (including shift sequences, if any), and the array shall contain a
1019 null wide character if, to equal the multibyte character sequence
1020 length given by the precision, the function would need to access a
1021 wide character one past the end of the array."
1022 This test fails on Solaris 10. */
1023 {
1024 static const wchar_t wstring[] = { 'a', 'b', (wchar_t) 0xfdfdfdfd, 0 };
1025 buf[0] = '\0';
1026 if (sprintf (buf, "%.2ls", wstring) < 0
1027 || strcmp (buf, "ab") != 0)
1028 result |= 8;
1029 }
1030 return result;
1031 }]])],
1032 [gl_cv_func_printf_directive_ls=yes],
1033 [gl_cv_func_printf_directive_ls=no],
1034 [
1035 changequote(,)dnl
1036 case "$host_os" in
1037 # Guess yes on OpenBSD >= 6.0.
1038 openbsd[1-5].*) gl_cv_func_printf_directive_ls="guessing no";;
1039 openbsd*) gl_cv_func_printf_directive_ls="guessing yes";;
1040 solaris*) gl_cv_func_printf_directive_ls="guessing no";;
1041 cygwin*) gl_cv_func_printf_directive_ls="guessing no";;
1042 beos* | haiku*) gl_cv_func_printf_directive_ls="guessing no";;
1043 # Guess no on Android.
1044 linux*-android*) gl_cv_func_printf_directive_ls="guessing no";;
1045 # Guess yes on native Windows.
1046 mingw* | windows*) gl_cv_func_printf_directive_ls="guessing yes";;
1047 *) gl_cv_func_printf_directive_ls="guessing yes";;
1048 esac
1049 changequote([,])dnl
1050 ])
1051 ])
1052 ])
1053
1054 dnl Test whether the *printf family of functions supports the %lc format
1055 dnl directive and in particular, when the argument is a null wide character,
1056 dnl whether the functions produce a NUL byte, as specified in ISO C 23
1057 dnl after the issue GB-141 was fixed.
1058 dnl Result is gl_cv_func_printf_directive_lc.
1059
1060 AC_DEFUN([gl_PRINTF_DIRECTIVE_LC],
1061 [
1062 AC_REQUIRE([AC_PROG_CC])
1063 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1064 AC_CACHE_CHECK([whether printf supports the 'lc' directive correctly],
1065 [gl_cv_func_printf_directive_lc],
1066 [
1067 AC_RUN_IFELSE(
1068 [AC_LANG_SOURCE([[
1069 #include <stdio.h>
1070 #include <wchar.h>
1071 #include <string.h>
1072 int main ()
1073 {
1074 int result = 0;
1075 char buf[100];
1076 /* This test fails on musl libc 1.2.4. */
1077 {
1078 buf[0] = '\0';
1079 if (sprintf (buf, "%lc%lc%lc", (wint_t) 'a', (wint_t) 0, (wint_t) 'z') < 0
1080 || memcmp (buf, "a\0z", 4) != 0)
1081 result |= 1;
1082 }
1083 return result;
1084 }]])],
1085 [gl_cv_func_printf_directive_lc=yes],
1086 [gl_cv_func_printf_directive_lc=no],
1087 [
1088 changequote(,)dnl
1089 case "$host_os" in
1090 # Guess no on musl libc.
1091 *-musl* | midipix*) gl_cv_func_printf_directive_lc="guessing no";;
1092 # Guess yes otherwise.
1093 *) gl_cv_func_printf_directive_lc="guessing yes";;
1094 esac
1095 changequote([,])dnl
1096 ])
1097 ])
1098 ])
1099
1100 dnl Test whether the *printf family of functions supports POSIX/XSI format
1101 dnl strings with positions. (POSIX:2001)
1102 dnl Result is gl_cv_func_printf_positions.
1103
1104 AC_DEFUN_ONCE([gl_PRINTF_POSITIONS],
1105 [
1106 AC_REQUIRE([AC_PROG_CC])
1107 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1108 AC_CACHE_CHECK([whether printf supports POSIX/XSI format strings with positions],
1109 [gl_cv_func_printf_positions],
1110 [
1111 AC_RUN_IFELSE(
1112 [AC_LANG_SOURCE([[
1113 #include <stdio.h>
1114 #include <string.h>
1115 /* The string "%2$d %1$d", with dollar characters protected from the shell's
1116 dollar expansion (possibly an autoconf bug). */
1117 static char format[] = { '%', '2', '$', 'd', ' ', '%', '1', '$', 'd', '\0' };
1118 static char buf[100];
1119 int main ()
1120 {
1121 sprintf (buf, format, 33, 55);
1122 return (strcmp (buf, "55 33") != 0);
1123 }]])],
1124 [gl_cv_func_printf_positions=yes],
1125 [gl_cv_func_printf_positions=no],
1126 [
1127 changequote(,)dnl
1128 case "$host_os" in
1129 netbsd[1-3]* | netbsdelf[1-3]* | netbsdaout[1-3]* | netbsdcoff[1-3]*)
1130 gl_cv_func_printf_positions="guessing no";;
1131 beos*)
1132 gl_cv_func_printf_positions="guessing no";;
1133 # Guess yes on Android.
1134 linux*-android*)
1135 gl_cv_func_printf_positions="guessing yes";;
1136 # Guess no on native Windows.
1137 mingw* | windows* | pw*)
1138 gl_cv_func_printf_positions="guessing no";;
1139 *)
1140 gl_cv_func_printf_positions="guessing yes";;
1141 esac
1142 changequote([,])dnl
1143 ])
1144 ])
1145 ])
1146
1147 dnl Test whether the *printf family of functions supports POSIX/XSI format
1148 dnl strings with the ' flag for grouping of decimal digits. (POSIX:2001)
1149 dnl Result is gl_cv_func_printf_flag_grouping.
1150
1151 AC_DEFUN([gl_PRINTF_FLAG_GROUPING],
1152 [
1153 AC_REQUIRE([AC_PROG_CC])
1154 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1155 AC_CACHE_CHECK([whether printf supports the grouping flag],
1156 [gl_cv_func_printf_flag_grouping],
1157 [
1158 AC_RUN_IFELSE(
1159 [AC_LANG_SOURCE([[
1160 #include <stdio.h>
1161 #include <string.h>
1162 static char buf[100];
1163 int main ()
1164 {
1165 if (sprintf (buf, "%'d %d", 1234567, 99) < 0
1166 || buf[strlen (buf) - 1] != '9')
1167 return 1;
1168 return 0;
1169 }]])],
1170 [gl_cv_func_printf_flag_grouping=yes],
1171 [gl_cv_func_printf_flag_grouping=no],
1172 [
1173 changequote(,)dnl
1174 case "$host_os" in
1175 cygwin*) gl_cv_func_printf_flag_grouping="guessing no";;
1176 netbsd*) gl_cv_func_printf_flag_grouping="guessing no";;
1177 # Guess no on Android.
1178 linux*-android*) gl_cv_func_printf_flag_grouping="guessing no";;
1179 # Guess no on native Windows.
1180 mingw* | windows* | pw*) gl_cv_func_printf_flag_grouping="guessing no";;
1181 *) gl_cv_func_printf_flag_grouping="guessing yes";;
1182 esac
1183 changequote([,])dnl
1184 ])
1185 ])
1186 ])
1187
1188 dnl Test whether the *printf family of functions supports POSIX/XSI format
1189 dnl strings with the ' flag for grouping of decimal digits on integers,
1190 dnl together with a precision.
1191 dnl Result is gl_cv_func_printf_flag_grouping_int_precision.
1192
1193 AC_DEFUN([gl_PRINTF_FLAG_GROUPING_INT_PRECISION],
1194 [
1195 AC_REQUIRE([AC_PROG_CC])
1196 AC_REQUIRE([AC_CANONICAL_HOST])
1197 AC_CACHE_CHECK([whether printf supports grouping on integers with a precision],
1198 [gl_cv_func_printf_flag_grouping_int_precision],
1199 [
1200 dnl Prepare a guess, used when cross-compiling or when specific locales
1201 dnl are not available.
1202 case "$host_os" in
1203 # Guess no on FreeBSD, NetBSD, Solaris, Cygwin, Haiku.
1204 freebsd* | dragonfly* | netbsd* | solaris* | cygwin* | haiku*)
1205 gl_cv_func_printf_flag_grouping_int_precision="guessing no";;
1206 *)
1207 gl_cv_func_printf_flag_grouping_int_precision="guessing yes";;
1208 esac
1209 AC_RUN_IFELSE(
1210 [AC_LANG_SOURCE([[
1211 #include <locale.h>
1212 #include <stdio.h>
1213 #include <string.h>
1214 static char buf[100];
1215 int main ()
1216 {
1217 if (setlocale (LC_ALL, "fr_FR.UTF-8") != NULL
1218 || setlocale (LC_ALL, "fr_FR") != NULL
1219 || setlocale (LC_ALL, "fr_FR.ISO-8859-1") != NULL
1220 || setlocale (LC_ALL, "fr_FR.ISO8859-1") != NULL)
1221 {
1222 if (sprintf (buf, "%'.10d", 1000) < 0)
1223 return 1;
1224 if (strlen (buf) == 10 && strcmp (buf, "0000001000") != 0)
1225 /* The sprintf implementation has produced fewer than 10 digits. */
1226 return 2;
1227 else
1228 return 0;
1229 }
1230 return 3;
1231 }]])],
1232 [gl_cv_func_printf_flag_grouping_int_precision=yes],
1233 [if test $? = 2; then
1234 gl_cv_func_printf_flag_grouping_int_precision=no
1235 fi
1236 ],
1237 [:])
1238 ])
1239 ])
1240
1241 dnl Test whether the *printf family of functions supports POSIX/XSI format
1242 dnl strings with the ' flag for grouping of decimal digits, when the thousands
1243 dnl separator is a multibyte character (such as U+00A0 or U+202F in a UTF-8
1244 dnl locale).
1245 dnl Result is gl_cv_func_printf_flag_grouping_multibyte.
1246
1247 AC_DEFUN([gl_PRINTF_FLAG_GROUPING_MULTIBYTE],
1248 [
1249 AC_REQUIRE([AC_PROG_CC])
1250 AC_REQUIRE([AC_CANONICAL_HOST])
1251 AC_CACHE_CHECK([whether printf supports grouping with a multibyte separator],
1252 [gl_cv_func_printf_flag_grouping_multibyte],
1253 [
1254 dnl Prepare a guess, used when cross-compiling or when specific locales
1255 dnl are not available.
1256 case "$host_os" in
1257 # Guess no on NetBSD and Solaris 11 OpenIndiana.
1258 netbsd* | solaris*)
1259 gl_cv_func_printf_flag_grouping_multibyte="guessing no";;
1260 *)
1261 gl_cv_func_printf_flag_grouping_multibyte="guessing yes";;
1262 esac
1263 AC_RUN_IFELSE(
1264 [AC_LANG_SOURCE([[
1265 #include <locale.h>
1266 #include <stdio.h>
1267 #include <string.h>
1268 static char buf[100];
1269 int main ()
1270 {
1271 if (setlocale (LC_ALL, "fr_FR.UTF-8") == NULL)
1272 return 0;
1273 if (sprintf (buf, "%'.0f", 1000.0) < 0)
1274 return 1;
1275 if (strlen (localeconv ()->thousands_sep) > 1)
1276 {
1277 if (strlen (buf) <= 4 + 1)
1278 return 2;
1279 else
1280 return 3;
1281 }
1282 return 0;
1283 }]])],
1284 [:],
1285 [case $? in
1286 2) gl_cv_func_printf_flag_grouping_multibyte=no ;;
1287 3) gl_cv_func_printf_flag_grouping_multibyte=yes ;;
1288 esac
1289 ],
1290 [:])
1291 ])
1292 ])
1293
1294 dnl Test whether the *printf family of functions supports the - flag correctly.
1295 dnl (ISO C99.) See
1296 dnl <https://lists.gnu.org/r/bug-coreutils/2008-02/msg00035.html>
1297 dnl Result is gl_cv_func_printf_flag_leftadjust.
1298
1299 AC_DEFUN([gl_PRINTF_FLAG_LEFTADJUST],
1300 [
1301 AC_REQUIRE([AC_PROG_CC])
1302 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1303 AC_CACHE_CHECK([whether printf supports the left-adjust flag correctly],
1304 [gl_cv_func_printf_flag_leftadjust],
1305 [
1306 AC_RUN_IFELSE(
1307 [AC_LANG_SOURCE([[
1308 #include <stdio.h>
1309 #include <string.h>
1310 static char buf[100];
1311 int main ()
1312 {
1313 /* Check that a '-' flag is not annihilated by a negative width. */
1314 if (sprintf (buf, "a%-*sc", -3, "b") < 0
1315 || strcmp (buf, "ab c") != 0)
1316 return 1;
1317 return 0;
1318 }]])],
1319 [gl_cv_func_printf_flag_leftadjust=yes],
1320 [gl_cv_func_printf_flag_leftadjust=no],
1321 [
1322 changequote(,)dnl
1323 case "$host_os" in
1324 # Guess yes on HP-UX 11.
1325 hpux11*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
1326 # Guess no on HP-UX 10 and older.
1327 hpux*) gl_cv_func_printf_flag_leftadjust="guessing no";;
1328 # Guess yes on Android.
1329 linux*-android*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
1330 # Guess yes on native Windows.
1331 mingw* | windows*) gl_cv_func_printf_flag_leftadjust="guessing yes";;
1332 # Guess yes otherwise.
1333 *) gl_cv_func_printf_flag_leftadjust="guessing yes";;
1334 esac
1335 changequote([,])dnl
1336 ])
1337 ])
1338 ])
1339
1340 dnl Test whether the *printf family of functions supports padding of non-finite
1341 dnl values with the 0 flag correctly. (ISO C99 + TC1 + TC2.) See
1342 dnl <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html>
1343 dnl Result is gl_cv_func_printf_flag_zero.
1344
1345 AC_DEFUN([gl_PRINTF_FLAG_ZERO],
1346 [
1347 AC_REQUIRE([AC_PROG_CC])
1348 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1349 AC_CACHE_CHECK([whether printf supports the zero flag correctly],
1350 [gl_cv_func_printf_flag_zero],
1351 [
1352 AC_RUN_IFELSE(
1353 [AC_LANG_SOURCE([[
1354 #include <stdio.h>
1355 #include <string.h>
1356 static char buf[100];
1357 static double zero = 0.0;
1358 int main ()
1359 {
1360 if (sprintf (buf, "%010f", 1.0 / zero, 33, 44, 55) < 0
1361 || (strcmp (buf, " inf") != 0
1362 && strcmp (buf, " infinity") != 0))
1363 return 1;
1364 return 0;
1365 }]])],
1366 [gl_cv_func_printf_flag_zero=yes],
1367 [gl_cv_func_printf_flag_zero=no],
1368 [
1369 changequote(,)dnl
1370 case "$host_os" in
1371 # Guess yes on glibc systems.
1372 *-gnu* | gnu*) gl_cv_func_printf_flag_zero="guessing yes";;
1373 # Guess yes on musl systems.
1374 *-musl* | midipix*) gl_cv_func_printf_flag_zero="guessing yes";;
1375 # Guess yes on BeOS.
1376 beos*) gl_cv_func_printf_flag_zero="guessing yes";;
1377 # Guess no on Android.
1378 linux*-android*) gl_cv_func_printf_flag_zero="guessing no";;
1379 # Guess no on native Windows.
1380 mingw* | windows*) gl_cv_func_printf_flag_zero="guessing no";;
1381 # If we don't know, obey --enable-cross-guesses.
1382 *) gl_cv_func_printf_flag_zero="$gl_cross_guess_normal";;
1383 esac
1384 changequote([,])dnl
1385 ])
1386 ])
1387 ])
1388
1389 dnl Test whether the *printf family of functions supports the # flag with a
1390 dnl zero precision and a zero value in the 'x' and 'X' directives correctly.
1391 dnl ISO C and POSIX specify that for the 'd', 'i', 'b', 'o', 'u', 'x', 'X'
1392 dnl directives: "The result of converting a zero value with a precision of
1393 dnl zero is no characters." But on Mac OS X 10.5, for the 'x', 'X' directives,
1394 dnl when a # flag is present, the output is "0" instead of "".
1395 dnl Result is gl_cv_func_printf_flag_alt_precision_zero.
1396
1397 AC_DEFUN([gl_PRINTF_FLAG_ALT_PRECISION_ZERO],
1398 [
1399 AC_REQUIRE([AC_PROG_CC])
1400 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1401 AC_CACHE_CHECK([whether printf supports the alternative flag with a zero precision],
1402 [gl_cv_func_printf_flag_alt_precision_zero],
1403 [
1404 AC_RUN_IFELSE(
1405 [AC_LANG_SOURCE([[
1406 #include <stdio.h>
1407 static char buf[10];
1408 int main ()
1409 {
1410 int result = 0;
1411 if (sprintf (buf, "%#.0x %d", 0, 33, 44) > 0 + 3)
1412 result |= 1;
1413 return result;
1414 }]])],
1415 [gl_cv_func_printf_flag_alt_precision_zero=yes],
1416 [gl_cv_func_printf_flag_alt_precision_zero=no],
1417 [
1418 changequote(,)dnl
1419 case "$host_os" in
1420 # Guess no only on macOS 10..12 systems.
1421 darwin[0-9] | darwin[0-9].* | \
1422 darwin1[0-9] | darwin1[0-9].* | \
1423 darwin2[0-1] | darwin2[0-1].*)
1424 gl_cv_func_printf_flag_alt_precision_zero="guessing no" ;;
1425 darwin*) gl_cv_func_printf_flag_alt_precision_zero="guessing yes" ;;
1426 *) gl_cv_func_printf_flag_alt_precision_zero="guessing yes" ;;
1427 esac
1428 changequote([,])dnl
1429 ])
1430 ])
1431 ])
1432
1433 dnl Test whether the *printf family of functions supports large precisions.
1434 dnl On mingw, precisions larger than 512 are treated like 512, in integer,
1435 dnl floating-point or pointer output. On Solaris 10/x86, precisions larger
1436 dnl than 510 in floating-point output crash the program. On Solaris 10/SPARC,
1437 dnl precisions larger than 510 in floating-point output yield wrong results.
1438 dnl On AIX 7.1, precisions larger than 998 in floating-point output yield
1439 dnl wrong results. On BeOS, precisions larger than 1044 crash the program.
1440 dnl Result is gl_cv_func_printf_precision.
1441
1442 AC_DEFUN([gl_PRINTF_PRECISION],
1443 [
1444 AC_REQUIRE([AC_PROG_CC])
1445 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1446 AC_CACHE_CHECK([whether printf supports large precisions],
1447 [gl_cv_func_printf_precision],
1448 [
1449 AC_RUN_IFELSE(
1450 [AC_LANG_SOURCE([[
1451 #include <stdio.h>
1452 #include <string.h>
1453 static char buf[5000];
1454 int main ()
1455 {
1456 int result = 0;
1457 #ifdef __BEOS__
1458 /* On BeOS, this would crash and show a dialog box. Avoid the crash. */
1459 return 1;
1460 #endif
1461 if (sprintf (buf, "%.4000d %d", 1, 33, 44) < 4000 + 3)
1462 result |= 1;
1463 if (sprintf (buf, "%.4000f %d", 1.0, 33, 44) < 4000 + 5)
1464 result |= 2;
1465 if (sprintf (buf, "%.511f %d", 1.0, 33, 44) < 511 + 5
1466 || buf[0] != '1')
1467 result |= 4;
1468 if (sprintf (buf, "%.999f %d", 1.0, 33, 44) < 999 + 5
1469 || buf[0] != '1')
1470 result |= 4;
1471 return result;
1472 }]])],
1473 [gl_cv_func_printf_precision=yes],
1474 [gl_cv_func_printf_precision=no],
1475 [
1476 changequote(,)dnl
1477 case "$host_os" in
1478 # Guess no only on Solaris, native Windows, and BeOS systems.
1479 solaris*) gl_cv_func_printf_precision="guessing no" ;;
1480 mingw* | windows* | pw*) gl_cv_func_printf_precision="guessing no" ;;
1481 beos*) gl_cv_func_printf_precision="guessing no" ;;
1482 # Guess yes on Android.
1483 linux*-android*) gl_cv_func_printf_precision="guessing yes" ;;
1484 *) gl_cv_func_printf_precision="guessing yes" ;;
1485 esac
1486 changequote([,])dnl
1487 ])
1488 ])
1489 ])
1490
1491 dnl Test whether the *printf family of functions recovers gracefully in case
1492 dnl of an out-of-memory condition, or whether it crashes the entire program.
1493 dnl Result is gl_cv_func_printf_enomem.
1494
1495 AC_DEFUN([gl_PRINTF_ENOMEM],
1496 [
1497 AC_REQUIRE([AC_PROG_CC])
1498 AC_REQUIRE([gl_MULTIARCH])
1499 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1500 AC_CACHE_CHECK([whether printf survives out-of-memory conditions],
1501 [gl_cv_func_printf_enomem],
1502 [
1503 gl_cv_func_printf_enomem="guessing no"
1504 if test "$cross_compiling" = no; then
1505 if test $APPLE_UNIVERSAL_BUILD = 0; then
1506 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
1507 ]GL_NOCRASH[
1508 #include <stdio.h>
1509 #include <sys/types.h>
1510 #include <sys/time.h>
1511 #include <sys/resource.h>
1512 #include <errno.h>
1513 int main()
1514 {
1515 struct rlimit limit;
1516 int ret;
1517 nocrash_init ();
1518 /* Some printf implementations allocate temporary space with malloc. */
1519 /* On BSD systems, malloc() is limited by RLIMIT_DATA. */
1520 #ifdef RLIMIT_DATA
1521 if (getrlimit (RLIMIT_DATA, &limit) < 0)
1522 return 77;
1523 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1524 limit.rlim_max = 5000000;
1525 limit.rlim_cur = limit.rlim_max;
1526 if (setrlimit (RLIMIT_DATA, &limit) < 0)
1527 return 77;
1528 #endif
1529 /* On Linux systems, malloc() is limited by RLIMIT_AS. */
1530 #ifdef RLIMIT_AS
1531 if (getrlimit (RLIMIT_AS, &limit) < 0)
1532 return 77;
1533 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1534 limit.rlim_max = 5000000;
1535 limit.rlim_cur = limit.rlim_max;
1536 if (setrlimit (RLIMIT_AS, &limit) < 0)
1537 return 77;
1538 #endif
1539 /* Some printf implementations allocate temporary space on the stack. */
1540 #ifdef RLIMIT_STACK
1541 if (getrlimit (RLIMIT_STACK, &limit) < 0)
1542 return 77;
1543 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
1544 limit.rlim_max = 5000000;
1545 limit.rlim_cur = limit.rlim_max;
1546 if (setrlimit (RLIMIT_STACK, &limit) < 0)
1547 return 77;
1548 #endif
1549 ret = printf ("%.5000000f", 1.0);
1550 return !(ret == 5000002 || (ret < 0 && errno == ENOMEM));
1551 }
1552 ]])])
1553 if AC_TRY_EVAL([ac_link]) && test -s conftest$ac_exeext; then
1554 (./conftest 2>&AS_MESSAGE_LOG_FD
1555 result=$?
1556 _AS_ECHO_LOG([\$? = $result])
1557 if test $result != 0 && test $result != 77; then result=1; fi
1558 exit $result
1559 ) >/dev/null 2>/dev/null
1560 case $? in
1561 0) gl_cv_func_printf_enomem="yes" ;;
1562 77) gl_cv_func_printf_enomem="guessing no" ;;
1563 *) gl_cv_func_printf_enomem="no" ;;
1564 esac
1565 else
1566 gl_cv_func_printf_enomem="guessing no"
1567 fi
1568 rm -fr conftest*
1569 else
1570 dnl A universal build on Apple Mac OS X platforms.
1571 dnl The result would be 'no' in 32-bit mode and 'yes' in 64-bit mode.
1572 dnl But we need a configuration result that is valid in both modes.
1573 gl_cv_func_printf_enomem="guessing no"
1574 fi
1575 fi
1576 if test "$gl_cv_func_printf_enomem" = "guessing no"; then
1577 changequote(,)dnl
1578 case "$host_os" in
1579 # Guess yes on glibc systems.
1580 *-gnu* | gnu*) gl_cv_func_printf_enomem="guessing yes";;
1581 # Guess yes on Solaris.
1582 solaris*) gl_cv_func_printf_enomem="guessing yes";;
1583 # Guess yes on AIX.
1584 aix*) gl_cv_func_printf_enomem="guessing yes";;
1585 # Guess yes on HP-UX/hppa.
1586 hpux*) case "$host_cpu" in
1587 hppa*) gl_cv_func_printf_enomem="guessing yes";;
1588 *) gl_cv_func_printf_enomem="guessing no";;
1589 esac
1590 ;;
1591 # Guess yes on BeOS.
1592 beos*) gl_cv_func_printf_enomem="guessing yes";;
1593 # Guess yes on Haiku.
1594 haiku*) gl_cv_func_printf_enomem="guessing yes";;
1595 # Guess no on Android.
1596 linux*-android*) gl_cv_func_printf_enomem="guessing no";;
1597 # If we don't know, obey --enable-cross-guesses.
1598 *) gl_cv_func_printf_enomem="$gl_cross_guess_normal";;
1599 esac
1600 changequote([,])dnl
1601 fi
1602 ])
1603 ])
1604
1605 dnl Test whether the snprintf function exists. (ISO C99, POSIX:2001)
1606 dnl Result is ac_cv_func_snprintf.
1607
1608 AC_DEFUN([gl_SNPRINTF_PRESENCE],
1609 [
1610 AC_CHECK_FUNCS_ONCE([snprintf])
1611 ])
1612
1613 dnl Test whether the string produced by the snprintf function is always NUL
1614 dnl terminated. (ISO C99, POSIX:2001)
1615 dnl Result is gl_cv_func_snprintf_truncation_c99.
1616
1617 AC_DEFUN_ONCE([gl_SNPRINTF_TRUNCATION_C99],
1618 [
1619 AC_REQUIRE([AC_PROG_CC])
1620 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1621 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1622 AC_CACHE_CHECK([whether snprintf truncates the result as in C99],
1623 [gl_cv_func_snprintf_truncation_c99],
1624 [
1625 AC_RUN_IFELSE(
1626 [AC_LANG_SOURCE([[
1627 #include <stdio.h>
1628 #include <string.h>
1629 #if HAVE_SNPRINTF
1630 # define my_snprintf snprintf
1631 #else
1632 # include <stdarg.h>
1633 static int my_snprintf (char *buf, int size, const char *format, ...)
1634 {
1635 va_list args;
1636 int ret;
1637 va_start (args, format);
1638 ret = vsnprintf (buf, size, format, args);
1639 va_end (args);
1640 return ret;
1641 }
1642 #endif
1643 static char buf[100];
1644 int main ()
1645 {
1646 strcpy (buf, "ABCDEF");
1647 my_snprintf (buf, 3, "%d %d", 4567, 89);
1648 if (memcmp (buf, "45\0DEF", 6) != 0)
1649 return 1;
1650 return 0;
1651 }]])],
1652 [gl_cv_func_snprintf_truncation_c99=yes],
1653 [gl_cv_func_snprintf_truncation_c99=no],
1654 [
1655 changequote(,)dnl
1656 case "$host_os" in
1657 # Guess yes on glibc systems.
1658 *-gnu* | gnu*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1659 # Guess yes on musl systems.
1660 *-musl* | midipix*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1661 # Guess yes on FreeBSD >= 5.
1662 freebsd[1-4].*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1663 freebsd* | kfreebsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1664 midnightbsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1665 # Guess yes on Mac OS X >= 10.3.
1666 darwin[1-6].*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1667 darwin*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1668 # Guess yes on OpenBSD >= 3.9.
1669 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
1670 gl_cv_func_snprintf_truncation_c99="guessing no";;
1671 openbsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1672 # Guess yes on Solaris >= 2.6.
1673 solaris2.[0-5] | solaris2.[0-5].*)
1674 gl_cv_func_snprintf_truncation_c99="guessing no";;
1675 solaris*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1676 # Guess yes on AIX >= 4.
1677 aix[1-3]*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1678 aix*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1679 # Guess yes on HP-UX >= 11.
1680 hpux[7-9]* | hpux10*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1681 hpux*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1682 # Guess yes on NetBSD >= 3.
1683 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1684 gl_cv_func_snprintf_truncation_c99="guessing no";;
1685 netbsd*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1686 # Guess yes on BeOS.
1687 beos*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1688 # Guess yes on Android.
1689 linux*-android*) gl_cv_func_snprintf_truncation_c99="guessing yes";;
1690 # Guess no on native Windows.
1691 mingw* | windows*) gl_cv_func_snprintf_truncation_c99="guessing no";;
1692 # If we don't know, obey --enable-cross-guesses.
1693 *) gl_cv_func_snprintf_truncation_c99="$gl_cross_guess_normal";;
1694 esac
1695 changequote([,])dnl
1696 ])
1697 ])
1698 ])
1699
1700 dnl Test whether the return value of the snprintf function is the number
1701 dnl of bytes (excluding the terminating NUL) that would have been produced
1702 dnl if the buffer had been large enough. (ISO C99, POSIX:2001)
1703 dnl For example, this test program fails on HP-UX 11.31 and Solaris 9:
1704 dnl ---------------------------------------------------------------------
1705 dnl #include <stdio.h>
1706 dnl int main()
1707 dnl {
1708 dnl static char buf[8];
1709 dnl int retval = snprintf (buf, 3, "%d", 12345);
1710 dnl return retval >= 0 && retval < 3;
1711 dnl }
1712 dnl ---------------------------------------------------------------------
1713 dnl Result is gl_cv_func_snprintf_retval_c99.
1714
1715 AC_DEFUN_ONCE([gl_SNPRINTF_RETVAL_C99],
1716 [
1717 AC_REQUIRE([AC_PROG_CC])
1718 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1719 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1720 AC_CACHE_CHECK([whether snprintf returns a byte count as in C99],
1721 [gl_cv_func_snprintf_retval_c99],
1722 [
1723 AC_RUN_IFELSE(
1724 [AC_LANG_SOURCE([[
1725 #include <stdio.h>
1726 #include <string.h>
1727 #if HAVE_SNPRINTF
1728 # define my_snprintf snprintf
1729 #else
1730 # include <stdarg.h>
1731 static int my_snprintf (char *buf, int size, const char *format, ...)
1732 {
1733 va_list args;
1734 int ret;
1735 va_start (args, format);
1736 ret = vsnprintf (buf, size, format, args);
1737 va_end (args);
1738 return ret;
1739 }
1740 #endif
1741 static char buf[100];
1742 int main ()
1743 {
1744 strcpy (buf, "ABCDEF");
1745 if (my_snprintf (buf, 3, "%d %d", 4567, 89) != 7)
1746 return 1;
1747 if (my_snprintf (buf, 0, "%d %d", 4567, 89) != 7)
1748 return 2;
1749 if (my_snprintf (NULL, 0, "%d %d", 4567, 89) != 7)
1750 return 3;
1751 return 0;
1752 }]])],
1753 [gl_cv_func_snprintf_retval_c99=yes],
1754 [gl_cv_func_snprintf_retval_c99=no],
1755 [case "$host_os" in
1756 changequote(,)dnl
1757 # Guess yes on glibc systems.
1758 *-gnu* | gnu*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1759 # Guess yes on musl systems.
1760 *-musl* | midipix*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1761 # Guess yes on FreeBSD >= 5.
1762 freebsd[1-4].*) gl_cv_func_snprintf_retval_c99="guessing no";;
1763 freebsd* | kfreebsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1764 midnightbsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1765 # Guess yes on Mac OS X >= 10.3.
1766 darwin[1-6].*) gl_cv_func_snprintf_retval_c99="guessing no";;
1767 darwin*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1768 # Guess yes on OpenBSD >= 3.9.
1769 openbsd[1-2].* | openbsd3.[0-8] | openbsd3.[0-8].*)
1770 gl_cv_func_snprintf_retval_c99="guessing no";;
1771 openbsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1772 # Guess yes on Solaris >= 2.10.
1773 solaris2.[1-9][0-9]*) gl_cv_func_printf_sizes_c99="guessing yes";;
1774 solaris*) gl_cv_func_printf_sizes_c99="guessing no";;
1775 # Guess yes on AIX >= 4.
1776 aix[1-3]*) gl_cv_func_snprintf_retval_c99="guessing no";;
1777 aix*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1778 # Guess yes on NetBSD >= 3.
1779 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1780 gl_cv_func_snprintf_retval_c99="guessing no";;
1781 netbsd*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1782 # Guess yes on BeOS.
1783 beos*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1784 # Guess yes on Android.
1785 linux*-android*) gl_cv_func_snprintf_retval_c99="guessing yes";;
1786 changequote([,])dnl
1787 # Guess yes on MSVC, no on mingw.
1788 windows*-msvc*) gl_cv_func_snprintf_retval_c99="guessing yes" ;;
1789 mingw* | windows*) AC_EGREP_CPP([Known], [
1790 #ifdef _MSC_VER
1791 Known
1792 #endif
1793 ],
1794 [gl_cv_func_snprintf_retval_c99="guessing yes"],
1795 [gl_cv_func_snprintf_retval_c99="guessing no"])
1796 ;;
1797 # If we don't know, obey --enable-cross-guesses.
1798 *) gl_cv_func_snprintf_retval_c99="$gl_cross_guess_normal";;
1799 esac
1800 ])
1801 ])
1802 ])
1803
1804 dnl Test whether the snprintf function supports the %n format directive
1805 dnl also in truncated portions of the format string. (ISO C99, POSIX:2001)
1806 dnl Result is gl_cv_func_snprintf_directive_n.
1807
1808 AC_DEFUN([gl_SNPRINTF_DIRECTIVE_N],
1809 [
1810 AC_REQUIRE([AC_PROG_CC])
1811 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1812 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1813 AC_CACHE_CHECK([whether snprintf fully supports the 'n' directive],
1814 [gl_cv_func_snprintf_directive_n],
1815 [
1816 AC_RUN_IFELSE(
1817 [AC_LANG_SOURCE([[
1818 #include <signal.h>
1819 #include <stdio.h>
1820 #include <string.h>
1821 #if defined _WIN32 && !defined __CYGWIN__
1822 # include <stdlib.h>
1823 #else
1824 # include <unistd.h>
1825 #endif
1826 #if HAVE_SNPRINTF
1827 # define my_snprintf snprintf
1828 #else
1829 # include <stdarg.h>
1830 static int my_snprintf (char *buf, int size, const char *format, ...)
1831 {
1832 va_list args;
1833 int ret;
1834 va_start (args, format);
1835 ret = vsnprintf (buf, size, format, args);
1836 va_end (args);
1837 return ret;
1838 }
1839 #endif
1840 static void
1841 abort_handler (int sig)
1842 {
1843 (void) sig;
1844 _exit (1);
1845 }
1846 static char fmtstring[10];
1847 static char buf[100];
1848 int main ()
1849 {
1850 int count = -1;
1851 signal (SIGABRT, abort_handler);
1852 /* Copy the format string. Some systems (glibc with _FORTIFY_SOURCE >= 2)
1853 support %n in format strings in read-only memory but not in writable
1854 memory. */
1855 strcpy (fmtstring, "%d %n");
1856 my_snprintf (buf, 4, fmtstring, 12345, &count, 33, 44, 55);
1857 if (count != 6)
1858 return 1;
1859 return 0;
1860 }]])],
1861 [gl_cv_func_snprintf_directive_n=yes],
1862 [gl_cv_func_snprintf_directive_n=no],
1863 [
1864 case "$host_os" in
1865 # Guess no on glibc when _FORTIFY_SOURCE >= 2.
1866 *-gnu* | gnu*) AC_COMPILE_IFELSE(
1867 [AC_LANG_SOURCE(
1868 [[#if _FORTIFY_SOURCE >= 2
1869 error fail
1870 #endif
1871 ]])],
1872 [gl_cv_func_snprintf_directive_n="guessing yes"],
1873 [gl_cv_func_snprintf_directive_n="guessing no"])
1874 ;;
1875 changequote(,)dnl
1876 # Guess yes on musl systems.
1877 *-musl* | midipix*) gl_cv_func_snprintf_directive_n="guessing yes";;
1878 # Guess yes on FreeBSD >= 5.
1879 freebsd[1-4].*) gl_cv_func_snprintf_directive_n="guessing no";;
1880 freebsd* | kfreebsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1881 midnightbsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1882 # Guess yes on Mac OS X >= 10.3.
1883 darwin[1-6].*) gl_cv_func_snprintf_directive_n="guessing no";;
1884 darwin*) gl_cv_func_snprintf_directive_n="guessing yes";;
1885 # Guess yes on Solaris >= 2.6.
1886 solaris2.[0-5] | solaris2.[0-5].*)
1887 gl_cv_func_snprintf_directive_n="guessing no";;
1888 solaris*) gl_cv_func_snprintf_directive_n="guessing yes";;
1889 # Guess yes on AIX >= 4.
1890 aix[1-3]*) gl_cv_func_snprintf_directive_n="guessing no";;
1891 aix*) gl_cv_func_snprintf_directive_n="guessing yes";;
1892 # Guess yes on NetBSD >= 3.
1893 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
1894 gl_cv_func_snprintf_directive_n="guessing no";;
1895 netbsd*) gl_cv_func_snprintf_directive_n="guessing yes";;
1896 # Guess yes on BeOS.
1897 beos*) gl_cv_func_snprintf_directive_n="guessing yes";;
1898 # Guess no on Android.
1899 linux*-android*) gl_cv_func_snprintf_directive_n="guessing no";;
1900 # Guess no on native Windows.
1901 mingw* | windows*) gl_cv_func_snprintf_directive_n="guessing no";;
1902 # If we don't know, obey --enable-cross-guesses.
1903 *) gl_cv_func_snprintf_directive_n="$gl_cross_guess_normal";;
1904 changequote([,])dnl
1905 esac
1906 ])
1907 ])
1908 ])
1909
1910 dnl Test whether the snprintf function, when passed a size = 1, writes any
1911 dnl output without bounds in this case, behaving like sprintf. This is the
1912 dnl case on Linux libc5.
1913 dnl Result is gl_cv_func_snprintf_size1.
1914
1915 AC_DEFUN([gl_SNPRINTF_SIZE1],
1916 [
1917 AC_REQUIRE([AC_PROG_CC])
1918 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1919 AC_REQUIRE([gl_SNPRINTF_PRESENCE])
1920 AC_CACHE_CHECK([whether snprintf respects a size of 1],
1921 [gl_cv_func_snprintf_size1],
1922 [
1923 AC_RUN_IFELSE(
1924 [AC_LANG_SOURCE([[
1925 #include <stdio.h>
1926 #if HAVE_SNPRINTF
1927 # define my_snprintf snprintf
1928 #else
1929 # include <stdarg.h>
1930 static int my_snprintf (char *buf, int size, const char *format, ...)
1931 {
1932 va_list args;
1933 int ret;
1934 va_start (args, format);
1935 ret = vsnprintf (buf, size, format, args);
1936 va_end (args);
1937 return ret;
1938 }
1939 #endif
1940 int main()
1941 {
1942 static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1943 my_snprintf (buf, 1, "%d", 12345);
1944 return buf[1] != 'E';
1945 }]])],
1946 [gl_cv_func_snprintf_size1=yes],
1947 [gl_cv_func_snprintf_size1=no],
1948 [case "$host_os" in
1949 # Guess yes on Android.
1950 linux*-android*) gl_cv_func_snprintf_size1="guessing yes" ;;
1951 # Guess yes on native Windows.
1952 mingw* | windows*) gl_cv_func_snprintf_size1="guessing yes" ;;
1953 *) gl_cv_func_snprintf_size1="guessing yes" ;;
1954 esac
1955 ])
1956 ])
1957 ])
1958
1959 dnl Test whether the vsnprintf function, when passed a zero size, produces no
1960 dnl output. (ISO C99, POSIX:2001)
1961 dnl For example, snprintf nevertheless writes a NUL byte in this case
1962 dnl on OSF/1 5.1:
1963 dnl ---------------------------------------------------------------------
1964 dnl #include <stdio.h>
1965 dnl int main()
1966 dnl {
1967 dnl static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1968 dnl snprintf (buf, 0, "%d", 12345);
1969 dnl return buf[0] != 'D';
1970 dnl }
1971 dnl ---------------------------------------------------------------------
1972 dnl And vsnprintf writes any output without bounds in this case, behaving like
1973 dnl vsprintf, on HP-UX 11 and OSF/1 5.1:
1974 dnl ---------------------------------------------------------------------
1975 dnl #include <stdarg.h>
1976 dnl #include <stdio.h>
1977 dnl static int my_snprintf (char *buf, int size, const char *format, ...)
1978 dnl {
1979 dnl va_list args;
1980 dnl int ret;
1981 dnl va_start (args, format);
1982 dnl ret = vsnprintf (buf, size, format, args);
1983 dnl va_end (args);
1984 dnl return ret;
1985 dnl }
1986 dnl int main()
1987 dnl {
1988 dnl static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
1989 dnl my_snprintf (buf, 0, "%d", 12345);
1990 dnl return buf[0] != 'D';
1991 dnl }
1992 dnl ---------------------------------------------------------------------
1993 dnl Result is gl_cv_func_vsnprintf_zerosize_c99.
1994
1995 AC_DEFUN([gl_VSNPRINTF_ZEROSIZE_C99],
1996 [
1997 AC_REQUIRE([AC_PROG_CC])
1998 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
1999 AC_CACHE_CHECK([whether vsnprintf respects a zero size as in C99],
2000 [gl_cv_func_vsnprintf_zerosize_c99],
2001 [
2002 AC_RUN_IFELSE(
2003 [AC_LANG_SOURCE([[
2004 #include <stdarg.h>
2005 #include <stdio.h>
2006 static int my_snprintf (char *buf, int size, const char *format, ...)
2007 {
2008 va_list args;
2009 int ret;
2010 va_start (args, format);
2011 ret = vsnprintf (buf, size, format, args);
2012 va_end (args);
2013 return ret;
2014 }
2015 int main()
2016 {
2017 static char buf[8] = { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F' };
2018 my_snprintf (buf, 0, "%d", 12345);
2019 return buf[0] != 'D';
2020 }]])],
2021 [gl_cv_func_vsnprintf_zerosize_c99=yes],
2022 [gl_cv_func_vsnprintf_zerosize_c99=no],
2023 [
2024 changequote(,)dnl
2025 case "$host_os" in
2026 # Guess yes on glibc systems.
2027 *-gnu* | gnu*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2028 # Guess yes on musl systems.
2029 *-musl* | midipix*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2030 # Guess yes on FreeBSD >= 5.
2031 freebsd[1-4].*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
2032 freebsd* | kfreebsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2033 midnightbsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2034 # Guess yes on Mac OS X >= 10.3.
2035 darwin[1-6].*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
2036 darwin*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2037 # Guess yes on Cygwin.
2038 cygwin*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2039 # Guess yes on Solaris >= 2.6.
2040 solaris2.[0-5] | solaris2.[0-5].*)
2041 gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
2042 solaris*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2043 # Guess yes on AIX >= 4.
2044 aix[1-3]*) gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
2045 aix*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2046 # Guess yes on NetBSD >= 3.
2047 netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*)
2048 gl_cv_func_vsnprintf_zerosize_c99="guessing no";;
2049 netbsd*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2050 # Guess yes on BeOS.
2051 beos*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2052 # Guess yes on Android.
2053 linux*-android*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2054 # Guess yes on native Windows.
2055 mingw* | windows* | pw*) gl_cv_func_vsnprintf_zerosize_c99="guessing yes";;
2056 # If we don't know, obey --enable-cross-guesses.
2057 *) gl_cv_func_vsnprintf_zerosize_c99="$gl_cross_guess_normal";;
2058 esac
2059 changequote([,])dnl
2060 ])
2061 ])
2062 ])
2063
2064 dnl Test whether the swprintf function works correctly when it produces output
2065 dnl that contains null wide characters.
2066 dnl Result is gl_cv_func_swprintf_works.
2067
2068 AC_DEFUN([gl_SWPRINTF_WORKS],
2069 [
2070 AC_REQUIRE([AC_PROG_CC])
2071 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
2072 AC_CHECK_FUNCS_ONCE([swprintf])
2073 AC_CACHE_CHECK([whether swprintf works],
2074 [gl_cv_func_swprintf_works],
2075 [
2076 AC_RUN_IFELSE(
2077 [AC_LANG_SOURCE([[
2078 #ifndef __USE_MINGW_ANSI_STDIO
2079 # define __USE_MINGW_ANSI_STDIO 1
2080 #endif
2081 #include <stdio.h>
2082 #include <wchar.h>
2083 int main()
2084 {
2085 int result = 0;
2086 { /* This test fails on musl libc 1.2.3, FreeBSD, NetBSD, OpenBSD, macOS, AIX. */
2087 wchar_t buf[5] = { 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF };
2088 int ret = swprintf (buf, 4, L"%cz", '\0');
2089 /* Expected result:
2090 ret = 2, buf[0] = 0x0, buf[1] = 0x7a, buf[2] = 0x0, buf[3] = 0xbeef
2091 musl libc 1.2.3:
2092 ret = 2, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0
2093 Reported at <https://www.openwall.com/lists/musl/2023/03/22/9>.
2094 FreeBSD 13.1, NetBSD 9.0, OpenBSD 7.2, macOS 12.5, AIX 7.2:
2095 ret = 2, buf[0] = 0x0, buf[1] = 0xbeef, buf[2] = 0xbeef, buf[3] = 0xbeef
2096 */
2097 if (ret < 0 || buf[1] != 'z')
2098 result |= 1;
2099 }
2100 { /* This test fails on mingw. */
2101 wchar_t buf[2];
2102 int ret = swprintf (buf, 2, L"%lc", (wint_t)0);
2103 /* Expected: ret = 1
2104 mingw: ret = 0
2105 */
2106 if (ret != 1)
2107 result |= 2;
2108 }
2109 return result;
2110 }]])],
2111 [gl_cv_func_swprintf_works=yes],
2112 [gl_cv_func_swprintf_works=no],
2113 [case "$host_os" in
2114 # Guess yes on glibc systems.
2115 *-gnu* | gnu*) gl_cv_func_swprintf_works="guessing yes";;
2116 # Guess no on musl systems.
2117 *-musl* | midipix*) gl_cv_func_swprintf_works="guessing no";;
2118 # Guess no on FreeBSD, NetBSD, OpenBSD, macOS, AIX.
2119 freebsd* | midnightbsd* | netbsd* | openbsd* | darwin* | aix*)
2120 gl_cv_func_swprintf_works="guessing no";;
2121 # Guess no on native Windows.
2122 mingw* | windows* | pw*) gl_cv_func_swprintf_works="guessing no";;
2123 # If we don't know, obey --enable-cross-guesses.
2124 *) gl_cv_func_swprintf_works="$gl_cross_guess_normal";;
2125 esac
2126 ])
2127 ])
2128 ])
2129
2130 dnl Test whether the *wprintf family of functions supports the 'a' and 'A'
2131 dnl conversion specifier for hexadecimal output of 'long double' numbers.
2132 dnl (ISO C99, POSIX:2001)
2133 dnl Result is gl_cv_func_swprintf_directive_la.
2134
2135 AC_DEFUN([gl_SWPRINTF_DIRECTIVE_LA],
2136 [
2137 AC_REQUIRE([AC_PROG_CC])
2138 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
2139 AC_CACHE_CHECK([whether swprintf supports the 'La' and 'LA' directives],
2140 [gl_cv_func_swprintf_directive_la],
2141 [
2142 AC_RUN_IFELSE(
2143 [AC_LANG_SOURCE([[
2144 #include <stdio.h>
2145 #include <wchar.h>
2146 static wchar_t buf[100];
2147 int main ()
2148 {
2149 int result = 0;
2150 /* This catches a glibc 2.15, Haiku 2022, NetBSD 10.0 bug. */
2151 if (swprintf (buf, sizeof (buf) / sizeof (wchar_t),
2152 L"%La %d", 3.1416015625L, 33, 44, 55) < 0
2153 || (wcscmp (buf, L"0x1.922p+1 33") != 0
2154 && wcscmp (buf, L"0x3.244p+0 33") != 0
2155 && wcscmp (buf, L"0x6.488p-1 33") != 0
2156 && wcscmp (buf, L"0xc.91p-2 33") != 0))
2157 result |= 1;
2158 return result;
2159 }]])],
2160 [gl_cv_func_swprintf_directive_la=yes],
2161 [gl_cv_func_swprintf_directive_la=no],
2162 [case "$host_os" in
2163 # Guess yes on glibc >= 2.17 systems.
2164 *-gnu* | gnu*)
2165 AC_EGREP_CPP([Unlucky], [
2166 #include <features.h>
2167 #ifdef __GNU_LIBRARY__
2168 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16) || (__GLIBC__ > 2)) && !defined __UCLIBC__
2169 Unlucky
2170 #endif
2171 #endif
2172 ],
2173 [gl_cv_func_swprintf_directive_la="guessing yes"],
2174 [gl_cv_func_swprintf_directive_la="guessing no"])
2175 ;;
2176 # Guess yes on musl systems.
2177 *-musl* | midipix*) gl_cv_func_swprintf_directive_la="guessing yes";;
2178 # Guess yes on Android.
2179 linux*-android*) gl_cv_func_swprintf_directive_la="guessing yes";;
2180 # Guess no on NetBSD.
2181 netbsd*) gl_cv_func_swprintf_directive_la="guessing no";;
2182 # Guess no on native Windows.
2183 mingw* | windows*) gl_cv_func_swprintf_directive_la="guessing no";;
2184 # If we don't know, obey --enable-cross-guesses.
2185 *) gl_cv_func_swprintf_directive_la="$gl_cross_guess_normal";;
2186 esac
2187 ])
2188 ])
2189 ])
2190
2191 dnl Test whether the *wprintf family of functions supports the 'lc' conversion
2192 dnl specifier for all wide characters.
2193 dnl (ISO C11, POSIX:2001)
2194 dnl Result is gl_cv_func_swprintf_directive_lc.
2195
2196 AC_DEFUN([gl_SWPRINTF_DIRECTIVE_LC],
2197 [
2198 AC_REQUIRE([AC_PROG_CC])
2199 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
2200 AC_CACHE_CHECK([whether swprintf supports the 'lc' directive],
2201 [gl_cv_func_swprintf_directive_lc],
2202 [
2203 AC_RUN_IFELSE(
2204 [AC_LANG_SOURCE([[
2205 #include <stdio.h>
2206 #include <wchar.h>
2207 static wchar_t buf[100];
2208 static wint_t L_invalid = (wchar_t) 0x76543210;
2209 int main ()
2210 {
2211 int result = 0;
2212 /* This catches a musl libc 1.2.4, Android bug.
2213 Reported at <https://www.openwall.com/lists/musl/2023/06/12/3>. */
2214 if (swprintf (buf, sizeof (buf) / sizeof (wchar_t),
2215 L"%lc %d", L_invalid, 33, 44, 55) < 0)
2216 result |= 1;
2217 return result;
2218 }]])],
2219 [gl_cv_func_swprintf_directive_lc=yes],
2220 [gl_cv_func_swprintf_directive_lc=no],
2221 [case "$host_os" in
2222 # Guess yes on glibc systems.
2223 *-gnu* | gnu*) gl_cv_func_swprintf_directive_lc="guessing yes";;
2224 # Guess no on musl systems.
2225 *-musl* | midipix*) gl_cv_func_swprintf_directive_lc="guessing no";;
2226 # Guess no on Android.
2227 linux*-android*) gl_cv_func_swprintf_directive_lc="guessing no";;
2228 # Guess yes on native Windows.
2229 mingw* | windows*) gl_cv_func_swprintf_directive_lc="guessing yes";;
2230 # If we don't know, obey --enable-cross-guesses.
2231 *) gl_cv_func_swprintf_directive_lc="$gl_cross_guess_normal";;
2232 esac
2233 ])
2234 ])
2235 ])
2236
2237 dnl The results of these tests on various platforms are:
2238 dnl
2239 dnl 1 = gl_PRINTF_SIZES_C99
2240 dnl 2 = gl_PRINTF_SIZES_C23
2241 dnl 3 = gl_PRINTF_LONG_DOUBLE
2242 dnl 4 = gl_PRINTF_INFINITE
2243 dnl 5 = gl_PRINTF_INFINITE_LONG_DOUBLE
2244 dnl 6 = gl_PRINTF_DIRECTIVE_A
2245 dnl 7 = gl_PRINTF_DIRECTIVE_B
2246 dnl 8 = gl_PRINTF_DIRECTIVE_UPPERCASE_B
2247 dnl 9 = gl_PRINTF_DIRECTIVE_F
2248 dnl 10 = gl_PRINTF_DIRECTIVE_N
2249 dnl 11 = gl_PRINTF_DIRECTIVE_LS
2250 dnl 12 = gl_PRINTF_DIRECTIVE_LC
2251 dnl 13 = gl_PRINTF_POSITIONS
2252 dnl 14 = gl_PRINTF_FLAG_GROUPING
2253 dnl 15 = gl_PRINTF_FLAG_GROUPING_INT_PRECISION
2254 dnl 16 = gl_PRINTF_FLAG_GROUPING_MULTIBYTE
2255 dnl 17 = gl_PRINTF_FLAG_LEFTADJUST
2256 dnl 18 = gl_PRINTF_FLAG_ZERO
2257 dnl 19 = gl_PRINTF_FLAG_ALT_PRECISION_ZERO
2258 dnl 20 = gl_PRINTF_PRECISION
2259 dnl 21 = gl_PRINTF_ENOMEM
2260 dnl 22 = gl_SNPRINTF_PRESENCE
2261 dnl 23 = gl_SNPRINTF_TRUNCATION_C99
2262 dnl 24 = gl_SNPRINTF_RETVAL_C99
2263 dnl 25 = gl_SNPRINTF_DIRECTIVE_N
2264 dnl 26 = gl_SNPRINTF_SIZE1
2265 dnl 27 = gl_VSNPRINTF_ZEROSIZE_C99
2266 dnl 28 = gl_SWPRINTF_WORKS
2267 dnl 29 = gl_SWPRINTF_DIRECTIVE_LA
2268 dnl 30 = gl_SWPRINTF_DIRECTIVE_LC
2269 dnl
2270 dnl 1 = checking whether printf supports size specifiers as in C99...
2271 dnl 2 = checking whether printf supports size specifiers as in C23...
2272 dnl 3 = checking whether printf supports 'long double' arguments...
2273 dnl 4 = checking whether printf supports infinite 'double' arguments...
2274 dnl 5 = checking whether printf supports infinite 'long double' arguments...
2275 dnl 6 = checking whether printf supports the 'a' and 'A' directives...
2276 dnl 7 = checking whether printf supports the 'b' directive...
2277 dnl 8 = checking whether printf supports the 'B' directive...
2278 dnl 9 = checking whether printf supports the 'F' directive...
2279 dnl 10 = checking whether printf supports the 'n' directive...
2280 dnl 11 = checking whether printf supports the 'ls' directive...
2281 dnl 12 = checking whether printf supports the 'lc' directive correctly...
2282 dnl 13 = checking whether printf supports POSIX/XSI format strings with positions...
2283 dnl 14 = checking whether printf supports the grouping flag...
2284 dnl 15 = checking whether printf supports grouping on integers with a precision...
2285 dnl 16 = checking whether printf supports grouping with a multibyte separator...
2286 dnl 17 = checking whether printf supports the left-adjust flag correctly...
2287 dnl 18 = checking whether printf supports the zero flag correctly...
2288 dnl 19 = checking whether printf supports the alternative flag with a zero precision...
2289 dnl 20 = checking whether printf supports large precisions...
2290 dnl 21 = checking whether printf survives out-of-memory conditions...
2291 dnl 22 = checking for snprintf...
2292 dnl 23 = checking whether snprintf truncates the result as in C99...
2293 dnl 24 = checking whether snprintf returns a byte count as in C99...
2294 dnl 25 = checking whether snprintf fully supports the 'n' directive...
2295 dnl 26 = checking whether snprintf respects a size of 1...
2296 dnl 27 = checking whether vsnprintf respects a zero size as in C99...
2297 dnl 28 = checking whether swprintf works...
2298 dnl 29 = checking whether swprintf supports the 'La' and 'LA' directives...
2299 dnl 30 = checking whether swprintf supports the 'lc' directive...
2300 dnl
2301 dnl . = yes, # = no.
2302 dnl
2303 dnl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
2304 dnl musl libc 1.2.3 . # . . . . # # . . . # . . . . . . . . . . . . . . . # . #
2305 dnl glibc 2.35 . # . . . . . . . . . . . . # . . . . . . . . . . . . . . .
2306 dnl glibc 2.5 . # . . . . # # . . . . . . # . . . . . . . . . . . . . # .
2307 dnl glibc 2.3.6 . # . . . # # # . . . . . . . . . . . . . . . . . . . . # .
2308 dnl FreeBSD 14.0 . . . . . # . . . . . . . . # . . . . . # . . . . . . # . #
2309 dnl FreeBSD 13.0 . # . . . # # # . . . . . . # . . . . . # . . . . . . # . #
2310 dnl FreeBSD 5.4, 6.1 . # . . . # # # . . . . . . . . . # ? . # . . . . . . # ? ?
2311 dnl Mac OS X 10.13.5 . # . . # # # # . # . . . . ? ? . . . . . . . . # . . # ? ?
2312 dnl Mac OS X 10.5.8 . # . . # # # # . . . . . . ? ? . # # . . . . . . . . # ? ?
2313 dnl Mac OS X 10.3.9 . # . . . # # # . . . . . . ? ? . # # . # . . . . . . # ? ?
2314 dnl OpenBSD 6.0, 6.7 . # . . . # # # . . . . . . . . . . . . # . . . . . . # . #
2315 dnl OpenBSD 3.9, 4.0 . # . # # # # # # . # . . # ? ? . # ? . # . . . . . . # ? ?
2316 dnl Cygwin 1.7.0 (2009) . # . . # . # # . . ? ? . . ? ? . . ? . ? . . . . . . ? ? ?
2317 dnl Cygwin 1.5.25 (2008) . # . . # # # # . . # ? . . ? ? . . ? . # . . . . . . ? ? ?
2318 dnl Cygwin 1.5.19 (2006) # # . . # # # # # . # ? . # ? ? . # ? # # . . . . . . ? ? ?
2319 dnl Solaris 11.4 . # . # # # # # . . # . . . # # . # . . . . . . . . . . # .
2320 dnl Solaris 11.3 . # . . . # # # . . # . . . ? ? . . . . . . . . . . . . # .
2321 dnl Solaris 11.0 . # . # # # # # . . # . . . ? ? . # . . . . . . . . . ? ? ?
2322 dnl Solaris 10 . # . # # # # # . . # . . . # # . # . # . . . . . . . . # .
2323 dnl Solaris 2.6 ... 9 # # . # # # # # # . # . . . ? ? . # ? # . . . # . . . ? ? ?
2324 dnl Solaris 2.5.1 # # . # # # # # # . # . . . ? ? . # ? . . # # # # # # ? ? ?
2325 dnl AIX 7.1 . # . # # # # # . . . . . . . . . # . # . . . . . . . # . .
2326 dnl AIX 5.2 . # . # # # # # . . . . . . ? ? . # ? . . . . . . . . # ? ?
2327 dnl AIX 4.3.2, 5.1 # # . # # # # # # . . . . . ? ? . # ? . . . . # . . . # ? ?
2328 dnl HP-UX 11.31 . # . . . # # # . . . ? . . ? ? . # ? . . . . # # . . ? ? ?
2329 dnl HP-UX 11.{00,11,23} # # . . . # # # # . . ? . . ? ? . # ? . . . . # # . # ? ? ?
2330 dnl HP-UX 10.20 # # . # . # # # # . ? ? . . ? ? # # ? . . . . # # ? # ? ? ?
2331 dnl IRIX 6.5 # # . # # # # # # . # . . . ? ? . # ? . . . . # . . . # ? ?
2332 dnl OSF/1 5.1 # # . # # # # # # . . ? . . ? ? . # ? . . . . # . . # ? ? ?
2333 dnl OSF/1 4.0d # # . # # # # # # . . ? . . ? ? . # ? . . # # # # # # ? ? ?
2334 dnl NetBSD 9.0 . # . . . # # # . . . . . . # # . . . . . . . . . . . # . #
2335 dnl NetBSD 5.0 . # . . # # # # . . . . . . ? ? . # ? . # . . . . . . # ? ?
2336 dnl NetBSD 4.0 . # ? ? ? ? # # ? . ? . . ? ? ? ? ? ? ? ? . . . ? ? ? # ? ?
2337 dnl NetBSD 3.0 . # . . . # # # # . ? . # # ? ? ? # ? . # . . . . . . # ? ?
2338 dnl Haiku . # . . # # # # # . # ? . . # . . . ? . ? . . ? . . . . # .
2339 dnl BeOS # # # . # # # # # . ? ? # . ? ? ? . ? # ? . . ? . . . ? ? ?
2340 dnl Android 4.3 . # . # # # # # # # # ? . # ? ? . # ? . # . . . # . . ? ? ?
2341 dnl old mingw / msvcrt # # # # # # # # # . . ? # # ? ? . # ? # ? . # # # . . # ? ?
2342 dnl MSVC 9 # # # # # # # # # # . ? # # ? ? . # ? # ? # # # # . . # ? ?
2343 dnl mingw 2009-2011 . # # . # . # # . . . ? # # ? ? . . ? . ? . . . . . . # ? ?
2344 dnl mingw-w64 2011 # # # # # # # # # . . ? # # ? ? . # ? # ? . # # # . . # ? ?