OpenCoverage

head.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/head.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* head -- output first part of file(s)-
2 Copyright (C) 1989-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/* Options: (see usage)-
18 Reads from standard input if no files are given or when a filename of-
19 ''-'' is encountered.-
20 By default, filename headers are printed only if more than one file-
21 is given.-
22 By default, prints the first 10 lines (head -n 10).-
23-
24 David MacKenzie <djm@gnu.ai.mit.edu> */-
25-
26#include <config.h>-
27-
28#include <stdio.h>-
29#include <getopt.h>-
30#include <sys/types.h>-
31-
32#include "system.h"-
33-
34#include "die.h"-
35#include "error.h"-
36#include "full-read.h"-
37#include "quote.h"-
38#include "safe-read.h"-
39#include "stat-size.h"-
40#include "xbinary-io.h"-
41#include "xdectoint.h"-
42-
43/* The official name of this program (e.g., no 'g' prefix). */-
44#define PROGRAM_NAME "head"-
45-
46#define AUTHORS \-
47 proper_name ("David MacKenzie"), \-
48 proper_name ("Jim Meyering")-
49-
50/* Number of lines/chars/blocks to head. */-
51#define DEFAULT_NUMBER 10-
52-
53/* Useful only when eliding tail bytes or lines.-
54 If true, skip the is-regular-file test used to determine whether-
55 to use the lseek optimization. Instead, use the more general (and-
56 more expensive) code unconditionally. Intended solely for testing. */-
57static bool presume_input_pipe;-
58-
59/* If true, print filename headers. */-
60static bool print_headers;-
61-
62/* Character to split lines by. */-
63static char line_end;-
64-
65/* When to print the filename banners. */-
66enum header_mode-
67{-
68 multiple_files, always, never-
69};-
70-
71/* Have we ever read standard input? */-
72static bool have_read_stdin;-
73-
74enum Copy_fd_status-
75 {-
76 COPY_FD_OK = 0,-
77 COPY_FD_READ_ERROR,-
78 COPY_FD_UNEXPECTED_EOF-
79 };-
80-
81/* For long options that have no equivalent short option, use a-
82 non-character as a pseudo short option, starting with CHAR_MAX + 1. */-
83enum-
84{-
85 PRESUME_INPUT_PIPE_OPTION = CHAR_MAX + 1-
86};-
87-
88static struct option const long_options[] =-
89{-
90 {"bytes", required_argument, NULL, 'c'},-
91 {"lines", required_argument, NULL, 'n'},-
92 {"-presume-input-pipe", no_argument, NULL,-
93 PRESUME_INPUT_PIPE_OPTION}, /* do not document */-
94 {"quiet", no_argument, NULL, 'q'},-
95 {"silent", no_argument, NULL, 'q'},-
96 {"verbose", no_argument, NULL, 'v'},-
97 {"zero-terminated", no_argument, NULL, 'z'},-
98 {GETOPT_HELP_OPTION_DECL},-
99 {GETOPT_VERSION_OPTION_DECL},-
100 {NULL, 0, NULL, 0}-
101};-
102-
103void-
104usage (int status)-
105{-
106 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • head
FALSEevaluated 10 times by 1 test
Evaluated by:
  • head
3-10
107 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • head
3
108 else-
109 {-
110 printf (_("\-
111Usage: %s [OPTION]... [FILE]...\n\-
112"),-
113 program_name);-
114 printf (_("\-
115Print the first %d lines of each FILE to standard output.\n\-
116With more than one FILE, precede each with a header giving the file name.\n\-
117"), DEFAULT_NUMBER);-
118-
119 emit_stdin_note ();-
120 emit_mandatory_arg_note ();-
121-
122 printf (_("\-
123 -c, --bytes=[-]NUM print the first NUM bytes of each file;\n\-
124 with the leading '-', print all but the last\n\-
125 NUM bytes of each file\n\-
126 -n, --lines=[-]NUM print the first NUM lines instead of the first %d;\n\-
127 with the leading '-', print all but the last\n\-
128 NUM lines of each file\n\-
129"), DEFAULT_NUMBER);-
130 fputs (_("\-
131 -q, --quiet, --silent never print headers giving file names\n\-
132 -v, --verbose always print headers giving file names\n\-
133"), stdout);-
134 fputs (_("\-
135 -z, --zero-terminated line delimiter is NUL, not newline\n\-
136"), stdout);-
137 fputs (HELP_OPTION_DESCRIPTION, stdout);-
138 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
139 fputs (_("\-
140\n\-
141NUM may have a multiplier suffix:\n\-
142b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\-
143GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\-
144"), stdout);-
145 emit_ancillary_info (PROGRAM_NAME);-
146 }
executed 10 times by 1 test: end of block
Executed by:
  • head
10
147 exit (status);
executed 13 times by 1 test: exit (status);
Executed by:
  • head
13
148}-
149-
150static void-
151diagnose_copy_fd_failure (enum Copy_fd_status err, char const *filename)-
152{-
153 switch (err)-
154 {-
155 case COPY_FD_READ_ERROR:
never executed: case COPY_FD_READ_ERROR:
0
156 error (0, errno, _("error reading %s"), quoteaf (filename));-
157 break;
never executed: break;
0
158 case COPY_FD_UNEXPECTED_EOF:
never executed: case COPY_FD_UNEXPECTED_EOF:
0
159 error (0, errno, _("%s: file has shrunk too much"), quotef (filename));-
160 break;
never executed: break;
0
161 default:
never executed: default:
0
162 abort ();
never executed: abort ();
0
163 }-
164}-
165-
166static void-
167write_header (const char *filename)-
168{-
169 static bool first_file = true;-
170-
171 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), filename);-
172 first_file = false;-
173}
never executed: end of block
0
174-
175/* Write N_BYTES from BUFFER to stdout.-
176 Exit immediately on error with a single diagnostic. */-
177-
178static void-
179xwrite_stdout (char const *buffer, size_t n_bytes)-
180{-
181 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
never executed: break;
(__builtin_exp...r++))) == (-1)Description
TRUEnever evaluated
FALSEnever evaluated
__cnt > 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_expe...write_end), 0)Description
TRUEnever evaluated
FALSEnever evaluated
n_bytes > 0Description
TRUEevaluated 3748 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
(__extension__...)))) < n_bytesDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • head
FALSEevaluated 3740 times by 1 test
Evaluated by:
  • head
__builtin_constant_p ( 1 )Description
TRUEevaluated 3748 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
__builtin_cons..._p ( n_bytes )Description
TRUEnever evaluated
FALSEevaluated 3748 times by 1 test
Evaluated by:
  • head
(size_t) ( 1 )...n_bytes ) <= 8Description
TRUEnever evaluated
FALSEnever evaluated
(size_t) ( 1 ) != 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( 1 )Description
TRUEevaluated 3748 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
(size_t) ( 1 ) == 0Description
TRUEnever evaluated
FALSEevaluated 3748 times by 1 test
Evaluated by:
  • head
__builtin_cons..._p ( n_bytes )Description
TRUEnever evaluated
FALSEevaluated 3748 times by 1 test
Evaluated by:
  • head
(size_t) ( n_bytes ) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-3748
182 {-
183 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */-
184 die (EXIT_FAILURE, errno, _("error writing %s"),-
185 quoteaf ("standard output"));-
186 }
never executed: end of block
0
187}
executed 3741 times by 1 test: end of block
Executed by:
  • head
3741
188-
189/* Copy no more than N_BYTES from file descriptor SRC_FD to stdout.-
190 Return an appropriate indication of success or read failure. */-
191-
192static enum Copy_fd_status-
193copy_fd (int src_fd, uintmax_t n_bytes)-
194{-
195 char buf[BUFSIZ];-
196 const size_t buf_size = sizeof (buf);-
197-
198 /* Copy the file contents. */-
199 while (0 < n_bytes)
0 < n_bytesDescription
TRUEevaluated 20 times by 1 test
Evaluated by:
  • head
FALSEevaluated 3 times by 1 test
Evaluated by:
  • head
3-20
200 {-
201 size_t n_to_read = MIN (buf_size, n_bytes);
(( buf_size )<( n_bytes ))Description
TRUEevaluated 17 times by 1 test
Evaluated by:
  • head
FALSEevaluated 3 times by 1 test
Evaluated by:
  • head
3-17
202 size_t n_read = safe_read (src_fd, buf, n_to_read);-
203 if (n_read == SAFE_READ_ERROR)
n_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 20 times by 1 test
Evaluated by:
  • head
0-20
204 return COPY_FD_READ_ERROR;
never executed: return COPY_FD_READ_ERROR;
0
205-
206 n_bytes -= n_read;-
207-
208 if (n_read == 0 && n_bytes != 0)
n_read == 0Description
TRUEnever evaluated
FALSEevaluated 20 times by 1 test
Evaluated by:
  • head
n_bytes != 0Description
TRUEnever evaluated
FALSEnever evaluated
0-20
209 return COPY_FD_UNEXPECTED_EOF;
never executed: return COPY_FD_UNEXPECTED_EOF;
0
210-
211 xwrite_stdout (buf, n_read);-
212 }
executed 16 times by 1 test: end of block
Executed by:
  • head
16
213-
214 return COPY_FD_OK;
executed 3 times by 1 test: return COPY_FD_OK;
Executed by:
  • head
3
215}-
216-
217/* Call lseek (FD, OFFSET, WHENCE), where file descriptor FD-
218 corresponds to the file FILENAME. WHENCE must be SEEK_SET or-
219 SEEK_CUR. Return the resulting offset. Give a diagnostic and-
220 return -1 if lseek fails. */-
221-
222static off_t-
223elseek (int fd, off_t offset, int whence, char const *filename)-
224{-
225 off_t new_offset = lseek (fd, offset, whence);-
226 char buf[INT_BUFSIZE_BOUND (offset)];-
227-
228 if (new_offset < 0)
new_offset < 0Description
TRUEnever evaluated
FALSEevaluated 2052 times by 1 test
Evaluated by:
  • head
0-2052
229 error (0, errno,
never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), whence == 0 ? "%s: cannot seek to offset %s" : "%s: cannot seek to relative offset %s" , 5) , quotearg_n_style_colon (0, shell_escape_quoting_style, filename), offtostr (offset, buf));
0
230 _(whence == SEEK_SET
never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), whence == 0 ? "%s: cannot seek to offset %s" : "%s: cannot seek to relative offset %s" , 5) , quotearg_n_style_colon (0, shell_escape_quoting_style, filename), offtostr (offset, buf));
0
231 ? N_("%s: cannot seek to offset %s")
never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), whence == 0 ? "%s: cannot seek to offset %s" : "%s: cannot seek to relative offset %s" , 5) , quotearg_n_style_colon (0, shell_escape_quoting_style, filename), offtostr (offset, buf));
0
232 : N_("%s: cannot seek to relative offset %s")),
never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), whence == 0 ? "%s: cannot seek to offset %s" : "%s: cannot seek to relative offset %s" , 5) , quotearg_n_style_colon (0, shell_escape_quoting_style, filename), offtostr (offset, buf));
0
233 quotef (filename),
never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), whence == 0 ? "%s: cannot seek to offset %s" : "%s: cannot seek to relative offset %s" , 5) , quotearg_n_style_colon (0, shell_escape_quoting_style, filename), offtostr (offset, buf));
0
234 offtostr (offset, buf));
never executed: error (0, (*__errno_location ()) , dcgettext (((void *)0), whence == 0 ? "%s: cannot seek to offset %s" : "%s: cannot seek to relative offset %s" , 5) , quotearg_n_style_colon (0, shell_escape_quoting_style, filename), offtostr (offset, buf));
0
235-
236 return new_offset;
executed 2052 times by 1 test: return new_offset;
Executed by:
  • head
