fclose.c 529 B

1234567891011121314151617181920212223242526272829303132
  1. #include "stdio_impl.h"
  2. #include "libc.h"
  3. static void dummy(FILE *f) { }
  4. weak_alias(dummy, __unlist_locked_file);
  5. int fclose(FILE *f)
  6. {
  7. int r;
  8. int perm;
  9. FLOCK(f);
  10. __unlist_locked_file(f);
  11. if (!(perm = f->flags & F_PERM)) {
  12. FILE **head = __ofl_lock();
  13. if (f->prev) f->prev->next = f->next;
  14. if (f->next) f->next->prev = f->prev;
  15. if (*head == f) *head = f->next;
  16. __ofl_unlock();
  17. }
  18. r = fflush(f);
  19. r |= f->close(f);
  20. if (f->getln_buf) free(f->getln_buf);
  21. if (!perm) free(f);
  22. else FUNLOCK(f);
  23. return r;
  24. }