close.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* close replacement.
  2. Copyright (C) 2008-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include "fd-hook.h"
  18. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  19. # include "msvc-inval.h"
  20. #endif
  21. #undef close
  22. #if defined _WIN32 && !defined __CYGWIN__
  23. # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  24. static int
  25. close_nothrow (int fd)
  26. {
  27. int result;
  28. TRY_MSVC_INVAL
  29. {
  30. result = _close (fd);
  31. }
  32. CATCH_MSVC_INVAL
  33. {
  34. result = -1;
  35. errno = EBADF;
  36. }
  37. DONE_MSVC_INVAL;
  38. return result;
  39. }
  40. # else
  41. # define close_nothrow _close
  42. # endif
  43. #else
  44. # define close_nothrow close
  45. #endif
  46. /* Override close() to call into other gnulib modules. */
  47. int
  48. rpl_close (int fd)
  49. {
  50. #if WINDOWS_SOCKETS
  51. int retval = execute_all_close_hooks (close_nothrow, fd);
  52. #else
  53. int retval = close_nothrow (fd);
  54. #endif
  55. #if REPLACE_FCHDIR
  56. if (retval >= 0)
  57. _gl_unregister_fd (fd);
  58. #endif
  59. return retval;
  60. }