New upstream version 1.9
[debian/gzip] / lib / fflush.c
1 /* fflush.c -- allow flushing input streams
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 /* Written by Eric Blake. */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <stdio.h>
23
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include "freading.h"
28
29 #include "stdio-impl.h"
30
31 #include "unused-parameter.h"
32
33 #undef fflush
34
35
36 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
37
38 /* Clear the stream's ungetc buffer, preserving the value of ftello (fp).  */
39 static void
40 clear_ungetc_buffer_preserving_position (FILE *fp)
41 {
42   if (fp->_flags & _IO_IN_BACKUP)
43     /* _IO_free_backup_area is a bit complicated.  Simply call fseek.  */
44     fseeko (fp, 0, SEEK_CUR);
45 }
46
47 #else
48
49 /* Clear the stream's ungetc buffer.  May modify the value of ftello (fp).  */
50 static void
51 clear_ungetc_buffer (FILE *fp)
52 {
53 # if defined __sferror || defined __DragonFly__ || defined __ANDROID__
54   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
55   if (HASUB (fp))
56     {
57       fp_->_p += fp_->_r;
58       fp_->_r = 0;
59     }
60 # elif defined __EMX__              /* emx+gcc */
61   if (fp->_ungetc_count > 0)
62     {
63       fp->_ungetc_count = 0;
64       fp->_rcount = - fp->_rcount;
65     }
66 # elif defined _IOERR               /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */
67   /* Nothing to do.  */
68 # else                              /* other implementations */
69   fseeko (fp, 0, SEEK_CUR);
70 # endif
71 }
72
73 #endif
74
75 #if ! (defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */)
76
77 # if (defined __sferror || defined __DragonFly__ || defined __ANDROID__) && defined __SNPT
78 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
79
80 static int
81 disable_seek_optimization (FILE *fp)
82 {
83   int saved_flags = fp_->_flags & (__SOPT | __SNPT);
84   fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
85   return saved_flags;
86 }
87
88 static void
89 restore_seek_optimization (FILE *fp, int saved_flags)
90 {
91   fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
92 }
93
94 # else
95
96 static void
97 update_fpos_cache (FILE *fp _GL_UNUSED_PARAMETER,
98                    off_t pos _GL_UNUSED_PARAMETER)
99 {
100 #  if defined __sferror || defined __DragonFly__ || defined __ANDROID__
101   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
102 #   if defined __CYGWIN__
103   /* fp_->_offset is typed as an integer.  */
104   fp_->_offset = pos;
105 #   else
106   /* fp_->_offset is an fpos_t.  */
107   /* Use a union, since on NetBSD, the compilation flags determine
108      whether fpos_t is typedef'd to off_t or a struct containing a
109      single off_t member.  */
110   union
111     {
112       fpos_t f;
113       off_t o;
114     } u;
115   u.o = pos;
116   fp_->_offset = u.f;
117 #   endif
118   fp_->_flags |= __SOFF;
119 #  endif
120 }
121 # endif
122 #endif
123
124 /* Flush all pending data on STREAM according to POSIX rules.  Both
125    output and seekable input streams are supported.  */
126 int
127 rpl_fflush (FILE *stream)
128 {
129   /* When stream is NULL, POSIX and C99 only require flushing of "output
130      streams and update streams in which the most recent operation was not
131      input", and all implementations do this.
132
133      When stream is "an output stream or an update stream in which the most
134      recent operation was not input", POSIX and C99 requires that fflush
135      writes out any buffered data, and all implementations do this.
136
137      When stream is, however, an input stream or an update stream in
138      which the most recent operation was input, C99 specifies nothing,
139      and POSIX only specifies behavior if the stream is seekable.
140      mingw, in particular, drops the input buffer, leaving the file
141      descriptor positioned at the end of the input buffer. I.e. ftell
142      (stream) is lost.  We don't want to call the implementation's
143      fflush in this case.
144
145      We test ! freading (stream) here, rather than fwriting (stream), because
146      what we need to know is whether the stream holds a "read buffer", and on
147      mingw this is indicated by _IOREAD, regardless of _IOWRT.  */
148   if (stream == NULL || ! freading (stream))
149     return fflush (stream);
150
151 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
152
153   clear_ungetc_buffer_preserving_position (stream);
154
155   return fflush (stream);
156
157 #else
158   {
159     /* Notes about the file-position indicator:
160        1) The file position indicator is incremented by fgetc() and decremented
161           by ungetc():
162           <http://www.opengroup.org/susv3/functions/fgetc.html>
163             "... the fgetc() function shall ... advance the associated file
164              position indicator for the stream ..."
165           <http://www.opengroup.org/susv3/functions/ungetc.html>
166             "The file-position indicator is decremented by each successful
167              call to ungetc()..."
168        2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
169             "The value of the file-position indicator for the stream after
170              reading or discarding all pushed-back bytes shall be the same
171              as it was before the bytes were pushed back."
172           Here we are discarding all pushed-back bytes.  But more specifically,
173        3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
174             "[After fflush(),] the file offset of the underlying open file
175              description shall be set to the file position of the stream, and
176              any characters pushed back onto the stream by ungetc() ... shall
177              be discarded."  */
178
179     /* POSIX does not specify fflush behavior for non-seekable input
180        streams.  Some implementations purge unread data, some return
181        EBADF, some do nothing.  */
182     off_t pos = ftello (stream);
183     if (pos == -1)
184       {
185         errno = EBADF;
186         return EOF;
187       }
188
189     /* Clear the ungetc buffer.  */
190     clear_ungetc_buffer (stream);
191
192     /* To get here, we must be flushing a seekable input stream, so the
193        semantics of fpurge are now appropriate to clear the buffer.  To
194        avoid losing data, the lseek is also necessary.  */
195     {
196       int result = fpurge (stream);
197       if (result != 0)
198         return result;
199     }
200
201 # if (defined __sferror || defined __DragonFly__ || defined __ANDROID__) && defined __SNPT
202     /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
203
204     {
205       /* Disable seek optimization for the next fseeko call.  This tells the
206          following fseeko call to seek to the desired position directly, rather
207          than to seek to a block-aligned boundary.  */
208       int saved_flags = disable_seek_optimization (stream);
209       int result = fseeko (stream, pos, SEEK_SET);
210
211       restore_seek_optimization (stream, saved_flags);
212       return result;
213     }
214
215 # else
216
217     pos = lseek (fileno (stream), pos, SEEK_SET);
218     if (pos == -1)
219       return EOF;
220     /* After a successful lseek, update the file descriptor's position cache
221        in the stream.  */
222     update_fpos_cache (stream, pos);
223
224     return 0;
225
226 # endif
227   }
228 #endif
229 }