xref: /csrg-svn/lib/libc/stdio/fclose.c (revision 46111)
1*46111Sbostic /*-
2*46111Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46111Sbostic  * All rights reserved.
4*46111Sbostic  *
5*46111Sbostic  * This code is derived from software contributed to Berkeley by
6*46111Sbostic  * Chris Torek.
7*46111Sbostic  *
8*46111Sbostic  * %sccs.include.redist.c%
9*46111Sbostic  */
10*46111Sbostic 
11*46111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46111Sbostic static char sccsid[] = "@(#)fclose.c	5.1 (Berkeley) 01/20/91";
13*46111Sbostic #endif /* LIBC_SCCS and not lint */
14*46111Sbostic 
15*46111Sbostic #include <errno.h>
16*46111Sbostic #include <stdio.h>
17*46111Sbostic #include <stdlib.h>
18*46111Sbostic #include "local.h"
19*46111Sbostic 
20*46111Sbostic fclose(fp)
21*46111Sbostic 	register FILE *fp;
22*46111Sbostic {
23*46111Sbostic 	register int r;
24*46111Sbostic 
25*46111Sbostic 	if (fp->_flags == 0) {	/* not open! */
26*46111Sbostic 		errno = EBADF;
27*46111Sbostic 		return (EOF);
28*46111Sbostic 	}
29*46111Sbostic 	r = fp->_flags & __SWR ? fflush(fp) : 0;
30*46111Sbostic 	if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
31*46111Sbostic 		r = EOF;
32*46111Sbostic 	if (fp->_flags & __SMBF)
33*46111Sbostic 		free((char *)fp->_bf._base);
34*46111Sbostic 	if (HASUB(fp))
35*46111Sbostic 		FREEUB(fp);
36*46111Sbostic 	if (HASLB(fp))
37*46111Sbostic 		FREELB(fp);
38*46111Sbostic 	fp->_flags = 0;		/* release this FILE for reuse */
39*46111Sbostic 	return (r);
40*46111Sbostic }
41