correct locking in stdio functions that tried to be lock-free
[musl] / src / stdio / fclose.c
1 #include "stdio_impl.h"
2
3 int fclose(FILE *f)
4 {
5         int r;
6         int perm;
7         
8         /* This lock is not paired with any unlock. */
9         FLOCK(f);
10
11         if (!(perm = f->flags & F_PERM)) {
12                 OFLLOCK();
13                 if (f->prev) f->prev->next = f->next;
14                 if (f->next) f->next->prev = f->prev;
15                 if (libc.ofl_head == f) libc.ofl_head = f->next;
16                 OFLUNLOCK();
17         }
18
19         r = fflush(f);
20         r |= f->close(f);
21
22         if (f->getln_buf) free(f->getln_buf);
23         if (!perm) free(f);
24         
25         return r;
26 }