xref: /freebsd/lib/libc/stdio/fclose.3 (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
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 July 4, 2015
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
73.Fa fdp
74argument will be different then NULL the file descriptor will be returned in it,
75If the stream does not have an associated file descriptor,
76.Fa fdp
77will be set to -1.
78This type of stream is created with functions such as
79.Xr fmemopen 3 ,
80.Xr funopen 3 ,
81or
82.Xr open_memstream 3 .
83.Pp
84The
85.Fn fcloseall
86function calls
87.Fn fclose
88on all open streams.
89.Sh RETURN VALUES
90.Fn fcloseall
91does not return a value.
92.Pp
93Upon successful completion the
94.Fn fclose
95and
96.Fn fdclose
97functions return 0.
98Otherwise,
99.Dv EOF
100is returned and the global variable
101.Va errno
102is set to indicate the error.
103.Sh ERRORS
104.Fn fdclose
105fails if:
106.Bl -tag -width Er
107.It Bq Er EOPNOTSUPP
108The stream does not have an associated file descriptor.
109.El
110.Pp
111The
112.Fn fclose
113and
114.Fn fdclose
115functions may also fail and set
116.Va errno
117for any of the errors specified for
118.Xr fflush 3 .
119.Pp
120The
121.Fn fclose
122function may also fail and set errno for any of the errors specified for
123.Xr close 2 .
124.Sh NOTES
125The
126.Fn fclose
127and
128.Fn fdclose
129functions do not handle NULL arguments in the
130.Fa stream
131variable; this will result in a segmentation violation.
132This is intentional.
133It makes it easier to make sure programs written under
134.Fx
135are bug free.
136This behaviour is an implementation detail, and programs should not
137rely upon it.
138.Sh SEE ALSO
139.Xr close 2 ,
140.Xr fflush 3 ,
141.Xr fopen 3 ,
142.Xr setbuf 3
143.Sh STANDARDS
144The
145.Fn fclose
146function
147conforms to
148.St -isoC .
149.Sh HISTORY
150The
151.Fn fcloseall
152function first appeared in
153.Fx 7.0 .
154.Pp
155The
156.Fn fdclose
157function first appeared in
158.Fx 11.0 .
159