fcloseall.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #ifdef __UCLIBC_MJN3_ONLY__
  23. #warning REMINDER: should probably have a get_head() operation
  24. #endif
  25. __STDIO_THREADLOCK_OPENLIST_ADD;
  26. f = _stdio_openlist;
  27. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  28. while (f) {
  29. #ifdef __UCLIBC_MJN3_ONLY__
  30. #warning REMINDER: should probably have a get_next() operation
  31. #endif
  32. FILE *n = f->__nextopen;
  33. __STDIO_AUTO_THREADLOCK_VAR;
  34. __STDIO_AUTO_THREADLOCK(f);
  35. /* Only call fclose on the stream if it is not already closed. */
  36. if ((f->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
  37. != (__FLAG_READONLY|__FLAG_WRITEONLY)
  38. ) {
  39. if (fclose(f)) {
  40. retval = EOF;
  41. }
  42. }
  43. __STDIO_AUTO_THREADUNLOCK(f);
  44. f = n;
  45. }
  46. __STDIO_OPENLIST_DEC_USE;
  47. return retval;
  48. #else
  49. #warning Always fails in this configuration because no open file list.
  50. return EOF;
  51. #endif
  52. }
  53. #endif