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