ptx: fix an invalid heap reference with short --width
[coreutils.git] / src / head.c
blob21ace70b415f3e7a6a1673f005a214fad037ef3c
1 /* head -- output first part of file(s)
2 Copyright (C) 1989-2016 Free Software Foundation, Inc.
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.
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.
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/>. */
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).
24 David MacKenzie <djm@gnu.ai.mit.edu> */
26 #include <config.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <sys/types.h>
32 #include "system.h"
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 "xfreopen.h"
41 #include "xdectoint.h"
43 /* The official name of this program (e.g., no 'g' prefix). */
44 #define PROGRAM_NAME "head"
46 #define AUTHORS \
47 proper_name ("David MacKenzie"), \
48 proper_name ("Jim Meyering")
50 /* Number of lines/chars/blocks to head. */
51 #define DEFAULT_NUMBER 10
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. */
57 static bool presume_input_pipe;
59 /* If true, print filename headers. */
60 static bool print_headers;
62 /* Character to split lines by. */
63 static char line_end;
65 /* When to print the filename banners. */
66 enum header_mode
68 multiple_files, always, never
71 /* Have we ever read standard input? */
72 static bool have_read_stdin;
74 enum Copy_fd_status
76 COPY_FD_OK = 0,
77 COPY_FD_READ_ERROR,
78 COPY_FD_UNEXPECTED_EOF
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. */
83 enum
85 PRESUME_INPUT_PIPE_OPTION = CHAR_MAX + 1
88 static struct option const long_options[] =
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}
103 void
104 usage (int status)
106 if (status != EXIT_SUCCESS)
107 emit_try_help ();
108 else
110 printf (_("\
111 Usage: %s [OPTION]... [FILE]...\n\
113 program_name);
114 printf (_("\
115 Print the first %d lines of each FILE to standard output.\n\
116 With more than one FILE, precede each with a header giving the file name.\n\
117 "), DEFAULT_NUMBER);
119 emit_stdin_note ();
120 emit_mandatory_arg_note ();
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 (_("\
141 NUM may have a multiplier suffix:\n\
142 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
143 GB 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);
147 exit (status);
150 static void
151 diagnose_copy_fd_failure (enum Copy_fd_status err, char const *filename)
153 switch (err)
155 case COPY_FD_READ_ERROR:
156 error (0, errno, _("error reading %s"), quoteaf (filename));
157 break;
158 case COPY_FD_UNEXPECTED_EOF:
159 error (0, errno, _("%s: file has shrunk too much"), quotef (filename));
160 break;
161 default:
162 abort ();
166 static void
167 write_header (const char *filename)
169 static bool first_file = true;
171 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), filename);
172 first_file = false;
175 /* Write N_BYTES from BUFFER to stdout.
176 Exit immediately on error with a single diagnostic. */
178 static void
179 xwrite_stdout (char const *buffer, size_t n_bytes)
181 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
183 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
184 die (EXIT_FAILURE, errno, _("error writing %s"),
185 quoteaf ("standard output"));
189 /* Copy no more than N_BYTES from file descriptor SRC_FD to stdout.
190 Return an appropriate indication of success or read failure. */
192 static enum Copy_fd_status
193 copy_fd (int src_fd, uintmax_t n_bytes)
195 char buf[BUFSIZ];
196 const size_t buf_size = sizeof (buf);
198 /* Copy the file contents. */
199 while (0 < n_bytes)
201 size_t n_to_read = MIN (buf_size, n_bytes);
202 size_t n_read = safe_read (src_fd, buf, n_to_read);
203 if (n_read == SAFE_READ_ERROR)
204 return COPY_FD_READ_ERROR;
206 n_bytes -= n_read;
208 if (n_read == 0 && n_bytes != 0)
209 return COPY_FD_UNEXPECTED_EOF;
211 xwrite_stdout (buf, n_read);
214 return COPY_FD_OK;
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. */
222 static off_t
223 elseek (int fd, off_t offset, int whence, char const *filename)
225 off_t new_offset = lseek (fd, offset, whence);
226 char buf[INT_BUFSIZE_BOUND (offset)];
228 if (new_offset < 0)
229 error (0, errno,
230 _(whence == SEEK_SET
231 ? N_("%s: cannot seek to offset %s")
232 : N_("%s: cannot seek to relative offset %s")),
233 quotef (filename),
234 offtostr (offset, buf));
236 return new_offset;
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. */
246 static bool
247 elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0,
248 off_t current_pos)
250 size_t n_elide = n_elide_0;
251 uintmax_t desired_pos = current_pos;
252 bool ok = true;
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
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
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
271 if (SIZE_MAX < n_elide_0 + READ_BUFSIZE)
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));
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
284 FIXME: profile, to see if double-buffering is worthwhile
286 CAUTION: do not fail (out of memory) when asked to elide
287 a ridiculous amount, but when given only a small input. */
289 if (n_elide <= HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD)
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;
299 for (i = false; ! eof ; i = !i)
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)
305 if (errno != 0)
307 error (0, errno, _("error reading %s"), quoteaf (filename));
308 ok = false;
309 break;
312 /* reached EOF */
313 if (n_read <= n_elide)
315 if (first)
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. */
321 else
323 delta = n_elide - n_read;
326 eof = true;
329 /* Output any (but maybe just part of the) elided data from
330 the previous round. */
331 if (! first)
333 desired_pos += n_elide - delta;
334 xwrite_stdout (b[!i] + READ_BUFSIZE, n_elide - delta);
336 first = false;
338 if (n_elide < n_read)
340 desired_pos += n_read - n_elide;
341 xwrite_stdout (b[i], n_read - n_elide);
345 free (b[0]);
347 else
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. */
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;
364 buffered_enough = false;
365 for (i = 0, i_next = 1; !eof; i = i_next, i_next = (i_next + 1) % n_bufs)
367 if (n_array_alloc == i)
369 /* reallocate between 16 and n_bufs entries. */
370 if (n_array_alloc == 0)
371 n_array_alloc = MIN (n_bufs, 16);
372 else if (n_array_alloc <= n_bufs / 2)
373 n_array_alloc *= 2;
374 else
375 n_array_alloc = n_bufs;
376 b = xnrealloc (b, n_array_alloc, sizeof *b);
379 if (! buffered_enough)
381 b[i] = xmalloc (READ_BUFSIZE);
382 n_alloc = i + 1;
384 n_read = full_read (fd, b[i], READ_BUFSIZE);
385 if (n_read < READ_BUFSIZE)
387 if (errno != 0)
389 error (0, errno, _("error reading %s"), quoteaf (filename));
390 ok = false;
391 goto free_mem;
393 eof = true;
396 if (i + 1 == n_bufs)
397 buffered_enough = true;
399 if (buffered_enough)
401 desired_pos += n_read;
402 xwrite_stdout (b[i_next], n_read);
406 /* Output any remainder: rem bytes from b[i] + n_read. */
407 if (rem)
409 if (buffered_enough)
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)
415 xwrite_stdout (b[i] + n_read, rem);
417 else
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);
423 else if (i + 1 == n_bufs)
425 /* This happens when n_elide < file_size < n_elide_round.
427 |READ_BUF.|
428 | | rem |
429 |---------!---------!---------!---------|
430 |---- n_elide ---------|
431 | | x |
432 | |y |
433 |---- file size -----------|
434 | |n_read|
435 |---- n_elide_round ----------|
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);
444 free_mem:
445 for (i = 0; i < n_alloc; i++)
446 free (b[i]);
447 free (b);
450 if (0 <= current_pos && elseek (fd, desired_pos, SEEK_SET, filename) < 0)
451 ok = false;
452 return ok;
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. */
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. */
463 static bool
464 elide_tail_bytes_file (const char *filename, int fd, uintmax_t n_elide,
465 struct stat const *st, off_t current_pos)
467 off_t size = st->st_size;
468 if (presume_input_pipe || size <= ST_BLKSIZE (*st))
469 return elide_tail_bytes_pipe (filename, fd, n_elide, current_pos);
470 else
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;
477 if (bytes_remaining <= n_elide)
478 return true;
480 enum Copy_fd_status err = copy_fd (fd, bytes_remaining - n_elide);
481 if (err == COPY_FD_OK)
482 return true;
484 diagnose_copy_fd_failure (err, filename);
485 return false;
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. */
496 static bool
497 elide_tail_lines_pipe (const char *filename, int fd, uintmax_t n_elide,
498 off_t current_pos)
500 struct linebuffer
502 char buffer[BUFSIZ];
503 size_t nbytes;
504 size_t nlines;
505 struct linebuffer *next;
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 */
514 first = last = xmalloc (sizeof (LBUFFER));
515 first->nbytes = first->nlines = 0;
516 first->next = NULL;
517 tmp = xmalloc (sizeof (LBUFFER));
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)
524 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
525 if (n_read == 0 || n_read == SAFE_READ_ERROR)
526 break;
528 if (! n_elide)
530 desired_pos += n_read;
531 xwrite_stdout (tmp->buffer, n_read);
532 continue;
535 tmp->nbytes = n_read;
536 tmp->nlines = 0;
537 tmp->next = NULL;
539 /* Count the number of newlines just read. */
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)))
545 ++p;
546 ++tmp->nlines;
549 total_lines += tmp->nlines;
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)
556 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
557 last->nbytes += tmp->nbytes;
558 last->nlines += tmp->nlines;
560 else
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)
570 desired_pos += first->nbytes;
571 xwrite_stdout (first->buffer, first->nbytes);
572 tmp = first;
573 total_lines -= first->nlines;
574 first = first->next;
576 else
577 tmp = xmalloc (sizeof (LBUFFER));
581 free (tmp);
583 if (n_read == SAFE_READ_ERROR)
585 error (0, errno, _("error reading %s"), quoteaf (filename));
586 ok = false;
587 goto free_lbuffers;
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)
594 ++last->nlines;
595 ++total_lines;
598 for (tmp = first; n_elide < total_lines - tmp->nlines; tmp = tmp->next)
600 desired_pos += tmp->nbytes;
601 xwrite_stdout (tmp->buffer, tmp->nbytes);
602 total_lines -= tmp->nlines;
605 /* Print the first 'total_lines - n_elide' lines of tmp->buffer. */
606 if (n_elide < total_lines)
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)))
613 ++p;
614 ++tmp->nlines;
615 --n;
617 desired_pos += p - tmp->buffer;
618 xwrite_stdout (tmp->buffer, p - tmp->buffer);
621 free_lbuffers:
622 while (first)
624 tmp = first->next;
625 free (first);
626 first = tmp;
629 if (0 <= current_pos && elseek (fd, desired_pos, SEEK_SET, filename) < 0)
630 ok = false;
631 return ok;
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.
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. */
645 static bool
646 elide_tail_lines_seekable (const char *pretty_filename, int fd,
647 uintmax_t n_lines,
648 off_t start_pos, off_t size)
650 char buffer[BUFSIZ];
651 size_t bytes_read;
652 off_t pos = size;
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)
658 bytes_read = BUFSIZ;
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)
663 return false;
664 bytes_read = safe_read (fd, buffer, bytes_read);
665 if (bytes_read == SAFE_READ_ERROR)
667 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
668 return false;
671 /* n_lines == 0 case needs special treatment. */
672 const bool all_lines = !n_lines;
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)
676 --n_lines;
678 while (1)
680 /* Scan backward, counting the newlines in this bufferfull. */
682 size_t n = bytes_read;
683 while (n)
685 if (all_lines)
686 n -= 1;
687 else
689 char const *nl;
690 nl = memrchr (buffer, line_end, n);
691 if (nl == NULL)
692 break;
693 n = nl - buffer;
695 if (n_lines-- == 0)
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)
702 enum Copy_fd_status err;
703 if (elseek (fd, start_pos, SEEK_SET, pretty_filename) < 0)
704 return false;
706 err = copy_fd (fd, pos - start_pos);
707 if (err != COPY_FD_OK)
709 diagnose_copy_fd_failure (err, pretty_filename);
710 return false;
714 /* Output the initial portion of the buffer
715 in which we found the desired newline byte. */
716 xwrite_stdout (buffer, n + 1);
718 /* Set file pointer to the byte after what we've output. */
719 return 0 <= elseek (fd, pos + n + 1, SEEK_SET, pretty_filename);
723 /* Not enough newlines in that bufferfull. */
724 if (pos == start_pos)
726 /* Not enough lines in the file. */
727 return true;
729 pos -= BUFSIZ;
730 if (elseek (fd, pos, SEEK_SET, pretty_filename) < 0)
731 return false;
733 bytes_read = safe_read (fd, buffer, BUFSIZ);
734 if (bytes_read == SAFE_READ_ERROR)
736 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
737 return false;
740 /* FIXME: is this dead code?
741 Consider the test, pos == start_pos, above. */
742 if (bytes_read == 0)
743 return true;
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. */
752 static bool
753 elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide,
754 struct stat const *st, off_t current_pos)
756 off_t size = st->st_size;
757 if (presume_input_pipe || size <= ST_BLKSIZE (*st))
758 return elide_tail_lines_pipe (filename, fd, n_elide, current_pos);
759 else
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. */
766 return (size <= current_pos
767 || elide_tail_lines_seekable (filename, fd, n_elide,
768 current_pos, size));
772 static bool
773 head_bytes (const char *filename, int fd, uintmax_t bytes_to_write)
775 char buffer[BUFSIZ];
776 size_t bytes_to_read = BUFSIZ;
778 while (bytes_to_write)
780 size_t bytes_read;
781 if (bytes_to_write < bytes_to_read)
782 bytes_to_read = bytes_to_write;
783 bytes_read = safe_read (fd, buffer, bytes_to_read);
784 if (bytes_read == SAFE_READ_ERROR)
786 error (0, errno, _("error reading %s"), quoteaf (filename));
787 return false;
789 if (bytes_read == 0)
790 break;
791 xwrite_stdout (buffer, bytes_read);
792 bytes_to_write -= bytes_read;
794 return true;
797 static bool
798 head_lines (const char *filename, int fd, uintmax_t lines_to_write)
800 char buffer[BUFSIZ];
802 while (lines_to_write)
804 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
805 size_t bytes_to_write = 0;
807 if (bytes_read == SAFE_READ_ERROR)
809 error (0, errno, _("error reading %s"), quoteaf (filename));
810 return false;
812 if (bytes_read == 0)
813 break;
814 while (bytes_to_write < bytes_read)
815 if (buffer[bytes_to_write++] == line_end && --lines_to_write == 0)
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)
823 struct stat st;
824 if (fstat (fd, &st) != 0 || S_ISREG (st.st_mode))
825 elseek (fd, -n_bytes_past_EOL, SEEK_CUR, filename);
827 break;
829 xwrite_stdout (buffer, bytes_to_write);
831 return true;
834 static bool
835 head (const char *filename, int fd, uintmax_t n_units, bool count_lines,
836 bool elide_from_end)
838 if (print_headers)
839 write_header (filename);
841 if (elide_from_end)
843 off_t current_pos = -1;
844 struct stat st;
845 if (fstat (fd, &st) != 0)
847 error (0, errno, _("cannot fstat %s"),
848 quoteaf (filename));
849 return false;
851 if (! presume_input_pipe && usable_st_size (&st))
853 current_pos = elseek (fd, 0, SEEK_CUR, filename);
854 if (current_pos < 0)
855 return false;
857 if (count_lines)
858 return elide_tail_lines_file (filename, fd, n_units, &st, current_pos);
859 else
860 return elide_tail_bytes_file (filename, fd, n_units, &st, current_pos);
862 if (count_lines)
863 return head_lines (filename, fd, n_units);
864 else
865 return head_bytes (filename, fd, n_units);
868 static bool
869 head_file (const char *filename, uintmax_t n_units, bool count_lines,
870 bool elide_from_end)
872 int fd;
873 bool ok;
874 bool is_stdin = STREQ (filename, "-");
876 if (is_stdin)
878 have_read_stdin = true;
879 fd = STDIN_FILENO;
880 filename = _("standard input");
881 if (O_BINARY && ! isatty (STDIN_FILENO))
882 xfreopen (NULL, "rb", stdin);
884 else
886 fd = open (filename, O_RDONLY | O_BINARY);
887 if (fd < 0)
889 error (0, errno, _("cannot open %s for reading"), quoteaf (filename));
890 return false;
894 ok = head (filename, fd, n_units, count_lines, elide_from_end);
895 if (!is_stdin && close (fd) != 0)
897 error (0, errno, _("failed to close %s"), quoteaf (filename));
898 return false;
900 return ok;
903 /* Convert a string of decimal digits, N_STRING, with an optional suffix
904 to an integral value. Upon successful conversion,
905 return that value. If it cannot be converted, give a diagnostic and exit.
906 COUNT_LINES indicates whether N_STRING is a number of bytes or a number
907 of lines. It is used solely to give a more specific diagnostic. */
909 static uintmax_t
910 string_to_integer (bool count_lines, const char *n_string)
912 return xdectoumax (n_string, 0, UINTMAX_MAX, "bkKmMGTPEZY0",
913 count_lines ? _("invalid number of lines")
914 : _("invalid number of bytes"), 0);
918 main (int argc, char **argv)
920 enum header_mode header_mode = multiple_files;
921 bool ok = true;
922 int c;
923 size_t i;
925 /* Number of items to print. */
926 uintmax_t n_units = DEFAULT_NUMBER;
928 /* If true, interpret the numeric argument as the number of lines.
929 Otherwise, interpret it as the number of bytes. */
930 bool count_lines = true;
932 /* Elide the specified number of lines or bytes, counting from
933 the end of the file. */
934 bool elide_from_end = false;
936 /* Initializer for file_list if no file-arguments
937 were specified on the command line. */
938 static char const *const default_file_list[] = {"-", NULL};
939 char const *const *file_list;
941 initialize_main (&argc, &argv);
942 set_program_name (argv[0]);
943 setlocale (LC_ALL, "");
944 bindtextdomain (PACKAGE, LOCALEDIR);
945 textdomain (PACKAGE);
947 atexit (close_stdout);
949 have_read_stdin = false;
951 print_headers = false;
953 line_end = '\n';
955 if (1 < argc && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
957 char *a = argv[1];
958 char *n_string = ++a;
959 char *end_n_string;
960 char multiplier_char = 0;
962 /* Old option syntax; a dash, one or more digits, and one or
963 more option letters. Move past the number. */
964 do ++a;
965 while (ISDIGIT (*a));
967 /* Pointer to the byte after the last digit. */
968 end_n_string = a;
970 /* Parse any appended option letters. */
971 for (; *a; a++)
973 switch (*a)
975 case 'c':
976 count_lines = false;
977 multiplier_char = 0;
978 break;
980 case 'b':
981 case 'k':
982 case 'm':
983 count_lines = false;
984 multiplier_char = *a;
985 break;
987 case 'l':
988 count_lines = true;
989 break;
991 case 'q':
992 header_mode = never;
993 break;
995 case 'v':
996 header_mode = always;
997 break;
999 case 'z':
1000 line_end = '\0';
1001 break;
1003 default:
1004 error (0, 0, _("invalid trailing option -- %c"), *a);
1005 usage (EXIT_FAILURE);
1009 /* Append the multiplier character (if any) onto the end of
1010 the digit string. Then add NUL byte if necessary. */
1011 *end_n_string = multiplier_char;
1012 if (multiplier_char)
1013 *(++end_n_string) = 0;
1015 n_units = string_to_integer (count_lines, n_string);
1017 /* Make the options we just parsed invisible to getopt. */
1018 argv[1] = argv[0];
1019 argv++;
1020 argc--;
1023 while ((c = getopt_long (argc, argv, "c:n:qvz0123456789", long_options, NULL))
1024 != -1)
1026 switch (c)
1028 case PRESUME_INPUT_PIPE_OPTION:
1029 presume_input_pipe = true;
1030 break;
1032 case 'c':
1033 count_lines = false;
1034 elide_from_end = (*optarg == '-');
1035 if (elide_from_end)
1036 ++optarg;
1037 n_units = string_to_integer (count_lines, optarg);
1038 break;
1040 case 'n':
1041 count_lines = true;
1042 elide_from_end = (*optarg == '-');
1043 if (elide_from_end)
1044 ++optarg;
1045 n_units = string_to_integer (count_lines, optarg);
1046 break;
1048 case 'q':
1049 header_mode = never;
1050 break;
1052 case 'v':
1053 header_mode = always;
1054 break;
1056 case 'z':
1057 line_end = '\0';
1058 break;
1060 case_GETOPT_HELP_CHAR;
1062 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1064 default:
1065 if (ISDIGIT (c))
1066 error (0, 0, _("invalid trailing option -- %c"), c);
1067 usage (EXIT_FAILURE);
1071 if (header_mode == always
1072 || (header_mode == multiple_files && optind < argc - 1))
1073 print_headers = true;
1075 if ( ! count_lines && elide_from_end && OFF_T_MAX < n_units)
1077 char umax_buf[INT_BUFSIZE_BOUND (n_units)];
1078 die (EXIT_FAILURE, EOVERFLOW, "%s: %s", _("invalid number of bytes"),
1079 quote (umaxtostr (n_units, umax_buf)));
1082 file_list = (optind < argc
1083 ? (char const *const *) &argv[optind]
1084 : default_file_list);
1086 if (O_BINARY && ! isatty (STDOUT_FILENO))
1087 xfreopen (NULL, "wb", stdout);
1089 for (i = 0; file_list[i]; ++i)
1090 ok &= head_file (file_list[i], n_units, count_lines, elide_from_end);
1092 if (have_read_stdin && close (STDIN_FILENO) < 0)
1093 die (EXIT_FAILURE, errno, "-");
1095 return ok ? EXIT_SUCCESS : EXIT_FAILURE;