3 # Copyright (C) 2007-2015 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # 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 if test $gl_cv_func_fflush_stdin != yes; then
22 dnl Determine whether fflush works on input streams.
23 dnl Sets gl_cv_func_fflush_stdin.
25 AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
27 AC_CHECK_HEADERS_ONCE([unistd.h])
28 AC_CACHE_CHECK([whether fflush works on input streams],
29 [gl_cv_func_fflush_stdin],
30 [echo hello world > conftest.txt
31 AC_RUN_IFELSE([AC_LANG_PROGRAM(
36 #else /* on Windows with MSVC */
39 ]], [[FILE *f = fopen ("conftest.txt", "r");
46 if (fd < 0 || fread (buffer, 1, 5, f) != 5)
48 /* For deterministic results, ensure f read a bigger buffer. */
49 if (lseek (fd, 0, SEEK_CUR) == 5)
51 /* POSIX requires fflush-fseek to set file offset of fd. This fails
52 on BSD systems and on mingw. */
53 if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
55 if (lseek (fd, 0, SEEK_CUR) != 5)
57 /* Verify behaviour of fflush after ungetc. See
58 <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> */
59 /* Verify behaviour of fflush after a backup ungetc. This fails on
66 /* Verify behaviour of fflush after a non-backup ungetc. This fails
67 on glibc 2.8 and on BSD systems. */
74 ]])], [gl_cv_func_fflush_stdin=yes], [gl_cv_func_fflush_stdin=no],
75 [gl_cv_func_fflush_stdin=cross])
78 case $gl_cv_func_fflush_stdin in
79 yes) gl_func_fflush_stdin=1 ;;
80 no) gl_func_fflush_stdin=0 ;;
81 *) gl_func_fflush_stdin='(-1)' ;;
83 AC_DEFINE_UNQUOTED([FUNC_FFLUSH_STDIN], [$gl_func_fflush_stdin],
84 [Define to 1 if fflush is known to work on stdin as per POSIX.1-2008,
85 0 if fflush is known to not work, -1 if unknown.])
88 # Prerequisites of lib/fflush.c.
89 AC_DEFUN([gl_PREREQ_FFLUSH], [:])