1 | # fflush.m4 serial 18
|
---|
2 |
|
---|
3 | # Copyright (C) 2007-2022 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.
|
---|
7 |
|
---|
8 | dnl From Eric Blake
|
---|
9 |
|
---|
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.
|
---|
12 |
|
---|
13 | AC_DEFUN([gl_FUNC_FFLUSH],
|
---|
14 | [
|
---|
15 | AC_REQUIRE([gl_STDIO_H_DEFAULTS])
|
---|
16 | gl_FUNC_FFLUSH_STDIN
|
---|
17 | case "$gl_cv_func_fflush_stdin" in
|
---|
18 | *yes) ;;
|
---|
19 | *) REPLACE_FFLUSH=1 ;;
|
---|
20 | esac
|
---|
21 | ])
|
---|
22 |
|
---|
23 | dnl Determine whether fflush works on input streams.
|
---|
24 | dnl Sets gl_cv_func_fflush_stdin.
|
---|
25 |
|
---|
26 | AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
|
---|
27 | [
|
---|
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(
|
---|
34 | [[
|
---|
35 | #include <stdio.h>
|
---|
36 | #if HAVE_UNISTD_H
|
---|
37 | # include <unistd.h>
|
---|
38 | #else /* on Windows with MSVC */
|
---|
39 | # include <io.h>
|
---|
40 | #endif
|
---|
41 | ]GL_MDA_DEFINES],
|
---|
42 | [[FILE *f = fopen ("conftest.txt", "r");
|
---|
43 | char buffer[10];
|
---|
44 | int fd;
|
---|
45 | int c;
|
---|
46 | if (f == NULL)
|
---|
47 | return 1;
|
---|
48 | fd = fileno (f);
|
---|
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
|
---|
63 | mingw. */
|
---|
64 | c = fgetc (f);
|
---|
65 | ungetc (c, f);
|
---|
66 | fflush (f);
|
---|
67 | if (fgetc (f) != c)
|
---|
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. */
|
---|
71 | c = fgetc (f);
|
---|
72 | ungetc ('@', f);
|
---|
73 | fflush (f);
|
---|
74 | if (fgetc (f) != c)
|
---|
75 | { fclose (f); return 7; }
|
---|
76 | fclose (f);
|
---|
77 | return 0;
|
---|
78 | ]])],
|
---|
79 | [gl_cv_func_fflush_stdin=yes],
|
---|
80 | [gl_cv_func_fflush_stdin=no],
|
---|
81 | [case "$host_os" in
|
---|
82 | # Guess no on native Windows.
|
---|
83 | mingw*) gl_cv_func_fflush_stdin="guessing no" ;;
|
---|
84 | *) gl_cv_func_fflush_stdin=cross ;;
|
---|
85 | esac
|
---|
86 | ])
|
---|
87 | rm conftest.txt
|
---|
88 | ])
|
---|
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)' ;;
|
---|
93 | esac
|
---|
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.])
|
---|
97 | ])
|
---|
98 |
|
---|
99 | # Prerequisites of lib/fflush.c.
|
---|
100 | AC_DEFUN([gl_PREREQ_FFLUSH], [:])
|
---|