xref: /original-bsd/lib/libc/stdio/fclose.c (revision 74d2773b)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.2 (Berkeley) 02/01/91";
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 	return (r);
40 }
41