2052
237}-
238-
239/* For an input file with name FILENAME and descriptor FD,-
240 output all but the last N_ELIDE_0 bytes.-
241 If CURRENT_POS is nonnegative, assume that the input file is-
242 positioned at CURRENT_POS and that it should be repositioned to-
243 just before the elided bytes before returning.-
244 Return true upon success.-
245 Give a diagnostic and return false upon error. */-
246static bool-
247elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0,-
248 off_t current_pos)-
249{-
250 size_t n_elide = n_elide_0;-
251 uintmax_t desired_pos = current_pos;-
252 bool ok = true;-
253-
254#ifndef HEAD_TAIL_PIPE_READ_BUFSIZE-
255# define HEAD_TAIL_PIPE_READ_BUFSIZE BUFSIZ-
256#endif-
257#define READ_BUFSIZE HEAD_TAIL_PIPE_READ_BUFSIZE-
258-
259 /* If we're eliding no more than this many bytes, then it's ok to allocate-
260 more memory in order to use a more time-efficient algorithm.-
261 FIXME: use a fraction of available memory instead, as in sort.-
262 FIXME: is this even worthwhile? */-
263#ifndef HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD-
264# define HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD 1024 * 1024-
265#endif-
266-
267#if HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD < 2 * READ_BUFSIZE-
268 "HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD must be at least 2 * READ_BUFSIZE"-
269#endif-
270-
271 if (SIZE_MAX < n_elide_0 + READ_BUFSIZE)
(1844674407370...elide_0 + 8192Description
TRUEnever evaluated
FALSEevaluated 955 times by 1 test
Evaluated by:
  • head
0-955
272 {-
273 char umax_buf[INT_BUFSIZE_BOUND (n_elide_0)];-
274 die (EXIT_FAILURE, 0, _("%s: number of bytes is too large"),-
275 umaxtostr (n_elide_0, umax_buf));-
276 }
never executed: end of block
0
277-
278 /* Two cases to consider...-
279 1) n_elide is small enough that we can afford to double-buffer:-
280 allocate 2 * (READ_BUFSIZE + n_elide) bytes-
281 2) n_elide is too big for that, so we allocate only-
282 (READ_BUFSIZE + n_elide) bytes-
283-
284 FIXME: profile, to see if double-buffering is worthwhile-
285-
286 CAUTION: do not fail (out of memory) when asked to elide-
287 a ridiculous amount, but when given only a small input. */-
288-
289 if (n_elide <= HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD)
n_elide <= 1024 * 1024Description
TRUEevaluated 954 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
1-954
290 {-
291 bool first = true;-
292 bool eof = false;-
293 size_t n_to_read = READ_BUFSIZE + n_elide;-
294 bool i;-
295 char *b[2];-
296 b[0] = xnmalloc (2, n_to_read);-
297 b[1] = b[0] + n_to_read;-
298-
299 for (i = false; ! eof ; i = !i)
! eofDescription
TRUEevaluated 954 times by 1 test
Evaluated by:
  • head
FALSEevaluated 952 times by 1 test
Evaluated by:
  • head
952-954
300 {-
301 size_t n_read = full_read (fd, b[i], n_to_read);-
302 size_t delta = 0;-
303 if (n_read < n_to_read)
n_read < n_to_readDescription
TRUEevaluated 952 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2 times by 1 test
Evaluated by:
  • head
2-952
304 {-
305 if (errno != 0)
(*__errno_location ()) != 0Description
TRUEnever evaluated
FALSEevaluated 952 times by 1 test
Evaluated by:
  • head
0-952
306 {-
307 error (0, errno, _("error reading %s"), quoteaf (filename));-
308 ok = false;-
309 break;
never executed: break;
0
310 }-
311-
312 /* reached EOF */-
313 if (n_read <= n_elide)
n_read <= n_elideDescription
TRUEevaluated 464 times by 1 test
Evaluated by:
  • head
FALSEevaluated 488 times by 1 test
Evaluated by:
  • head
464-488
314 {-
315 if (first)
firstDescription
TRUEevaluated 464 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-464
316 {-
317 /* The input is no larger than the number of bytes-
318 to elide. So there's nothing to output, and-
319 we're done. */-
320 }
executed 464 times by 1 test: end of block
Executed by:
  • head
464
321 else-
322 {-
323 delta = n_elide - n_read;-
324 }
never executed: end of block
0
325 }-
326 eof = true;-
327 }
executed 952 times by 1 test: end of block
Executed by:
  • head
952
328-
329 /* Output any (but maybe just part of the) elided data from-
330 the previous round. */-
331 if (! first)
! firstDescription
TRUEnever evaluated
FALSEevaluated 954 times by 1 test
Evaluated by:
  • head
0-954
332 {-
333 desired_pos += n_elide - delta;-
334 xwrite_stdout (b[!i] + READ_BUFSIZE, n_elide - delta);-
335 }
never executed: end of block
0
336 first = false;-
337-
338 if (n_elide < n_read)
n_elide < n_readDescription
TRUEevaluated 490 times by 1 test
Evaluated by:
  • head
FALSEevaluated 464 times by 1 test
Evaluated by:
  • head
464-490
339 {-
340 desired_pos += n_read - n_elide;-
341 xwrite_stdout (b[i], n_read - n_elide);-
342 }
executed 488 times by 1 test: end of block
Executed by:
  • head
488
343 }
executed 952 times by 1 test: end of block
Executed by:
  • head
952
344-
345 free (b[0]);-
346 }
executed 952 times by 1 test: end of block
Executed by:
  • head
952
347 else-
348 {-
349 /* Read blocks of size READ_BUFSIZE, until we've read at least n_elide-
350 bytes. Then, for each new buffer we read, also write an old one. */-
351-
352 bool eof = false;-
353 size_t n_read;-
354 bool buffered_enough;-
355 size_t i, i_next;-
356 char **b = NULL;-
357 /* Round n_elide up to a multiple of READ_BUFSIZE. */-
358 size_t rem = READ_BUFSIZE - (n_elide % READ_BUFSIZE);-
359 size_t n_elide_round = n_elide + rem;-
360 size_t n_bufs = n_elide_round / READ_BUFSIZE + 1;-
361 size_t n_alloc = 0;-
362 size_t n_array_alloc = 0;-
363-
364 buffered_enough = false;-
365 for (i = 0, i_next = 1; !eof; i = i_next, i_next = (i_next + 1) % n_bufs)
!eofDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
1
366 {-
367 if (n_array_alloc == i)
n_array_alloc == iDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-1
368 {-
369 /* reallocate between 16 and n_bufs entries. */-
370 if (n_array_alloc == 0)
n_array_alloc == 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-1
371 n_array_alloc = MIN (n_bufs, 16);
executed 1 time by 1 test: n_array_alloc = ((( n_bufs )<( 16 ))?( n_bufs ):( 16 )) ;
Executed by:
  • head
(( n_bufs )<( 16 ))Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
0-1
372 else if (n_array_alloc <= n_bufs / 2)
n_array_alloc <= n_bufs / 2Description
TRUEnever evaluated
FALSEnever evaluated
0
373 n_array_alloc *= 2;
never executed: n_array_alloc *= 2;
0
374 else-
375 n_array_alloc = n_bufs;
never executed: n_array_alloc = n_bufs;
0
376 b = xnrealloc (b, n_array_alloc, sizeof *b);-
377 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
378-
379 if (! buffered_enough)
! buffered_enoughDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-1
380 {-
381 b[i] = xmalloc (READ_BUFSIZE);-
382 n_alloc = i + 1;-
383 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
384 n_read = full_read (fd, b[i], READ_BUFSIZE);-
385 if (n_read < READ_BUFSIZE)
n_read < 8192Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-1
386 {-
387 if (errno != 0)
(*__errno_location ()) != 0Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
0-1
388 {-
389 error (0, errno, _("error reading %s"), quoteaf (filename));-
390 ok = false;-
391 goto free_mem;
never executed: goto free_mem;
0
392 }-
393 eof = true;-
394 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
395-
396 if (i + 1 == n_bufs)
i + 1 == n_bufsDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
0-1
397 buffered_enough = true;
never executed: buffered_enough = 1 ;
0
398-
399 if (buffered_enough)
buffered_enoughDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
0-1
400 {-
401 desired_pos += n_read;-
402 xwrite_stdout (b[i_next], n_read);-
403 }
never executed: end of block
0
404 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
405-
406 /* Output any remainder: rem bytes from b[i] + n_read. */-
407 if (rem)
remDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-1
408 {-
409 if (buffered_enough)
buffered_enoughDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
0-1
410 {-
411 size_t n_bytes_left_in_b_i = READ_BUFSIZE - n_read;-
412 desired_pos += rem;-
413 if (rem < n_bytes_left_in_b_i)
rem < n_bytes_left_in_b_iDescription
TRUEnever evaluated
FALSEnever evaluated
0
414 {-
415 xwrite_stdout (b[i] + n_read, rem);-
416 }
never executed: end of block
0
417 else-
418 {-
419 xwrite_stdout (b[i] + n_read, n_bytes_left_in_b_i);-
420 xwrite_stdout (b[i_next], rem - n_bytes_left_in_b_i);-
421 }
never executed: end of block
0
422 }-
423 else if (i + 1 == n_bufs)
i + 1 == n_bufsDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
0-1
424 {-
425 /* This happens when n_elide < file_size < n_elide_round.-
426-
427 |READ_BUF.|-
428 | | rem |-
429 |---------!---------!---------!---------|-
430 |---- n_elide ---------|-
431 | | x |-
432 | |y |-
433 |---- file size -----------|-
434 | |n_read|-
435 |---- n_elide_round ----------|-
436 */-
437 size_t y = READ_BUFSIZE - rem;-
438 size_t x = n_read - y;-
439 desired_pos += x;-
440 xwrite_stdout (b[i_next], x);-
441 }
never executed: end of block
0
442 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
443-
444 free_mem:
code before this statement executed 1 time by 1 test: free_mem:
Executed by:
  • head
1
445 for (i = 0; i < n_alloc; i++)
i < n_allocDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
1
446 free (b[i]);
executed 1 time by 1 test: free (b[i]);
Executed by:
  • head
1
447 free (b);-
448 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
449-
450 if (0 <= current_pos && elseek (fd, desired_pos, SEEK_SET, filename) < 0)
0 <= current_posDescription
TRUEevaluated 509 times by 1 test
Evaluated by:
  • head
FALSEevaluated 444 times by 1 test
Evaluated by:
  • head
elseek (fd, de... filename) < 0Description
TRUEnever evaluated
FALSEevaluated 509 times by 1 test
Evaluated by:
  • head
0-509
451 ok = false;
never executed: ok = 0 ;
0
452 return ok;
executed 953 times by 1 test: return ok;
Executed by:
  • head
953
453}-
454-
455/* For the file FILENAME with descriptor FD, output all but the last N_ELIDE-
456 bytes. If SIZE is nonnegative, this is a regular file positioned-
457 at CURRENT_POS with SIZE bytes. Return true on success.-
458 Give a diagnostic and return false upon error. */-
459-
460/* NOTE: if the input file shrinks by more than N_ELIDE bytes between-
461 the length determination and the actual reading, then head fails. */-
462-
463static bool-
464elide_tail_bytes_file (const char *filename, int fd, uintmax_t n_elide,-
465 struct stat const *st, off_t current_pos)-
466{-
467 off_t size = st->st_size;-
468 if (presume_input_pipe || current_pos < 0 || size <= ST_BLKSIZE (*st))
presume_input_pipeDescription
TRUEevaluated 441 times by 1 test
Evaluated by:
  • head
FALSEevaluated 518 times by 1 test
Evaluated by:
  • head
current_pos < 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • head
FALSEevaluated 513 times by 1 test
Evaluated by:
  • head
size <= ((0 < ...lksize : 512 )Description
TRUEevaluated 509 times by 1 test
Evaluated by:
  • head
FALSEevaluated 4 times by 1 test
Evaluated by:
  • head
0 < (*st).st_blksizeDescription
TRUEevaluated 513 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
(*st).st_blksi..._t)-1) / 8 + 1Description
TRUEevaluated 513 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-518
469 return elide_tail_bytes_pipe (filename, fd, n_elide, current_pos);
executed 955 times by 1 test: return elide_tail_bytes_pipe (filename, fd, n_elide, current_pos);
Executed by:
  • head
955
470 else-
471 {-
472 /* Be careful here. The current position may actually be-
473 beyond the end of the file. */-
474 off_t diff = size - current_pos;-
475 off_t bytes_remaining = diff < 0 ? 0 : diff;
diff < 0Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • head
0-4
476-
477 if (bytes_remaining <= n_elide)
bytes_remaining <= n_elideDescription
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • head
0-4
478 return true;
never executed: return 1 ;
0
479-
480 enum Copy_fd_status err = copy_fd (fd, bytes_remaining - n_elide);-
481 if (err == COPY_FD_OK)
err == COPY_FD_OKDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-2
482 return true;
executed 2 times by 1 test: return 1 ;
Executed by:
  • head
2
483-
484 diagnose_copy_fd_failure (err, filename);-
485 return false;
never executed: return 0 ;
0
486 }-
487}-
488-
489/* For an input file with name FILENAME and descriptor FD,-
490 output all but the last N_ELIDE_0 bytes.-
491 If CURRENT_POS is nonnegative, the input file is positioned there-
492 and should be repositioned to just before the elided bytes.-
493 Buffer the specified number of lines as a linked list of LBUFFERs,-
494 adding them as needed. Return true if successful. */-
495-
496static bool-
497elide_tail_lines_pipe (const char *filename, int fd, uintmax_t n_elide,-
498 off_t current_pos)-
499{-
500 struct linebuffer-
501 {-
502 char buffer[BUFSIZ];-
503 size_t nbytes;-
504 size_t nlines;-
505 struct linebuffer *next;-
506 };-
507 uintmax_t desired_pos = current_pos;-
508 typedef struct linebuffer LBUFFER;-
509 LBUFFER *first, *last, *tmp;-
510 size_t total_lines = 0; /* Total number of newlines in all buffers. */-
511 bool ok = true;-
512 size_t n_read; /* Size in bytes of most recent read */-
513-
514 first = last = xmalloc (sizeof (LBUFFER));-
515 first->nbytes = first->nlines = 0;-
516 first->next = NULL;-
517 tmp = xmalloc (sizeof (LBUFFER));-
518-
519 /* Always read into a fresh buffer.-
520 Read, (producing no output) until we've accumulated at least-
521 n_elide newlines, or until EOF, whichever comes first. */-
522 while (1)-
523 {-
524 n_read = safe_read (fd, tmp->buffer, BUFSIZ);-
525 if (n_read == 0 || n_read == SAFE_READ_ERROR)
n_read == 0Description
TRUEevaluated 976 times by 1 test
Evaluated by:
  • head
FALSEevaluated 934 times by 1 test
Evaluated by:
  • head
n_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 934 times by 1 test
Evaluated by:
  • head
0-976
526 break;
executed 976 times by 1 test: break;
Executed by:
  • head
976
527-
528 if (! n_elide)
! n_elideDescription
TRUEevaluated 45 times by 1 test
Evaluated by:
  • head
FALSEevaluated 889 times by 1 test
Evaluated by:
  • head
45-889
529 {-
530 desired_pos += n_read;-
531 xwrite_stdout (tmp->buffer, n_read);-
532 continue;
executed 44 times by 1 test: continue;
Executed by:
  • head
44
533 }-
534-
535 tmp->nbytes = n_read;-
536 tmp->nlines = 0;-
537 tmp->next = NULL;-
538-
539 /* Count the number of newlines just read. */-
540 {-
541 char const *buffer_end = tmp->buffer + n_read;-
542 char const *p = tmp->buffer;-
543 while ((p = memchr (p, line_end, buffer_end - p)))
(p = memchr (p...ffer_end - p))Description
TRUEevaluated 17858 times by 1 test
Evaluated by:
  • head
FALSEevaluated 889 times by 1 test
Evaluated by:
  • head
889-17858
544 {-
545 ++p;-
546 ++tmp->nlines;-
547 }
executed 17858 times by 1 test: end of block
Executed by:
  • head
17858
548 }-
549 total_lines += tmp->nlines;-
550-
551 /* If there is enough room in the last buffer read, just append the new-
552 one to it. This is because when reading from a pipe, 'n_read' can-
553 often be very small. */-
554 if (tmp->nbytes + last->nbytes < BUFSIZ)
tmp->nbytes + ...>nbytes < 8192Description
TRUEevaluated 887 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2 times by 1 test
Evaluated by:
  • head
2-887
555 {-
556 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);-
557 last->nbytes += tmp->nbytes;-
558 last->nlines += tmp->nlines;-
559 }
executed 887 times by 1 test: end of block
Executed by:
  • head
887
560 else-
561 {-
562 /* If there's not enough room, link the new buffer onto the end of-
563 the list, then either free up the oldest buffer for the next-
564 read if that would leave enough lines, or else malloc a new one.-
565 Some compaction mechanism is possible but probably not-
566 worthwhile. */-
567 last = last->next = tmp;-
568 if (n_elide < total_lines - first->nlines)
n_elide < tota... first->nlinesDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-2
569 {-
570 desired_pos += first->nbytes;-
571 xwrite_stdout (first->buffer, first->nbytes);-
572 tmp = first;-
573 total_lines -= first->nlines;-
574 first = first->next;-
575 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
576 else-
577 tmp = xmalloc (sizeof (LBUFFER));
never executed: tmp = xmalloc (sizeof (LBUFFER));
0
578 }-
579 }-
580-
581 free (tmp);-
582-
583 if (n_read == SAFE_READ_ERROR)
n_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 976 times by 1 test
Evaluated by:
  • head
0-976
584 {-
585 error (0, errno, _("error reading %s"), quoteaf (filename));-
586 ok = false;-
587 goto free_lbuffers;
never executed: goto free_lbuffers;
0
588 }-
589-
590 /* If we read any bytes at all, count the incomplete line-
591 on files that don't end with a newline. */-
592 if (last->nbytes && last->buffer[last->nbytes - 1] != line_end)
last->nbytesDescription
TRUEevaluated 887 times by 1 test
Evaluated by:
  • head
FALSEevaluated 89 times by 1 test
Evaluated by:
  • head
last->buffer[l...1] != line_endDescription
TRUEevaluated 44 times by 1 test
Evaluated by:
  • head
FALSEevaluated 843 times by 1 test
Evaluated by:
  • head
44-887
593 {-
594 ++last->nlines;-
595 ++total_lines;-
596 }
executed 44 times by 1 test: end of block
Executed by:
  • head
44
597-
598 for (tmp = first; n_elide < total_lines - tmp->nlines; tmp = tmp->next)
n_elide < tota... - tmp->nlinesDescription
TRUEnever evaluated
FALSEevaluated 976 times by 1 test
Evaluated by:
  • head
0-976
599 {-
600 desired_pos += tmp->nbytes;-
601 xwrite_stdout (tmp->buffer, tmp->nbytes);-
602 total_lines -= tmp->nlines;-
603 }
never executed: end of block
0
604-
605 /* Print the first 'total_lines - n_elide' lines of tmp->buffer. */-
606 if (n_elide < total_lines)
n_elide < total_linesDescription
TRUEevaluated 423 times by 1 test
Evaluated by:
  • head
FALSEevaluated 553 times by 1 test
Evaluated by:
  • head
423-553
607 {-
608 size_t n = total_lines - n_elide;-
609 char const *buffer_end = tmp->buffer + tmp->nbytes;-
610 char const *p = tmp->buffer;-
611 while (n && (p = memchr (p, line_end, buffer_end - p)))
nDescription
TRUEevaluated 3083 times by 1 test
Evaluated by:
  • head
FALSEevaluated 423 times by 1 test
Evaluated by:
  • head
(p = memchr (p...ffer_end - p))Description
TRUEevaluated 3083 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-3083
612 {-
613 ++p;-
614 ++tmp->nlines;-
615 --n;-
616 }
executed 3083 times by 1 test: end of block
Executed by:
  • head
3083
617 desired_pos += p - tmp->buffer;-
618 xwrite_stdout (tmp->buffer, p - tmp->buffer);-
619 }
executed 423 times by 1 test: end of block
Executed by:
  • head
423
620-
621free_lbuffers:
code before this statement executed 976 times by 1 test: free_lbuffers:
Executed by:
  • head
976
622 while (first)
firstDescription
TRUEevaluated 976 times by 1 test
Evaluated by:
  • head
FALSEevaluated 976 times by 1 test
Evaluated by:
  • head
976
623 {-
624 tmp = first->next;-
625 free (first);-
626 first = tmp;-
627 }
executed 976 times by 1 test: end of block
Executed by:
  • head
976
628-
629 if (0 <= current_pos && elseek (fd, desired_pos, SEEK_SET, filename) < 0)
0 <= current_posDescription
TRUEevaluated 492 times by 1 test
Evaluated by:
  • head
FALSEevaluated 484 times by 1 test
Evaluated by:
  • head
elseek (fd, de... filename) < 0Description
TRUEnever evaluated
FALSEevaluated 492 times by 1 test
Evaluated by:
  • head
0-492
630 ok = false;
never executed: ok = 0 ;
0
631 return ok;
executed 976 times by 1 test: return ok;
Executed by:
  • head
976
632}-
633-
634/* Output all but the last N_LINES lines of the input stream defined by-
635 FD, START_POS, and SIZE.-
636 START_POS is the starting position of the read pointer for the file-
637 associated with FD (may be nonzero).-
638 SIZE is the file size in bytes.-
639 Return true upon success.-
640 Give a diagnostic and return false upon error.-
641-
642 NOTE: this code is very similar to that of tail.c's file_lines function.-
643 Unfortunately, factoring out some common core looks like it'd result-
644 in a less efficient implementation or a messy interface. */-
645static bool-
646elide_tail_lines_seekable (const char *pretty_filename, int fd,-
647 uintmax_t n_lines,-
648 off_t start_pos, off_t size)-
649{-
650 char buffer[BUFSIZ];-
651 size_t bytes_read;-
652 off_t pos = size;-
653-
654 /* Set 'bytes_read' to the size of the last, probably partial, buffer;-
655 0 < 'bytes_read' <= 'BUFSIZ'. */-
656 bytes_read = (pos - start_pos) % BUFSIZ;-
657 if (bytes_read == 0)
bytes_read == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
1-2
658 bytes_read = BUFSIZ;
executed 2 times by 1 test: bytes_read = 8192 ;
Executed by:
  • head
2
659 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all-
660 reads will be on block boundaries, which might increase efficiency. */-
661 pos -= bytes_read;-
662 if (elseek (fd, pos, SEEK_SET, pretty_filename) < 0)
elseek (fd, po..._filename) < 0Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • head
0-3
663 return false;
never executed: return 0 ;
0
664 bytes_read = safe_read (fd, buffer, bytes_read);-
665 if (bytes_read == SAFE_READ_ERROR)
bytes_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • head
0-3
666 {-
667 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));-
668 return false;
never executed: return 0 ;
0
669 }-
670-
671 /* n_lines == 0 case needs special treatment. */-
672 const bool all_lines = !n_lines;-
673-
674 /* Count the incomplete line on files that don't end with a newline. */-
675 if (n_lines && bytes_read && buffer[bytes_read - 1] != line_end)
n_linesDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
bytes_readDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
buffer[bytes_r...1] != line_endDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • head
0-2
676 --n_lines;
never executed: --n_lines;
0
677-
678 while (1)-
679 {-
680 /* Scan backward, counting the newlines in this bufferfull. */-
681-
682 size_t n = bytes_read;-
683 while (n)
nDescription
TRUEevaluated 50040 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-50040
684 {-
685 if (all_lines)
all_linesDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • head
FALSEevaluated 50039 times by 1 test
Evaluated by:
  • head
1-50039
686 n -= 1;
executed 1 time by 1 test: n -= 1;
Executed by:
  • head
1
687 else-
688 {-
689 char const *nl;-
690 nl = memrchr (buffer, line_end, n);-
691 if (nl == NULL)
nl == ((void *)0)Description
TRUEevaluated 36 times by 1 test
Evaluated by:
  • head
FALSEevaluated 50003 times by 1 test
Evaluated by:
  • head
36-50003
692 break;
executed 36 times by 1 test: break;
Executed by:
  • head
36
693 n = nl - buffer;-
694 }
executed 50003 times by 1 test: end of block
Executed by:
  • head
50003
695 if (n_lines-- == 0)
n_lines-- == 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • head
FALSEevaluated 50001 times by 1 test
Evaluated by:
  • head
3-50001
696 {-
697 /* Found it. */-
698 /* If necessary, restore the file pointer and copy-
699 input to output up to position, POS. */-
700 if (start_pos < pos)
start_pos < posDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-3
701 {-
702 enum Copy_fd_status err;-
703 if (elseek (fd, start_pos, SEEK_SET, pretty_filename) < 0)
elseek (fd, st..._filename) < 0Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • head
0-3
704 return false;
never executed: return 0 ;
0
705-
706 err = copy_fd (fd, pos - start_pos);-
707 if (err != COPY_FD_OK)
err != COPY_FD_OKDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • head
0-1
708 {-
709 diagnose_copy_fd_failure (err, pretty_filename);-
710 return false;
never executed: return 0 ;
0
711 }-
712 }
executed 1 time by 1 test: end of block
Executed by:
  • head
1
713-
714 /* Output the initial portion of the buffer-
715 in which we found the desired newline byte. */-
716 xwrite_stdout (buffer, n + 1);-
717-
718 /* Set file pointer to the byte after what we've output. */-
719 return 0 <= elseek (fd, pos + n + 1, SEEK_SET, pretty_filename);
executed 1 time by 1 test: return 0 <= elseek (fd, pos + n + 1, 0 , pretty_filename);
Executed by:
  • head
1
720 }-
721 }
executed 50001 times by 1 test: end of block
Executed by:
  • head
50001
722-
723 /* Not enough newlines in that bufferfull. */-
724 if (pos == start_pos)
pos == start_posDescription
TRUEnever evaluated
FALSEevaluated 36 times by 1 test
Evaluated by:
  • head
0-36
725 {-
726 /* Not enough lines in the file. */-
727 return true;
never executed: return 1 ;
0
728 }-
729 pos -= BUFSIZ;-
730 if (elseek (fd, pos, SEEK_SET, pretty_filename) < 0)
elseek (fd, po..._filename) < 0Description
TRUEnever evaluated
FALSEevaluated 36 times by 1 test
Evaluated by:
  • head
0-36
731 return false;
never executed: return 0 ;
0
732-
733 bytes_read = safe_read (fd, buffer, BUFSIZ);-
734 if (bytes_read == SAFE_READ_ERROR)
bytes_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 36 times by 1 test
Evaluated by:
  • head
0-36
735 {-
736 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));-
737 return false;
never executed: return 0 ;
0
738 }-
739-
740 /* FIXME: is this dead code?-
741 Consider the test, pos == start_pos, above. */-
742 if (bytes_read == 0)
bytes_read == 0Description
TRUEnever evaluated
FALSEevaluated 36 times by 1 test
Evaluated by:
  • head
0-36
743 return true;
never executed: return 1 ;
0
744 }
executed 36 times by 1 test: end of block
Executed by:
  • head
36
745}
never executed: end of block
0
746-
747/* For the file FILENAME with descriptor FD, output all but the last N_ELIDE-
748 lines. If SIZE is nonnegative, this is a regular file positioned-
749 at START_POS with SIZE bytes. Return true on success.-
750 Give a diagnostic and return nonzero upon error. */-
751-
752static bool-
753elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide,-
754 struct stat const *st, off_t current_pos)-
755{-
756 off_t size = st->st_size;-
757 if (presume_input_pipe || current_pos < 0 || size <= ST_BLKSIZE (*st))
presume_input_pipeDescription
TRUEevaluated 484 times by 1 test
Evaluated by:
  • head
FALSEevaluated 497 times by 1 test
Evaluated by:
  • head
current_pos < 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • head
FALSEevaluated 495 times by 1 test
Evaluated by:
  • head
size <= ((0 < ...lksize : 512 )Description
TRUEevaluated 492 times by 1 test
Evaluated by:
  • head
FALSEevaluated 3 times by 1 test
Evaluated by:
  • head
0 < (*st).st_blksizeDescription
TRUEevaluated 495 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
(*st).st_blksi..._t)-1) / 8 + 1Description
TRUEevaluated 495 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
0-497
758 return elide_tail_lines_pipe (filename, fd, n_elide, current_pos);
executed 978 times by 1 test: return elide_tail_lines_pipe (filename, fd, n_elide, current_pos);
Executed by:
  • head
978
759 else-
760 {-
761 /* Find the offset, OFF, of the Nth newline from the end,-
762 but not counting the last byte of the file.-
763 If found, write from current position to OFF, inclusive.-
764 Otherwise, just return true. */-
765-
766 return (size <= current_pos
executed 3 times by 1 test: return (size <= current_pos || elide_tail_lines_seekable (filename, fd, n_elide, current_pos, size));
Executed by:
  • head
3
767 || elide_tail_lines_seekable (filename, fd, n_elide,
executed 3 times by 1 test: return (size <= current_pos || elide_tail_lines_seekable (filename, fd, n_elide, current_pos, size));
Executed by:
  • head
3
768 current_pos, size));
executed 3 times by 1 test: return (size <= current_pos || elide_tail_lines_seekable (filename, fd, n_elide, current_pos, size));
Executed by:
  • head
3
769 }-
770}-
771-
772static bool-
773head_bytes (const char *filename, int fd, uintmax_t bytes_to_write)-
774{-
775 char buffer[BUFSIZ];-
776 size_t bytes_to_read = BUFSIZ;-
777-
778 while (bytes_to_write)
bytes_to_writeDescription
TRUEevaluated 1577 times by 1 test
Evaluated by:
  • head
FALSEevaluated 140 times by 1 test
Evaluated by:
  • head
140-1577
779 {-
780 size_t bytes_read;-
781 if (bytes_to_write < bytes_to_read)
bytes_to_write < bytes_to_readDescription
TRUEevaluated 147 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1430 times by 1 test
Evaluated by:
  • head
147-1430
782 bytes_to_read = bytes_to_write;
executed 147 times by 1 test: bytes_to_read = bytes_to_write;
Executed by:
  • head
147
783 bytes_read = safe_read (fd, buffer, bytes_to_read);-
784 if (bytes_read == SAFE_READ_ERROR)
bytes_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 1577 times by 1 test
Evaluated by:
  • head
0-1577
785 {-
786 error (0, errno, _("error reading %s"), quoteaf (filename));-
787 return false;
never executed: return 0 ;
0
788 }-
789 if (bytes_read == 0)
bytes_read == 0Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1569 times by 1 test
Evaluated by:
  • head
8-1569
790 break;
executed 8 times by 1 test: break;
Executed by:
  • head
8
791 xwrite_stdout (buffer, bytes_read);-
792 bytes_to_write -= bytes_read;-
793 }
executed 1569 times by 1 test: end of block
Executed by:
  • head
1569
794 return true;
executed 148 times by 1 test: return 1 ;
Executed by:
  • head
148
795}-
796-
797static bool-
798head_lines (const char *filename, int fd, uintmax_t lines_to_write)-
799{-
800 char buffer[BUFSIZ];-
801-
802 while (lines_to_write)
lines_to_writeDescription
TRUEevaluated 1225 times by 1 test
Evaluated by:
  • head
FALSEevaluated 502 times by 1 test
Evaluated by:
  • head
502-1225
803 {-
804 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);-
805 size_t bytes_to_write = 0;-
806-
807 if (bytes_read == SAFE_READ_ERROR)
bytes_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 1225 times by 1 test
Evaluated by:
  • head
0-1225
808 {-
809 error (0, errno, _("error reading %s"), quoteaf (filename));-
810 return false;
never executed: return 0 ;
0
811 }-
812 if (bytes_read == 0)
bytes_read == 0Description
TRUEevaluated 26 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1199 times by 1 test
Evaluated by:
  • head
26-1199
813 break;
executed 26 times by 1 test: break;
Executed by:
  • head
26
814 while (bytes_to_write < bytes_read)
bytes_to_write < bytes_readDescription
TRUEevaluated 5611464 times by 1 test
Evaluated by:
  • head
FALSEevaluated 697 times by 1 test
Evaluated by:
  • head
697-5611464
815 if (buffer[bytes_to_write++] == line_end && --lines_to_write == 0)
buffer[bytes_t...+] == line_endDescription
TRUEevaluated 2709035 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2902429 times by 1 test
Evaluated by:
  • head
--lines_to_write == 0Description
TRUEevaluated 502 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2708533 times by 1 test
Evaluated by:
  • head
502-2902429
816 {-
817 off_t n_bytes_past_EOL = bytes_read - bytes_to_write;-
818 /* If we have read more data than that on the specified number-
819 of lines, try to seek back to the position we would have-
820 gotten to had we been reading one byte at a time. */-
821 if (lseek (fd, -n_bytes_past_EOL, SEEK_CUR) < 0)
lseek (fd, -n_...t_EOL, 1 ) < 0Description
TRUEevaluated 44 times by 1 test
Evaluated by:
  • head
FALSEevaluated 458 times by 1 test
Evaluated by:
  • head
44-458
822 {-
823 struct stat st;-
824 if (fstat (fd, &st) != 0 || S_ISREG (st.st_mode))
fstat (fd, &st) != 0Description
TRUEnever evaluated
FALSEevaluated 44 times by 1 test
Evaluated by:
  • head
(((( st.st_mod... == (0100000))Description
TRUEnever evaluated
FALSEevaluated 44 times by 1 test
Evaluated by:
  • head
0-44
825 elseek (fd, -n_bytes_past_EOL, SEEK_CUR, filename);
never executed: elseek (fd, -n_bytes_past_EOL, 1 , filename);
0
826 }
executed 44 times by 1 test: end of block
Executed by:
  • head
44
827 break;
executed 502 times by 1 test: break;
Executed by:
  • head
502
828 }-
829 xwrite_stdout (buffer, bytes_to_write);-
830 }
executed 1199 times by 1 test: end of block
Executed by:
  • head
1199
831 return true;
executed 528 times by 1 test: return 1 ;
Executed by:
  • head
528
832}-
833-
834static bool-
835head (const char *filename, int fd, uintmax_t n_units, bool count_lines,-
836 bool elide_from_end)-
837{-
838 if (print_headers)
print_headersDescription
TRUEnever evaluated
FALSEevaluated 2616 times by 1 test
Evaluated by:
  • head
0-2616
839 write_header (filename);
never executed: write_header (filename);
0
840-
841 if (elide_from_end)
elide_from_endDescription
TRUEevaluated 1940 times by 1 test
Evaluated by:
  • head
FALSEevaluated 676 times by 1 test
Evaluated by:
  • head
676-1940
842 {-
843 off_t current_pos = -1;-
844 struct stat st;-
845 if (fstat (fd, &st) != 0)
fstat (fd, &st) != 0Description
TRUEnever evaluated
FALSEevaluated 1940 times by 1 test
Evaluated by:
  • head
0-1940
846 {-
847 error (0, errno, _("cannot fstat %s"),-
848 quoteaf (filename));-
849 return false;
never executed: return 0 ;
0
850 }-
851 if (! presume_input_pipe && usable_st_size (&st))
! presume_input_pipeDescription
TRUEevaluated 1015 times by 1 test
Evaluated by:
  • head
FALSEevaluated 925 times by 1 test
Evaluated by:
  • head
usable_st_size (&st)Description
TRUEevaluated 1008 times by 1 test
Evaluated by:
  • head
FALSEevaluated 7 times by 1 test
Evaluated by:
  • head
7-1015
852 {-
853 current_pos = elseek (fd, 0, SEEK_CUR, filename);-
854 if (current_pos < 0)
current_pos < 0Description
TRUEnever evaluated
FALSEevaluated 1008 times by 1 test
Evaluated by:
  • head
0-1008
855 return false;
never executed: return 0 ;
0
856 }
executed 1008 times by 1 test: end of block
Executed by:
  • head
1008
857 if (count_lines)
count_linesDescription
TRUEevaluated 981 times by 1 test
Evaluated by:
  • head
FALSEevaluated 959 times by 1 test
Evaluated by:
  • head
959-981
858 return elide_tail_lines_file (filename, fd, n_units, &st, current_pos);
executed 981 times by 1 test: return elide_tail_lines_file (filename, fd, n_units, &st, current_pos);
Executed by:
  • head
981
859 else-
860 return elide_tail_bytes_file (filename, fd, n_units, &st, current_pos);
executed 959 times by 1 test: return elide_tail_bytes_file (filename, fd, n_units, &st, current_pos);
Executed by:
  • head
959
861 }-
862 if (count_lines)
count_linesDescription
TRUEevaluated 528 times by 1 test
Evaluated by:
  • head
FALSEevaluated 148 times by 1 test
Evaluated by:
  • head
148-528
863 return head_lines (filename, fd, n_units);
executed 528 times by 1 test: return head_lines (filename, fd, n_units);
Executed by:
  • head
528
864 else-
865 return head_bytes (filename, fd, n_units);
executed 148 times by 1 test: return head_bytes (filename, fd, n_units);
Executed by:
  • head
148
866}-
867-
868static bool-
869head_file (const char *filename, uintmax_t n_units, bool count_lines,-
870 bool elide_from_end)-
871{-
872 int fd;-
873 bool ok;-
874 bool is_stdin = STREQ (filename, "-");
never executed: __result = (((const unsigned char *) (const char *) ( filename ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "-" ))[3] - __s2[3]);
never executed: end of block
executed 276 times by 1 test: end of block
Executed by:
  • head
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEevaluated 2616 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
__result == 0Description
TRUEevaluated 276 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2340 times by 1 test
Evaluated by:
  • head
__s2_len > 1Description
TRUEnever evaluated
FALSEevaluated 276 times by 1 test
Evaluated by:
  • head
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-2616
875-
876 if (is_stdin)
is_stdinDescription
TRUEevaluated 276 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2340 times by 1 test
Evaluated by:
  • head
276-2340
877 {-
878 have_read_stdin = true;-
879 fd = STDIN_FILENO;-
880 filename = _("standard input");-
881 xset_binary_mode (STDIN_FILENO, O_BINARY);-
882 }
executed 276 times by 1 test: end of block
Executed by:
  • head
276
883 else-
884 {-
885 fd = open (filename, O_RDONLY | O_BINARY);-
886 if (fd < 0)
fd < 0Description
TRUEnever evaluated
FALSEevaluated 2340 times by 1 test
Evaluated by:
  • head
0-2340
887 {-
888 error (0, errno, _("cannot open %s for reading"), quoteaf (filename));-
889 return false;
never executed: return 0 ;
0
890 }-
891 }
executed 2340 times by 1 test: end of block
Executed by:
  • head
2340
892-
893 ok = head (filename, fd, n_units, count_lines, elide_from_end);-
894 if (!is_stdin && close (fd) != 0)
!is_stdinDescription
TRUEevaluated 2336 times by 1 test
Evaluated by:
  • head
FALSEevaluated 272 times by 1 test
Evaluated by:
  • head
close (fd) != 0Description
TRUEnever evaluated
FALSEevaluated 2336 times by 1 test
Evaluated by:
  • head
0-2336
895 {-
896 error (0, errno, _("failed to close %s"), quoteaf (filename));-
897 return false;
never executed: return 0 ;
0
898 }-
899 return ok;
executed 2608 times by 1 test: return ok;
Executed by:
  • head
2608
900}-
901-
902/* Convert a string of decimal digits, N_STRING, with an optional suffix-
903 to an integral value. Upon successful conversion,-
904 return that value. If it cannot be converted, give a diagnostic and exit.-
905 COUNT_LINES indicates whether N_STRING is a number of bytes or a number-
906 of lines. It is used solely to give a more specific diagnostic. */-
907-
908static uintmax_t-
909string_to_integer (bool count_lines, const char *n_string)-
910{-
911 return xdectoumax (n_string, 0, UINTMAX_MAX, "bkKmMGTPEZY0",
executed 2594 times by 1 test: return xdectoumax (n_string, 0, (18446744073709551615UL) , "bkKmMGTPEZY0", count_lines ? dcgettext (((void *)0), "invalid number of lines" , 5) : dcgettext (((void *)0), "invalid number of bytes" , 5) , 0);
Executed by:
  • head
2594
912 count_lines ? _("invalid number of lines")
executed 2594 times by 1 test: return xdectoumax (n_string, 0, (18446744073709551615UL) , "bkKmMGTPEZY0", count_lines ? dcgettext (((void *)0), "invalid number of lines" , 5) : dcgettext (((void *)0), "invalid number of bytes" , 5) , 0);
Executed by:
  • head
2594
913 : _("invalid number of bytes"), 0);
executed 2594 times by 1 test: return xdectoumax (n_string, 0, (18446744073709551615UL) , "bkKmMGTPEZY0", count_lines ? dcgettext (((void *)0), "invalid number of lines" , 5) : dcgettext (((void *)0), "invalid number of bytes" , 5) , 0);
Executed by:
  • head
2594
914}-
915-
916int-
917main (int argc, char **argv)-
918{-
919 enum header_mode header_mode = multiple_files;-
920 bool ok = true;-
921 int c;-
922 size_t i;-
923-
924 /* Number of items to print. */-
925 uintmax_t n_units = DEFAULT_NUMBER;-
926-
927 /* If true, interpret the numeric argument as the number of lines.-
928 Otherwise, interpret it as the number of bytes. */-
929 bool count_lines = true;-
930-
931 /* Elide the specified number of lines or bytes, counting from-
932 the end of the file. */-
933 bool elide_from_end = false;-
934-
935 /* Initializer for file_list if no file-arguments-
936 were specified on the command line. */-
937 static char const *const default_file_list[] = {"-", NULL};-
938 char const *const *file_list;-
939-
940 initialize_main (&argc, &argv);-
941 set_program_name (argv[0]);-
942 setlocale (LC_ALL, "");-
943 bindtextdomain (PACKAGE, LOCALEDIR);-
944 textdomain (PACKAGE);-
945-
946 atexit (close_stdout);-
947-
948 have_read_stdin = false;-
949-
950 print_headers = false;-
951-
952 line_end = '\n';-
953-
954 if (1 < argc && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
1 < argcDescription
TRUEevaluated 2623 times by 1 test
Evaluated by:
  • head
FALSEevaluated 18 times by 1 test
Evaluated by:
  • head
argv[1][0] == '-'Description
TRUEevaluated 2615 times by 1 test
Evaluated by:
  • head
FALSEevaluated 8 times by 1 test
Evaluated by:
  • head
((unsigned int...]) - '0' <= 9)Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2591 times by 1 test
Evaluated by:
  • head
8-2623
955 {-
956 char *a = argv[1];-
957 char *n_string = ++a;-
958 char *end_n_string;-
959 char multiplier_char = 0;-
960-
961 /* Old option syntax; a dash, one or more digits, and one or-
962 more option letters. Move past the number. */-
963 do ++a;
executed 36 times by 1 test: ++a;
Executed by:
  • head
36
964 while (ISDIGIT (*a));
((unsigned int...a) - '0' <= 9)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • head
FALSEevaluated 24 times by 1 test
Evaluated by:
  • head
12-24
965-
966 /* Pointer to the byte after the last digit. */-
967 end_n_string = a;-
968-
969 /* Parse any appended option letters. */-
970 for (; *a; a++)
*aDescription
TRUEevaluated 15 times by 1 test
Evaluated by:
  • head
FALSEevaluated 24 times by 1 test
Evaluated by:
  • head
15-24
971 {-
972 switch (*a)-
973 {-
974 case 'c':
executed 9 times by 1 test: case 'c':
Executed by:
  • head
9
975 count_lines = false;-
976 multiplier_char = 0;-
977 break;
executed 9 times by 1 test: break;
Executed by:
  • head
9
978-
979 case 'b':
executed 3 times by 1 test: case 'b':
Executed by:
  • head
3
980 case 'k':
executed 3 times by 1 test: case 'k':
Executed by:
  • head
3
981 case 'm':
never executed: case 'm':
0
982 count_lines = false;-
983 multiplier_char = *a;-
984 break;
executed 6 times by 1 test: break;
Executed by:
  • head
6
985-
986 case 'l':
never executed: case 'l':
0
987 count_lines = true;-
988 break;
never executed: break;
0
989-
990 case 'q':
never executed: case 'q':
0
991 header_mode = never;-
992 break;
never executed: break;
0
993-
994 case 'v':
never executed: case 'v':
0
995 header_mode = always;-
996 break;
never executed: break;
0
997-
998 case 'z':
never executed: case 'z':
0
999 line_end = '\0';-
1000 break;
never executed: break;
0
1001-
1002 default:
never executed: default:
0
1003 error (0, 0, _("invalid trailing option -- %c"), *a);-
1004 usage (EXIT_FAILURE);-
1005 }
never executed: end of block
0
1006 }-
1007-
1008 /* Append the multiplier character (if any) onto the end of-
1009 the digit string. Then add NUL byte if necessary. */-
1010 *end_n_string = multiplier_char;-
1011 if (multiplier_char)
multiplier_charDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • head
FALSEevaluated 18 times by 1 test
Evaluated by:
  • head
6-18
1012 *(++end_n_string) = 0;
executed 6 times by 1 test: *(++end_n_string) = 0;
Executed by:
  • head
6
1013-
1014 n_units = string_to_integer (count_lines, n_string);-
1015-
1016 /* Make the options we just parsed invisible to getopt. */-
1017 argv[1] = argv[0];-
1018 argv++;-
1019 argc--;-
1020 }
executed 24 times by 1 test: end of block
Executed by:
  • head
24
1021-
1022 while ((c = getopt_long (argc, argv, "c:n:qvz0123456789", long_options, NULL))
(c = getopt_lo... *)0) )) != -1Description
TRUEevaluated 3529 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2616 times by 1 test
Evaluated by:
  • head
2616-3529
1023 != -1)
(c = getopt_lo... *)0) )) != -1Description
TRUEevaluated 3529 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2616 times by 1 test
Evaluated by:
  • head
