close.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* close replacement.
  2. Copyright (C) 2008-2021 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 <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. }