Mercurial > hg > octave-shane > gnulib-hg
annotate lib/fflush.c @ 17255:d81be792518a
update from texinfo
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Tue, 01 Jan 2013 15:51:49 -0800 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
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 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
17187
diff
changeset
|
2 Copyright (C) 2007-2013 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 | 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 |
16731
bbd2679d746e
fflush: Fix syntax error.
Simon Josefsson <simon@josefsson.org>
parents:
16724
diff
changeset
|
31 #include "unused-parameter.h" |
bbd2679d746e
fflush: Fix syntax error.
Simon Josefsson <simon@josefsson.org>
parents:
16724
diff
changeset
|
32 |
8657
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
33 #undef fflush |
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
34 |
11018
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 #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
|
37 |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
38 /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */ |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
39 static void |
11018
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
40 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
|
41 { |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
42 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
|
43 /* _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
|
44 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
|
45 } |
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 #else |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
48 |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
49 /* Clear the stream's ungetc buffer. May modify the value of ftello (fp). */ |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
50 static void |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
51 clear_ungetc_buffer (FILE *fp) |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
52 { |
16935
498a2211d839
Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents:
16731
diff
changeset
|
53 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */ |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
54 if (HASUB (fp)) |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
55 { |
9981
e3d6988a9347
Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents:
9980
diff
changeset
|
56 fp_->_p += fp_->_r; |
e3d6988a9347
Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents:
9980
diff
changeset
|
57 fp_->_r = 0; |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
58 } |
11018
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
59 # elif defined __EMX__ /* emx+gcc */ |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
60 if (fp->_ungetc_count > 0) |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
61 { |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
62 fp->_ungetc_count = 0; |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
63 fp->_rcount = - fp->_rcount; |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
64 } |
15485
20a2699b8a90
stdioext: Add support for Minix.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
65 # 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
|
66 /* Nothing to do. */ |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
67 # else /* other implementations */ |
12255
83ddd76ac091
fflush, freadseek: use fseeko, not fseek
Eric Blake <ebb9@byu.net>
parents:
11653
diff
changeset
|
68 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
|
69 # endif |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
70 } |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
71 |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
72 #endif |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
73 |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
74 #if ! (defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */) |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
75 |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
76 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */ |
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
77 |
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
78 static int |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
79 disable_seek_optimization (FILE *fp) |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
80 { |
9981
e3d6988a9347
Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents:
9980
diff
changeset
|
81 int saved_flags = fp_->_flags & (__SOPT | __SNPT); |
e3d6988a9347
Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents:
9980
diff
changeset
|
82 fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT; |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
83 return saved_flags; |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
84 } |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
85 |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
86 static void |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
87 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
|
88 { |
9981
e3d6988a9347
Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents:
9980
diff
changeset
|
89 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
|
90 } |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
91 |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
92 # else |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
93 |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
94 static void |
16724
1d4824d7f352
fflush: avoid compiler warning
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
95 update_fpos_cache (FILE *fp _GL_UNUSED_PARAMETER, |
1d4824d7f352
fflush: avoid compiler warning
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
96 off_t pos _GL_UNUSED_PARAMETER) |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
97 { |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
98 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */ |
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
99 # if defined __CYGWIN__ |
12459 | 100 /* fp_->_offset is typed as an integer. */ |
101 fp_->_offset = pos; | |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
102 # else |
12459 | 103 /* fp_->_offset is an fpos_t. */ |
12452
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
104 /* 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
|
105 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
|
106 single off_t member. */ |
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
107 union |
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
108 { |
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
109 fpos_t f; |
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
110 off_t o; |
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
111 } u; |
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
112 u.o = pos; |
cc0faab513a7
fflush: avoid compilation error on NetBSD
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
113 fp_->_offset = u.f; |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
114 # endif |
9981
e3d6988a9347
Add tentative support for DragonFly BSD.
Bruno Haible <bruno@clisp.org>
parents:
9980
diff
changeset
|
115 fp_->_flags |= __SOFF; |
17187
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
116 # endif |
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
117 } |
4cd6b652c293
fflush, stat: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16935
diff
changeset
|
118 # endif |
9976
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
119 #endif |
621cb9082339
Put system dependent routines into inline functions.
Bruno Haible <bruno@clisp.org>
parents:
9776
diff
changeset
|
120 |
8657
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
121 /* 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
|
122 output and seekable input streams are supported. */ |
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
123 int |
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
124 rpl_fflush (FILE *stream) |
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
125 { |
8725 | 126 /* When stream is NULL, POSIX and C99 only require flushing of "output |
127 streams and update streams in which the most recent operation was not | |
128 input", and all implementations do this. | |
129 | |
130 When stream is "an output stream or an update stream in which the most | |
131 recent operation was not input", POSIX and C99 requires that fflush | |
132 writes out any buffered data, and all implementations do this. | |
133 | |
8731
2e1ecfc17762
* lib/freading.h: Improve comments.
Eric Blake <ebb9@byu.net>
parents:
8725
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 (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
|
140 fflush in this case. |
8725 | 141 |
142 We test ! freading (stream) here, rather than fwriting (stream), because | |
143 what we need to know is whether the stream holds a "read buffer", and on | |
144 mingw this is indicated by _IOREAD, regardless of _IOWRT. */ | |
145 if (stream == NULL || ! freading (stream)) | |
8721 | 146 return fflush (stream); |
8657
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
147 |
11018
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
148 #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
|
149 |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
150 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
|
151 |
11018
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
152 return fflush (stream); |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
153 |
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
154 #else |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
155 { |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
156 /* Notes about the file-position indicator: |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
157 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
|
158 by ungetc(): |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
159 <http://www.opengroup.org/susv3/functions/fgetc.html> |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
160 "... the fgetc() function shall ... advance the associated file |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
161 position indicator for the stream ..." |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
162 <http://www.opengroup.org/susv3/functions/ungetc.html> |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
163 "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
|
164 call to ungetc()..." |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
165 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
|
166 "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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 "[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
|
172 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
|
173 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
|
174 be discarded." */ |
11018
8a3de59b56aa
Make fflush-after-ungetc POSIX compliant on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
11017
diff
changeset
|
175 |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
176 /* 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
|
177 streams. Some implementations purge unread data, some return |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
178 EBADF, some do nothing. */ |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
179 off_t pos = ftello (stream); |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
180 if (pos == -1) |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
181 { |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
182 errno = EBADF; |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
183 return EOF; |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
184 } |
11017
95eb3a6eb21d
Make fflush-after-ungetc POSIX compliant on glibc systems.
Bruno Haible <bruno@clisp.org>
parents:
9982
diff
changeset
|
185 |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
186 /* Clear the ungetc buffer. */ |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
187 clear_ungetc_buffer (stream); |
8657
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
188 |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
189 /* 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
|
190 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
|
191 avoid losing data, the lseek is also necessary. */ |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
192 { |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
193 int result = fpurge (stream); |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
194 if (result != 0) |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
195 return result; |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
196 } |
8725 | 197 |
16935
498a2211d839
Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents:
16731
diff
changeset
|
198 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */ |
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 { |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
201 /* 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
|
202 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
|
203 than to seek to a block-aligned boundary. */ |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
204 int saved_flags = disable_seek_optimization (stream); |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
205 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
|
206 |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
207 restore_seek_optimization (stream, saved_flags); |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
208 return result; |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
209 } |
9134
38df3bd8e54e
Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
8731
diff
changeset
|
210 |
11017
95eb3a6eb21d
Make fflush-after-ungetc POSIX compliant on glibc systems.
Bruno Haible <bruno@clisp.org>
parents:
9982
diff
changeset
|
211 # else |
9134
38df3bd8e54e
Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
8731
diff
changeset
|
212 |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
213 pos = lseek (fileno (stream), pos, SEEK_SET); |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
214 if (pos == -1) |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
215 return EOF; |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
216 /* 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
|
217 in the stream. */ |
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
218 update_fpos_cache (stream, pos); |
8725 | 219 |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
220 return 0; |
9134
38df3bd8e54e
Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
8731
diff
changeset
|
221 |
11017
95eb3a6eb21d
Make fflush-after-ungetc POSIX compliant on glibc systems.
Bruno Haible <bruno@clisp.org>
parents:
9982
diff
changeset
|
222 # endif |
11096
ff0639a6260b
fflush: avoid warnings on modern systems
Jim Meyering <meyering@redhat.com>
parents:
11018
diff
changeset
|
223 } |
9134
38df3bd8e54e
Use a slightly cleaner hack on BSD systems.
Bruno Haible <bruno@clisp.org>
parents:
8731
diff
changeset
|
224 #endif |
8657
05f21166eb81
* lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
225 } |