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 /* Number of lines/chars/blocks to head. */
43 #define DEFAULT_NUMBER 10
45 /* Size of atomic reads. */
46 #define BUFSIZE (512 * 8)
48 /* If nonzero, print filename headers. */
49 static int print_headers
;
51 /* When to print the filename banners. */
54 multiple_files
, always
, never
57 /* The name this program was run with. */
60 /* Have we ever read standard input? */
61 static int have_read_stdin
;
63 static struct option
const long_options
[] =
65 {"bytes", required_argument
, NULL
, 'c'},
66 {"lines", required_argument
, NULL
, 'n'},
67 {"quiet", no_argument
, NULL
, 'q'},
68 {"silent", no_argument
, NULL
, 'q'},
69 {"verbose", no_argument
, NULL
, 'v'},
70 {GETOPT_HELP_OPTION_DECL
},
71 {GETOPT_VERSION_OPTION_DECL
},
79 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
84 Usage: %s [OPTION]... [FILE]...\n\
88 Print first 10 lines of each FILE to standard output.\n\
89 With more than one FILE, precede each with a header giving the file name.\n\
90 With no FILE, or when FILE is -, read standard input.\n\
92 -c, --bytes=SIZE print first SIZE bytes\n\
93 -n, --lines=NUMBER print first NUMBER lines instead of first 10\n\
94 -q, --quiet, --silent never print headers giving file names\n\
95 -v, --verbose always print headers giving file names\n\
96 --help display this help and exit\n\
97 --version output version information and exit\n\
99 SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.\n\
100 If -VALUE is used as first OPTION, read -c VALUE when one of\n\
101 multipliers bkm follows concatenated, else read -n VALUE.\n\
103 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
105 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
109 write_header (const char *filename
)
111 static int first_file
= 1;
113 printf ("%s==> %s <==\n", (first_file
? "" : "\n"), filename
);
118 head_bytes (const char *filename
, int fd
, uintmax_t bytes_to_write
)
120 char buffer
[BUFSIZE
];
123 /* Need BINARY I/O for the byte counts to be accurate. */
124 SET_BINARY2 (fd
, fileno (stdout
));
126 while (bytes_to_write
)
128 bytes_read
= safe_read (fd
, buffer
, BUFSIZE
);
131 error (0, errno
, "%s", filename
);
136 if (bytes_read
> bytes_to_write
)
137 bytes_read
= bytes_to_write
;
138 if (fwrite (buffer
, 1, bytes_read
, stdout
) == 0)
139 error (EXIT_FAILURE
, errno
, _("write error"));
140 bytes_to_write
-= bytes_read
;
146 head_lines (const char *filename
, int fd
, uintmax_t lines_to_write
)
148 char buffer
[BUFSIZE
];
150 /* Need BINARY I/O for the byte counts to be accurate. */
151 SET_BINARY2 (fd
, fileno (stdout
));
153 while (lines_to_write
)
155 int bytes_read
= safe_read (fd
, buffer
, BUFSIZE
);
156 int bytes_to_write
= 0;
160 error (0, errno
, "%s", filename
);
165 while (bytes_to_write
< bytes_read
)
166 if (buffer
[bytes_to_write
++] == '\n' && --lines_to_write
== 0)
168 if (fwrite (buffer
, 1, bytes_to_write
, stdout
) == 0)
169 error (EXIT_FAILURE
, errno
, _("write error"));
175 head (const char *filename
, int fd
, uintmax_t n_units
, int count_lines
)
178 return head_lines (filename
, fd
, n_units
);
180 return head_bytes (filename
, fd
, n_units
);
184 head_file (const char *filename
, uintmax_t n_units
, int count_lines
)
188 if (STREQ (filename
, "-"))
191 filename
= _("standard input");
193 write_header (filename
);
194 return head (filename
, 0, n_units
, count_lines
);
198 fd
= open (filename
, O_RDONLY
);
204 write_header (filename
);
205 errors
= head (filename
, fd
, n_units
, count_lines
);
209 error (0, errno
, "%s", filename
);
214 /* Convert a string of digits, N_STRING, with a single, optional suffix
215 character (b, k, or m) to an integral value. Upon successful conversion,
216 return that value. If it cannot be converted, give a diagnostic and exit.
217 COUNT_LINES indicates whether N_STRING is a number of bytes or a number
218 of lines. It is used solely to give a more specific diagnostic. */
221 string_to_integer (int count_lines
, const char *n_string
)
226 s_err
= xstrtoumax (n_string
, NULL
, 0, &n
, "bkm");
228 if (s_err
== LONGINT_INVALID
)
230 error (EXIT_FAILURE
, 0, "%s: %s", n_string
,
232 ? _("invalid number of lines")
233 : _("invalid number of bytes")));
236 if (s_err
!= LONGINT_OK
)
238 error (EXIT_FAILURE
, 0,
239 _("%s: %s is so large that it is not representable"), n_string
,
240 count_lines
? _("number of lines") : _("number of bytes"));
247 main (int argc
, char **argv
)
249 enum header_mode header_mode
= multiple_files
;
254 /* Number of items to print. */
255 uintmax_t n_units
= DEFAULT_NUMBER
;
257 /* If nonzero, interpret the numeric argument as the number of lines.
258 Otherwise, interpret it as the number of bytes. */
261 program_name
= argv
[0];
262 setlocale (LC_ALL
, "");
263 bindtextdomain (PACKAGE
, LOCALEDIR
);
264 textdomain (PACKAGE
);
270 if (argc
> 1 && argv
[1][0] == '-' && ISDIGIT (argv
[1][1]))
273 char multiplier_char
= 0;
275 n_string
= &argv
[1][1];
277 /* Old option syntax; a dash, one or more digits, and one or
278 more option letters. Move past the number. */
279 for (++argv
[1]; ISDIGIT (*argv
[1]); ++argv
[1])
284 /* Pointer to the byte after the last digit. */
285 end_n_string
= argv
[1];
287 /* Parse any appended option letters. */
301 multiplier_char
= *argv
[1];
313 header_mode
= always
;
317 error (0, 0, _("unrecognized option `-%c'"), *argv
[1]);
323 /* Append the multiplier character (if any) onto the end of
324 the digit string. Then add NUL byte if necessary. */
325 *end_n_string
= multiplier_char
;
327 *(++end_n_string
) = 0;
329 n_units
= string_to_integer (count_lines
, n_string
);
331 /* Make the options we just parsed invisible to getopt. */
336 /* FIXME: allow POSIX options if there were obsolescent ones? */
340 while ((c
= getopt_long (argc
, argv
, "c:n:qv", long_options
, NULL
)) != -1)
349 n_units
= string_to_integer (count_lines
, optarg
);
354 n_units
= string_to_integer (count_lines
, optarg
);
362 header_mode
= always
;
365 case_GETOPT_HELP_CHAR
;
367 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
374 if (header_mode
== always
375 || (header_mode
== multiple_files
&& optind
< argc
- 1))
379 exit_status
|= head_file ("-", n_units
, count_lines
);
381 for (; optind
< argc
; ++optind
)
382 exit_status
|= head_file (argv
[optind
], n_units
, count_lines
);
384 if (have_read_stdin
&& close (0) < 0)
385 error (EXIT_FAILURE
, errno
, "-");
386 if (fclose (stdout
) == EOF
)
387 error (EXIT_FAILURE
, errno
, _("write error"));
389 exit (exit_status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);