NEWS.stable: update
[gnulib.git] / m4 / fflush.m4
1 # fflush.m4 serial 11
2
3 # Copyright (C) 2007-2011 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   gl_FUNC_FFLUSH_STDIN
16   if test $gl_cv_func_fflush_stdin = no; then
17     gl_REPLACE_FFLUSH
18   fi
19 ])
20
21 dnl Determine whether fflush works on input streams.
22 dnl Sets gl_cv_func_fflush_stdin.
23
24 AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
25 [
26   AC_CACHE_CHECK([whether fflush works on input streams],
27     [gl_cv_func_fflush_stdin],
28     [echo hello world > conftest.txt
29      AC_RUN_IFELSE([AC_LANG_PROGRAM(
30        [[
31 #include <stdio.h>
32 #include <unistd.h>
33        ]], [[FILE *f = fopen ("conftest.txt", "r");
34          char buffer[10];
35          int fd;
36          int c;
37          if (f == NULL)
38            return 1;
39          fd = fileno (f);
40          if (fd < 0 || fread (buffer, 1, 5, f) != 5)
41            return 2;
42          /* For deterministic results, ensure f read a bigger buffer.  */
43          if (lseek (fd, 0, SEEK_CUR) == 5)
44            return 3;
45          /* POSIX requires fflush-fseek to set file offset of fd.  This fails
46             on BSD systems and on mingw.  */
47          if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
48            return 4;
49          if (lseek (fd, 0, SEEK_CUR) != 5)
50            return 5;
51          /* Verify behaviour of fflush after ungetc. See
52             <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt>  */
53          /* Verify behaviour of fflush after a backup ungetc.  This fails on
54             mingw.  */
55          c = fgetc (f);
56          ungetc (c, f);
57          fflush (f);
58          if (fgetc (f) != c)
59            return 6;
60          /* Verify behaviour of fflush after a non-backup ungetc.  This fails
61             on glibc 2.8 and on BSD systems.  */
62          c = fgetc (f);
63          ungetc ('@', f);
64          fflush (f);
65          if (fgetc (f) != c)
66            return 7;
67          return 0;
68        ]])], [gl_cv_func_fflush_stdin=yes], [gl_cv_func_fflush_stdin=no],
69      [dnl Pessimistically assume fflush is broken.
70       gl_cv_func_fflush_stdin=no])
71      rm conftest.txt
72     ])
73 ])
74
75 AC_DEFUN([gl_REPLACE_FFLUSH],
76 [
77   AC_LIBOBJ([fflush])
78   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
79   REPLACE_FFLUSH=1
80   gl_PREREQ_FFLUSH
81 ])
82
83 # Prerequisites of lib/fflush.c.
84 AC_DEFUN([gl_PREREQ_FFLUSH],
85 [
86   AC_REQUIRE([AC_C_INLINE])
87   :
88 ])