OpenCoverage

fclose.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/fclose.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* fclose replacement.-
2 Copyright (C) 2008-2018 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 <https://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#if HAVE_MSVC_INVALID_PARAMETER_HANDLER-
27# include "msvc-inval.h"-
28#endif-
29-
30#undef fclose-
31-
32#if HAVE_MSVC_INVALID_PARAMETER_HANDLER-
33static int-
34fclose_nothrow (FILE *fp)-
35{-
36 int result;-
37-
38 TRY_MSVC_INVAL-
39 {-
40 result = fclose (fp);-
41 }-
42 CATCH_MSVC_INVAL-
43 {-
44 result = EOF;-
45 errno = EBADF;-
46 }-
47 DONE_MSVC_INVAL;-
48-
49 return result;-
50}-
51#else-
52# define fclose_nothrow fclose-
53#endif-
54-
55/* Override fclose() to call the overridden fflush() or close(). */-
56-
57int-
58rpl_fclose (FILE *fp)-
59{-
60 int saved_errno = 0;-
61 int fd;-
62 int result = 0;-
63-
64 /* Don't change behavior on memstreams. */-
65 fd = fileno (fp);-
66 if (fd < 0)
fd < 0Description
TRUEnever evaluated
FALSEevaluated 90762 times by 106 tests
Evaluated by:
  • [
  • b2sum
  • base32
  • base64
  • basename
  • cat
  • chcon
  • chgrp
  • chmod
  • chown
  • chroot
  • cksum
  • comm
  • cp
  • csplit
  • cut
  • date
  • dd
  • df
  • dir
  • dircolors
  • dirname
  • du
  • echo
  • env
  • ...
0-90762
67 return fclose_nothrow (fp);
never executed: return fclose (fp);
0
68-
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)
!(__freading (fp) != 0)Description
TRUEevaluated 79246 times by 106 tests
Evaluated by:
  • [
  • b2sum
  • base32
  • base64
  • basename
  • cat
  • chcon
  • chgrp
  • chmod
  • chown
  • chroot
  • cksum
  • comm
  • cp
  • csplit
  • cut
  • date
  • dd
  • df
  • dir
  • dircolors
  • dirname
  • du
  • echo
  • env
  • ...
FALSEevaluated 11516 times by 39 tests
Evaluated by:
  • b2sum
  • base32
  • base64
  • cksum
  • comm
  • cp
  • cut
  • date
  • df
  • dircolors
  • du
  • expand
  • fmt
  • fold
  • ginstall
  • join
  • ln
  • md5sum
  • mv
  • nl
  • od
  • paste
  • pr
  • ptx
  • rm
  • ...
lseek (fileno ..., 0, 1 ) != -1Description
TRUEevaluated 8312 times by 39 tests
Evaluated by:
  • b2sum
  • base32
  • base64
  • cksum
  • comm
  • cp
  • cut
  • date
  • df
  • dircolors
  • du
  • expand
  • fmt
  • fold
  • ginstall
  • join
  • ln
  • md5sum
  • mv
  • nl
  • od
  • paste
  • pr
  • ptx
  • rm
  • ...
FALSEevaluated 3204 times by 25 tests
Evaluated by:
  • base32
  • base64
  • cksum
  • cp
  • cut
  • expand
  • fold
  • md5sum
  • mv
  • nl
  • od
  • paste
  • pr
  • rm
  • sha1sum
  • sha224sum
  • sha256sum
  • sha384sum
  • sha512sum
  • shuf
  • sort
  • sum
  • tsort
  • unexpand
  • uniq
3204-79246
73 && fflush (fp))
rpl_fflush (fp)Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • mktemp
FALSEevaluated 87554 times by 106 tests
Evaluated by:
  • [
  • b2sum
  • base32
  • base64
  • basename
  • cat
  • chcon
  • chgrp
  • chmod
  • chown
  • chroot
  • cksum
  • comm
  • cp
  • csplit
  • cut
  • date
  • dd
  • df
  • dir
  • dircolors
  • dirname
  • du
  • echo
  • env
  • ...
4-87554
74 saved_errno = errno;
executed 4 times by 1 test: saved_errno = (*__errno_location ()) ;
Executed by:
  • mktemp
4
75-
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;-
84-
85 fclose_nothrow (fp); /* will fail with errno = EBADF,-
86 if we did not lose a race */-
87-
88#else /* !WINDOWS_SOCKETS */-
89 /* Call fclose() and invoke all hooks of the overridden close(). */-
90-
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-
102-
103#endif /* !WINDOWS_SOCKETS */-
104-
105 if (saved_errno != 0)
saved_errno != 0Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • mktemp
FALSEevaluated 90758 times by 106 tests
Evaluated by:
  • [
  • b2sum
  • base32
  • base64
  • basename
  • cat
  • chcon
  • chgrp
  • chmod
  • chown
  • chroot
  • cksum
  • comm
  • cp
  • csplit
  • cut
  • date
  • dd
  • df
  • dir
  • dircolors
  • dirname
  • du
  • echo
  • env
  • ...
4-90758
106 {-
107 errno = saved_errno;-
108 result = EOF;-
109 }
executed 4 times by 1 test: end of block
Executed by:
  • mktemp
4
110-
111 return result;
executed 90762 times by 106 tests: return result;
Executed by:
  • [
  • b2sum
  • base32
  • base64
  • basename
  • cat
  • chcon
  • chgrp
  • chmod
  • chown
  • chroot
  • cksum
  • comm
  • cp
  • csplit
  • cut
  • date
  • dd
  • df
  • dir
  • dircolors
  • dirname
  • du
  • echo
  • env
  • ...
90762
112}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2