support linux kernel apis (new archs) with old syscalls removed
[oweals/musl.git] / src / stdio / fclose.c
1 #include "stdio_impl.h"
2
3 int fclose(FILE *f)
4 {
5         int r;
6         int perm;
7         
8         FFINALLOCK(f);
9
10         if (!(perm = f->flags & F_PERM)) {
11                 OFLLOCK();
12                 if (f->prev) f->prev->next = f->next;
13                 if (f->next) f->next->prev = f->prev;
14                 if (libc.ofl_head == f) libc.ofl_head = f->next;
15                 OFLUNLOCK();
16         }
17
18         r = fflush(f);
19         r |= f->close(f);
20
21         if (f->getln_buf) free(f->getln_buf);
22         if (!perm) free(f);
23         
24         return r;
25 }