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