Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * head implementation for busybox |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame^] | 9 | //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 Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 26 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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 */ |
| 30 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 36 | //usage: "\n -n N[kbm] Print first N lines" |
| 37 | //usage: IF_FEATURE_FANCY_HEAD( |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 38 | //usage: "\n -n -N[kbm] Print all except N last lines" |
| 39 | //usage: "\n -c [-]N[kbm] Print first N bytes" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 40 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 51 | #include "libbb.h" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 52 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 53 | /* This is a NOEXEC applet. Be very careful! */ |
| 54 | |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 55 | #if !ENABLE_FEATURE_FANCY_HEAD |
| 56 | # define print_first_N(fp,count,bytes) print_first_N(fp,count) |
| 57 | #endif |
| 58 | static void |
| 59 | print_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 |
| 75 | static void |
| 76 | print_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 | |
| 104 | static void |
| 105 | print_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 */ |
| 140 | void print_except_N_last_bytes(FILE *fp, unsigned count); |
| 141 | void 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 |
| 147 | static unsigned long |
| 148 | eat_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 Vlasenko | c72b43c | 2013-07-13 23:49:45 +0200 | [diff] [blame] | 156 | return xatoul_sfx(p, bkm_suffixes); |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 159 | static const char head_opts[] ALIGN1 = |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 160 | "n:" |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 161 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 162 | "c:qv" |
| 163 | #endif |
| 164 | ; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 165 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 166 | #define header_fmt_str "\n==> %s <==\n" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 168 | int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | int head_main(int argc, char **argv) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 170 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 171 | unsigned long count = 10; |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 172 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | int header_threshhold = 1; |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 174 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 180 | #endif |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 181 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 182 | const char *fmt; |
| 183 | char *p; |
| 184 | int opt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 185 | int retval = EXIT_SUCCESS; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 187 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 188 | /* Allow legacy syntax of an initial numeric option without -n. */ |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 189 | if (argv[1] && argv[1][0] == '-' |
Denis Vlasenko | 459903b | 2006-11-27 14:44:18 +0000 | [diff] [blame] | 190 | && isdigit(argv[1][1]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 191 | ) { |
| 192 | --argc; |
| 193 | ++argv; |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 194 | p = argv[0] + 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 195 | goto GET_COUNT; |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 196 | } |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame] | 197 | #endif |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 199 | /* No size benefit in converting this to getopt32 */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 200 | while ((opt = getopt(argc, argv, head_opts)) > 0) { |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 201 | switch (opt) { |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 202 | #if ENABLE_FEATURE_FANCY_HEAD |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 203 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 212 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 213 | case 'n': |
| 214 | p = optarg; |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 215 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
| 216 | GET_COUNT: |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 217 | #endif |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 218 | count = eat_num(&negative_N, p); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 219 | break; |
| 220 | default: |
| 221 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 225 | argc -= optind; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | argv += optind; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 227 | if (!*argv) |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 228 | *--argv = (char*)"-"; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 229 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 230 | fmt = header_fmt_str + 1; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 231 | if (argc <= header_threshhold) { |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 232 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 233 | header_threshhold = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 234 | #else |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 235 | fmt += 11; /* "" */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 236 | #endif |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 237 | } |
| 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 242 | |
| 243 | do { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 244 | fp = fopen_or_warn_stdin(*argv); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 245 | if (fp) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 246 | if (fp == stdin) { |
| 247 | *argv = (char *) bb_msg_standard_input; |
| 248 | } |
| 249 | if (header_threshhold) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 250 | printf(fmt, *argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 251 | } |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 252 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 260 | } |
Denys Vlasenko | af0255f | 2013-02-25 01:24:32 +0100 | [diff] [blame] | 261 | die_if_ferror_stdout(); |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 262 | if (fclose_if_not_stdin(fp)) { |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 263 | bb_simple_perror_msg(*argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 264 | retval = EXIT_FAILURE; |
| 265 | } |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 266 | } else { |
| 267 | retval = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 268 | } |
| 269 | fmt = header_fmt_str; |
| 270 | } while (*++argv); |
| 271 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 272 | fflush_stdout_and_exit(retval); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 273 | } |