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 |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "head (3.8 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: head is used to print the first specified number of lines |
| 14 | //config: from files. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | //config: |
| 16 | //config:config FEATURE_FANCY_HEAD |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 17 | //config: bool "Enable -c, -q, and -v" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 18 | //config: default y |
| 19 | //config: depends on HEAD |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 20 | |
| 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 Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 24 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | /* 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 Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 29 | //usage:#define head_trivial_usage |
| 30 | //usage: "[OPTIONS] [FILE]..." |
| 31 | //usage:#define head_full_usage "\n\n" |
Denys Vlasenko | b9258b8 | 2021-06-02 04:01:10 +0200 | [diff] [blame] | 32 | //usage: "Print first 10 lines of FILEs (or stdin).\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 33 | //usage: "With more than one FILE, precede each with a filename header.\n" |
Denys Vlasenko | b9258b8 | 2021-06-02 04:01:10 +0200 | [diff] [blame] | 34 | //usage: "\n -n N[bkm] Print first N lines" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 35 | //usage: IF_FEATURE_FANCY_HEAD( |
Denys Vlasenko | b9258b8 | 2021-06-02 04:01:10 +0200 | [diff] [blame] | 36 | //usage: "\n -n -N[bkm] Print all except N last lines" |
| 37 | //usage: "\n -c [-]N[bkm] Print first N bytes" |
| 38 | //usage: ) |
| 39 | //usage: "\n (b:*512 k:*1024 m:*1024^2)" |
| 40 | //usage: IF_FEATURE_FANCY_HEAD( |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 41 | //usage: "\n -q Never print headers" |
| 42 | //usage: "\n -v Always print headers" |
| 43 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 44 | //usage: |
| 45 | //usage:#define head_example_usage |
| 46 | //usage: "$ head -n 2 /etc/passwd\n" |
| 47 | //usage: "root:x:0:0:root:/root:/bin/bash\n" |
| 48 | //usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n" |
| 49 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 50 | #include "libbb.h" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 51 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 52 | /* This is a NOEXEC applet. Be very careful! */ |
| 53 | |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 54 | #if !ENABLE_FEATURE_FANCY_HEAD |
| 55 | # define print_first_N(fp,count,bytes) print_first_N(fp,count) |
| 56 | #endif |
| 57 | static void |
| 58 | print_first_N(FILE *fp, unsigned long count, bool count_bytes) |
| 59 | { |
| 60 | #if !ENABLE_FEATURE_FANCY_HEAD |
| 61 | const int count_bytes = 0; |
| 62 | #endif |
| 63 | while (count) { |
| 64 | int c = getc(fp); |
| 65 | if (c == EOF) |
| 66 | break; |
| 67 | if (count_bytes || (c == '\n')) |
| 68 | --count; |
| 69 | putchar(c); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #if ENABLE_FEATURE_FANCY_HEAD |
| 74 | static void |
| 75 | print_except_N_last_bytes(FILE *fp, unsigned count) |
| 76 | { |
| 77 | unsigned char *circle = xmalloc(++count); |
| 78 | unsigned head = 0; |
Denys Vlasenko | 1e825ac | 2022-01-18 00:31:27 +0100 | [diff] [blame] | 79 | for (;;) { |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 80 | int c; |
| 81 | c = getc(fp); |
| 82 | if (c == EOF) |
| 83 | goto ret; |
| 84 | circle[head++] = c; |
| 85 | if (head == count) |
| 86 | break; |
| 87 | } |
| 88 | for (;;) { |
| 89 | int c; |
| 90 | if (head == count) |
| 91 | head = 0; |
| 92 | putchar(circle[head]); |
| 93 | c = getc(fp); |
| 94 | if (c == EOF) |
| 95 | goto ret; |
| 96 | circle[head] = c; |
| 97 | head++; |
| 98 | } |
| 99 | ret: |
| 100 | free(circle); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | print_except_N_last_lines(FILE *fp, unsigned count) |
| 105 | { |
| 106 | char **circle = xzalloc((++count) * sizeof(circle[0])); |
| 107 | unsigned head = 0; |
Denys Vlasenko | 1e825ac | 2022-01-18 00:31:27 +0100 | [diff] [blame] | 108 | for (;;) { |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 109 | char *c; |
| 110 | c = xmalloc_fgets(fp); |
| 111 | if (!c) |
| 112 | goto ret; |
| 113 | circle[head++] = c; |
| 114 | if (head == count) |
| 115 | break; |
| 116 | } |
| 117 | for (;;) { |
| 118 | char *c; |
| 119 | if (head == count) |
| 120 | head = 0; |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 121 | fputs_stdout(circle[head]); |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 122 | c = xmalloc_fgets(fp); |
| 123 | if (!c) |
| 124 | goto ret; |
| 125 | free(circle[head]); |
| 126 | circle[head++] = c; |
| 127 | } |
| 128 | ret: |
| 129 | head = 0; |
Denys Vlasenko | 1e825ac | 2022-01-18 00:31:27 +0100 | [diff] [blame] | 130 | for (;;) { |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 131 | free(circle[head++]); |
| 132 | if (head == count) |
| 133 | break; |
| 134 | } |
| 135 | free(circle); |
| 136 | } |
| 137 | #else |
| 138 | /* Must never be called */ |
| 139 | void print_except_N_last_bytes(FILE *fp, unsigned count); |
| 140 | void print_except_N_last_lines(FILE *fp, unsigned count); |
| 141 | #endif |
| 142 | |
| 143 | #if !ENABLE_FEATURE_FANCY_HEAD |
| 144 | # define eat_num(negative_N,p) eat_num(p) |
| 145 | #endif |
| 146 | static unsigned long |
| 147 | eat_num(bool *negative_N, const char *p) |
| 148 | { |
| 149 | #if ENABLE_FEATURE_FANCY_HEAD |
| 150 | if (*p == '-') { |
| 151 | *negative_N = 1; |
| 152 | p++; |
| 153 | } |
| 154 | #endif |
Denys Vlasenko | c72b43c | 2013-07-13 23:49:45 +0200 | [diff] [blame] | 155 | return xatoul_sfx(p, bkm_suffixes); |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 158 | static const char head_opts[] ALIGN1 = |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 159 | "n:" |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 160 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 161 | "c:qv" |
| 162 | #endif |
| 163 | ; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 164 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 165 | #define header_fmt_str "\n==> %s <==\n" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 166 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 167 | int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 168 | int head_main(int argc, char **argv) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 169 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 170 | unsigned long count = 10; |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 171 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | int header_threshhold = 1; |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 173 | bool count_bytes = 0; |
| 174 | bool negative_N = 0; |
| 175 | #else |
| 176 | # define header_threshhold 1 |
| 177 | # define count_bytes 0 |
| 178 | # define negative_N 0 |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 179 | #endif |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 180 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 181 | const char *fmt; |
| 182 | char *p; |
| 183 | int opt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 184 | int retval = EXIT_SUCCESS; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 186 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 187 | /* Allow legacy syntax of an initial numeric option without -n. */ |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 188 | if (argv[1] && argv[1][0] == '-' |
Denis Vlasenko | 459903b | 2006-11-27 14:44:18 +0000 | [diff] [blame] | 189 | && isdigit(argv[1][1]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 190 | ) { |
| 191 | --argc; |
| 192 | ++argv; |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 193 | p = argv[0] + 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 194 | goto GET_COUNT; |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 195 | } |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame] | 196 | #endif |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 197 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 198 | /* No size benefit in converting this to getopt32 */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 199 | while ((opt = getopt(argc, argv, head_opts)) > 0) { |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 200 | switch (opt) { |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 201 | #if ENABLE_FEATURE_FANCY_HEAD |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 202 | case 'q': |
| 203 | header_threshhold = INT_MAX; |
| 204 | break; |
| 205 | case 'v': |
| 206 | header_threshhold = -1; |
| 207 | break; |
| 208 | case 'c': |
| 209 | count_bytes = 1; |
| 210 | /* fall through */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 211 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 212 | case 'n': |
| 213 | p = optarg; |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 214 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
| 215 | GET_COUNT: |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 216 | #endif |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 217 | count = eat_num(&negative_N, p); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 218 | break; |
| 219 | default: |
| 220 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 224 | argc -= optind; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 225 | argv += optind; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 226 | if (!*argv) |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 227 | *--argv = (char*)"-"; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 228 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 229 | fmt = header_fmt_str + 1; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 230 | if (argc <= header_threshhold) { |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 231 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 232 | header_threshhold = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 233 | #else |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 234 | fmt += 11; /* "" */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 235 | #endif |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 236 | } |
| 237 | if (negative_N) { |
| 238 | if (count >= INT_MAX / sizeof(char*)) |
| 239 | bb_error_msg("count is too big: %lu", count); |
| 240 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 241 | |
| 242 | do { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 243 | fp = fopen_or_warn_stdin(*argv); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 244 | if (fp) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 245 | if (fp == stdin) { |
| 246 | *argv = (char *) bb_msg_standard_input; |
| 247 | } |
| 248 | if (header_threshhold) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 249 | printf(fmt, *argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 250 | } |
Denys Vlasenko | 40c6da4 | 2013-02-25 01:26:09 +0100 | [diff] [blame] | 251 | if (negative_N) { |
| 252 | if (count_bytes) { |
| 253 | print_except_N_last_bytes(fp, count); |
| 254 | } else { |
| 255 | print_except_N_last_lines(fp, count); |
| 256 | } |
| 257 | } else { |
| 258 | print_first_N(fp, count, count_bytes); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 259 | } |
Denys Vlasenko | af0255f | 2013-02-25 01:24:32 +0100 | [diff] [blame] | 260 | die_if_ferror_stdout(); |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 261 | if (fclose_if_not_stdin(fp)) { |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 262 | bb_simple_perror_msg(*argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 263 | retval = EXIT_FAILURE; |
| 264 | } |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 265 | } else { |
| 266 | retval = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 267 | } |
| 268 | fmt = header_fmt_str; |
| 269 | } while (*++argv); |
| 270 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 271 | fflush_stdout_and_exit(retval); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 272 | } |