fclose.c 2.8 KB

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