|
Packit |
8f70b4 |
# fflush.m4 serial 17
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
# Copyright (C) 2007-2018 Free Software Foundation, Inc.
|
|
Packit |
8f70b4 |
# This file is free software; the Free Software Foundation
|
|
Packit |
8f70b4 |
# gives unlimited permission to copy and/or distribute it,
|
|
Packit |
8f70b4 |
# with or without modifications, as long as this notice is preserved.
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
dnl From Eric Blake
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
dnl Find out how to obey POSIX semantics of fflush(stdin) discarding
|
|
Packit |
8f70b4 |
dnl unread input on seekable streams, rather than C99 undefined semantics.
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
AC_DEFUN([gl_FUNC_FFLUSH],
|
|
Packit |
8f70b4 |
[
|
|
Packit |
8f70b4 |
AC_REQUIRE([gl_STDIO_H_DEFAULTS])
|
|
Packit |
8f70b4 |
gl_FUNC_FFLUSH_STDIN
|
|
Packit |
8f70b4 |
case "$gl_cv_func_fflush_stdin" in
|
|
Packit |
8f70b4 |
*yes) ;;
|
|
Packit |
8f70b4 |
*) REPLACE_FFLUSH=1 ;;
|
|
Packit |
8f70b4 |
esac
|
|
Packit |
8f70b4 |
])
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
dnl Determine whether fflush works on input streams.
|
|
Packit |
8f70b4 |
dnl Sets gl_cv_func_fflush_stdin.
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
|
|
Packit |
8f70b4 |
[
|
|
Packit |
8f70b4 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
|
|
Packit |
8f70b4 |
AC_CHECK_HEADERS_ONCE([unistd.h])
|
|
Packit |
8f70b4 |
AC_CACHE_CHECK([whether fflush works on input streams],
|
|
Packit |
8f70b4 |
[gl_cv_func_fflush_stdin],
|
|
Packit |
8f70b4 |
[echo hello world > conftest.txt
|
|
Packit |
8f70b4 |
AC_RUN_IFELSE([AC_LANG_PROGRAM(
|
|
Packit |
8f70b4 |
[[
|
|
Packit |
8f70b4 |
#include <stdio.h>
|
|
Packit |
8f70b4 |
#if HAVE_UNISTD_H
|
|
Packit |
8f70b4 |
# include <unistd.h>
|
|
Packit |
8f70b4 |
#else /* on Windows with MSVC */
|
|
Packit |
8f70b4 |
# include <io.h>
|
|
Packit |
8f70b4 |
#endif
|
|
Packit |
8f70b4 |
]], [[FILE *f = fopen ("conftest.txt", "r");
|
|
Packit |
8f70b4 |
char buffer[10];
|
|
Packit |
8f70b4 |
int fd;
|
|
Packit |
8f70b4 |
int c;
|
|
Packit |
8f70b4 |
if (f == NULL)
|
|
Packit |
8f70b4 |
return 1;
|
|
Packit |
8f70b4 |
fd = fileno (f);
|
|
Packit |
8f70b4 |
if (fd < 0 || fread (buffer, 1, 5, f) != 5)
|
|
Packit |
8f70b4 |
{ fclose (f); return 2; }
|
|
Packit |
8f70b4 |
/* For deterministic results, ensure f read a bigger buffer. */
|
|
Packit |
8f70b4 |
if (lseek (fd, 0, SEEK_CUR) == 5)
|
|
Packit |
8f70b4 |
{ fclose (f); return 3; }
|
|
Packit |
8f70b4 |
/* POSIX requires fflush-fseek to set file offset of fd. This fails
|
|
Packit |
8f70b4 |
on BSD systems and on mingw. */
|
|
Packit |
8f70b4 |
if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
|
|
Packit |
8f70b4 |
{ fclose (f); return 4; }
|
|
Packit |
8f70b4 |
if (lseek (fd, 0, SEEK_CUR) != 5)
|
|
Packit |
8f70b4 |
{ fclose (f); return 5; }
|
|
Packit |
8f70b4 |
/* Verify behaviour of fflush after ungetc. See
|
|
Packit |
8f70b4 |
<http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> */
|
|
Packit |
8f70b4 |
/* Verify behaviour of fflush after a backup ungetc. This fails on
|
|
Packit |
8f70b4 |
mingw. */
|
|
Packit |
8f70b4 |
c = fgetc (f);
|
|
Packit |
8f70b4 |
ungetc (c, f);
|
|
Packit |
8f70b4 |
fflush (f);
|
|
Packit |
8f70b4 |
if (fgetc (f) != c)
|
|
Packit |
8f70b4 |
{ fclose (f); return 6; }
|
|
Packit |
8f70b4 |
/* Verify behaviour of fflush after a non-backup ungetc. This fails
|
|
Packit |
8f70b4 |
on glibc 2.8 and on BSD systems. */
|
|
Packit |
8f70b4 |
c = fgetc (f);
|
|
Packit |
8f70b4 |
ungetc ('@', f);
|
|
Packit |
8f70b4 |
fflush (f);
|
|
Packit |
8f70b4 |
if (fgetc (f) != c)
|
|
Packit |
8f70b4 |
{ fclose (f); return 7; }
|
|
Packit |
8f70b4 |
fclose (f);
|
|
Packit |
8f70b4 |
return 0;
|
|
Packit |
8f70b4 |
]])],
|
|
Packit |
8f70b4 |
[gl_cv_func_fflush_stdin=yes],
|
|
Packit |
8f70b4 |
[gl_cv_func_fflush_stdin=no],
|
|
Packit |
8f70b4 |
[case "$host_os" in
|
|
Packit |
8f70b4 |
# Guess no on native Windows.
|
|
Packit |
8f70b4 |
mingw*) gl_cv_func_fflush_stdin="guessing no" ;;
|
|
Packit |
8f70b4 |
*) gl_cv_func_fflush_stdin=cross ;;
|
|
Packit |
8f70b4 |
esac
|
|
Packit |
8f70b4 |
])
|
|
Packit |
8f70b4 |
rm conftest.txt
|
|
Packit |
8f70b4 |
])
|
|
Packit |
8f70b4 |
case "$gl_cv_func_fflush_stdin" in
|
|
Packit |
8f70b4 |
*yes) gl_func_fflush_stdin=1 ;;
|
|
Packit |
8f70b4 |
*no) gl_func_fflush_stdin=0 ;;
|
|
Packit |
8f70b4 |
*) gl_func_fflush_stdin='(-1)' ;;
|
|
Packit |
8f70b4 |
esac
|
|
Packit |
8f70b4 |
AC_DEFINE_UNQUOTED([FUNC_FFLUSH_STDIN], [$gl_func_fflush_stdin],
|
|
Packit |
8f70b4 |
[Define to 1 if fflush is known to work on stdin as per POSIX.1-2008,
|
|
Packit |
8f70b4 |
0 if fflush is known to not work, -1 if unknown.])
|
|
Packit |
8f70b4 |
])
|
|
Packit |
8f70b4 |
|
|
Packit |
8f70b4 |
# Prerequisites of lib/fflush.c.
|
|
Packit |
8f70b4 |
AC_DEFUN([gl_PREREQ_FFLUSH], [:])
|