xref: /csrg-svn/lib/libc/stdio/fclose.c (revision 61178)
146111Sbostic /*-
2*61178Sbostic  * Copyright (c) 1990, 1993
3*61178Sbostic  *	The Regents of the University of California.  All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61178Sbostic static char sccsid[] = "@(#)fclose.c	8.1 (Berkeley) 06/04/93";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546111Sbostic #include <errno.h>
1646111Sbostic #include <stdio.h>
1746111Sbostic #include <stdlib.h>
1846111Sbostic #include "local.h"
1946111Sbostic 
fclose(fp)2046111Sbostic fclose(fp)
2146111Sbostic 	register FILE *fp;
2246111Sbostic {
2346111Sbostic 	register int r;
2446111Sbostic 
2546111Sbostic 	if (fp->_flags == 0) {	/* not open! */
2646111Sbostic 		errno = EBADF;
2746111Sbostic 		return (EOF);
2846111Sbostic 	}
2946210Sbostic 	r = fp->_flags & __SWR ? __sflush(fp) : 0;
3046111Sbostic 	if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
3146111Sbostic 		r = EOF;
3246111Sbostic 	if (fp->_flags & __SMBF)
3346111Sbostic 		free((char *)fp->_bf._base);
3446111Sbostic 	if (HASUB(fp))
3546111Sbostic 		FREEUB(fp);
3646111Sbostic 	if (HASLB(fp))
3746111Sbostic 		FREELB(fp);
3856071Sbostic 	fp->_flags = 0;		/* Release this FILE for reuse. */
3956071Sbostic 	fp->_r = fp->_w = 0;	/* Mess up if reaccessed. */
4046111Sbostic 	return (r);
4146111Sbostic }
42