fcloseall.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. libc_hidden_proto(fclose)
  11. /* NOTE: GLIBC difference!!! -- fcloseall
  12. * According to the info pages, glibc actually fclose()s all open files.
  13. * Apparently, glibc's new version only fflush()s and unbuffers all
  14. * writing streams to cope with unordered destruction of c++ static
  15. * objects.
  16. */
  17. int fcloseall (void)
  18. {
  19. #ifdef __STDIO_HAS_OPENLIST
  20. int retval = 0;
  21. FILE *f;
  22. __STDIO_OPENLIST_INC_USE;
  23. #ifdef __UCLIBC_MJN3_ONLY__
  24. #warning REMINDER: should probably have a get_head() operation
  25. #endif
  26. __STDIO_THREADLOCK_OPENLIST_ADD;
  27. f = _stdio_openlist;
  28. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  29. while (f) {
  30. #ifdef __UCLIBC_MJN3_ONLY__
  31. #warning REMINDER: should probably have a get_next() operation
  32. #endif
  33. FILE *n = f->__nextopen;
  34. __STDIO_AUTO_THREADLOCK_VAR;
  35. __STDIO_AUTO_THREADLOCK(f);
  36. /* Only call fclose on the stream if it is not already closed. */
  37. if ((f->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
  38. != (__FLAG_READONLY|__FLAG_WRITEONLY)
  39. ) {
  40. if (fclose(f)) {
  41. retval = EOF;
  42. }
  43. }
  44. __STDIO_AUTO_THREADUNLOCK(f);
  45. f = n;
  46. }
  47. __STDIO_OPENLIST_DEC_USE;
  48. return retval;
  49. #else
  50. #warning Always fails in this configuration because no open file list.
  51. return EOF;
  52. #endif
  53. }
  54. #endif