xref: /freebsd/lib/libc/stdio/fflush.3 (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd May 1, 2020
33.Dt FFLUSH 3
34.Os
35.Sh NAME
36.Nm fflush ,
37.Nm fflush_unlocked ,
38.Nm fpurge
39.Nd flush a stream
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In stdio.h
44.Ft int
45.Fn fflush "FILE *stream"
46.Ft int
47.Fn fflush_unlocked "FILE *stream"
48.Ft int
49.Fn fpurge "FILE *stream"
50.Sh DESCRIPTION
51The function
52.Fn fflush
53forces a write of all buffered data for the given output or update
54.Fa stream
55via the stream's underlying write function.
56The open status of the stream is unaffected.
57.Pp
58If the
59.Fa stream
60argument is
61.Dv NULL ,
62.Fn fflush
63flushes
64.Em all
65open output streams.
66.Pp
67The
68.Fn fflush_unlocked
69function is equivalent to
70.Fn fflush ,
71except that the caller is responsible for locking the stream with
72.Xr flockfile 3
73before calling it.
74This function may be used to avoid the overhead of locking the stream and to
75prevent races when multiple threads are operating on the same stream.
76.Pp
77The function
78.Fn fpurge
79erases any input or output buffered in the given
80.Fa stream .
81For output streams this discards any unwritten output.
82For input streams this discards any input read from the underlying object
83but not yet obtained via
84.Xr getc 3 ;
85this includes any text pushed back via
86.Xr ungetc 3 .
87.Sh RETURN VALUES
88Upon successful completion 0 is returned.
89Otherwise,
90.Dv EOF
91is returned and the global variable
92.Va errno
93is set to indicate the error.
94.Sh ERRORS
95.Bl -tag -width Er
96.It Bq Er EBADF
97The
98.Fa stream
99argument
100is not an open stream.
101.El
102.Pp
103The function
104.Fn fflush
105may also fail and set
106.Va errno
107for any of the errors specified for the routine
108.Xr write 2 ,
109except that in case of
110.Fa stream
111being a read-only descriptor,
112.Fn fflush
113returns 0.
114.Sh SEE ALSO
115.Xr write 2 ,
116.Xr fclose 3 ,
117.Xr fopen 3 ,
118.Xr setbuf 3
119.Sh STANDARDS
120The
121.Fn fflush
122function
123conforms to
124.St -isoC .
125.Sh HISTORY
126The
127.Fn fflush
128function first appeared in
129.At v4 .
130The
131.Fn fpurge
132function first appeared in
133.Bx 4.4 .
134