3 dnl Copyright (C) 2007-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.
10 dnl Find out how to obey POSIX semantics of fflush(stdin) discarding
11 dnl unread input on seekable streams, rather than C99 undefined semantics.
13 AC_DEFUN([gl_FUNC_FFLUSH],
15 AC_REQUIRE([gl_STDIO_H_DEFAULTS])
17 case "$gl_cv_func_fflush_stdin" in
19 *) REPLACE_FFLUSH=1 ;;
23 dnl Determine whether fflush works on input streams.
24 dnl Sets gl_cv_func_fflush_stdin.
26 AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
28 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
29 AC_CHECK_HEADERS_ONCE([unistd.h])
30 AC_CACHE_CHECK([whether fflush works on input streams],
31 [gl_cv_func_fflush_stdin],
32 [echo hello world > conftest.txt
33 AC_RUN_IFELSE([AC_LANG_PROGRAM(
38 #else /* on Windows with MSVC */
42 [[FILE *f = fopen ("conftest.txt", "r");
49 if (fd < 0 || fread (buffer, 1, 5, f) != 5)
50 { fclose (f); return 2; }
51 /* For deterministic results, ensure f read a bigger buffer. */
52 if (lseek (fd, 0, SEEK_CUR) == 5)
53 { fclose (f); return 3; }
54 /* POSIX requires fflush-fseek to set file offset of fd. This fails
55 on BSD systems and on mingw. */
56 if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
57 { fclose (f); return 4; }
58 if (lseek (fd, 0, SEEK_CUR) != 5)
59 { fclose (f); return 5; }
60 /* Verify behaviour of fflush after ungetc. See
61 <https://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> */
62 /* Verify behaviour of fflush after a backup ungetc. This fails on
68 { fclose (f); return 6; }
69 /* Verify behaviour of fflush after a non-backup ungetc. This fails
70 on glibc 2.8 and on BSD systems. */
75 { fclose (f); return 7; }
79 [gl_cv_func_fflush_stdin=yes],
80 [gl_cv_func_fflush_stdin=no],
82 # Guess no on native Windows.
83 mingw* | windows*) gl_cv_func_fflush_stdin="guessing no" ;;
84 *) gl_cv_func_fflush_stdin=cross ;;
89 case "$gl_cv_func_fflush_stdin" in
90 *yes) gl_func_fflush_stdin=1 ;;
91 *no) gl_func_fflush_stdin=0 ;;
92 *) gl_func_fflush_stdin='(-1)' ;;
94 AC_DEFINE_UNQUOTED([FUNC_FFLUSH_STDIN], [$gl_func_fflush_stdin],
95 [Define to 1 if fflush is known to work on stdin as per POSIX.1-2008,
96 0 if fflush is known to not work, -1 if unknown.])
99 # Prerequisites of lib/fflush.c.
100 AC_DEFUN([gl_PREREQ_FFLUSH], [:])