xref: /csrg-svn/lib/libc/stdio/fclose.c (revision 46210)
146111Sbostic /*-
246111Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346111Sbostic  * 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*46210Sbostic static char sccsid[] = "@(#)fclose.c	5.2 (Berkeley) 02/01/91";
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 
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 	}
29*46210Sbostic 	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);
3846111Sbostic 	fp->_flags = 0;		/* release this FILE for reuse */
3946111Sbostic 	return (r);
4046111Sbostic }
41