fclose.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* fclose replacement.
  2. Copyright (C) 2008-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include "freading.h"
  19. #include "msvc-inval.h"
  20. #undef fclose
  21. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  22. static int
  23. fclose_nothrow (FILE *fp)
  24. {
  25. int result;
  26. TRY_MSVC_INVAL
  27. {
  28. result = fclose (fp);
  29. }
  30. CATCH_MSVC_INVAL
  31. {
  32. result = EOF;
  33. errno = EBADF;
  34. }
  35. DONE_MSVC_INVAL;
  36. return result;
  37. }
  38. #else
  39. # define fclose_nothrow fclose
  40. #endif
  41. /* Override fclose() to call the overridden fflush() or close(). */
  42. int
  43. rpl_fclose (FILE *fp)
  44. {
  45. int saved_errno = 0;
  46. int fd;
  47. int result = 0;
  48. /* Don't change behavior on memstreams. */
  49. fd = fileno (fp);
  50. if (fd < 0)
  51. return fclose_nothrow (fp);
  52. /* We only need to flush the file if it is not reading or if it is
  53. seekable. This only guarantees the file position of input files
  54. if the fflush module is also in use. */
  55. if ((!freading (fp) || lseek (fileno (fp), 0, SEEK_CUR) != -1)
  56. && fflush (fp))
  57. saved_errno = errno;
  58. /* fclose() calls close(), but we need to also invoke all hooks that our
  59. overridden close() function invokes. See lib/close.c. */
  60. #if WINDOWS_SOCKETS
  61. /* Call the overridden close(), then the original fclose().
  62. Note about multithread-safety: There is a race condition where some
  63. other thread could open fd between our close and fclose. */
  64. if (close (fd) < 0 && saved_errno == 0)
  65. saved_errno = errno;
  66. fclose_nothrow (fp); /* will fail with errno = EBADF,
  67. if we did not lose a race */
  68. #else /* !WINDOWS_SOCKETS */
  69. /* Call fclose() and invoke all hooks of the overridden close(). */
  70. # if REPLACE_FCHDIR
  71. /* Note about multithread-safety: There is a race condition here as well.
  72. Some other thread could open fd between our calls to fclose and
  73. _gl_unregister_fd. */
  74. result = fclose_nothrow (fp);
  75. if (result == 0)
  76. _gl_unregister_fd (fd);
  77. # else
  78. /* No race condition here. */
  79. result = fclose_nothrow (fp);
  80. # endif
  81. #endif /* !WINDOWS_SOCKETS */
  82. if (saved_errno != 0)
  83. {
  84. errno = saved_errno;
  85. result = EOF;
  86. }
  87. return result;
  88. }