fix false ownership of stdio FILEs due to tid reuse
[musl] / src / stdio / fclose.c
1 #include "stdio_impl.h"
2 #include "libc.h"
3
4 static void dummy(FILE *f) { }
5 weak_alias(dummy, __unlist_locked_file);
6
7 int fclose(FILE *f)
8 {
9         int r;
10         int perm;
11         
12         FFINALLOCK(f);
13
14         __unlist_locked_file(f);
15
16         if (!(perm = f->flags & F_PERM)) {
17                 OFLLOCK();
18                 if (f->prev) f->prev->next = f->next;
19                 if (f->next) f->next->prev = f->prev;
20                 if (libc.ofl_head == f) libc.ofl_head = f->next;
21                 OFLUNLOCK();
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         
30         return r;
31 }