The Pedigree Project  0.1
glue-fpurge.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21 FUNCTION
22 <<fpurge>>---discard pending file I/O
23 
24 INDEX
25  fpurge
26 INDEX
27  _fpurge_r
28 
29 ANSI_SYNOPSIS
30  #include <stdio.h>
31  int fpurge(FILE *<[fp]>);
32 
33  int _fpurge_r(struct _reent *<[reent]>, FILE *<[fp]>);
34 
35 DESCRIPTION
36 Use <<fpurge>> to clear all buffers of the given stream. For output
37 streams, this discards data not yet written to disk. For input streams,
38 this discards any data from <<ungetc>> and any data retrieved from disk
39 but not yet read via <<getc>>. This is more severe than <<fflush>>,
40 and generally is only needed when manually altering the underlying file
41 descriptor of a stream.
42 
43 The alternate function <<_fpurge_r>> is a reentrant version, where the
44 extra argument <[reent]> is a pointer to a reentrancy structure, and
45 <[fp]> must not be NULL.
46 
47 RETURNS
48 <<fpurge>> returns <<0>> unless <[fp]> is not valid, in which case it
49 returns <<EOF>> and sets <<errno>>.
50 
51 PORTABILITY
52 These functions are not portable to any standard.
53 
54 No supporting OS subroutines are required.
55 */
56 
57 #define _COMPILING_NEWLIB
58 
59 #include <newlib.h>
60 
61 #include "local.h"
62 #include <_ansi.h>
63 #include <errno.h>
64 #include <stdio.h>
65 
66 /* Discard I/O from a single file. */
67 
68 int _fpurge_r(struct _reent *ptr, register FILE *fp)
69 {
70  int t;
71 
72  CHECK_INIT(ptr, fp);
73 
74  _flockfile(fp);
75 
76  t = fp->_flags;
77  if (!t)
78  {
79  ptr->_errno = EBADF;
80  _funlockfile(fp);
81  return EOF;
82  }
83  fp->_p = fp->_bf._base;
84  if ((t & __SWR) == 0)
85  {
86  fp->_r = 0;
87  if (HASUB(fp))
88  FREEUB(ptr, fp);
89  }
90  else
91  fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
92  _funlockfile(fp);
93  return 0;
94 }
95 
96 int fpurge(register FILE *fp)
97 {
98  return _fpurge_r(_REENT, fp);
99 }