OpenCoverage

freadseek.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/freadseek.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* Skipping input from a FILE stream.-
2 Copyright (C) 2007-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 "freadseek.h"-
21-
22#include <stdlib.h>-
23#include <unistd.h>-
24-
25#include "freadahead.h"-
26#include "freadptr.h"-
27-
28#include "stdio-impl.h"-
29-
30/* Increment the in-memory pointer. INCREMENT must be at most the buffer size-
31 returned by freadptr().-
32 This is very cheap (no system calls). */-
33static void-
34freadptrinc (FILE *fp, size_t increment)-
35{-
36 /* Keep this code in sync with freadptr! */-
37#if HAVE___FREADPTRINC /* musl libc */-
38 __freadptrinc (fp, increment);-
39#elif defined _IO_EOF_SEEN || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */-
40 fp->_IO_read_ptr += increment;-
41#elif defined __sferror || defined __DragonFly__ || defined __ANDROID__-
42 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */-
43 fp_->_p += increment;-
44 fp_->_r -= increment;-
45#elif defined __EMX__ /* emx+gcc */-
46 fp->_ptr += increment;-
47 fp->_rcount -= increment;-
48#elif defined __minix /* Minix */-
49 fp_->_ptr += increment;-
50 fp_->_count -= increment;-
51#elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */-
52 fp_->_ptr += increment;-
53 fp_->_cnt -= increment;-
54#elif defined __UCLIBC__ /* uClibc */-
55# ifdef __STDIO_BUFFERS-
56 fp->__bufpos += increment;-
57# else-
58 abort ();-
59# endif-
60#elif defined __QNX__ /* QNX */-
61 fp->_Next += increment;-
62#elif defined __MINT__ /* Atari FreeMiNT */-
63 fp->__bufp += increment;-
64#elif defined EPLAN9 /* Plan9 */-
65 fp->rp += increment;-
66#elif defined SLOW_BUT_NO_HACKS /* users can define this */-
67#else-
68 #error "Please port gnulib freadseek.c to your platform! Look at the definition of getc, getc_unlocked on your system, then report this to bug-gnulib."-
69#endif-
70}
executed 110 times by 1 test: end of block
Executed by:
  • cut
110
71-
72int-
73freadseek (FILE *fp, size_t offset)-
74{-
75 size_t total_buffered;-
76 int fd;-
77-
78 if (offset == 0)
offset == 0Description
TRUEnever evaluated
FALSEevaluated 110 times by 1 test
Evaluated by:
  • cut
0-110
79 return 0;
never executed: return 0;
0
80-
81 /* Seek over the already read and buffered input as quickly as possible,-
82 without doing any system calls. */-
83 total_buffered = freadahead (fp);-
84 /* This loop is usually executed at most twice: once for ungetc buffer (if-
85 present) and once for the main buffer. */-
86 while (total_buffered > 0)
total_buffered > 0Description
TRUEevaluated 110 times by 1 test
Evaluated by:
  • cut
FALSEnever evaluated
0-110
87 {-
88 size_t buffered;-
89-
90 if (freadptr (fp, &buffered) != NULL && buffered > 0)
freadptr (fp, ...!= ((void *)0)Description
TRUEevaluated 110 times by 1 test
Evaluated by:
  • cut
FALSEnever evaluated
buffered > 0Description
TRUEevaluated 110 times by 1 test
Evaluated by:
  • cut
FALSEnever evaluated
0-110
91 {-
92 size_t increment = (buffered < offset ? buffered : offset);
buffered < offsetDescription
TRUEnever evaluated
FALSEevaluated 110 times by 1 test
Evaluated by:
  • cut
0-110
93-
94 freadptrinc (fp, increment);-
95 offset -= increment;-
96 if (offset == 0)
offset == 0Description
TRUEevaluated 110 times by 1 test
Evaluated by:
  • cut
FALSEnever evaluated
0-110
97 return 0;
executed 110 times by 1 test: return 0;
Executed by:
  • cut
110
98 total_buffered -= increment;-
99 if (total_buffered == 0)
total_buffered == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
100 break;
never executed: break;
0
101 }
never executed: end of block
0
102 /* Read one byte. If we were reading from the ungetc buffer, this-
103 switches the stream back to the main buffer. */-
104 if (fgetc (fp) == EOF)
fgetc (fp) == (-1)Description
TRUEnever evaluated
FALSEnever evaluated
0
105 goto eof;
never executed: goto eof;
0
106 offset--;-
107 if (offset == 0)
offset == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
108 return 0;
never executed: return 0;
0
109 total_buffered--;-
110 }
never executed: end of block
0
111-
112 /* Test whether the stream is seekable or not. */-
113 fd = fileno (fp);-
114 if (fd >= 0 && lseek (fd, 0, SEEK_CUR) >= 0)
fd >= 0Description
TRUEnever evaluated
FALSEnever evaluated
lseek (fd, 0, 1 ) >= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
115 {-
116 /* FP refers to a regular file. fseek is most efficient in this case. */-
117 return fseeko (fp, offset, SEEK_CUR);
never executed: return rpl_fseeko (fp, offset, 1 );
0
118 }-
119 else-
120 {-
121 /* FP is a non-seekable stream, possibly not even referring to a file-
122 descriptor. Read OFFSET bytes explicitly and discard them. */-
123 char buf[4096];-
124-
125 do-
126 {-
127 size_t count = (sizeof (buf) < offset ? sizeof (buf) : offset);
sizeof (buf) < offsetDescription
TRUEnever evaluated
FALSEnever evaluated
0
128 if (fread (buf, 1, count, fp) < count)
fread (buf, 1,...t, fp) < countDescription
TRUEnever evaluated
FALSEnever evaluated
0
129 goto eof;
never executed: goto eof;
0
130 offset -= count;-
131 }
never executed: end of block
0
132 while (offset > 0);
offset > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
133-
134 return 0;
never executed: return 0;
0
135 }-
136-
137 eof:-
138 /* EOF, or error before or while reading. */-
139 if (ferror (fp))
ferror (fp)Description
TRUEnever evaluated
FALSEnever evaluated
0
140 return EOF;
never executed: return (-1) ;
0
141 else-
142 /* Encountered EOF. */-
143 return 0;
never executed: return 0;
0
144}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2