1#include "stdio_impl.h"
2#include "libc.h"
3
4static void dummy(FILE *f) { }
5weak_alias(dummy, __unlist_locked_file);
6
7int fclose(FILE *f)
8{
9	int r;
10	int perm;
11
12	FLOCK(f);
13
14	__unlist_locked_file(f);
15
16	if (!(perm = f->flags & F_PERM)) {
17		FILE **head = __ofl_lock();
18		if (f->prev) f->prev->next = f->next;
19		if (f->next) f->next->prev = f->prev;
20		if (*head == f) *head = f->next;
21		__ofl_unlock();
22	}
23
24	r = fflush(f);
25	r |= f->close(f);
26
27	if (f->getln_buf) free(f->getln_buf);
28	if (!perm) free(f);
29	else FUNLOCK(f);
30
31	return r;
32}
33