annotate lib/fflush.c @ 16201:8250f2777afc

maint: update all copyright year number ranges Run "make update-copyright".
author Jim Meyering <meyering@redhat.com>
date Sun, 01 Jan 2012 10:04:58 +0100
parents 20a2699b8a90
children 1d4824d7f352
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* fflush.c -- allow flushing input streams
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 15485
diff changeset
2 Copyright (C) 2007-2012 Free Software Foundation, Inc.
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9134
diff changeset
4 This program is free software: you can redistribute it and/or modify
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9134
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9134
diff changeset
7 (at your option) any later version.
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9134
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
8705
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
20
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
21 /* Specification. */
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
22 #include <stdio.h>
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
23
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24 #include <errno.h>
8705
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
25 #include <unistd.h>
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26
8721
5122cdde0f7b Fix fflush on mingw.
Eric Blake <ebb9@byu.net>
parents: 8717
diff changeset
27 #include "freading.h"
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28
9980
2c1ba629f5d5 New private include file lib/stdio-impl.h.
Bruno Haible <bruno@clisp.org>
parents: 9976
diff changeset
29 #include "stdio-impl.h"
2c1ba629f5d5 New private include file lib/stdio-impl.h.
Bruno Haible <bruno@clisp.org>
parents: 9976
diff changeset
30
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31 #undef fflush
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
33
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
34 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
35
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
36 /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
37 static inline void
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
38 clear_ungetc_buffer_preserving_position (FILE *fp)
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
39 {
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
40 if (fp->_flags & _IO_IN_BACKUP)
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
41 /* _IO_free_backup_area is a bit complicated. Simply call fseek. */
12255
83ddd76ac091 fflush, freadseek: use fseeko, not fseek
Eric Blake <ebb9@byu.net>
parents: 11653
diff changeset
42 fseeko (fp, 0, SEEK_CUR);
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
43 }
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
44
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
45 #else
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
46
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
47 /* Clear the stream's ungetc buffer. May modify the value of ftello (fp). */
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
48 static inline void
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
49 clear_ungetc_buffer (FILE *fp)
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
50 {
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
51 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
52 if (HASUB (fp))
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
53 {
9981
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
54 fp_->_p += fp_->_r;
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
55 fp_->_r = 0;
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
56 }
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
57 # elif defined __EMX__ /* emx+gcc */
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
58 if (fp->_ungetc_count > 0)
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
59 {
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
60 fp->_ungetc_count = 0;
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
61 fp->_rcount = - fp->_rcount;
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
62 }
15485
20a2699b8a90 stdioext: Add support for Minix.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
63 # elif defined _IOERR /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
64 /* Nothing to do. */
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
65 # else /* other implementations */
12255
83ddd76ac091 fflush, freadseek: use fseeko, not fseek
Eric Blake <ebb9@byu.net>
parents: 11653
diff changeset
66 fseeko (fp, 0, SEEK_CUR);
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
67 # endif
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
68 }
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
69
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
70 #endif
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
71
9981
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
72 #if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
73
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
74 static inline int
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
75 disable_seek_optimization (FILE *fp)
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
76 {
9981
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
77 int saved_flags = fp_->_flags & (__SOPT | __SNPT);
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
78 fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
79 return saved_flags;
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
80 }
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
81
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
82 static inline void
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
83 restore_seek_optimization (FILE *fp, int saved_flags)
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
84 {
9981
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
85 fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
86 }
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
87
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
88 #endif
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
89
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
90 static inline void
9982
a4502ab3c8d6 Oops, fix mistake in third-to-last commit.
Bruno Haible <bruno@clisp.org>
parents: 9981
diff changeset
91 update_fpos_cache (FILE *fp, off_t pos)
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
92 {
9981
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
93 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
12459
717d6fde6006 fflush: tweak
Bruno Haible <bruno@clisp.org>
parents: 12452
diff changeset
94 # if defined __CYGWIN__
717d6fde6006 fflush: tweak
Bruno Haible <bruno@clisp.org>
parents: 12452
diff changeset
95 /* fp_->_offset is typed as an integer. */
717d6fde6006 fflush: tweak
Bruno Haible <bruno@clisp.org>
parents: 12452
diff changeset
96 fp_->_offset = pos;
717d6fde6006 fflush: tweak
Bruno Haible <bruno@clisp.org>
parents: 12452
diff changeset
97 # else
717d6fde6006 fflush: tweak
Bruno Haible <bruno@clisp.org>
parents: 12452
diff changeset
98 /* fp_->_offset is an fpos_t. */
12452
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
99 /* Use a union, since on NetBSD, the compilation flags determine
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
100 whether fpos_t is typedef'd to off_t or a struct containing a
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
101 single off_t member. */
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
102 union
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
103 {
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
104 fpos_t f;
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
105 off_t o;
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
106 } u;
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
107 u.o = pos;
cc0faab513a7 fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents: 12255
diff changeset
108 fp_->_offset = u.f;
12459
717d6fde6006 fflush: tweak
Bruno Haible <bruno@clisp.org>
parents: 12452
diff changeset
109 # endif
9981
e3d6988a9347 Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents: 9980
diff changeset
110 fp_->_flags |= __SOFF;
9976
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
111 #endif
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
112 }
621cb9082339 Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents: 9776
diff changeset
113
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
114 /* Flush all pending data on STREAM according to POSIX rules. Both
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
115 output and seekable input streams are supported. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
116 int
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
117 rpl_fflush (FILE *stream)
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
118 {
8725
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
119 /* When stream is NULL, POSIX and C99 only require flushing of "output
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
120 streams and update streams in which the most recent operation was not
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
121 input", and all implementations do this.
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
122
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
123 When stream is "an output stream or an update stream in which the most
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
124 recent operation was not input", POSIX and C99 requires that fflush
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
125 writes out any buffered data, and all implementations do this.
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
126
8731
2e1ecfc17762 * lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents: 8725
diff changeset
127 When stream is, however, an input stream or an update stream in
2e1ecfc17762 * lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents: 8725
diff changeset
128 which the most recent operation was input, C99 specifies nothing,
2e1ecfc17762 * lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents: 8725
diff changeset
129 and POSIX only specifies behavior if the stream is seekable.
2e1ecfc17762 * lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents: 8725
diff changeset
130 mingw, in particular, drops the input buffer, leaving the file
2e1ecfc17762 * lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents: 8725
diff changeset
131 descriptor positioned at the end of the input buffer. I.e. ftell
2e1ecfc17762 * lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents: 8725
diff changeset
132 (stream) is lost. We don't want to call the implementation's
2e1ecfc17762 * lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents: 8725
diff changeset
133 fflush in this case.
8725
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
134
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
135 We test ! freading (stream) here, rather than fwriting (stream), because
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
136 what we need to know is whether the stream holds a "read buffer", and on
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
137 mingw this is indicated by _IOREAD, regardless of _IOWRT. */
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
138 if (stream == NULL || ! freading (stream))
8721
5122cdde0f7b Fix fflush on mingw.
Eric Blake <ebb9@byu.net>
parents: 8717
diff changeset
139 return fflush (stream);
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
140
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
141 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
142
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
143 clear_ungetc_buffer_preserving_position (stream);
9772
cfd020716bf2 Make fflush after ungetc work on BSD platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
144
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
145 return fflush (stream);
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
146
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
147 #else
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
148 {
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
149 /* Notes about the file-position indicator:
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
150 1) The file position indicator is incremented by fgetc() and decremented
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
151 by ungetc():
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
152 <http://www.opengroup.org/susv3/functions/fgetc.html>
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
153 "... the fgetc() function shall ... advance the associated file
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
154 position indicator for the stream ..."
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
155 <http://www.opengroup.org/susv3/functions/ungetc.html>
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
156 "The file-position indicator is decremented by each successful
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
157 call to ungetc()..."
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
158 2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
159 "The value of the file-position indicator for the stream after
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
160 reading or discarding all pushed-back bytes shall be the same
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
161 as it was before the bytes were pushed back."
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
162 Here we are discarding all pushed-back bytes. But more specifically,
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
163 3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
164 "[After fflush(),] the file offset of the underlying open file
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
165 description shall be set to the file position of the stream, and
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
166 any characters pushed back onto the stream by ungetc() ... shall
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
167 be discarded." */
11018
8a3de59b56aa Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 11017
diff changeset
168
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
169 /* POSIX does not specify fflush behavior for non-seekable input
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
170 streams. Some implementations purge unread data, some return
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
171 EBADF, some do nothing. */
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
172 off_t pos = ftello (stream);
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
173 if (pos == -1)
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
174 {
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
175 errno = EBADF;
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
176 return EOF;
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
177 }
11017
95eb3a6eb21d Make fflush-after-ungetc POSIX compliant on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 9982
diff changeset
178
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
179 /* Clear the ungetc buffer. */
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
180 clear_ungetc_buffer (stream);
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
181
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
182 /* To get here, we must be flushing a seekable input stream, so the
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
183 semantics of fpurge are now appropriate to clear the buffer. To
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
184 avoid losing data, the lseek is also necessary. */
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
185 {
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
186 int result = fpurge (stream);
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
187 if (result != 0)
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
188 return result;
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
189 }
8725
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
190
11017
95eb3a6eb21d Make fflush-after-ungetc POSIX compliant on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 9982
diff changeset
191 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
9134
38df3bd8e54e Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 8731
diff changeset
192
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
193 {
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
194 /* Disable seek optimization for the next fseeko call. This tells the
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
195 following fseeko call to seek to the desired position directly, rather
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
196 than to seek to a block-aligned boundary. */
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
197 int saved_flags = disable_seek_optimization (stream);
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
198 int result = fseeko (stream, pos, SEEK_SET);
9134
38df3bd8e54e Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 8731
diff changeset
199
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
200 restore_seek_optimization (stream, saved_flags);
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
201 return result;
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
202 }
9134
38df3bd8e54e Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 8731
diff changeset
203
11017
95eb3a6eb21d Make fflush-after-ungetc POSIX compliant on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 9982
diff changeset
204 # else
9134
38df3bd8e54e Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 8731
diff changeset
205
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
206 pos = lseek (fileno (stream), pos, SEEK_SET);
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
207 if (pos == -1)
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
208 return EOF;
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
209 /* After a successful lseek, update the file descriptor's position cache
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
210 in the stream. */
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
211 update_fpos_cache (stream, pos);
8725
c9dd634e50d9 More comments.
Bruno Haible <bruno@clisp.org>
parents: 8721
diff changeset
212
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
213 return 0;
9134
38df3bd8e54e Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 8731
diff changeset
214
11017
95eb3a6eb21d Make fflush-after-ungetc POSIX compliant on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 9982
diff changeset
215 # endif
11096
ff0639a6260b fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents: 11018
diff changeset
216 }
9134
38df3bd8e54e Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 8731
diff changeset
217 #endif
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
218 }