blob: 176e91e3aa0b556aa21a650d49b82cef6a1018f8 [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
17//config: bool "Enable head options (-c, -q, and -v)"
18//config: default y
19//config: depends on HEAD
20//config: help
21//config: This enables the head options (-c, -q, and -v).
22
23//applet:IF_HEAD(APPLET_NOEXEC(head, head, BB_DIR_USR_BIN, BB_SUID_DROP, head))
24
25//kbuild:lib-$(CONFIG_HEAD) += head.o
John Beppu3157b1f1999-12-10 07:42:50 +000026
Manuel Novoa III cad53642003-03-19 09:13:01 +000027/* 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 */
30
Pere Orga34425382011-03-31 14:43:25 +020031//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"
Pere Orga34425382011-03-31 14:43:25 +020036//usage: "\n -n N[kbm] Print first N lines"
37//usage: IF_FEATURE_FANCY_HEAD(
Denys Vlasenko40c6da42013-02-25 01:26:09 +010038//usage: "\n -n -N[kbm] Print all except N last lines"
39//usage: "\n -c [-]N[kbm] Print first N bytes"
Pere Orga34425382011-03-31 14:43:25 +020040//usage: "\n -q Never print headers"
41//usage: "\n -v Always print headers"
42//usage: )
43//usage: "\n"
44//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
45//usage:
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"
50
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000051#include "libbb.h"
John Beppu3157b1f1999-12-10 07:42:50 +000052
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070053/* This is a NOEXEC applet. Be very careful! */
54
Denys Vlasenko40c6da42013-02-25 01:26:09 +010055#if !ENABLE_FEATURE_FANCY_HEAD
56# define print_first_N(fp,count,bytes) print_first_N(fp,count)
57#endif
58static void
59print_first_N(FILE *fp, unsigned long count, bool count_bytes)
60{
61#if !ENABLE_FEATURE_FANCY_HEAD
62 const int count_bytes = 0;
63#endif
64 while (count) {
65 int c = getc(fp);
66 if (c == EOF)
67 break;
68 if (count_bytes || (c == '\n'))
69 --count;
70 putchar(c);
71 }
72}
73
74#if ENABLE_FEATURE_FANCY_HEAD
75static void
76print_except_N_last_bytes(FILE *fp, unsigned count)
77{
78 unsigned char *circle = xmalloc(++count);
79 unsigned head = 0;
80 for(;;) {
81 int c;
82 c = getc(fp);
83 if (c == EOF)
84 goto ret;
85 circle[head++] = c;
86 if (head == count)
87 break;
88 }
89 for (;;) {
90 int c;
91 if (head == count)
92 head = 0;
93 putchar(circle[head]);
94 c = getc(fp);
95 if (c == EOF)
96 goto ret;
97 circle[head] = c;
98 head++;
99 }
100 ret:
101 free(circle);
102}
103
104static void
105print_except_N_last_lines(FILE *fp, unsigned count)
106{
107 char **circle = xzalloc((++count) * sizeof(circle[0]));
108 unsigned head = 0;
109 for(;;) {
110 char *c;
111 c = xmalloc_fgets(fp);
112 if (!c)
113 goto ret;
114 circle[head++] = c;
115 if (head == count)
116 break;
117 }
118 for (;;) {
119 char *c;
120 if (head == count)
121 head = 0;
122 fputs(circle[head], stdout);
123 c = xmalloc_fgets(fp);
124 if (!c)
125 goto ret;
126 free(circle[head]);
127 circle[head++] = c;
128 }
129 ret:
130 head = 0;
131 for(;;) {
132 free(circle[head++]);
133 if (head == count)
134 break;
135 }
136 free(circle);
137}
138#else
139/* Must never be called */
140void print_except_N_last_bytes(FILE *fp, unsigned count);
141void print_except_N_last_lines(FILE *fp, unsigned count);
142#endif
143
144#if !ENABLE_FEATURE_FANCY_HEAD
145# define eat_num(negative_N,p) eat_num(p)
146#endif
147static unsigned long
148eat_num(bool *negative_N, const char *p)
149{
150#if ENABLE_FEATURE_FANCY_HEAD
151 if (*p == '-') {
152 *negative_N = 1;
153 p++;
154 }
155#endif
Denys Vlasenkoc72b43c2013-07-13 23:49:45 +0200156 return xatoul_sfx(p, bkm_suffixes);
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100157}
158
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000159static const char head_opts[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 "n:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000161#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 "c:qv"
163#endif
164 ;
John Beppu3157b1f1999-12-10 07:42:50 +0000165
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200166#define header_fmt_str "\n==> %s <==\n"
John Beppu3157b1f1999-12-10 07:42:50 +0000167
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000168int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +0000170{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 unsigned long count = 10;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000172#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 int header_threshhold = 1;
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100174 bool count_bytes = 0;
175 bool negative_N = 0;
176#else
177# define header_threshhold 1
178# define count_bytes 0
179# define negative_N 0
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180#endif
Matt Kraaic0321f92000-09-27 04:09:22 +0000181 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 const char *fmt;
183 char *p;
184 int opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000185 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +0000186
Denis Vlasenko08492072006-12-22 13:56:36 +0000187#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 /* Allow legacy syntax of an initial numeric option without -n. */
Denis Vlasenko0d873672008-11-11 22:43:10 +0000189 if (argv[1] && argv[1][0] == '-'
Denis Vlasenko459903b2006-11-27 14:44:18 +0000190 && isdigit(argv[1][1])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 ) {
192 --argc;
193 ++argv;
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100194 p = argv[0] + 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000196 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000197#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000198
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000199 /* No size benefit in converting this to getopt32 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200 while ((opt = getopt(argc, argv, head_opts)) > 0) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000201 switch (opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000202#if ENABLE_FEATURE_FANCY_HEAD
Denis Vlasenko13858992006-10-08 12:49:22 +0000203 case 'q':
204 header_threshhold = INT_MAX;
205 break;
206 case 'v':
207 header_threshhold = -1;
208 break;
209 case 'c':
210 count_bytes = 1;
211 /* fall through */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000213 case 'n':
214 p = optarg;
Denis Vlasenko08492072006-12-22 13:56:36 +0000215#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
216 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000217#endif
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100218 count = eat_num(&negative_N, p);
Denis Vlasenko13858992006-10-08 12:49:22 +0000219 break;
220 default:
221 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 }
223 }
224
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000225 argc -= optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000226 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000227 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000228 *--argv = (char*)"-";
John Beppu3157b1f1999-12-10 07:42:50 +0000229
Manuel Novoa III cad53642003-03-19 09:13:01 +0000230 fmt = header_fmt_str + 1;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000231 if (argc <= header_threshhold) {
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100232#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 header_threshhold = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234#else
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000235 fmt += 11; /* "" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236#endif
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100237 }
238 if (negative_N) {
239 if (count >= INT_MAX / sizeof(char*))
240 bb_error_msg("count is too big: %lu", count);
241 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242
243 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000244 fp = fopen_or_warn_stdin(*argv);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000245 if (fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000246 if (fp == stdin) {
247 *argv = (char *) bb_msg_standard_input;
248 }
249 if (header_threshhold) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000250 printf(fmt, *argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 }
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100252 if (negative_N) {
253 if (count_bytes) {
254 print_except_N_last_bytes(fp, count);
255 } else {
256 print_except_N_last_lines(fp, count);
257 }
258 } else {
259 print_first_N(fp, count, count_bytes);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000260 }
Denys Vlasenkoaf0255f2013-02-25 01:24:32 +0100261 die_if_ferror_stdout();
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000262 if (fclose_if_not_stdin(fp)) {
Denis Vlasenko0d873672008-11-11 22:43:10 +0000263 bb_simple_perror_msg(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000264 retval = EXIT_FAILURE;
265 }
Denis Vlasenko0d873672008-11-11 22:43:10 +0000266 } else {
267 retval = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000268 }
269 fmt = header_fmt_str;
270 } while (*++argv);
271
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000272 fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000273}