Blame m4/fflush.m4

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