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

static void dummy(FILE *f) { }
weak_alias(dummy, __unlist_locked_file);
R
Rich Felker 已提交
6 7 8 9

int fclose(FILE *f)
{
	int r;
10 11
	int perm;
	
R
Rich Felker 已提交
12
	FFINALLOCK(f);
R
Rich Felker 已提交
13

14 15
	__unlist_locked_file(f);

16
	if (!(perm = f->flags & F_PERM)) {
R
Rich Felker 已提交
17 18 19
		OFLLOCK();
		if (f->prev) f->prev->next = f->next;
		if (f->next) f->next->prev = f->prev;
20
		if (libc.ofl_head == f) libc.ofl_head = f->next;
R
Rich Felker 已提交
21 22 23
		OFLUNLOCK();
	}

24 25
	r = fflush(f);
	r |= f->close(f);
R
Rich Felker 已提交
26

R
Rich Felker 已提交
27
	if (f->getln_buf) free(f->getln_buf);
R
Rich Felker 已提交
28 29 30 31
	if (!perm) free(f);
	
	return r;
}