/* Copyright (C) 2009 Eric Blake * Permission to use, copy, modify, and distribute this software * is freely granted, provided that this notice is preserved. */ /* FUNCTION <>---discard pending file I/O INDEX fpurge INDEX _fpurge_r INDEX __fpurge SYNOPSIS #include int fpurge(FILE *<[fp]>); int fpurge( FILE *<[fp]>); #include #include void __fpurge(FILE *<[fp]>); DESCRIPTION Use <> to clear all buffers of the given stream. For output streams, this discards data not yet written to disk. For input streams, this discards any data from <> and any data retrieved from disk but not yet read via <>. This is more severe than <>, and generally is only needed when manually altering the underlying file descriptor of a stream. <<__fpurge>> behaves exactly like <> but does not return a value. The alternate function <<_fpurge_r>> is a reentrant version, where the extra argument <[reent]> is a pointer to a reentrancy structure, and <[fp]> must not be NULL. RETURNS <> returns <<0>> unless <[fp]> is not valid, in which case it returns <> and sets <>. PORTABILITY These functions are not portable to any standard. No supporting OS subroutines are required. */ #define _DEFAULT_SOURCE #include <_ansi.h> #include #ifndef __rtems__ #include #endif #include #include "local.h" /* Discard I/O from a single file. */ int fpurge ( register FILE * fp) { int t; CHECK_INIT (ptr, fp); _newlib_flockfile_start (fp); t = fp->_flags; if (!t) { _REENT_ERRNO(ptr) = EBADF; _newlib_flockfile_exit (fp); return EOF; } fp->_p = fp->_bf._base; if ((t & __SWR) == 0) { fp->_r = 0; if (HASUB (fp)) FREEUB (ptr, fp); } else fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size; _newlib_flockfile_end (fp); return 0; }