openat: don’t close (-1)
[gnulib.git] / lib / fclose.c
blob77c74d65662ae13ac39322feabca8cdd20d96a06
1 /* fclose replacement.
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/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <stdio.h>
22 #include <errno.h>
23 #include <unistd.h>
25 #include "freading.h"
26 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
27 # include "msvc-inval.h"
28 #endif
30 #undef fclose
32 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
33 static int
34 fclose_nothrow (FILE *fp)
36 int result;
38 TRY_MSVC_INVAL
40 result = fclose (fp);
42 CATCH_MSVC_INVAL
44 result = EOF;
45 errno = EBADF;
47 DONE_MSVC_INVAL;
49 return result;
51 #else
52 # define fclose_nothrow fclose
53 #endif
55 /* Override fclose() to call the overridden fflush() or close(). */
57 int
58 rpl_fclose (FILE *fp)
60 int saved_errno = 0;
61 int fd;
62 int result = 0;
64 /* Don't change behavior on memstreams. */
65 fd = fileno (fp);
66 if (fd < 0)
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)
73 && fflush (fp))
74 saved_errno = errno;
76 /* fclose() calls close(), but we need to also invoke all hooks that our
77 overridden close() function invokes. See lib/close.c. */
78 #if WINDOWS_SOCKETS
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)
83 saved_errno = errno;
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(). */
91 # if REPLACE_FCHDIR
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
94 _gl_unregister_fd. */
95 result = fclose_nothrow (fp);
96 if (result == 0)
97 _gl_unregister_fd (fd);
98 # else
99 /* No race condition here. */
100 result = fclose_nothrow (fp);
101 # endif
103 #endif /* !WINDOWS_SOCKETS */
105 if (saved_errno != 0)
107 errno = saved_errno;
108 result = EOF;
111 return result;