date and time created 91/03/07 10:23:53 by bostic
[unix-history] / usr / src / lib / libc / stdio / fclose.c
CommitLineData
c9be6cfe
KB
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)
7999665b 12static char sccsid[] = "@(#)fclose.c 5.2 (Berkeley) %G%";
c9be6cfe
KB
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
20fclose(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 }
7999665b 29 r = fp->_flags & __SWR ? __sflush(fp) : 0;
c9be6cfe
KB
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}