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