15a69da8d7af6a388a4ad5b3dd619017b0030031aElliott Hughes/*	$OpenBSD: fclose.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*-
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 1990, 1993
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	The Regents of the University of California.  All rights reserved.
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This code is derived from software contributed to Berkeley by
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Chris Torek.
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
17b2e1abda0503874a5bb097e0b55ad4f8ce211a8dElliott Hughes * 3. Neither the name of the University nor the names of its contributors
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    may be used to endorse or promote products derived from this software
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    without specific prior written permission.
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <errno.h>
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h>
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h>
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "local.h"
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectfclose(FILE *fp)
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	int r;
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (fp->_flags == 0) {	/* not open! */
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		errno = EBADF;
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		return (EOF);
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
48f582340a6a48588aa50da17e1620e8f91b146941Kenny Root	FLOCKFILE(fp);
495a69da8d7af6a388a4ad5b3dd619017b0030031aElliott Hughes	WCIO_FREE(fp);
501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	r = fp->_flags & __SWR ? __sflush(fp) : 0;
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		r = EOF;
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (fp->_flags & __SMBF)
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		free((char *)fp->_bf._base);
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (HASUB(fp))
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		FREEUB(fp);
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (HASLB(fp))
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		FREELB(fp);
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	fp->_r = fp->_w = 0;	/* Mess up if reaccessed. */
60f582340a6a48588aa50da17e1620e8f91b146941Kenny Root	fp->_flags = 0;		/* Release this FILE for reuse. */
61f582340a6a48588aa50da17e1620e8f91b146941Kenny Root	FUNLOCKFILE(fp);
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	return (r);
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
64