xref: /freebsd/lib/libc/stdio/fclose.3 (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1.\" Copyright (c) 1990, 1991, 1993 The Regents of the University of California.
2.\" Copyright (c) 2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
3.\" All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" Chris Torek and the American National Standards Committee X3,
7.\" on Information Processing Systems.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. Neither the name of the University nor the names of its contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.Dd February 13, 2024
34.Dt FCLOSE 3
35.Os
36.Sh NAME
37.Nm fclose ,
38.Nm fdclose ,
39.Nm fcloseall
40.Nd close a stream
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In stdio.h
45.Ft int
46.Fn fclose "FILE *stream"
47.Ft int
48.Fn fdclose "FILE *stream" "int *fdp"
49.Ft void
50.Fn fcloseall void
51.Sh DESCRIPTION
52The
53.Fn fclose
54function
55dissociates the named
56.Fa stream
57from its underlying file or set of functions.
58If the stream was being used for output, any buffered data is written
59first, using
60.Xr fflush 3 .
61.Pp
62The
63.Fn fdclose
64function is equivalent to
65.Fn fclose
66except that it does not close the underlying file descriptor.
67If
68.Fa fdp
69is not
70.Dv NULL ,
71the file descriptor will be written to it.
72If the stream does not have an associated file descriptor,
73.Fa fdp
74will be set to -1.
75This type of stream is created with functions such as
76.Xr fmemopen 3 ,
77.Xr funopen 3 ,
78or
79.Xr open_memstream 3 .
80.Pp
81The
82.Fn fcloseall
83function calls
84.Fn fclose
85on all open streams.
86.Sh RETURN VALUES
87.Fn fcloseall
88does not return a value.
89.Pp
90Upon successful completion the
91.Fn fclose
92and
93.Fn fdclose
94functions return 0.
95Otherwise,
96.Dv EOF
97is returned and the global variable
98.Va errno
99is set to indicate the error.
100.Sh ERRORS
101.Fn fdclose
102fails if:
103.Bl -tag -width Er
104.It Bq Er EOPNOTSUPP
105The stream does not have an associated file descriptor.
106.El
107.Pp
108The
109.Fn fclose
110and
111.Fn fdclose
112functions may also fail and set
113.Va errno
114for any of the errors specified for
115.Xr fflush 3 .
116.Pp
117The
118.Fn fclose
119function may also fail and set errno for any of the errors specified for
120.Xr close 2 .
121.Sh NOTES
122The
123.Fn fclose
124and
125.Fn fdclose
126functions do not handle NULL arguments in the
127.Fa stream
128variable; this will result in a segmentation violation.
129This is intentional.
130It makes it easier to make sure programs written under
131.Fx
132are bug free.
133This behaviour is an implementation detail, and programs should not
134rely upon it.
135.Sh SEE ALSO
136.Xr close 2 ,
137.Xr fflush 3 ,
138.Xr fopen 3 ,
139.Xr setbuf 3
140.Sh STANDARDS
141The
142.Fn fclose
143function
144conforms to
145.St -isoC .
146.Sh HISTORY
147The
148.Fn fcloseall
149function first appeared in
150.Fx 7.0 .
151.Pp
152The
153.Fn fdclose
154function first appeared in
155.Fx 11.0 .
156