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 | * |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 16 | static const char head_opts[] ALIGN1 = |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | "n:" |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 18 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | "c:qv" |
| 20 | #endif |
| 21 | ; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 22 | |
"Vladimir N. Oleynik" | e75b41d | 2006-01-30 11:15:11 +0000 | [diff] [blame] | 23 | static const struct suffix_mult head_suffixes[] = { |
| 24 | { "b", 512 }, |
| 25 | { "k", 1024 }, |
| 26 | { "m", 1024*1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 27 | { "", 0 } |
"Vladimir N. Oleynik" | e75b41d | 2006-01-30 11:15:11 +0000 | [diff] [blame] | 28 | }; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 29 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 30 | #define header_fmt_str "\n==> %s <==\n" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 32 | int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | int head_main(int argc, char **argv) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 34 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | unsigned long count = 10; |
| 36 | unsigned long i; |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 37 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | int count_bytes = 0; |
| 39 | int header_threshhold = 1; |
| 40 | #endif |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 41 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | const char *fmt; |
| 43 | char *p; |
| 44 | int opt; |
| 45 | int c; |
| 46 | int retval = EXIT_SUCCESS; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 48 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | /* Allow legacy syntax of an initial numeric option without -n. */ |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 50 | if (argv[1] && argv[1][0] == '-' |
Denis Vlasenko | 459903b | 2006-11-27 14:44:18 +0000 | [diff] [blame] | 51 | && isdigit(argv[1][1]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | ) { |
| 53 | --argc; |
| 54 | ++argv; |
| 55 | p = (*argv) + 1; |
| 56 | goto GET_COUNT; |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 57 | } |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame] | 58 | #endif |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 60 | /* No size benefit in converting this to getopt32 */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | while ((opt = getopt(argc, argv, head_opts)) > 0) { |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 62 | switch (opt) { |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 63 | #if ENABLE_FEATURE_FANCY_HEAD |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 64 | case 'q': |
| 65 | header_threshhold = INT_MAX; |
| 66 | break; |
| 67 | case 'v': |
| 68 | header_threshhold = -1; |
| 69 | break; |
| 70 | case 'c': |
| 71 | count_bytes = 1; |
| 72 | /* fall through */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 74 | case 'n': |
| 75 | p = optarg; |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 76 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
| 77 | GET_COUNT: |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 78 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 79 | count = xatoul_sfx(p, head_suffixes); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 80 | break; |
| 81 | default: |
| 82 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 86 | argc -= optind; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | argv += optind; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 88 | if (!*argv) |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 89 | *--argv = (char*)"-"; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 90 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | fmt = header_fmt_str + 1; |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 92 | #if ENABLE_FEATURE_FANCY_HEAD |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 93 | if (argc <= header_threshhold) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | header_threshhold = 0; |
| 95 | } |
| 96 | #else |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 97 | if (argc <= 1) { |
| 98 | fmt += 11; /* "" */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | } |
| 100 | /* Now define some things here to avoid #ifdefs in the code below. |
| 101 | * These should optimize out of the if conditions below. */ |
| 102 | #define header_threshhold 1 |
| 103 | #define count_bytes 0 |
| 104 | #endif |
| 105 | |
| 106 | do { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 107 | fp = fopen_or_warn_stdin(*argv); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 108 | if (fp) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | if (fp == stdin) { |
| 110 | *argv = (char *) bb_msg_standard_input; |
| 111 | } |
| 112 | if (header_threshhold) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 113 | printf(fmt, *argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | } |
| 115 | i = count; |
| 116 | while (i && ((c = getc(fp)) != EOF)) { |
| 117 | if (count_bytes || (c == '\n')) { |
| 118 | --i; |
| 119 | } |
| 120 | putchar(c); |
| 121 | } |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 122 | if (fclose_if_not_stdin(fp)) { |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 123 | bb_simple_perror_msg(*argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 124 | retval = EXIT_FAILURE; |
| 125 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 126 | die_if_ferror_stdout(); |
Denis Vlasenko | 0d87367 | 2008-11-11 22:43:10 +0000 | [diff] [blame] | 127 | } else { |
| 128 | retval = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | } |
| 130 | fmt = header_fmt_str; |
| 131 | } while (*++argv); |
| 132 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 133 | fflush_stdout_and_exit(retval); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 134 | } |