doc: Remove references to POSIX 202x.
[gnulib.git] / m4 / ungetc.m4
blob42f7ec327858b5e7a74be57ec1d34df2296bf3dc
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.
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([[
16 #include <stdio.h>
17       ]], [[FILE *f;
18             if (!(f = fopen ("conftest.tmp", "w+")))
19               return 1;
20             if (fputs ("abc", f) < 0)
21               { fclose (f); return 2; }
22             rewind (f);
23             if (fgetc (f) != 'a')
24               { fclose (f); return 3; }
25             if (fgetc (f) != 'b')
26               { fclose (f); return 4; }
27             if (ungetc ('d', f) != 'd')
28               { fclose (f); return 5; }
29             if (ftell (f) != 1)
30               { fclose (f); return 6; }
31             if (fgetc (f) != 'd')
32               { fclose (f); return 7; }
33             if (ftell (f) != 2)
34               { fclose (f); return 8; }
35             if (fseek (f, 0, SEEK_CUR) != 0)
36               { fclose (f); return 9; }
37             if (ftell (f) != 2)
38               { fclose (f); return 10; }
39             if (fgetc (f) != 'c')
40               { fclose (f); return 11; }
41             fclose (f);
42             remove ("conftest.tmp");
43           ]])],
44         [gl_cv_func_ungetc_works=yes], [gl_cv_func_ungetc_works=no],
45         [case "$host_os" in
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" ;;
56          esac
57         ])
58     ])
59   gl_ftello_broken_after_ungetc=no
60   case "$gl_cv_func_ungetc_works" in
61     *yes) ;;
62     *)
63       dnl On macOS >= 10.15, where the above program fails with exit code 6,
64       dnl we fix it through an ftello override.
65       case "$host_os" in
66         darwin*) gl_ftello_broken_after_ungetc=yes ;;
67         *)
68           AC_DEFINE([FUNC_UNGETC_BROKEN], [1],
69             [Define to 1 if ungetc is broken when used on arbitrary bytes.])
70           ;;
71       esac
72       ;;
73   esac