Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / fpurge.c
blob93ac0e2a9ce76490fc559765a89b4148a1b5ad57
1 /* Copyright (C) 2009 Eric Blake
2 * Permission to use, copy, modify, and distribute this software
3 * is freely granted, provided that this notice is preserved.
4 */
6 /*
7 FUNCTION
8 <<fpurge>>---discard pending file I/O
10 INDEX
11 fpurge
12 INDEX
13 _fpurge_r
14 INDEX
15 __fpurge
17 SYNOPSIS
18 #include <stdio.h>
19 int fpurge(FILE *<[fp]>);
21 int _fpurge_r(struct _reent *<[reent]>, FILE *<[fp]>);
23 #include <stdio.h>
24 #include <stdio_ext.h>
25 void __fpurge(FILE *<[fp]>);
28 DESCRIPTION
29 Use <<fpurge>> to clear all buffers of the given stream. For output
30 streams, this discards data not yet written to disk. For input streams,
31 this discards any data from <<ungetc>> and any data retrieved from disk
32 but not yet read via <<getc>>. This is more severe than <<fflush>>,
33 and generally is only needed when manually altering the underlying file
34 descriptor of a stream.
36 <<__fpurge>> behaves exactly like <<fpurge>> but does not return a value.
38 The alternate function <<_fpurge_r>> is a reentrant version, where the
39 extra argument <[reent]> is a pointer to a reentrancy structure, and
40 <[fp]> must not be NULL.
42 RETURNS
43 <<fpurge>> returns <<0>> unless <[fp]> is not valid, in which case it
44 returns <<EOF>> and sets <<errno>>.
46 PORTABILITY
47 These functions are not portable to any standard.
49 No supporting OS subroutines are required.
52 #include <_ansi.h>
53 #include <stdio.h>
54 #ifndef __rtems__
55 #include <stdio_ext.h>
56 #endif
57 #include <errno.h>
58 #include "local.h"
60 /* Discard I/O from a single file. */
62 int
63 _fpurge_r (struct _reent *ptr,
64 register FILE * fp)
66 int t;
68 CHECK_INIT (ptr, fp);
70 _newlib_flockfile_start (fp);
72 t = fp->_flags;
73 if (!t)
75 _REENT_ERRNO(ptr) = EBADF;
76 _newlib_flockfile_exit (fp);
77 return EOF;
79 fp->_p = fp->_bf._base;
80 if ((t & __SWR) == 0)
82 fp->_r = 0;
83 if (HASUB (fp))
84 FREEUB (ptr, fp);
86 else
87 fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
88 _newlib_flockfile_end (fp);
89 return 0;
92 #ifndef _REENT_ONLY
94 int
95 fpurge (register FILE * fp)
97 return _fpurge_r (_REENT, fp);
100 #ifndef __rtems__
102 void
103 __fpurge (register FILE * fp)
105 _fpurge_r (_REENT, fp);
108 #endif
110 #endif /* _REENT_ONLY */