New upstream version 1.8
[debian/gzip] / m4 / fflush.m4
1 # fflush.m4 serial 15
2
3 # Copyright (C) 2007-2016 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   if test $gl_cv_func_fflush_stdin != yes; then
18     REPLACE_FFLUSH=1
19   fi
20 ])
21
22 dnl Determine whether fflush works on input streams.
23 dnl Sets gl_cv_func_fflush_stdin.
24
25 AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
26 [
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(
32        [[
33 #include <stdio.h>
34 #if HAVE_UNISTD_H
35 # include <unistd.h>
36 #else /* on Windows with MSVC */
37 # include <io.h>
38 #endif
39        ]], [[FILE *f = fopen ("conftest.txt", "r");
40          char buffer[10];
41          int fd;
42          int c;
43          if (f == NULL)
44            return 1;
45          fd = fileno (f);
46          if (fd < 0 || fread (buffer, 1, 5, f) != 5)
47            return 2;
48          /* For deterministic results, ensure f read a bigger buffer.  */
49          if (lseek (fd, 0, SEEK_CUR) == 5)
50            return 3;
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)
54            return 4;
55          if (lseek (fd, 0, SEEK_CUR) != 5)
56            return 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
60             mingw.  */
61          c = fgetc (f);
62          ungetc (c, f);
63          fflush (f);
64          if (fgetc (f) != c)
65            return 6;
66          /* Verify behaviour of fflush after a non-backup ungetc.  This fails
67             on glibc 2.8 and on BSD systems.  */
68          c = fgetc (f);
69          ungetc ('@', f);
70          fflush (f);
71          if (fgetc (f) != c)
72            return 7;
73          return 0;
74        ]])], [gl_cv_func_fflush_stdin=yes], [gl_cv_func_fflush_stdin=no],
75      [gl_cv_func_fflush_stdin=cross])
76      rm conftest.txt
77     ])
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)' ;;
82   esac
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.])
86 ])
87
88 # Prerequisites of lib/fflush.c.
89 AC_DEFUN([gl_PREREQ_FFLUSH], [:])