xref: /freebsd/lib/libc/stdio/fclose.3 (revision edf8578117e8844e02c0121147f45e4609b30680)
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.\"     @(#)fclose.3	8.1 (Berkeley) 6/4/93
34.\"
35.Dd July 4, 2015
36.Dt FCLOSE 3
37.Os
38.Sh NAME
39.Nm fclose ,
40.Nm fdclose ,
41.Nm fcloseall
42.Nd close a stream
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdio.h
47.Ft int
48.Fn fclose "FILE *stream"
49.Ft int
50.Fn fdclose "FILE *stream" "int *fdp"
51.Ft void
52.Fn fcloseall void
53.Sh DESCRIPTION
54The
55.Fn fclose
56function
57dissociates the named
58.Fa stream
59from its underlying file or set of functions.
60If the stream was being used for output, any buffered data is written
61first, using
62.Xr fflush 3 .
63.Pp
64The
65.Fn fdclose
66function is equivalent to
67.Fn fclose
68except that it does not close the underlying file descriptor.
69If
70.Fa fdp
71is not
72.Dv NULL ,
73the file descriptor will be written to it.
74If the
75.Fa fdp
76argument will be different then NULL the file descriptor will be returned in it,
77If the stream does not have an associated file descriptor,
78.Fa fdp
79will be set to -1.
80This type of stream is created with functions such as
81.Xr fmemopen 3 ,
82.Xr funopen 3 ,
83or
84.Xr open_memstream 3 .
85.Pp
86The
87.Fn fcloseall
88function calls
89.Fn fclose
90on all open streams.
91.Sh RETURN VALUES
92.Fn fcloseall
93does not return a value.
94.Pp
95Upon successful completion the
96.Fn fclose
97and
98.Fn fdclose
99functions return 0.
100Otherwise,
101.Dv EOF
102is returned and the global variable
103.Va errno
104is set to indicate the error.
105.Sh ERRORS
106.Fn fdclose
107fails if:
108.Bl -tag -width Er
109.It Bq Er EOPNOTSUPP
110The stream does not have an associated file descriptor.
111.El
112.Pp
113The
114.Fn fclose
115and
116.Fn fdclose
117functions may also fail and set
118.Va errno
119for any of the errors specified for
120.Xr fflush 3 .
121.Pp
122The
123.Fn fclose
124function may also fail and set errno for any of the errors specified for
125.Xr close 2 .
126.Sh NOTES
127The
128.Fn fclose
129and
130.Fn fdclose
131functions do not handle NULL arguments in the
132.Fa stream
133variable; this will result in a segmentation violation.
134This is intentional.
135It makes it easier to make sure programs written under
136.Fx
137are bug free.
138This behaviour is an implementation detail, and programs should not
139rely upon it.
140.Sh SEE ALSO
141.Xr close 2 ,
142.Xr fflush 3 ,
143.Xr fopen 3 ,
144.Xr setbuf 3
145.Sh STANDARDS
146The
147.Fn fclose
148function
149conforms to
150.St -isoC .
151.Sh HISTORY
152The
153.Fn fcloseall
154function first appeared in
155.Fx 7.0 .
156.Pp
157The
158.Fn fdclose
159function first appeared in
160.Fx 11.0 .
161