2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
26 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
27 # include "msvc-inval.h"
32 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
34 fclose_nothrow (FILE *fp
)
52 # define fclose_nothrow fclose
55 /* Override fclose() to call the overridden fflush() or close(). */
64 /* Don't change behavior on memstreams. */
67 return fclose_nothrow (fp
);
69 /* We only need to flush the file if it is not reading or if it is
70 seekable. This only guarantees the file position of input files
71 if the fflush module is also in use. */
72 if ((!freading (fp
) || lseek (fileno (fp
), 0, SEEK_CUR
) != -1)
76 /* fclose() calls close(), but we need to also invoke all hooks that our
77 overridden close() function invokes. See lib/close.c. */
79 /* Call the overridden close(), then the original fclose().
80 Note about multithread-safety: There is a race condition where some
81 other thread could open fd between our close and fclose. */
82 if (close (fd
) < 0 && saved_errno
== 0)
85 fclose_nothrow (fp
); /* will fail with errno = EBADF,
86 if we did not lose a race */
88 #else /* !WINDOWS_SOCKETS */
89 /* Call fclose() and invoke all hooks of the overridden close(). */
92 /* Note about multithread-safety: There is a race condition here as well.
93 Some other thread could open fd between our calls to fclose and
95 result
= fclose_nothrow (fp
);
97 _gl_unregister_fd (fd
);
99 /* No race condition here. */
100 result
= fclose_nothrow (fp
);
103 #endif /* !WINDOWS_SOCKETS */
105 if (saved_errno
!= 0)