New upstream version 1.10
[debian/gzip] / lib / fpurge.c
1 /* Flushing buffers of a FILE stream.
2    Copyright (C) 2007-2018 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 /* Specification.  */
20 #include <stdio.h>
21
22 #if HAVE___FPURGE                   /* glibc >= 2.2, Haiku, Solaris >= 7, Android API >= 23 */
23 # include <stdio_ext.h>
24 #endif
25 #include <stdlib.h>
26
27 #include "stdio-impl.h"
28
29 int
30 fpurge (FILE *fp)
31 {
32 #if HAVE___FPURGE                   /* glibc >= 2.2, Haiku, Solaris >= 7, Android API >= 23, musl libc */
33
34   __fpurge (fp);
35   /* The __fpurge function does not have a return value.  */
36   return 0;
37
38 #elif HAVE_FPURGE                   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin 1.7 */
39
40   /* Call the system's fpurge function.  */
41 # undef fpurge
42 # if !HAVE_DECL_FPURGE
43   extern int fpurge (FILE *);
44 # endif
45   int result = fpurge (fp);
46 # if defined __sferror || defined __DragonFly__ || defined __ANDROID__
47   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
48   if (result == 0)
49     /* Correct the invariants that fpurge broke.
50        <stdio.h> on BSD systems says:
51          "The following always hold: if _flags & __SRD, _w is 0."
52        If this invariant is not fulfilled and the stream is read-write but
53        currently reading, subsequent putc or fputc calls will write directly
54        into the buffer, although they shouldn't be allowed to.  */
55     if ((fp_->_flags & __SRD) != 0)
56       fp_->_w = 0;
57 # endif
58   return result;
59
60 #else
61
62   /* Most systems provide FILE as a struct and the necessary bitmask in
63      <stdio.h>, because they need it for implementing getc() and putc() as
64      fast macros.  */
65 # if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
66   /* GNU libc, BeOS, Haiku, Linux libc5 */
67   fp->_IO_read_end = fp->_IO_read_ptr;
68   fp->_IO_write_ptr = fp->_IO_write_base;
69   /* Avoid memory leak when there is an active ungetc buffer.  */
70   if (fp->_IO_save_base != NULL)
71     {
72       free (fp->_IO_save_base);
73       fp->_IO_save_base = NULL;
74     }
75   return 0;
76 # elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
77   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
78   fp_->_p = fp_->_bf._base;
79   fp_->_r = 0;
80   fp_->_w = ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
81              ? fp_->_bf._size
82              : 0);
83   /* Avoid memory leak when there is an active ungetc buffer.  */
84   if (fp_ub._base != NULL)
85     {
86       if (fp_ub._base != fp_->_ubuf)
87         free (fp_ub._base);
88       fp_ub._base = NULL;
89     }
90   return 0;
91 # elif defined __EMX__              /* emx+gcc */
92   fp->_ptr = fp->_buffer;
93   fp->_rcount = 0;
94   fp->_wcount = 0;
95   fp->_ungetc_count = 0;
96   return 0;
97 # elif defined __minix              /* Minix */
98   fp->_ptr = fp->_buf;
99   if (fp->_ptr != NULL)
100     fp->_count = 0;
101   return 0;
102 # elif defined _IOERR               /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */
103   fp_->_ptr = fp_->_base;
104   if (fp_->_ptr != NULL)
105     fp_->_cnt = 0;
106   return 0;
107 # elif defined __UCLIBC__           /* uClibc */
108 #  ifdef __STDIO_BUFFERS
109   if (fp->__modeflags & __FLAG_WRITING)
110     fp->__bufpos = fp->__bufstart;
111   else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
112     fp->__bufpos = fp->__bufread;
113 #  endif
114   return 0;
115 # elif defined __QNX__              /* QNX */
116   fp->_Rback = fp->_Back + sizeof (fp->_Back);
117   fp->_Rsave = NULL;
118   if (fp->_Mode & 0x2000 /* _MWRITE */)
119     /* fp->_Buf <= fp->_Next <= fp->_Wend */
120     fp->_Next = fp->_Buf;
121   else
122     /* fp->_Buf <= fp->_Next <= fp->_Rend */
123     fp->_Rend = fp->_Next;
124   return 0;
125 # elif defined __MINT__             /* Atari FreeMiNT */
126   if (fp->__pushed_back)
127     {
128       fp->__bufp = fp->__pushback_bufp;
129       fp->__pushed_back = 0;
130     }
131   /* Preserve the current file position.  */
132   if (fp->__target != -1)
133     fp->__target += fp->__bufp - fp->__buffer;
134   fp->__bufp = fp->__buffer;
135   /* Nothing in the buffer, next getc is nontrivial.  */
136   fp->__get_limit = fp->__bufp;
137   /* Nothing in the buffer, next putc is nontrivial.  */
138   fp->__put_limit = fp->__buffer;
139   return 0;
140 # elif defined EPLAN9               /* Plan9 */
141   fp->rp = fp->wp = fp->lp = fp->buf;
142   return 0;
143 # else
144 #  error "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."
145 # endif
146
147 #endif
148 }