iofclose.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (C) 1993-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include "libioP.h"
  23. #include <stdlib.h>
  24. #include "../iconv/gconv_int.h"
  25. #include <shlib-compat.h>
  26. int
  27. _IO_new_fclose (FILE *fp)
  28. {
  29. int status;
  30. CHECK_FILE(fp, EOF);
  31. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
  32. /* We desperately try to help programs which are using streams in a
  33. strange way and mix old and new functions. Detect old streams
  34. here. */
  35. if (_IO_vtable_offset (fp) != 0)
  36. return _IO_old_fclose (fp);
  37. #endif
  38. /* First unlink the stream. */
  39. if (fp->_flags & _IO_IS_FILEBUF)
  40. _IO_un_link ((struct _IO_FILE_plus *) fp);
  41. _IO_acquire_lock (fp);
  42. if (fp->_flags & _IO_IS_FILEBUF)
  43. status = _IO_file_close_it (fp);
  44. else
  45. status = fp->_flags & _IO_ERR_SEEN ? -1 : 0;
  46. _IO_release_lock (fp);
  47. _IO_FINISH (fp);
  48. if (fp->_mode > 0)
  49. {
  50. /* This stream has a wide orientation. This means we have to free
  51. the conversion functions. */
  52. struct _IO_codecvt *cc = fp->_codecvt;
  53. __libc_lock_lock (__gconv_lock);
  54. __gconv_release_step (cc->__cd_in.__cd.__steps);
  55. __gconv_release_step (cc->__cd_out.__cd.__steps);
  56. __libc_lock_unlock (__gconv_lock);
  57. }
  58. else
  59. {
  60. if (_IO_have_backup (fp))
  61. _IO_free_backup_area (fp);
  62. }
  63. if (fp != _IO_stdin && fp != _IO_stdout && fp != _IO_stderr)
  64. {
  65. fp->_flags = 0;
  66. free(fp);
  67. }
  68. return status;
  69. }
  70. versioned_symbol (libc, _IO_new_fclose, _IO_fclose, GLIBC_2_1);
  71. strong_alias (_IO_new_fclose, __new_fclose)
  72. versioned_symbol (libc, __new_fclose, fclose, GLIBC_2_1);