* lib/freading.h: Improve comments.
[gnulib.git] / lib / fflush.c
1 /* fflush.c -- allow flushing input streams
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Eric Blake. */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <stdio.h>
24
25 #include <errno.h>
26 #include <unistd.h>
27
28 #include "freading.h"
29 #include "fpurge.h"
30
31 #undef fflush
32
33 /* Flush all pending data on STREAM according to POSIX rules.  Both
34    output and seekable input streams are supported.  */
35 int
36 rpl_fflush (FILE *stream)
37 {
38   int result;
39   off_t pos;
40
41   /* When stream is NULL, POSIX and C99 only require flushing of "output
42      streams and update streams in which the most recent operation was not
43      input", and all implementations do this.
44
45      When stream is "an output stream or an update stream in which the most
46      recent operation was not input", POSIX and C99 requires that fflush
47      writes out any buffered data, and all implementations do this.
48
49      When stream is, however, an input stream or an update stream in
50      which the most recent operation was input, C99 specifies nothing,
51      and POSIX only specifies behavior if the stream is seekable.
52      mingw, in particular, drops the input buffer, leaving the file
53      descriptor positioned at the end of the input buffer. I.e. ftell
54      (stream) is lost.  We don't want to call the implementation's
55      fflush in this case.
56
57      We test ! freading (stream) here, rather than fwriting (stream), because
58      what we need to know is whether the stream holds a "read buffer", and on
59      mingw this is indicated by _IOREAD, regardless of _IOWRT.  */
60   if (stream == NULL || ! freading (stream))
61     return fflush (stream);
62
63   /* POSIX does not specify fflush behavior for non-seekable input
64      streams.  Some implementations purge unread data, some return
65      EBADF, some do nothing.  */
66   pos = ftello (stream);
67   if (pos == -1)
68     {
69       errno = EBADF;
70       return EOF;
71     }
72
73   /* To get here, we must be flushing a seekable input stream, so the
74      semantics of fpurge are now appropriate to clear the buffer.  To
75      avoid losing data, the lseek is also necessary.  */
76   result = fpurge (stream);
77   if (result != 0)
78     return result;
79
80   pos = lseek (fileno (stream), pos, SEEK_SET);
81   if (pos == -1)
82     return EOF;
83   /* After a successful lseek, update the file descriptor's position cache
84      in the stream.  */
85 #if defined __sferror           /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
86   stream->_offset = pos;
87   stream->_flags |= __SOFF;
88 #endif
89
90   return 0;
91 }