fcloseall.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include <features.h>
  8. #ifdef __USE_GNU
  9. #include "_stdio.h"
  10. /* NOTE: GLIBC difference!!! -- fcloseall
  11. * According to the info pages, glibc actually fclose()s all open files.
  12. * Apparently, glibc's new version only fflush()s and unbuffers all
  13. * writing streams to cope with unordered destruction of c++ static
  14. * objects.
  15. */
  16. int fcloseall (void)
  17. {
  18. #ifdef __STDIO_HAS_OPENLIST
  19. int retval = 0;
  20. FILE *f;
  21. __STDIO_OPENLIST_INC_USE;
  22. __STDIO_THREADLOCK_OPENLIST_ADD;
  23. f = _stdio_openlist;
  24. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  25. while (f) {
  26. FILE *n = f->__nextopen;
  27. __STDIO_AUTO_THREADLOCK_VAR;
  28. __STDIO_AUTO_THREADLOCK(f);
  29. /* Only call fclose on the stream if it is not already closed. */
  30. if ((f->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
  31. != (__FLAG_READONLY|__FLAG_WRITEONLY)
  32. ) {
  33. if (fclose(f)) {
  34. retval = EOF;
  35. }
  36. }
  37. __STDIO_AUTO_THREADUNLOCK(f);
  38. f = n;
  39. }
  40. __STDIO_OPENLIST_DEC_USE;
  41. return retval;
  42. #else
  43. #warning Always fails in this configuration because no open file list.
  44. return EOF;
  45. #endif
  46. }
  47. #endif