3 <<fclose>>---close a file
10 int fclose(FILE *<[fp]>);
18 If the file or stream identified by <[fp]> is open, <<fclose>> closes
19 it, after first ensuring that any pending data is written (by calling
23 <<fclose>> returns <<0>> if successful (including when <[fp]> is
24 <<NULL>> or not an open file); otherwise, it returns <<EOF>>.
27 <<fclose>> is required by ANSI C.
29 Required OS subroutines: <<close>>, <<fstat>>, <<isatty>>, <<lseek>>,
30 <<read>>, <<sbrk>>, <<write>>.
34 * Copyright (c) 1990 The Regents of the University of California.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms are permitted
38 * provided that the above copyright notice and this paragraph are
39 * duplicated in all such forms and that any documentation,
40 * advertising materials, and other materials related to such
41 * distribution and use acknowledge that the software was developed
42 * by the University of California, Berkeley. The name of the
43 * University may not be used to endorse or promote products derived
44 * from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
47 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
66 return (0); /* on NULL */
72 if (fp
->_flags
== 0) /* not open! */
77 r
= fp
->_flags
& __SWR
? fflush (fp
) : 0;
78 if (fp
->_close
!= NULL
&& (*fp
->_close
) (fp
->_cookie
) < 0)
80 if (fp
->_flags
& __SMBF
)
81 _free_r (_REENT
, (char *) fp
->_bf
._base
);
87 #ifndef __SINGLE_THREAD__
88 __lock_close_recursive (*(_LOCK_RECURSIVE_T
*)&fp
->_lock
);
90 fp
->_flags
= 0; /* release this FILE for reuse */