fstat: Fix module dependency conditions.
[gnulib/ericb.git] / lib / close.c
blob6a72c322993650488b8d7893fab91fb7a825f10c
1 /* close replacement.
2 Copyright (C) 2008-2017 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <unistd.h>
22 #include <errno.h>
24 #include "fd-hook.h"
25 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
26 # include "msvc-inval.h"
27 #endif
29 #undef close
31 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
32 static int
33 close_nothrow (int fd)
35 int result;
37 TRY_MSVC_INVAL
39 result = close (fd);
41 CATCH_MSVC_INVAL
43 result = -1;
44 errno = EBADF;
46 DONE_MSVC_INVAL;
48 return result;
50 #else
51 # define close_nothrow close
52 #endif
54 /* Override close() to call into other gnulib modules. */
56 int
57 rpl_close (int fd)
59 #if WINDOWS_SOCKETS
60 int retval = execute_all_close_hooks (close_nothrow, fd);
61 #else
62 int retval = close_nothrow (fd);
63 #endif
65 #if REPLACE_FCHDIR
66 if (retval >= 0)
67 _gl_unregister_fd (fd);
68 #endif
70 return retval;