1 /* head -- output first part of file(s)
2 Copyright (C) 89, 90, 91, 1995-1999 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Options: (see usage)
19 Reads from standard input if no files are given or when a filename of
21 By default, filename headers are printed only if more than one file
23 By default, prints the first 10 lines (head -n 10).
25 David MacKenzie <djm@gnu.ai.mit.edu> */
31 #include <sys/types.h>
35 #include "safe-read.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "head"
40 #define AUTHORS "David MacKenzie"
42 /* FIXME: someday, make this really *be* `long long'. */
43 typedef long int U_LONG_LONG;
45 /* Number of lines/chars/blocks to head. */
46 #define DEFAULT_NUMBER 10
48 /* Size of atomic reads. */
49 #define BUFSIZE (512 * 8)
51 /* If nonzero, print filename headers. */
52 static int print_headers;
54 /* When to print the filename banners. */
57 multiple_files, always, never
60 /* The name this program was run with. */
63 /* Have we ever read standard input? */
64 static int have_read_stdin;
66 static struct option const long_options[] =
68 {"bytes", required_argument, NULL, 'c'},
69 {"lines", required_argument, NULL, 'n'},
70 {"quiet", no_argument, NULL, 'q'},
71 {"silent", no_argument, NULL, 'q'},
72 {"verbose", no_argument, NULL, 'v'},
73 {GETOPT_HELP_OPTION_DECL},
74 {GETOPT_VERSION_OPTION_DECL},
82 fprintf (stderr, _("Try `%s --help' for more information.\n"),
87 Usage: %s [OPTION]... [FILE]...\n\
91 Print first 10 lines of each FILE to standard output.\n\
92 With more than one FILE, precede each with a header giving the file name.\n\
93 With no FILE, or when FILE is -, read standard input.\n\
95 -c, --bytes=SIZE print first SIZE bytes\n\
96 -n, --lines=NUMBER print first NUMBER lines instead of first 10\n\
97 -q, --quiet, --silent never print headers giving file names\n\
98 -v, --verbose always print headers giving file names\n\
99 --help display this help and exit\n\
100 --version output version information and exit\n\
102 SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
103 If -VALUE is used as first OPTION, read -c VALUE when one of\n\
104 multipliers bkm follows concatenated, else read -n VALUE.\n\
106 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
108 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
112 write_header (const char *filename)
114 static int first_file = 1;
116 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), filename);
121 head_bytes (const char *filename, int fd, U_LONG_LONG bytes_to_write)
123 char buffer[BUFSIZE];
126 /* Need BINARY I/O for the byte counts to be accurate. */
127 SET_BINARY2 (fd, fileno (stdout));
129 while (bytes_to_write)
131 bytes_read = safe_read (fd, buffer, BUFSIZE);
134 error (0, errno, "%s", filename);
139 if (bytes_read > bytes_to_write)
140 bytes_read = bytes_to_write;
141 if (fwrite (buffer, 1, bytes_read, stdout) == 0)
142 error (EXIT_FAILURE, errno, _("write error"));
143 bytes_to_write -= bytes_read;
149 head_lines (const char *filename, int fd, U_LONG_LONG lines_to_write)
151 char buffer[BUFSIZE];
155 /* Need BINARY I/O for the byte counts to be accurate. */
156 SET_BINARY2 (fd, fileno (stdout));
158 while (lines_to_write)
160 bytes_read = safe_read (fd, buffer, BUFSIZE);
163 error (0, errno, "%s", filename);
169 while (bytes_to_write < bytes_read)
170 if (buffer[bytes_to_write++] == '\n' && --lines_to_write == 0)
172 if (fwrite (buffer, 1, bytes_to_write, stdout) == 0)
173 error (EXIT_FAILURE, errno, _("write error"));
179 head (const char *filename, int fd, U_LONG_LONG n_units, int count_lines)
182 return head_lines (filename, fd, n_units);
184 return head_bytes (filename, fd, n_units);
188 head_file (const char *filename, U_LONG_LONG n_units, int count_lines)
192 if (STREQ (filename, "-"))
195 filename = _("standard input");
197 write_header (filename);
198 return head (filename, 0, n_units, count_lines);
202 fd = open (filename, O_RDONLY);
208 write_header (filename);
209 errors = head (filename, fd, n_units, count_lines);
213 error (0, errno, "%s", filename);
218 /* Convert a string of digits, N_STRING, with a single, optional suffix
219 character (b, k, or m) to an integral value. Upon successful conversion,
220 return that value. If it cannot be converted, give a diagnostic and exit.
221 COUNT_LINES indicates whether N_STRING is a number of bytes or a number
222 of lines. It is used solely to give a more specific diagnostic. */
225 string_to_ull (int count_lines, const char *n_string)
228 unsigned long int tmp_ulong;
230 s_err = xstrtoul (n_string, NULL, 0, &tmp_ulong, "bkm");
232 if (s_err == LONGINT_INVALID)
234 error (EXIT_FAILURE, 0, "%s: %s", n_string,
236 ? _("invalid number of lines")
237 : _("invalid number of bytes")));
240 if (s_err != LONGINT_OK)
242 error (EXIT_FAILURE, 0,
243 _("%s: %s is so large that it is not representable"), n_string,
244 count_lines ? _("number of lines") : _("number of bytes"));
251 main (int argc, char **argv)
253 enum header_mode header_mode = multiple_files;
258 /* Number of items to print. */
259 U_LONG_LONG n_units = DEFAULT_NUMBER;
261 /* If nonzero, interpret the numeric argument as the number of lines.
262 Otherwise, interpret it as the number of bytes. */
265 program_name = argv[0];
266 setlocale (LC_ALL, "");
267 bindtextdomain (PACKAGE, LOCALEDIR);
268 textdomain (PACKAGE);
274 if (argc > 1 && argv[1][0] == '-' && ISDIGIT (argv[1][1]))
277 char multiplier_char = 0;
279 n_string = &argv[1][1];
281 /* Old option syntax; a dash, one or more digits, and one or
282 more option letters. Move past the number. */
283 for (++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
288 /* Pointer to the byte after the last digit. */
289 end_n_string = argv[1];
291 /* Parse any appended option letters. */
305 multiplier_char = *argv[1];
317 header_mode = always;
321 error (0, 0, _("unrecognized option `-%c'"), *argv[1]);
327 /* Append the multiplier character (if any) onto the end of
328 the digit string. Then add NUL byte if necessary. */
329 *end_n_string = multiplier_char;
331 *(++end_n_string) = 0;
333 n_units = string_to_ull (count_lines, n_string);
335 /* Make the options we just parsed invisible to getopt. */
340 /* FIXME: allow POSIX options if there were obsolescent ones? */
344 while ((c = getopt_long (argc, argv, "c:n:qv", long_options, NULL)) != -1)
353 n_units = string_to_ull (count_lines, optarg);
358 n_units = string_to_ull (count_lines, optarg);
366 header_mode = always;
369 case_GETOPT_HELP_CHAR;
371 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
378 if (header_mode == always
379 || (header_mode == multiple_files && optind < argc - 1))
383 exit_status |= head_file ("-", n_units, count_lines);
385 for (; optind < argc; ++optind)
386 exit_status |= head_file (argv[optind], n_units, count_lines);
388 if (have_read_stdin && close (0) < 0)
389 error (EXIT_FAILURE, errno, "-");
390 if (fclose (stdout) == EOF)
391 error (EXIT_FAILURE, errno, _("write error"));
393 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);