3 dnl Copyright (C) 2009-2024 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.
8 AC_DEFUN_ONCE([gl_FUNC_UNGETC_WORKS],
10 AC_REQUIRE([AC_PROG_CC])
11 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13 AC_CACHE_CHECK([whether ungetc works on arbitrary bytes],
14 [gl_cv_func_ungetc_works],
15 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
18 if (!(f = fopen ("conftest.tmp", "w+")))
20 if (fputs ("abc", f) < 0)
21 { fclose (f); return 2; }
24 { fclose (f); return 3; }
26 { fclose (f); return 4; }
27 if (ungetc ('d', f) != 'd')
28 { fclose (f); return 5; }
30 { fclose (f); return 6; }
32 { fclose (f); return 7; }
34 { fclose (f); return 8; }
35 if (fseek (f, 0, SEEK_CUR) != 0)
36 { fclose (f); return 9; }
38 { fclose (f); return 10; }
40 { fclose (f); return 11; }
42 remove ("conftest.tmp");
44 [gl_cv_func_ungetc_works=yes], [gl_cv_func_ungetc_works=no],
46 # Guess yes on glibc systems.
47 *-gnu* | gnu*) gl_cv_func_ungetc_works="guessing yes" ;;
48 # Guess yes on musl systems.
49 *-musl* | midipix*) gl_cv_func_ungetc_works="guessing yes" ;;
50 # Guess yes on bionic systems.
51 *-android*) gl_cv_func_ungetc_works="guessing yes" ;;
52 # Guess yes on native Windows.
53 mingw* | windows*) gl_cv_func_ungetc_works="guessing yes" ;;
54 # If we don't know, obey --enable-cross-guesses.
55 *) gl_cv_func_ungetc_works="$gl_cross_guess_normal" ;;
59 gl_ftello_broken_after_ungetc=no
60 case "$gl_cv_func_ungetc_works" in
63 dnl On macOS >= 10.15, where the above program fails with exit code 6,
64 dnl we fix it through an ftello override.
66 darwin*) gl_ftello_broken_after_ungetc=yes ;;
68 AC_DEFINE([FUNC_UNGETC_BROKEN], [1],
69 [Define to 1 if ungetc is broken when used on arbitrary bytes.])