annotate lib/fflush.c @ 8717:3afefd650c3a

On BSD implementations, when we call lseek(), we must also update or disable the stream's file descriptor position cache.
author Bruno Haible <bruno@clisp.org>
date Thu, 26 Apr 2007 09:25:05 +0000
parents caaaf4823aec
children 5122cdde0f7b
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
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
2 Copyright (C) 2007 Free Software Foundation, Inc.
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
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
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 any later version.
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
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
15 along with this program; if not, write to the Free Software Foundation,
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18 /* Written by Eric Blake. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20 #include <config.h>
8705
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
21
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
22 /* Specification. */
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
23 #include <stdio.h>
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
24
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25 #include <errno.h>
8705
caaaf4823aec * lib/fflush.c: Fix missing include.
Eric Blake <ebb9@byu.net>
parents: 8700
diff changeset
26 #include <unistd.h>
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27
8700
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
28 #include "fpurge.h"
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30 #undef fflush
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 /* 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
33 output and seekable input streams are supported. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 int
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35 rpl_fflush (FILE *stream)
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
36 {
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37 int result;
8700
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
38 off_t pos;
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 /* Try flushing the stream. C89 guarantees behavior of output
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
41 streams, so we only need to worry if failure might have been on
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 an input stream. When stream is NULL, POSIX only requires
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 flushing of output streams. */
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 result = fflush (stream);
8700
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
45 if (! stream || result == 0 || errno != EBADF)
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46 return result;
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
47
8700
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
48 /* POSIX does not specify fflush behavior for non-seekable input
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
49 streams. */
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
50 pos = ftello (stream);
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
51 if (pos == -1)
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52 {
8700
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
53 errno = EBADF;
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54 return EOF;
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 }
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
57 /* To get here, we must be flushing a seekable input stream, so the
8700
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
58 semantics of fpurge are now appropriate to clear the buffer. To
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
59 avoid losing data, the lseek is also necessary. */
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 result = fpurge (stream);
8717
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
61 if (result != 0)
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
62 return result;
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
63 pos = lseek (fileno (stream), pos, SEEK_SET);
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
64 if (pos == -1)
8700
f82b54db5bb8 Make fflush rely on fpurge.
Eric Blake <ebb9@byu.net>
parents: 8669
diff changeset
65 return EOF;
8717
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
66 #if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
67 stream->_offset = pos;
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
68 stream->_flags |= __SOFF;
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
69 #endif
3afefd650c3a On BSD implementations, when we call lseek(), we must also update or disable
Bruno Haible <bruno@clisp.org>
parents: 8705
diff changeset
70 return 0;
8657
05f21166eb81 * lib/stdio_.h [REPLACE_FFLUSH]: Declare rpl_fflush.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
71 }