xref: /illumos-gate/usr/src/man/man3c/fdclose.3c (revision b1c37be59f1fe13f2e54002b32bc505358776e9f)
1.\"
2.\" This file and its contents are supplied under the terms of the
3.\" Common Development and Distribution License ("CDDL"), version 1.0.
4.\" You may only use this file in accordance with the terms of version
5.\" 1.0 of the CDDL.
6.\"
7.\" A full copy of the text of the CDDL should have accompanied this
8.\" source.  A copy of the CDDL is also available via the Internet at
9.\" http://www.illumos.org/license/CDDL.
10.\"
11.\"
12.\" Copyright (c) 2025, Hans Rosenfeld
13.\"
14.Dd "April 25, 2024"
15.Dt FDCLOSE 3C
16.Os
17.Sh NAME
18.Nm fdclose
19.Nd close a standard I/O stream without closing the underlying file descriptor
20.Sh LIBRARY
21.Lb libc
22.Sh SYNOPSIS
23.In stdio.h
24.Ft int
25.Fo fdclose
26.Fa "FILE *stream"
27.Fa "int *fdp"
28.Fc
29.Sh DESCRIPTION
30The
31.Fn fdclose
32function is equivalent to
33.Xr fclose 3C ,
34closing the given standard I/O stream, except that it does not close the
35underlying file descriptor.
36.Pp
37If
38.Fa fdp
39is not
40.Dv NULL ,
41it will be used to return the underlying file descriptor of the I/O stream.
42If the stream does not have an underlying file descriptor associated with it,
43.Fa fdp
44will be set to
45.Sy -1 .
46This is always done, regardless of any error returned by
47.Fn fdclose .
48.Pp
49If
50.Fa fdp
51is
52.Dv NULL ,
53no file descriptor will be returned.
54Note that regardless of the value passed for
55.Fa fdp ,
56the I/O stream will be closed without closing the underlying file descriptor.
57Usually callers who pass
58.Dv NULL
59for
60.Fa fdp
61already have the file descriptor's value available.
62.Pp
63The equivalent of
64.Xr fflush 3C
65is called on the stream before closing, thus any buffered or pending
66input is discarded while any buffered or pending output is written out
67to the underlying file.
68This includes the standard streams,
69.Va stdin ,
70.Va stdout ,
71and
72.Va stderr .
73.Pp
74After the call to
75.Fn fdclose ,
76any use of
77.Fa stream
78causes undefined
79behavior.
80.Sh RETURN VALUES
81Upon successful completion, the
82.Fn fdclose
83function returns 0.
84Otherwise,
85.Dv EOF
86is returned
87and the global variable
88.Va errno
89is set to indicate the error.
90.Sh ERRORS
91The
92.Fn fdclose
93function will fail if:
94.Bl -tag -width Er
95.It Er ENOTSUP
96The stream does not have an associated file descriptor.
97The stream was closed nonetheless, and if
98.Fa fdp
99was not
100.Dv NULL ,
101it will have been set to -1.
102.El
103.Pp
104The
105.Fn fdclose
106function may also fail and set
107.Va errno
108for any of the errors specified in
109.Xr fflush 3C .
110.Pp
111Even in the case of an error returned by
112.Fn fdclose ,
113.Fa stream
114should be considered closed.
115.Sh INTERFACE STABILITY
116.Sy Committed
117.Sh MT-LEVEL
118.Sy Mt-Safe
119.Sh SEE ALSO
120.Xr fclose 3C ,
121.Xr fflush 3C ,
122.Xr attributes 7
123