2616-3529
1024 {-
1025 switch (c)-
1026 {-
1027 case PRESUME_INPUT_PIPE_OPTION:
executed 925 times by 1 test: case PRESUME_INPUT_PIPE_OPTION:
Executed by:
  • head
925
1028 presume_input_pipe = true;-
1029 break;
executed 925 times by 1 test: break;
Executed by:
  • head
925
1030-
1031 case 'c':
executed 1094 times by 1 test: case 'c':
Executed by:
  • head
1094
1032 count_lines = false;-
1033 elide_from_end = (*optarg == '-');-
1034 if (elide_from_end)
elide_from_endDescription
TRUEevaluated 961 times by 1 test
Evaluated by:
  • head
FALSEevaluated 133 times by 1 test
Evaluated by:
  • head
133-961
1035 ++optarg;
executed 961 times by 1 test: ++optarg;
Executed by:
  • head
961
1036 n_units = string_to_integer (count_lines, optarg);-
1037 break;
executed 1092 times by 1 test: break;
Executed by:
  • head
1092
1038-
1039 case 'n':
executed 1476 times by 1 test: case 'n':
Executed by:
  • head
1476
1040 count_lines = true;-
1041 elide_from_end = (*optarg == '-');-
1042 if (elide_from_end)
elide_from_endDescription
TRUEevaluated 983 times by 1 test
Evaluated by:
  • head
FALSEevaluated 493 times by 1 test
Evaluated by:
  • head
493-983
1043 ++optarg;
executed 983 times by 1 test: ++optarg;
Executed by:
  • head
983
1044 n_units = string_to_integer (count_lines, optarg);-
1045 break;
executed 1474 times by 1 test: break;
Executed by:
  • head
1474
1046-
1047 case 'q':
executed 3 times by 1 test: case 'q':
Executed by:
  • head
3
1048 header_mode = never;-
1049 break;
executed 3 times by 1 test: break;
Executed by:
  • head
3
1050-
1051 case 'v':
executed 2 times by 1 test: case 'v':
Executed by:
  • head
2
1052 header_mode = always;-
1053 break;
executed 2 times by 1 test: break;
Executed by:
  • head
2
1054-
1055 case 'z':
executed 8 times by 1 test: case 'z':
Executed by:
  • head
8
1056 line_end = '\0';-
1057 break;
executed 8 times by 1 test: break;
Executed by:
  • head
8
1058-
1059 case_GETOPT_HELP_CHAR;
never executed: break;
executed 10 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • head
0-10
1060-
1061 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 8 times by 1 test: exit ( 0 );
Executed by:
  • head
never executed: break;
executed 8 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • head
0-8
1062-
1063 default:
executed 3 times by 1 test: default:
Executed by:
  • head
3
1064 if (ISDIGIT (c))
((unsigned int...c) - '0' <= 9)Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • head
0-3
1065 error (0, 0, _("invalid trailing option -- %c"), c);
never executed: error (0, 0, dcgettext (((void *)0), "invalid trailing option -- %c" , 5) , c);
0
1066 usage (EXIT_FAILURE);-
1067 }
never executed: end of block
0
1068 }-
1069-
1070 if (header_mode == always
header_mode == alwaysDescription
TRUEnever evaluated
FALSEevaluated 2616 times by 1 test
Evaluated by:
  • head
0-2616
1071 || (header_mode == multiple_files && optind < argc - 1))
header_mode == multiple_filesDescription
TRUEevaluated 2616 times by 1 test
Evaluated by:
  • head
