]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/fpurge.c
1 /* fpurge - Flushing buffers of a FILE stream. */
3 /* Copyright (C) 2007 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
27 /* Specification. Same as in ../../externs.h. */
28 #define NEED_FPURGE_DECL
30 # define fpurge _bash_fpurge
32 extern int fpurge
__P (( FILE * stream
));
34 #if HAVE___FPURGE /* glibc >= 2.2, Haiku, Solaris >= 7 */
35 # include <stdio_ext.h>
42 #if HAVE___FPURGE /* glibc >= 2.2, Haiku, Solaris >= 7 */
45 /* The __fpurge function does not have a return value. */
48 #elif HAVE_FPURGE /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X */
50 /* Call the system's fpurge function. */
52 # if !HAVE_DECL_FPURGE
53 extern int fpurge ( FILE *);
55 int result
= fpurge ( fp
);
56 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
58 /* Correct the invariants that fpurge broke.
59 <stdio.h> on BSD systems says:
60 "The following always hold: if _flags & __SRD, _w is 0."
61 If this invariant is not fulfilled and the stream is read-write but
62 currently writing, subsequent putc or fputc calls will write directly
63 into the buffer, although they shouldn't be allowed to. */
64 if (( fp
-> _flags
& __SRD
) != 0 )
71 /* Most systems provide FILE as a struct and the necessary bitmask in
72 <stdio.h>, because they need it for implementing getc() and putc() as
74 # if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
75 fp
-> _IO_read_end
= fp
-> _IO_read_ptr
;
76 fp
-> _IO_write_ptr
= fp
-> _IO_write_base
;
77 /* Avoid memory leak when there is an active ungetc buffer. */
78 if ( fp
-> _IO_save_base
!= NULL
)
80 free ( fp
-> _IO_save_base
);
81 fp
-> _IO_save_base
= NULL
;
84 # elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
85 fp_
-> _p
= fp_
-> _bf
. _base
;
87 fp_
-> _w
= (( fp_
-> _flags
& ( __SLBF
| __SNBF
| __SRD
)) == 0 /* fully buffered and not currently reading? */
90 /* Avoid memory leak when there is an active ungetc buffer. */
91 if ( fp_ub
. _base
!= NULL
)
93 if ( fp_ub
. _base
!= fp_
-> _ubuf
)
98 # elif defined __EMX__ /* emx+gcc */
99 fp
-> _ptr
= fp
-> _buffer
;
102 fp
-> _ungetc_count
= 0 ;
104 # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
105 fp
-> _ptr
= fp
-> _base
;
106 if ( fp
-> _ptr
!= NULL
)
109 # elif defined __UCLIBC__ /* uClibc */
110 # ifdef __STDIO_BUFFERS
111 if ( fp
-> __modeflags
& __FLAG_WRITING
)
112 fp
-> __bufpos
= fp
-> __bufstart
;
113 else if ( fp
-> __modeflags
& ( __FLAG_READONLY
| __FLAG_READING
))
114 fp
-> __bufpos
= fp
-> __bufread
;
117 # elif defined __QNX__ /* QNX */
118 fp
-> _Rback
= fp
-> _Back
+ sizeof ( fp
-> _Back
);
120 if ( fp
-> _Mode
& 0x2000 /* _MWRITE */ )
121 /* fp->_Buf <= fp->_Next <= fp->_Wend */
122 fp
-> _Next
= fp
-> _Buf
;
124 /* fp->_Buf <= fp->_Next <= fp->_Rend */
125 fp
-> _Rend
= fp
-> _Next
;
127 # elif defined __MINT__ /* Atari FreeMiNT */
128 if ( fp
-> __pushed_back
)
130 fp
-> __bufp
= fp
-> __pushback_bufp
;
131 fp
-> __pushed_back
= 0 ;
133 /* Preserve the current file position. */
134 if ( fp
-> __target
!= - 1 )
135 fp
-> __target
+= fp
-> __bufp
- fp
-> __buffer
;
136 fp
-> __bufp
= fp
-> __buffer
;
137 /* Nothing in the buffer, next getc is nontrivial. */
138 fp
-> __get_limit
= fp
-> __bufp
;
139 /* Nothing in the buffer, next putc is nontrivial. */
140 fp
-> __put_limit
= fp
-> __buffer
;
143 # warning "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."