fclose.c 891 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "stdio_impl.h"
  2. #include <stdlib.h>
  3. static void dummy(FILE *f) { }
  4. weak_alias(dummy, __unlist_locked_file);
  5. int fclose(FILE *f)
  6. {
  7. int r;
  8. FLOCK(f);
  9. r = fflush(f);
  10. r |= f->close(f);
  11. FUNLOCK(f);
  12. /* Past this point, f is closed and any further explict access
  13. * to it is undefined. However, it still exists as an entry in
  14. * the open file list and possibly in the thread's locked files
  15. * list, if it was closed while explicitly locked. Functions
  16. * which process these lists must tolerate dead FILE objects
  17. * (which necessarily have inactive buffer pointers) without
  18. * producing any side effects. */
  19. if (f->flags & F_PERM) return r;
  20. __unlist_locked_file(f);
  21. FILE **head = __ofl_lock();
  22. if (f->prev) f->prev->next = f->next;
  23. if (f->next) f->next->prev = f->prev;
  24. if (*head == f) *head = f->next;
  25. __ofl_unlock();
  26. free(f->getln_buf);
  27. free(f);
  28. return r;
  29. }