Debian Codegraph @

m4/fflush.m4

raw
1 # fflush.m4
2 # serial 20
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.
7 dnl This file is offered as-is, without any warranty.
8
9 dnl From Eric Blake
10
11 dnl Find out how to obey POSIX semantics of fflush(stdin) discarding
12 dnl unread input on seekable streams, rather than C99 undefined semantics.
13
14 AC_DEFUN([gl_FUNC_FFLUSH],
15 [
16 AC_REQUIRE([gl_STDIO_H_DEFAULTS])
17 gl_FUNC_FFLUSH_STDIN
18 case "$gl_cv_func_fflush_stdin" in
19 *yes) ;;
20 *) REPLACE_FFLUSH=1 ;;
21 esac
22 ])
23
24 dnl Determine whether fflush works on input streams.
25 dnl Sets gl_cv_func_fflush_stdin.
26
27 AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
28 [
29 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
30 AC_CHECK_HEADERS_ONCE([unistd.h])
31 AC_CACHE_CHECK([whether fflush works on input streams],
32 [gl_cv_func_fflush_stdin],
33 [echo hello world > conftest.txt
34 AC_RUN_IFELSE([AC_LANG_PROGRAM(
35 [[
36 #include <stdio.h>
37 #if HAVE_UNISTD_H
38 # include <unistd.h>
39 #else /* on Windows with MSVC */
40 # include <io.h>
41 #endif
42 ]GL_MDA_DEFINES],
43 [[FILE *f = fopen ("conftest.txt", "r");
44 char buffer[10];
45 int fd;
46 int c;
47 if (f == NULL)
48 return 1;
49 fd = fileno (f);
50 if (fd < 0 || fread (buffer, 1, 5, f) != 5)
51 { fclose (f); return 2; }
52 /* For deterministic results, ensure f read a bigger buffer. */
53 if (lseek (fd, 0, SEEK_CUR) == 5)
54 { fclose (f); return 3; }
55 /* POSIX requires fflush-fseek to set file offset of fd. This fails
56 on BSD systems and on mingw. */
57 if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
58 { fclose (f); return 4; }
59 if (lseek (fd, 0, SEEK_CUR) != 5)
60 { fclose (f); return 5; }
61 /* Verify behaviour of fflush after ungetc. See
62 <https://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> */
63 /* Verify behaviour of fflush after a backup ungetc. This fails on
64 mingw. */
65 c = fgetc (f);
66 ungetc (c, f);
67 fflush (f);
68 if (fgetc (f) != c)
69 { fclose (f); return 6; }
70 /* Verify behaviour of fflush after a non-backup ungetc. This fails
71 on glibc 2.8 and on BSD systems. */
72 c = fgetc (f);
73 ungetc ('@', f);
74 fflush (f);
75 if (fgetc (f) != c)
76 { fclose (f); return 7; }
77 fclose (f);
78 return 0;
79 ]])],
80 [gl_cv_func_fflush_stdin=yes],
81 [gl_cv_func_fflush_stdin=no],
82 [case "$host_os" in
83 # Guess no on NetBSD, OpenBSD, native Windows.
84 netbsd* | openbsd* | mingw* | windows*)
85 gl_cv_func_fflush_stdin="guessing no" ;;
86 *) gl_cv_func_fflush_stdin=cross ;;
87 esac
88 ])
89 rm conftest.txt
90 ])
91 case "$gl_cv_func_fflush_stdin" in
92 *yes) gl_func_fflush_stdin=1 ;;
93 *no) gl_func_fflush_stdin=0 ;;
94 *) gl_func_fflush_stdin='(-1)' ;;
95 esac
96 AC_DEFINE_UNQUOTED([FUNC_FFLUSH_STDIN], [$gl_func_fflush_stdin],
97 [Define to 1 if fflush is known to work on stdin as per POSIX.1-2008
98 or later, 0 if fflush is known to not work, -1 if unknown.])
99 ])
100
101 # Prerequisites of lib/fflush.c.
102 AC_DEFUN([gl_PREREQ_FFLUSH], [:])