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