blob: 291e1ce37045ca73843b3c37ca976192ecc5d9de [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 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* 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 */
13
Denys Vlasenkoaf0255f2013-02-25 01:24:32 +010014//kbuild:lib-$(CONFIG_HEAD) += head.o
15//kbuild:lib-$(CONFIG_HEAD) += head_tail.o
16
Pere Orga34425382011-03-31 14:43:25 +020017//usage:#define head_trivial_usage
18//usage: "[OPTIONS] [FILE]..."
19//usage:#define head_full_usage "\n\n"
20//usage: "Print first 10 lines of each FILE (or stdin) to stdout.\n"
21//usage: "With more than one FILE, precede each with a filename header.\n"
Pere Orga34425382011-03-31 14:43:25 +020022//usage: "\n -n N[kbm] Print first N lines"
23//usage: IF_FEATURE_FANCY_HEAD(
Denys Vlasenko40c6da42013-02-25 01:26:09 +010024//usage: "\n -n -N[kbm] Print all except N last lines"
25//usage: "\n -c [-]N[kbm] Print first N bytes"
Pere Orga34425382011-03-31 14:43:25 +020026//usage: "\n -q Never print headers"
27//usage: "\n -v Always print headers"
28//usage: )
29//usage: "\n"
30//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
31//usage:
32//usage:#define head_example_usage
33//usage: "$ head -n 2 /etc/passwd\n"
34//usage: "root:x:0:0:root:/root:/bin/bash\n"
35//usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Denys Vlasenkoaf0255f2013-02-25 01:24:32 +010038#include "head_tail.h"
John Beppu3157b1f1999-12-10 07:42:50 +000039
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070040/* This is a NOEXEC applet. Be very careful! */
41
Denys Vlasenko40c6da42013-02-25 01:26:09 +010042#if !ENABLE_FEATURE_FANCY_HEAD
43# define print_first_N(fp,count,bytes) print_first_N(fp,count)
44#endif
45static void
46print_first_N(FILE *fp, unsigned long count, bool count_bytes)
47{
48#if !ENABLE_FEATURE_FANCY_HEAD
49 const int count_bytes = 0;
50#endif
51 while (count) {
52 int c = getc(fp);
53 if (c == EOF)
54 break;
55 if (count_bytes || (c == '\n'))
56 --count;
57 putchar(c);
58 }
59}
60
61#if ENABLE_FEATURE_FANCY_HEAD
62static void
63print_except_N_last_bytes(FILE *fp, unsigned count)
64{
65 unsigned char *circle = xmalloc(++count);
66 unsigned head = 0;
67 for(;;) {
68 int c;
69 c = getc(fp);
70 if (c == EOF)
71 goto ret;
72 circle[head++] = c;
73 if (head == count)
74 break;
75 }
76 for (;;) {
77 int c;
78 if (head == count)
79 head = 0;
80 putchar(circle[head]);
81 c = getc(fp);
82 if (c == EOF)
83 goto ret;
84 circle[head] = c;
85 head++;
86 }
87 ret:
88 free(circle);
89}
90
91static void
92print_except_N_last_lines(FILE *fp, unsigned count)
93{
94 char **circle = xzalloc((++count) * sizeof(circle[0]));
95 unsigned head = 0;
96 for(;;) {
97 char *c;
98 c = xmalloc_fgets(fp);
99 if (!c)
100 goto ret;
101 circle[head++] = c;
102 if (head == count)
103 break;
104 }
105 for (;;) {
106 char *c;
107 if (head == count)
108 head = 0;
109 fputs(circle[head], stdout);
110 c = xmalloc_fgets(fp);
111 if (!c)
112 goto ret;
113 free(circle[head]);
114 circle[head++] = c;
115 }
116 ret:
117 head = 0;
118 for(;;) {
119 free(circle[head++]);
120 if (head == count)
121 break;
122 }
123 free(circle);
124}
125#else
126/* Must never be called */
127void print_except_N_last_bytes(FILE *fp, unsigned count);
128void print_except_N_last_lines(FILE *fp, unsigned count);
129#endif
130
131#if !ENABLE_FEATURE_FANCY_HEAD
132# define eat_num(negative_N,p) eat_num(p)
133#endif
134static unsigned long
135eat_num(bool *negative_N, const char *p)
136{
137#if ENABLE_FEATURE_FANCY_HEAD
138 if (*p == '-') {
139 *negative_N = 1;
140 p++;
141 }
142#endif
143 return xatoul_sfx(p, head_tail_suffixes);
144}
145
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000146static const char head_opts[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 "n:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000148#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 "c:qv"
150#endif
151 ;
John Beppu3157b1f1999-12-10 07:42:50 +0000152
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200153#define header_fmt_str "\n==> %s <==\n"
John Beppu3157b1f1999-12-10 07:42:50 +0000154
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000155int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +0000157{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 unsigned long count = 10;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000159#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 int header_threshhold = 1;
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100161 bool count_bytes = 0;
162 bool negative_N = 0;
163#else
164# define header_threshhold 1
165# define count_bytes 0
166# define negative_N 0
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167#endif
Matt Kraaic0321f92000-09-27 04:09:22 +0000168 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 const char *fmt;
170 char *p;
171 int opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +0000173
Denis Vlasenko08492072006-12-22 13:56:36 +0000174#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000175 /* Allow legacy syntax of an initial numeric option without -n. */
Denis Vlasenko0d873672008-11-11 22:43:10 +0000176 if (argv[1] && argv[1][0] == '-'
Denis Vlasenko459903b2006-11-27 14:44:18 +0000177 && isdigit(argv[1][1])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 ) {
179 --argc;
180 ++argv;
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100181 p = argv[0] + 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +0000183 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +0000184#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +0000185
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000186 /* No size benefit in converting this to getopt32 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000187 while ((opt = getopt(argc, argv, head_opts)) > 0) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000188 switch (opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000189#if ENABLE_FEATURE_FANCY_HEAD
Denis Vlasenko13858992006-10-08 12:49:22 +0000190 case 'q':
191 header_threshhold = INT_MAX;
192 break;
193 case 'v':
194 header_threshhold = -1;
195 break;
196 case 'c':
197 count_bytes = 1;
198 /* fall through */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000199#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000200 case 'n':
201 p = optarg;
Denis Vlasenko08492072006-12-22 13:56:36 +0000202#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
203 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000204#endif
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100205 count = eat_num(&negative_N, p);
Denis Vlasenko13858992006-10-08 12:49:22 +0000206 break;
207 default:
208 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 }
210 }
211
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000212 argc -= optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000214 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000215 *--argv = (char*)"-";
John Beppu3157b1f1999-12-10 07:42:50 +0000216
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 fmt = header_fmt_str + 1;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000218 if (argc <= header_threshhold) {
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100219#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 header_threshhold = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221#else
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000222 fmt += 11; /* "" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223#endif
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100224 }
225 if (negative_N) {
226 if (count >= INT_MAX / sizeof(char*))
227 bb_error_msg("count is too big: %lu", count);
228 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229
230 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000231 fp = fopen_or_warn_stdin(*argv);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000232 if (fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 if (fp == stdin) {
234 *argv = (char *) bb_msg_standard_input;
235 }
236 if (header_threshhold) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000237 printf(fmt, *argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000238 }
Denys Vlasenko40c6da42013-02-25 01:26:09 +0100239 if (negative_N) {
240 if (count_bytes) {
241 print_except_N_last_bytes(fp, count);
242 } else {
243 print_except_N_last_lines(fp, count);
244 }
245 } else {
246 print_first_N(fp, count, count_bytes);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000247 }
Denys Vlasenkoaf0255f2013-02-25 01:24:32 +0100248 die_if_ferror_stdout();
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000249 if (fclose_if_not_stdin(fp)) {
Denis Vlasenko0d873672008-11-11 22:43:10 +0000250 bb_simple_perror_msg(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 retval = EXIT_FAILURE;
252 }
Denis Vlasenko0d873672008-11-11 22:43:10 +0000253 } else {
254 retval = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255 }
256 fmt = header_fmt_str;
257 } while (*++argv);
258
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000259 fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000260}