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
16 //usage:#define head_trivial_usage
17 //usage: "[OPTIONS] [FILE]..."
18 //usage:#define head_full_usage "\n\n"
19 //usage: "Print first 10 lines of each FILE (or stdin) to stdout.\n"
20 //usage: "With more than one FILE, precede each with a filename header.\n"
21 //usage: "\n -n N[kbm] Print first N lines"
22 //usage: IF_FEATURE_FANCY_HEAD(
23 //usage: "\n -n -N[kbm] Print all except N last lines"
24 //usage: "\n -c [-]N[kbm] Print first N bytes"
25 //usage: "\n -q Never print headers"
26 //usage: "\n -v Always print headers"
29 //usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
31 //usage:#define head_example_usage
32 //usage: "$ head -n 2 /etc/passwd\n"
33 //usage: "root:x:0:0:root:/root:/bin/bash\n"
34 //usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
38 /* This is a NOEXEC applet. Be very careful! */
40 #if !ENABLE_FEATURE_FANCY_HEAD
41 # define print_first_N(fp,count,bytes) print_first_N(fp,count)
44 print_first_N(FILE *fp, unsigned long count, bool count_bytes)
46 #if !ENABLE_FEATURE_FANCY_HEAD
47 const int count_bytes = 0;
53 if (count_bytes || (c == '\n'))
59 #if ENABLE_FEATURE_FANCY_HEAD
61 print_except_N_last_bytes(FILE *fp, unsigned count)
63 unsigned char *circle = xmalloc(++count);
78 putchar(circle[head]);
90 print_except_N_last_lines(FILE *fp, unsigned count)
92 char **circle = xzalloc((++count) * sizeof(circle[0]));
96 c = xmalloc_fgets(fp);
107 fputs(circle[head], stdout);
108 c = xmalloc_fgets(fp);
117 free(circle[head++]);
124 /* Must never be called */
125 void print_except_N_last_bytes(FILE *fp, unsigned count);
126 void print_except_N_last_lines(FILE *fp, unsigned count);
129 #if !ENABLE_FEATURE_FANCY_HEAD
130 # define eat_num(negative_N,p) eat_num(p)
133 eat_num(bool *negative_N, const char *p)
135 #if ENABLE_FEATURE_FANCY_HEAD
141 return xatoul_sfx(p, bkm_suffixes);
144 static const char head_opts[] ALIGN1 =
146 #if ENABLE_FEATURE_FANCY_HEAD
151 #define header_fmt_str "\n==> %s <==\n"
153 int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
154 int head_main(int argc, char **argv)
156 unsigned long count = 10;
157 #if ENABLE_FEATURE_FANCY_HEAD
158 int header_threshhold = 1;
159 bool count_bytes = 0;
162 # define header_threshhold 1
163 # define count_bytes 0
164 # define negative_N 0
170 int retval = EXIT_SUCCESS;
172 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
173 /* Allow legacy syntax of an initial numeric option without -n. */
174 if (argv[1] && argv[1][0] == '-'
175 && isdigit(argv[1][1])
184 /* No size benefit in converting this to getopt32 */
185 while ((opt = getopt(argc, argv, head_opts)) > 0) {
187 #if ENABLE_FEATURE_FANCY_HEAD
189 header_threshhold = INT_MAX;
192 header_threshhold = -1;
200 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
203 count = eat_num(&negative_N, p);
213 *--argv = (char*)"-";
215 fmt = header_fmt_str + 1;
216 if (argc <= header_threshhold) {
217 #if ENABLE_FEATURE_FANCY_HEAD
218 header_threshhold = 0;
224 if (count >= INT_MAX / sizeof(char*))
225 bb_error_msg("count is too big: %lu", count);
229 fp = fopen_or_warn_stdin(*argv);
232 *argv = (char *) bb_msg_standard_input;
234 if (header_threshhold) {
239 print_except_N_last_bytes(fp, count);
241 print_except_N_last_lines(fp, count);
244 print_first_N(fp, count, count_bytes);
246 die_if_ferror_stdout();
247 if (fclose_if_not_stdin(fp)) {
248 bb_simple_perror_msg(*argv);
249 retval = EXIT_FAILURE;
252 retval = EXIT_FAILURE;
254 fmt = header_fmt_str;
257 fflush_stdout_and_exit(retval);