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