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