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 //config: bool "head (3.8 kb)"
13 //config: head is used to print the first specified number of lines
16 //config:config FEATURE_FANCY_HEAD
17 //config: bool "Enable -c, -q, and -v"
19 //config: depends on HEAD
21 //applet:IF_HEAD(APPLET_NOEXEC(head, head, BB_DIR_USR_BIN, BB_SUID_DROP, head))
23 //kbuild:lib-$(CONFIG_HEAD) += head.o
25 /* BB_AUDIT SUSv3 compliant */
26 /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
27 /* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
29 //usage:#define head_trivial_usage
30 //usage: "[OPTIONS] [FILE]..."
31 //usage:#define head_full_usage "\n\n"
32 //usage: "Print first 10 lines of each FILE (or stdin) to stdout.\n"
33 //usage: "With more than one FILE, precede each with a filename header.\n"
34 //usage: "\n -n N[kbm] Print first N lines"
35 //usage: IF_FEATURE_FANCY_HEAD(
36 //usage: "\n -n -N[kbm] Print all except N last lines"
37 //usage: "\n -c [-]N[kbm] Print first N bytes"
38 //usage: "\n -q Never print headers"
39 //usage: "\n -v Always print headers"
42 //usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
44 //usage:#define head_example_usage
45 //usage: "$ head -n 2 /etc/passwd\n"
46 //usage: "root:x:0:0:root:/root:/bin/bash\n"
47 //usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
51 /* This is a NOEXEC applet. Be very careful! */
53 #if !ENABLE_FEATURE_FANCY_HEAD
54 # define print_first_N(fp,count,bytes) print_first_N(fp,count)
57 print_first_N(FILE *fp, unsigned long count, bool count_bytes)
59 #if !ENABLE_FEATURE_FANCY_HEAD
60 const int count_bytes = 0;
66 if (count_bytes || (c == '\n'))
72 #if ENABLE_FEATURE_FANCY_HEAD
74 print_except_N_last_bytes(FILE *fp, unsigned count)
76 unsigned char *circle = xmalloc(++count);
91 putchar(circle[head]);
103 print_except_N_last_lines(FILE *fp, unsigned count)
105 char **circle = xzalloc((++count) * sizeof(circle[0]));
109 c = xmalloc_fgets(fp);
120 fputs(circle[head], stdout);
121 c = xmalloc_fgets(fp);
130 free(circle[head++]);
137 /* Must never be called */
138 void print_except_N_last_bytes(FILE *fp, unsigned count);
139 void print_except_N_last_lines(FILE *fp, unsigned count);
142 #if !ENABLE_FEATURE_FANCY_HEAD
143 # define eat_num(negative_N,p) eat_num(p)
146 eat_num(bool *negative_N, const char *p)
148 #if ENABLE_FEATURE_FANCY_HEAD
154 return xatoul_sfx(p, bkm_suffixes);
157 static const char head_opts[] ALIGN1 =
159 #if ENABLE_FEATURE_FANCY_HEAD
164 #define header_fmt_str "\n==> %s <==\n"
166 int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
167 int head_main(int argc, char **argv)
169 unsigned long count = 10;
170 #if ENABLE_FEATURE_FANCY_HEAD
171 int header_threshhold = 1;
172 bool count_bytes = 0;
175 # define header_threshhold 1
176 # define count_bytes 0
177 # define negative_N 0
183 int retval = EXIT_SUCCESS;
185 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
186 /* Allow legacy syntax of an initial numeric option without -n. */
187 if (argv[1] && argv[1][0] == '-'
188 && isdigit(argv[1][1])
197 /* No size benefit in converting this to getopt32 */
198 while ((opt = getopt(argc, argv, head_opts)) > 0) {
200 #if ENABLE_FEATURE_FANCY_HEAD
202 header_threshhold = INT_MAX;
205 header_threshhold = -1;
213 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
216 count = eat_num(&negative_N, p);
226 *--argv = (char*)"-";
228 fmt = header_fmt_str + 1;
229 if (argc <= header_threshhold) {
230 #if ENABLE_FEATURE_FANCY_HEAD
231 header_threshhold = 0;
237 if (count >= INT_MAX / sizeof(char*))
238 bb_error_msg("count is too big: %lu", count);
242 fp = fopen_or_warn_stdin(*argv);
245 *argv = (char *) bb_msg_standard_input;
247 if (header_threshhold) {
252 print_except_N_last_bytes(fp, count);
254 print_except_N_last_lines(fp, count);
257 print_first_N(fp, count, count_bytes);
259 die_if_ferror_stdout();
260 if (fclose_if_not_stdin(fp)) {
261 bb_simple_perror_msg(*argv);
262 retval = EXIT_FAILURE;
265 retval = EXIT_FAILURE;
267 fmt = header_fmt_str;
270 fflush_stdout_and_exit(retval);