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.
13 //config: head is used to print the first specified number of lines
16 //config:config FEATURE_FANCY_HEAD
17 //config: bool "Enable head options (-c, -q, and -v)"
19 //config: depends on HEAD
21 //config: This enables the head options (-c, -q, and -v).
23 //applet:IF_HEAD(APPLET_NOEXEC(head, head, BB_DIR_USR_BIN, BB_SUID_DROP, head))
25 //kbuild:lib-$(CONFIG_HEAD) += head.o
27 /* BB_AUDIT SUSv3 compliant */
28 /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
29 /* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
31 //usage:#define head_trivial_usage
32 //usage: "[OPTIONS] [FILE]..."
33 //usage:#define head_full_usage "\n\n"
34 //usage: "Print first 10 lines of each FILE (or stdin) to stdout.\n"
35 //usage: "With more than one FILE, precede each with a filename header.\n"
36 //usage: "\n -n N[kbm] Print first N lines"
37 //usage: IF_FEATURE_FANCY_HEAD(
38 //usage: "\n -n -N[kbm] Print all except N last lines"
39 //usage: "\n -c [-]N[kbm] Print first N bytes"
40 //usage: "\n -q Never print headers"
41 //usage: "\n -v Always print headers"
44 //usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
46 //usage:#define head_example_usage
47 //usage: "$ head -n 2 /etc/passwd\n"
48 //usage: "root:x:0:0:root:/root:/bin/bash\n"
49 //usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
53 /* This is a NOEXEC applet. Be very careful! */
55 #if !ENABLE_FEATURE_FANCY_HEAD
56 # define print_first_N(fp,count,bytes) print_first_N(fp,count)
59 print_first_N(FILE *fp, unsigned long count, bool count_bytes)
61 #if !ENABLE_FEATURE_FANCY_HEAD
62 const int count_bytes = 0;
68 if (count_bytes || (c == '\n'))
74 #if ENABLE_FEATURE_FANCY_HEAD
76 print_except_N_last_bytes(FILE *fp, unsigned count)
78 unsigned char *circle = xmalloc(++count);
93 putchar(circle[head]);
105 print_except_N_last_lines(FILE *fp, unsigned count)
107 char **circle = xzalloc((++count) * sizeof(circle[0]));
111 c = xmalloc_fgets(fp);
122 fputs(circle[head], stdout);
123 c = xmalloc_fgets(fp);
132 free(circle[head++]);
139 /* Must never be called */
140 void print_except_N_last_bytes(FILE *fp, unsigned count);
141 void print_except_N_last_lines(FILE *fp, unsigned count);
144 #if !ENABLE_FEATURE_FANCY_HEAD
145 # define eat_num(negative_N,p) eat_num(p)
148 eat_num(bool *negative_N, const char *p)
150 #if ENABLE_FEATURE_FANCY_HEAD
156 return xatoul_sfx(p, bkm_suffixes);
159 static const char head_opts[] ALIGN1 =
161 #if ENABLE_FEATURE_FANCY_HEAD
166 #define header_fmt_str "\n==> %s <==\n"
168 int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
169 int head_main(int argc, char **argv)
171 unsigned long count = 10;
172 #if ENABLE_FEATURE_FANCY_HEAD
173 int header_threshhold = 1;
174 bool count_bytes = 0;
177 # define header_threshhold 1
178 # define count_bytes 0
179 # define negative_N 0
185 int retval = EXIT_SUCCESS;
187 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
188 /* Allow legacy syntax of an initial numeric option without -n. */
189 if (argv[1] && argv[1][0] == '-'
190 && isdigit(argv[1][1])
199 /* No size benefit in converting this to getopt32 */
200 while ((opt = getopt(argc, argv, head_opts)) > 0) {
202 #if ENABLE_FEATURE_FANCY_HEAD
204 header_threshhold = INT_MAX;
207 header_threshhold = -1;
215 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
218 count = eat_num(&negative_N, p);
228 *--argv = (char*)"-";
230 fmt = header_fmt_str + 1;
231 if (argc <= header_threshhold) {
232 #if ENABLE_FEATURE_FANCY_HEAD
233 header_threshhold = 0;
239 if (count >= INT_MAX / sizeof(char*))
240 bb_error_msg("count is too big: %lu", count);
244 fp = fopen_or_warn_stdin(*argv);
247 *argv = (char *) bb_msg_standard_input;
249 if (header_threshhold) {
254 print_except_N_last_bytes(fp, count);
256 print_except_N_last_lines(fp, count);
259 print_first_N(fp, count, count_bytes);
261 die_if_ferror_stdout();
262 if (fclose_if_not_stdin(fp)) {
263 bb_simple_perror_msg(*argv);
264 retval = EXIT_FAILURE;
267 retval = EXIT_FAILURE;
269 fmt = header_fmt_str;
272 fflush_stdout_and_exit(retval);