New upstream version 1.8
[debian/gzip] / lib / fclose.c
1 /* fclose replacement.
2    Copyright (C) 2008-2016 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 /* Specification.  */
20 #include <stdio.h>
21
22 #include <errno.h>
23 #include <unistd.h>
24
25 #include "freading.h"
26 #include "msvc-inval.h"
27
28 #undef fclose
29
30 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
31 static int
32 fclose_nothrow (FILE *fp)
33 {
34   int result;
35
36   TRY_MSVC_INVAL
37     {
38       result = fclose (fp);
39     }
40   CATCH_MSVC_INVAL
41     {
42       result = EOF;
43       errno = EBADF;
44     }
45   DONE_MSVC_INVAL;
46
47   return result;
48 }
49 #else
50 # define fclose_nothrow fclose
51 #endif
52
53 /* Override fclose() to call the overridden fflush() or close().  */
54
55 int
56 rpl_fclose (FILE *fp)
57 {
58   int saved_errno = 0;
59   int fd;
60   int result = 0;
61
62   /* Don't change behavior on memstreams.  */
63   fd = fileno (fp);
64   if (fd < 0)
65     return fclose_nothrow (fp);
66
67   /* We only need to flush the file if it is not reading or if it is
68      seekable.  This only guarantees the file position of input files
69      if the fflush module is also in use.  */
70   if ((!freading (fp) || lseek (fileno (fp), 0, SEEK_CUR) != -1)
71       && fflush (fp))
72     saved_errno = errno;
73
74   /* fclose() calls close(), but we need to also invoke all hooks that our
75      overridden close() function invokes.  See lib/close.c.  */
76 #if WINDOWS_SOCKETS
77   /* Call the overridden close(), then the original fclose().
78      Note about multithread-safety: There is a race condition where some
79      other thread could open fd between our close and fclose.  */
80   if (close (fd) < 0 && saved_errno == 0)
81     saved_errno = errno;
82
83   fclose_nothrow (fp); /* will fail with errno = EBADF,
84                           if we did not lose a race */
85
86 #else /* !WINDOWS_SOCKETS */
87   /* Call fclose() and invoke all hooks of the overridden close().  */
88
89 # if REPLACE_FCHDIR
90   /* Note about multithread-safety: There is a race condition here as well.
91      Some other thread could open fd between our calls to fclose and
92      _gl_unregister_fd.  */
93   result = fclose_nothrow (fp);
94   if (result == 0)
95     _gl_unregister_fd (fd);
96 # else
97   /* No race condition here.  */
98   result = fclose_nothrow (fp);
99 # endif
100
101 #endif /* !WINDOWS_SOCKETS */
102
103   if (saved_errno != 0)
104     {
105       errno = saved_errno;
106       result = EOF;
107     }
108
109   return result;
110 }