fclose.c 438 字节
Newer Older
R
Rich Felker 已提交
1 2 3 4 5
#include "stdio_impl.h"

int fclose(FILE *f)
{
	int r;
6 7 8 9
	int perm;
	
	/* This lock is not paired with any unlock. */
	FLOCK(f);
R
Rich Felker 已提交
10

11
	if (!(perm = f->flags & F_PERM)) {
R
Rich Felker 已提交
12 13 14
		OFLLOCK();
		if (f->prev) f->prev->next = f->next;
		if (f->next) f->next->prev = f->prev;
15
		if (libc.ofl_head == f) libc.ofl_head = f->next;
R
Rich Felker 已提交
16 17 18
		OFLUNLOCK();
	}

19 20
	r = fflush(f);
	r |= f->close(f);
R
Rich Felker 已提交
21

R
Rich Felker 已提交
22
	if (f->getln_buf) free(f->getln_buf);
R
Rich Felker 已提交
23 24 25 26
	if (!perm) free(f);
	
	return r;
}