fclose.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "_stdio.h"
  8. int fclose(register FILE *stream)
  9. {
  10. int rv = 0;
  11. __STDIO_AUTO_THREADLOCK_VAR;
  12. #ifdef __STDIO_HAS_OPENLIST
  13. #if !defined(__UCLIBC_HAS_THREADS__) || !defined(__STDIO_BUFFERS)
  14. /* First, remove the file from the open file list. */
  15. {
  16. FILE *ptr;
  17. __STDIO_THREADLOCK_OPENLIST_DEL;
  18. __STDIO_THREADLOCK_OPENLIST_ADD;
  19. ptr = _stdio_openlist;
  20. if ((ptr = _stdio_openlist) == stream) {
  21. _stdio_openlist = stream->__nextopen;
  22. } else {
  23. while (ptr) {
  24. if (ptr->__nextopen == stream) {
  25. ptr->__nextopen = stream->__nextopen;
  26. break;
  27. }
  28. ptr = ptr->__nextopen;
  29. }
  30. }
  31. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  32. __STDIO_THREADUNLOCK_OPENLIST_DEL;
  33. }
  34. #endif
  35. #endif
  36. __STDIO_AUTO_THREADLOCK(stream);
  37. __STDIO_STREAM_VALIDATE(stream);
  38. #ifdef __STDIO_BUFFERS
  39. /* Write any pending buffered chars. */
  40. if (__STDIO_STREAM_IS_WRITING(stream)) {
  41. rv = fflush_unlocked(stream);
  42. }
  43. #endif
  44. if (__CLOSE(stream) < 0) { /* Must close even if fflush failed. */
  45. rv = EOF;
  46. }
  47. stream->__filedes = -1;
  48. /* We need a way for freopen to know that a file has been closed.
  49. * Since a file can't be both readonly and writeonly, that makes
  50. * an effective signal. It also has the benefit of disabling
  51. * transitions to either reading or writing. */
  52. #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  53. /* Before we mark the file as closed, make sure we increment the openlist use count
  54. * so it isn't freed under us while still cleaning up. */
  55. __STDIO_OPENLIST_INC_USE;
  56. #endif
  57. stream->__modeflags &= (__FLAG_FREEBUF|__FLAG_FREEFILE);
  58. stream->__modeflags |= (__FLAG_READONLY|__FLAG_WRITEONLY);
  59. #ifndef NDEBUG
  60. /* Reinitialize everything (including putc since fflush could fail). */
  61. __STDIO_STREAM_DISABLE_GETC(stream);
  62. __STDIO_STREAM_DISABLE_PUTC(stream);
  63. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  64. # ifdef __UCLIBC_HAS_WCHAR__
  65. stream->__ungot_width[0] = 0;
  66. # endif
  67. # ifdef __STDIO_MBSTATE
  68. __INIT_MBSTATE(&(stream->__state));
  69. # endif
  70. #endif
  71. __STDIO_AUTO_THREADUNLOCK(stream);
  72. __STDIO_STREAM_FREE_BUFFER(stream);
  73. #ifdef __UCLIBC_MJN3_ONLY__
  74. #warning REMINDER: inefficient - locks and unlocks twice and walks whole list
  75. #endif
  76. #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  77. /* inefficient - locks/unlocks twice and walks whole list */
  78. __STDIO_OPENLIST_INC_DEL_CNT;
  79. __STDIO_OPENLIST_DEC_USE; /* This with free the file if necessary. */
  80. #else
  81. __STDIO_STREAM_FREE_FILE(stream);
  82. #endif
  83. return rv;
  84. }
  85. libc_hidden_def(fclose)