From 4abab5ad6c8465a7528ccdd5f49367da05f78bbd Mon Sep 17 00:00:00 2001 From: Vladimir Azarov Date: Tue, 1 Oct 2024 15:47:05 +0200 Subject: Initial version --- src/stdio/fclose.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/stdio/fclose.c (limited to 'src/stdio/fclose.c') diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c new file mode 100644 index 0000000..d594532 --- /dev/null +++ b/src/stdio/fclose.c @@ -0,0 +1,38 @@ +#include "stdio_impl.h" +#include + +static void dummy(FILE *f) { } +weak_alias(dummy, __unlist_locked_file); + +int fclose(FILE *f) +{ + int r; + + FLOCK(f); + r = fflush(f); + r |= f->close(f); + FUNLOCK(f); + + /* Past this point, f is closed and any further explict access + * to it is undefined. However, it still exists as an entry in + * the open file list and possibly in the thread's locked files + * list, if it was closed while explicitly locked. Functions + * which process these lists must tolerate dead FILE objects + * (which necessarily have inactive buffer pointers) without + * producing any side effects. */ + + if (f->flags & F_PERM) return r; + + __unlist_locked_file(f); + + FILE **head = __ofl_lock(); + if (f->prev) f->prev->next = f->next; + if (f->next) f->next->prev = f->prev; + if (*head == f) *head = f->next; + __ofl_unlock(); + + free(f->getln_buf); + free(f); + + return r; +} -- cgit v1.2.3