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