New upstream version 1.9
[debian/gzip] / m4 / fflush.m4
1 # fflush.m4 serial 17
2
3 # Copyright (C) 2007-2018 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        ]], [[FILE *f = fopen ("conftest.txt", "r");
42          char buffer[10];
43          int fd;
44          int c;
45          if (f == NULL)
46            return 1;
47          fd = fileno (f);
48          if (fd < 0 || fread (buffer, 1, 5, f) != 5)
49            { fclose (f); return 2; }
50          /* For deterministic results, ensure f read a bigger buffer.  */
51          if (lseek (fd, 0, SEEK_CUR) == 5)
52            { fclose (f); return 3; }
53          /* POSIX requires fflush-fseek to set file offset of fd.  This fails
54             on BSD systems and on mingw.  */
55          if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
56            { fclose (f); return 4; }
57          if (lseek (fd, 0, SEEK_CUR) != 5)
58            { fclose (f); return 5; }
59          /* Verify behaviour of fflush after ungetc. See
60             <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt>  */
61          /* Verify behaviour of fflush after a backup ungetc.  This fails on
62             mingw.  */
63          c = fgetc (f);
64          ungetc (c, f);
65          fflush (f);
66          if (fgetc (f) != c)
67            { fclose (f); return 6; }
68          /* Verify behaviour of fflush after a non-backup ungetc.  This fails
69             on glibc 2.8 and on BSD systems.  */
70          c = fgetc (f);
71          ungetc ('@', f);
72          fflush (f);
73          if (fgetc (f) != c)
74            { fclose (f); return 7; }
75          fclose (f);
76          return 0;
77        ]])],
78        [gl_cv_func_fflush_stdin=yes],
79        [gl_cv_func_fflush_stdin=no],
80        [case "$host_os" in
81                   # Guess no on native Windows.
82           mingw*) gl_cv_func_fflush_stdin="guessing no" ;;
83           *)      gl_cv_func_fflush_stdin=cross ;;
84         esac
85        ])
86      rm conftest.txt
87     ])
88   case "$gl_cv_func_fflush_stdin" in
89     *yes) gl_func_fflush_stdin=1 ;;
90     *no)  gl_func_fflush_stdin=0 ;;
91     *)    gl_func_fflush_stdin='(-1)' ;;
92   esac
93   AC_DEFINE_UNQUOTED([FUNC_FFLUSH_STDIN], [$gl_func_fflush_stdin],
94     [Define to 1 if fflush is known to work on stdin as per POSIX.1-2008,
95      0 if fflush is known to not work, -1 if unknown.])
96 ])
97
98 # Prerequisites of lib/fflush.c.
99 AC_DEFUN([gl_PREREQ_FFLUSH], [:])