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