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