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 */
5
6 /*
7 FUNCTION
8 <<fpurge>>---discard pending file I/O
9
10 INDEX
11 fpurge
12 INDEX
13 _fpurge_r
14 INDEX
15 __fpurge
16
17 SYNOPSIS
18 #include <stdio.h>
19 int fpurge(FILE *<[fp]>);
20
21 int fpurge( FILE *<[fp]>);
22
23 #include <stdio.h>
24 #include <stdio_ext.h>
25 void __fpurge(FILE *<[fp]>);
26
27
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.
35
36 <<__fpurge>> behaves exactly like <<fpurge>> but does not return a value.
37
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.
41
42 RETURNS
43 <<fpurge>> returns <<0>> unless <[fp]> is not valid, in which case it
44 returns <<EOF>> and sets <<errno>>.
45
46 PORTABILITY
47 These functions are not portable to any standard.
48
49 No supporting OS subroutines are required.
50 */
51
52 #define _DEFAULT_SOURCE
53 #include <_ansi.h>
54 #include <stdio.h>
55 #ifndef __rtems__
56 #include <stdio_ext.h>
57 #endif
58 #include <errno.h>
59 #include "local.h"
60
61 /* Discard I/O from a single file. */
62
63 int
fpurge(register FILE * fp)64 fpurge (
65 register FILE * fp)
66 {
67 int t;
68
69 CHECK_INIT (ptr, fp);
70
71 _newlib_flockfile_start (fp);
72
73 t = fp->_flags;
74 if (!t)
75 {
76 _REENT_ERRNO(ptr) = EBADF;
77 _newlib_flockfile_exit (fp);
78 return EOF;
79 }
80 fp->_p = fp->_bf._base;
81 if ((t & __SWR) == 0)
82 {
83 fp->_r = 0;
84 if (HASUB (fp))
85 FREEUB (ptr, fp);
86 }
87 else
88 fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
89 _newlib_flockfile_end (fp);
90 return 0;
91 }
92