annotate m4/fflush.m4 @ 8657:05f21166eb81

* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush. * modules/stdio (Makefile.am): Support fflush. * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Likewise. * modules/fflush: New file. * lib/fflush.c: Likewise. * m4/fflush.m4: Likewise. * modules/fflush-tests: New test. * tests/test-fflush.c: Likewise. * MODULES.html.sh (Input/output <stdio.h>): Document new module.
author Eric Blake <ebb9@byu.net>
date Tue, 10 Apr 2007 03:09:07 +0000
parents
children 996bed2dc377
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 #serial 1
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
2
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3 # Copyright (C) 2007 Free Software Foundation, Inc.
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 # This file is free software; the Free Software Foundation
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 # gives unlimited permission to copy and/or distribute it,
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 # with or without modifications, as long as this notice is preserved.
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8 dnl From Eric Blake
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 dnl Find out how to obey POSIX semantics of fflush(stdin) discarding
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 dnl unread input, rather than C99 undefined semantics. fpurge is not
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 dnl standardized, but has the desired properties.
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 AC_DEFUN([gl_FUNC_FFLUSH],
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
15 [
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16 AC_CACHE_CHECK([whether fflush works on input streams],
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 [gl_cv_func_fflush_stdin],
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18 [echo hello world > conftest.txt
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 AC_RUN_IFELSE([AC_LANG_PROGRAM(
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20 [[
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include <stdio.h>
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22 #include <unistd.h>
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23 ]], [[FILE *f = fopen ("conftest.txt", "r");
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24 char buffer[10];
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25 int fd = fileno (f);
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26 if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27 return 2;
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28 /* For deterministic results, ensure f read a bigger buffer. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29 if (lseek (fd, 0, SEEK_CUR) == 5)
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30 return 3;
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31 /* POSIX requires fflush-fseek to set file offset of fd. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33 return 4;
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 return !(lseek (fd, 0, SEEK_CUR) == 5);
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35 ]])], [gl_cv_func_fflush_stdin=yes], [gl_cv_func_fflush_stdin=no],
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
36 [dnl Pessimistically assume fflush is broken. This is wrong for
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37 dnl at least glibc and cygwin; but lib/fflush.c takes this into account.
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
38 gl_cv_func_fflush_stdin=no])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39 rm conftest.txt
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 ])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
41 if test $gl_cv_func_fflush_stdin = no; then
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 gl_REPLACE_FFLUSH
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 fi
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 ])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46 AC_DEFUN([gl_REPLACE_FFLUSH],
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
47 [
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
48 AC_CHECK_HEADERS_ONCE([stdio_ext.h])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
49 AC_CHECK_FUNCS_ONCE([fpurge __fpurge])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
50 dnl Linux documents int fpurge(), but only declares void __fpurge().
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
51 AC_CHECK_DECLS([fpurge], [], [], [[
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52 #include <stdio.h>
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
53 #if HAVE_STDIO_EXT_H
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54 # include <stdio_ext.h>
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 #endif
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56 ]])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
57 AC_LIBOBJ([fflush])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58 AC_REQUIRE([gl_STDIO_H_DEFAULTS])
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 REPLACE_FFLUSH=1
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 ])