This generalizes the fpurge() function from the BSDs (FreeBSD, MacOS X, OpenBSD, NetBSD).
2007-04-13 Bruno Haible <[EMAIL PROTECTED]> * modules/fpurge: New file. * lib/fpurge.h: New file. * lib/fpurge.c: New file. * m4/fpurge.m4: New file. ============================ modules/fpurge ============================== Description: fpurge() function: Flush buffers. Files: lib/fpurge.h lib/fpurge.c m4/fpurge.m4 Depends-on: configure.ac: gl_FUNC_FPURGE Makefile.am: Include: "fpurge.h" License: LGPL Maintainer: Bruno Haible, Eric Blake ============================= lib/fpurge.h =============================== /* Flushing buffers of a FILE stream. Copyright (C) 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include <stdio.h> /* Discard all pending buffered I/O on the stream STREAM. STREAM must not be wide-character oriented. Return 0 if successful. Upon error, return -1 and set errno. */ #if HAVE___FPURGE /* glibc >= 2.2, Solaris >= 7 */ # include <stdio_ext.h> # define fpurge(stream) (__fpurge (stream), 0) #elif ! HAVE_DECL_FPURGE # ifdef __cplusplus extern "C" { # endif extern int fpurge (FILE *stream); # ifdef __cplusplus } # endif #endif ============================= lib/fpurge.c =============================== /* Flushing buffers of a FILE stream. Copyright (C) 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include <config.h> /* Specification. */ #include "fpurge.h" int fpurge (FILE *fp) { /* Most systems provide FILE as a struct and the necessary bitmask in <stdio.h>, because they need it for implementing getc() and putc() as fast macros. */ #if defined _IO_ferror_unlocked /* GNU libc, BeOS */ fp->_IO_read_end = fp->_IO_read_ptr; fp->_IO_write_ptr = fp->_IO_write_base; return 0; #elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */ fp->_p = fp->_bf._base; fp->_r = 0; fp->_w = ((fp->_flags & (__SLBF | __SNBF) == 0) /* fully buffered? */ ? fp->_bf._size : 0); #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */ fp->_ptr = fp->_base; if (fp->_ptr != NULL) fp->_cnt = 0; return 0; #else #error "Please port gnulib fpurge.c to your platform!" #endif } ============================= m4/fpurge.m4 =============================== # fpurge.m4 serial 1 dnl Copyright (C) 2007 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_FPURGE], [ AC_CHECK_FUNCS_ONCE([fpurge]) AC_CHECK_FUNCS_ONCE([__fpurge]) AC_CHECK_DECLS([fpurge], , , [#include <stdio.h>]) if test $ac_cv_func_fpurge = no && test $ac_cv_func___fpurge = no; then AC_LIBOBJ([fpurge]) AC_CHECK_FUNCS([fpurge]) fi ])