FALSEnever evaluated
optind < argc - 1Description
TRUEnever evaluated
FALSEevaluated 2616 times by 1 test
Evaluated by:
  • head
0-2616
1072 print_headers = true;
never executed: print_headers = 1 ;
0
1073-
1074 if ( ! count_lines && elide_from_end && OFF_T_MAX < n_units)
! count_linesDescription
TRUEevaluated 1107 times by 1 test
Evaluated by:
  • head
FALSEevaluated 1509 times by 1 test
Evaluated by:
  • head
elide_from_endDescription
TRUEevaluated 959 times by 1 test
Evaluated by:
  • head
FALSEevaluated 148 times by 1 test
Evaluated by:
  • head
((off_t) (! (!...1))) < n_unitsDescription
TRUEnever evaluated
FALSEevaluated 959 times by 1 test
Evaluated by:
  • head
0-1509
1075 {-
1076 char umax_buf[INT_BUFSIZE_BOUND (n_units)];-
1077 die (EXIT_FAILURE, EOVERFLOW, "%s: %s", _("invalid number of bytes"),-
1078 quote (umaxtostr (n_units, umax_buf)));-
1079 }
never executed: end of block
0
1080-
1081 file_list = (optind < argc
optind < argcDescription
TRUEevaluated 2340 times by 1 test
Evaluated by:
  • head
FALSEevaluated 276 times by 1 test
Evaluated by:
  • head
276-2340
1082 ? (char const *const *) &argv[optind]-
1083 : default_file_list);-
1084-
1085 xset_binary_mode (STDOUT_FILENO, O_BINARY);-
1086-
1087 for (i = 0; file_list[i]; ++i)
file_list[i]Description
TRUEevaluated 2616 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2608 times by 1 test
Evaluated by:
  • head
2608-2616
1088 ok &= head_file (file_list[i], n_units, count_lines, elide_from_end);
executed 2616 times by 1 test: ok &= head_file (file_list[i], n_units, count_lines, elide_from_end);
Executed by:
  • head
2616
1089-
1090 if (have_read_stdin && close (STDIN_FILENO) < 0)
have_read_stdinDescription
TRUEevaluated 272 times by 1 test
Evaluated by:
  • head
FALSEevaluated 2336 times by 1 test
Evaluated by:
  • head
close ( 0 ) < 0Description
TRUEnever evaluated
FALSEevaluated 272 times by 1 test
Evaluated by:
  • head
0-2336
1091 die (EXIT_FAILURE, errno, "-");
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), \"-\"), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ()) , "-"), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , "-"), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
1092-
1093 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
executed 2608 times by 1 test: return ok ? 0 : 1 ;
Executed by:
  • head
2608
1094}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2