1 /* vi: set sw=4 ts=4: */
3 * head implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 /* BB_AUDIT SUSv3 compliant */
11 /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
14 //kbuild:lib-$(CONFIG_HEAD) += head.o
15 //kbuild:lib-$(CONFIG_HEAD) += head_tail.o
17 //usage:#define head_trivial_usage
18 //usage: "[OPTIONS] [FILE]..."
19 //usage:#define head_full_usage "\n\n"
20 //usage: "Print first 10 lines of each FILE (or stdin) to stdout.\n"
21 //usage: "With more than one FILE, precede each with a filename header.\n"
22 //usage: "\n -n N[kbm] Print first N lines"
23 //usage: IF_FEATURE_FANCY_HEAD(
24 //usage: "\n -n -N[kbm] Print all except N last lines"
25 //usage: "\n -c [-]N[kbm] Print first N bytes"
26 //usage: "\n -q Never print headers"
27 //usage: "\n -v Always print headers"
30 //usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
32 //usage:#define head_example_usage
33 //usage: "$ head -n 2 /etc/passwd\n"
34 //usage: "root:x:0:0:root:/root:/bin/bash\n"
35 //usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
38 #include "head_tail.h"
40 /* This is a NOEXEC applet. Be very careful! */
42 #if !ENABLE_FEATURE_FANCY_HEAD
43 # define print_first_N(fp,count,bytes) print_first_N(fp,count)
46 print_first_N(FILE *fp, unsigned long count, bool count_bytes)
48 #if !ENABLE_FEATURE_FANCY_HEAD
49 const int count_bytes = 0;
55 if (count_bytes || (c == '\n'))
61 #if ENABLE_FEATURE_FANCY_HEAD
63 print_except_N_last_bytes(FILE *fp, unsigned count)
65 unsigned char *circle = xmalloc(++count);
80 putchar(circle[head]);
92 print_except_N_last_lines(FILE *fp, unsigned count)
94 char **circle = xzalloc((++count) * sizeof(circle[0]));
98 c = xmalloc_fgets(fp);
109 fputs(circle[head], stdout);
110 c = xmalloc_fgets(fp);
119 free(circle[head++]);
126 /* Must never be called */
127 void print_except_N_last_bytes(FILE *fp, unsigned count);
128 void print_except_N_last_lines(FILE *fp, unsigned count);
131 #if !ENABLE_FEATURE_FANCY_HEAD
132 # define eat_num(negative_N,p) eat_num(p)
135 eat_num(bool *negative_N, const char *p)
137 #if ENABLE_FEATURE_FANCY_HEAD
143 return xatoul_sfx(p, head_tail_suffixes);
146 static const char head_opts[] ALIGN1 =
148 #if ENABLE_FEATURE_FANCY_HEAD
153 #define header_fmt_str "\n==> %s <==\n"
155 int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
156 int head_main(int argc, char **argv)
158 unsigned long count = 10;
159 #if ENABLE_FEATURE_FANCY_HEAD
160 int header_threshhold = 1;
161 bool count_bytes = 0;
164 # define header_threshhold 1
165 # define count_bytes 0
166 # define negative_N 0
172 int retval = EXIT_SUCCESS;
174 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
175 /* Allow legacy syntax of an initial numeric option without -n. */
176 if (argv[1] && argv[1][0] == '-'
177 && isdigit(argv[1][1])
186 /* No size benefit in converting this to getopt32 */
187 while ((opt = getopt(argc, argv, head_opts)) > 0) {
189 #if ENABLE_FEATURE_FANCY_HEAD
191 header_threshhold = INT_MAX;
194 header_threshhold = -1;
202 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
205 count = eat_num(&negative_N, p);
215 *--argv = (char*)"-";
217 fmt = header_fmt_str + 1;
218 if (argc <= header_threshhold) {
219 #if ENABLE_FEATURE_FANCY_HEAD
220 header_threshhold = 0;
226 if (count >= INT_MAX / sizeof(char*))
227 bb_error_msg("count is too big: %lu", count);
231 fp = fopen_or_warn_stdin(*argv);
234 *argv = (char *) bb_msg_standard_input;
236 if (header_threshhold) {
241 print_except_N_last_bytes(fp, count);
243 print_except_N_last_lines(fp, count);
246 print_first_N(fp, count, count_bytes);
248 die_if_ferror_stdout();
249 if (fclose_if_not_stdin(fp)) {
250 bb_simple_perror_msg(*argv);
251 retval = EXIT_FAILURE;
254 retval = EXIT_FAILURE;
256 fmt = header_fmt_str;
259 fflush_stdout_and_exit(retval);