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 <stdio.h>
54 #ifndef __rtems__
55 #include <stdio_ext.h>
56 #endif
57 #include <errno.h>
58 #include "local.h"
59
60 /* Discard I/O from a single file. */
61
62 int
fpurge(register FILE * fp)63 fpurge (
64 register FILE * fp)
65 {
66 int t;
67
68 CHECK_INIT (ptr, fp);
69
70 _newlib_flockfile_start (fp);
71
72 t = fp->_flags;
73 if (!t)
74 {
75 _REENT_ERRNO(ptr) = EBADF;
76 _newlib_flockfile_exit (fp);
77 return EOF;
78 }
79 fp->_p = fp->_bf._base;
80 if ((t & __SWR) == 0)
81 {
82 fp->_r = 0;
83 if (HASUB (fp))
84 FREEUB (ptr, fp);
85 }
86 else
87 fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
88 _newlib_flockfile_end (fp);
89 return 0;
90 }
91