close.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* close 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 <unistd.h>
  16. #include <errno.h>
  17. #include "fd-hook.h"
  18. #include "msvc-inval.h"
  19. #undef close
  20. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  21. static int
  22. close_nothrow (int fd)
  23. {
  24. int result;
  25. TRY_MSVC_INVAL
  26. {
  27. result = close (fd);
  28. }
  29. CATCH_MSVC_INVAL
  30. {
  31. result = -1;
  32. errno = EBADF;
  33. }
  34. DONE_MSVC_INVAL;
  35. return result;
  36. }
  37. #else
  38. # define close_nothrow close
  39. #endif
  40. /* Override close() to call into other gnulib modules. */
  41. int
  42. rpl_close (int fd)
  43. {
  44. #if WINDOWS_SOCKETS
  45. int retval = execute_all_close_hooks (close_nothrow, fd);
  46. #else
  47. int retval = close_nothrow (fd);
  48. #endif
  49. #if REPLACE_FCHDIR
  50. if (retval >= 0)
  51. _gl_unregister_fd (fd);
  52. #endif
  53. return retval;
  54. }