blob: d49113e7f87205f15a1e639d25fc11a3811547f2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu3157b1f1999-12-10 07:42:50 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * head implementation for busybox
John Beppu3157b1f1999-12-10 07:42:50 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
John Beppu3157b1f1999-12-10 07:42:50 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
John Beppu3157b1f1999-12-10 07:42:50 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config HEAD
10//config: bool "head"
11//config: default y
12//config: help
13//config: head is used to print the first specified number of lines
14//config: from files.
15//config:
16//config:config FEATURE_FANCY_HEAD
Denys Vlasenkof5604222017-01-10 14:58:54 +010017//config: bool "Enable -c, -q, and -v"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010018//config: default y
19//config: depends on HEAD
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010020
21//applet:IF_HEAD(APPLET_NOEXEC(head, head, BB_DIR_USR_BIN, BB_SUID_DROP, head))
22
23//kbuild:lib-$(CONFIG_HEAD) += head.o
John Beppu3157b1f1999-12-10 07:42:50 +000024
Manuel Novoa III cad53642003-03-19 09:13:01 +000025/* 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 */
28
Pere Orga34425382011-03-31 14:43:25 +020029//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"
Pere Orga34425382011-03-31 14:43:25 +020034//usage: "\n -n N[kbm] Print first N lines"
35//usage: IF_FEATURE_FANCY_HEAD(
Denys Vlasenko40c6da42013-02-25 01:26:09 +010036//usage: "\n -n -N[kbm] Print all except N last lines"
37//usage: "\n -c [-]N[kbm] Print first N bytes"
Pere Orga34425382011-03-31 14:43:25 +020038//usage: "\n -q Never print headers"
39//usage: "\n -v Always print headers"
40//usage: )
41//usage: "\n"
42//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
43//usage:
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"
48
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000049#include "libbb.h"
John Beppu3157b1f1999-12-10 07:42:50 +000050
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070051/* This is a NOEXEC applet. Be very careful! */
52
Denys Vlasenko40c6da42013-02-25 01:26:09 +010053#if !ENABLE_FEATURE_FANCY_HEAD
54# define print_first_N(fp,count,bytes) print_first_N(fp,count)
55#endif
56static void
57print_first_N(FILE *fp, unsigned long count, bool count_bytes)
58{
59#if !ENABLE_FEATURE_FANCY_HEAD
60 const int count_bytes = 0;
61#endif
62 while (count) {
63 int c = getc(fp);
64 if (c == EOF)
65 break;
66 if (count_bytes || (c == '\n'))
67 --count;
68 putchar(c);
69 }
70}
71
72#if ENABLE_FEATURE_FANCY_HEAD
73static void
74print_except_N_last_bytes(FILE *fp, unsigned count)
75{
76 unsigned char *circle = xmalloc(++count);
77 unsigned head = 0;
78 for(;;) {
79 int c;
80 c = getc(fp);
81 if (c == EOF)
82 goto ret;
83 circle[head++] = c;
84 if (head == count)
85 break;
86 }
87 for (;;) {
88 int c;
89 if (head == count)
90 head = 0;
91 putchar(circle[head]);
92 c = getc(fp);
93 if (c == EOF)
94 goto ret;
95 circle[head] = c;
96 head++;
97 }
98 ret:
99 free(circle);
100}
101
102static void
103print_except_N_last_lines(FILE *fp, unsigned count)
104{
105 char **circle = xzalloc((++count) * sizeof(circle[0]));
106 unsigned head = 0;
107 for(;;) {
108 char *c;
109 c = xmalloc_fgets(fp);
110 if (!c)
111 goto ret;
112 circle[head++] = c;
113 if (head == count)
114 break;
115 }
116 for (;;) {
117 char *c;
118 if (head == count)
119 head = 0;
120 fputs(circle[head], stdout);
121 c = xmalloc_fgets(fp);
122 if (!c)
123 goto ret;
124 free(circle[head]);
125 circle[head++] = c;
126 }
127 ret:
128 head = 0;
129 for(;;) {
130 free(circle[head++]);
131 if (head == count)
132 break;
133 }
134 free(circle);
135}
136#else
137/* Must never be called */
138void print_except_N_last_bytes(FILE *fp, unsigned count);
139void print_except_N_last_lines(FILE *fp, unsigned count);
140#endif
141
142#if !ENABLE_FEATURE_FANCY_HEAD
143# define eat_num(negative_N,p) eat_num(p)
144#endif
145static unsigned long
146eat_num(bool *negative_N, const char *p)
147{
148#if ENABLE_FEATURE_FANCY_HEAD
149 if (*p == '-') {
150 *negative_N = 1;
151 p++;
152 }
153#endif
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +0200154 return xatoul_sfx(p, bkm_suffixes);
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100155}
156
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000157static const char head_opts[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 "n:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000159#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 "c:qv"
161#endif
162 ;
John Beppu3157b1f1999-12-10 07:42:50 +0000163
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200164#define header_fmt_str "\n==> %s <==\n"
John Beppu3157b1f1999-12-10 07:42:50 +0000165
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000166int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +0000168{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 unsigned long count = 10;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000170#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 int header_threshhold = 1;
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100172 bool count_bytes = 0;
173 bool negative_N = 0;
174#else
175# define header_threshhold 1
176# define count_bytes 0
177# define negative_N 0
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178#endif
Matt Kraaic0321f92000-09-27 04:09:22 +0000179 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 const char *fmt;
181 char *p;
182 int opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +0000184
Denis Vlasenko08492072006-12-22 13:56:36 +0000185#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186 /* Allow legacy syntax of an initial numeric option without -n. */
Denis Vlasenko0d873672008-11-11 22:43:10 +0000187 if (argv[1] && argv[1][0] == '-'
Denis Vlasenko459903b2006-11-27 14:44:18 +0000188 && isdigit(argv[1][1])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 ) {
190 --argc;
191 ++argv;
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100192 p = argv[0] + 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000194 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000195#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000196
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000197 /* No size benefit in converting this to getopt32 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 while ((opt = getopt(argc, argv, head_opts)) > 0) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000199 switch (opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000200#if ENABLE_FEATURE_FANCY_HEAD
Denis Vlasenko13858992006-10-08 12:49:22 +0000201 case 'q':
202 header_threshhold = INT_MAX;
203 break;
204 case 'v':
205 header_threshhold = -1;
206 break;
207 case 'c':
208 count_bytes = 1;
209 /* fall through */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000211 case 'n':
212 p = optarg;
Denis Vlasenko08492072006-12-22 13:56:36 +0000213#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
214 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000215#endif
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100216 count = eat_num(&negative_N, p);
Denis Vlasenko13858992006-10-08 12:49:22 +0000217 break;
218 default:
219 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 }
221 }
222
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000223 argc -= optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000225 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000226 *--argv = (char*)"-";
John Beppu3157b1f1999-12-10 07:42:50 +0000227
Manuel Novoa III cad53642003-03-19 09:13:01 +0000228 fmt = header_fmt_str + 1;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000229 if (argc <= header_threshhold) {
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100230#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000231 header_threshhold = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232#else
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000233 fmt += 11; /* "" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234#endif
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100235 }
236 if (negative_N) {
237 if (count >= INT_MAX / sizeof(char*))
238 bb_error_msg("count is too big: %lu", count);
239 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240
241 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000242 fp = fopen_or_warn_stdin(*argv);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000243 if (fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000244 if (fp == stdin) {
245 *argv = (char *) bb_msg_standard_input;
246 }
247 if (header_threshhold) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000248 printf(fmt, *argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000249 }
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100250 if (negative_N) {
251 if (count_bytes) {
252 print_except_N_last_bytes(fp, count);
253 } else {
254 print_except_N_last_lines(fp, count);
255 }
256 } else {
257 print_first_N(fp, count, count_bytes);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000258 }
Denys Vlasenkoaf0255f2013-02-25 01:24:32 +0100259 die_if_ferror_stdout();
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000260 if (fclose_if_not_stdin(fp)) {
Denis Vlasenko0d873672008-11-11 22:43:10 +0000261 bb_simple_perror_msg(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000262 retval = EXIT_FAILURE;
263 }
Denis Vlasenko0d873672008-11-11 22:43:10 +0000264 } else {
265 retval = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000266 }
267 fmt = header_fmt_str;
268 } while (*++argv);
269
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000270 fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000271}