Debian Codegraph @

m4/ungetc.m4

raw
1 # ungetc.m4
2 # serial 12
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.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN_ONCE([gl_FUNC_UNGETC_WORKS],
10 [
11 AC_REQUIRE([AC_PROG_CC])
12 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13
14 AC_CACHE_CHECK([whether ungetc works on arbitrary bytes],
15 [gl_cv_func_ungetc_works],
16 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
17 #include <stdio.h>
18 ]], [[FILE *f;
19 if (!(f = fopen ("conftest.tmp", "w+")))
20 return 1;
21 if (fputs ("abc", f) < 0)
22 { fclose (f); return 2; }
23 rewind (f);
24 if (fgetc (f) != 'a')
25 { fclose (f); return 3; }
26 if (fgetc (f) != 'b')
27 { fclose (f); return 4; }
28 if (ungetc ('d', f) != 'd')
29 { fclose (f); return 5; }
30 if (ftell (f) != 1)
31 { fclose (f); return 6; }
32 if (fgetc (f) != 'd')
33 { fclose (f); return 7; }
34 if (ftell (f) != 2)
35 { fclose (f); return 8; }
36 if (fseek (f, 0, SEEK_CUR) != 0)
37 { fclose (f); return 9; }
38 if (ftell (f) != 2)
39 { fclose (f); return 10; }
40 if (fgetc (f) != 'c')
41 { fclose (f); return 11; }
42 fclose (f);
43 remove ("conftest.tmp");
44 ]])],
45 [gl_cv_func_ungetc_works=yes], [gl_cv_func_ungetc_works=no],
46 [case "$host_os" in
47 # Guess yes on glibc systems.
48 *-gnu* | gnu*) gl_cv_func_ungetc_works="guessing yes" ;;
49 # Guess yes on musl systems.
50 *-musl* | midipix*) gl_cv_func_ungetc_works="guessing yes" ;;
51 # Guess yes on bionic systems.
52 *-android*) gl_cv_func_ungetc_works="guessing yes" ;;
53 # Guess yes on native Windows.
54 mingw* | windows*) gl_cv_func_ungetc_works="guessing yes" ;;
55 # If we don't know, obey --enable-cross-guesses.
56 *) gl_cv_func_ungetc_works="$gl_cross_guess_normal" ;;
57 esac
58 ])
59 ])
60 gl_ftello_broken_after_ungetc=no
61 case "$gl_cv_func_ungetc_works" in
62 *yes) ;;
63 *)
64 dnl On macOS >= 10.15, where the above program fails with exit code 6,
65 dnl we fix it through an ftello override.
66 case "$host_os" in
67 darwin*) gl_ftello_broken_after_ungetc=yes ;;
68 *)
69 AC_DEFINE([FUNC_UNGETC_BROKEN], [1],
70 [Define to 1 if ungetc is broken when used on arbitrary bytes.])
71 ;;
72 esac
73 ;;
74 esac
75 ])