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 | |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 16 | #include <limits.h> |
Robert Griebl | 53146cc | 2002-05-27 22:24:53 +0000 | [diff] [blame] | 17 | #include <ctype.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 18 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 19 | #include "busybox.h" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 20 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | static const char head_opts[] = |
| 22 | "n:" |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 23 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 24 | "c:qv" |
| 25 | #endif |
| 26 | ; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 27 | |
"Vladimir N. Oleynik" | e75b41d | 2006-01-30 11:15:11 +0000 | [diff] [blame] | 28 | #if ENABLE_FEATURE_FANCY_HEAD |
| 29 | static const struct suffix_mult head_suffixes[] = { |
| 30 | { "b", 512 }, |
| 31 | { "k", 1024 }, |
| 32 | { "m", 1024*1024 }, |
| 33 | { NULL, 0 } |
| 34 | }; |
| 35 | #endif |
| 36 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | static const char header_fmt_str[] = "\n==> %s <==\n"; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 38 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | int head_main(int argc, char **argv) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 40 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | unsigned long count = 10; |
| 42 | unsigned long i; |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 43 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | int count_bytes = 0; |
| 45 | int header_threshhold = 1; |
| 46 | #endif |
| 47 | |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 48 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | const char *fmt; |
| 50 | char *p; |
| 51 | int opt; |
| 52 | int c; |
| 53 | int retval = EXIT_SUCCESS; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 54 | |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 55 | #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | /* Allow legacy syntax of an initial numeric option without -n. */ |
| 57 | if ((argc > 1) && (argv[1][0] == '-') |
| 58 | /* && (isdigit)(argv[1][1]) */ |
| 59 | && (((unsigned int)(argv[1][1] - '0')) <= 9) |
| 60 | ) { |
| 61 | --argc; |
| 62 | ++argv; |
| 63 | p = (*argv) + 1; |
| 64 | goto GET_COUNT; |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 65 | } |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame] | 66 | #endif |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 67 | |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame] | 68 | /* No size benefit in converting this to bb_getopt_ulflags */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 69 | while ((opt = getopt(argc, argv, head_opts)) > 0) { |
| 70 | switch(opt) { |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 71 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | case 'q': |
| 73 | header_threshhold = INT_MAX; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | break; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | case 'v': |
| 76 | header_threshhold = -1; |
| 77 | break; |
| 78 | case 'c': |
| 79 | count_bytes = 1; |
| 80 | /* fall through */ |
| 81 | #endif |
| 82 | case 'n': |
| 83 | p = optarg; |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 84 | #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | GET_COUNT: |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 86 | #endif |
"Vladimir N. Oleynik" | e75b41d | 2006-01-30 11:15:11 +0000 | [diff] [blame] | 87 | |
| 88 | #if !ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | count = bb_xgetularg10(p); |
"Vladimir N. Oleynik" | e75b41d | 2006-01-30 11:15:11 +0000 | [diff] [blame] | 90 | #else |
| 91 | count = bb_xgetularg_bnd_sfx(p, 10, |
| 92 | 0, ULONG_MAX, |
| 93 | head_suffixes); |
| 94 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | break; |
| 96 | default: |
| 97 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | argv += optind; |
| 102 | if (!*argv) { |
| 103 | *--argv = "-"; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 104 | } |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 105 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | fmt = header_fmt_str + 1; |
Bernhard Reutner-Fischer | 5816ccb | 2005-12-13 10:48:45 +0000 | [diff] [blame] | 107 | #if ENABLE_FEATURE_FANCY_HEAD |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | if (argc - optind <= header_threshhold) { |
| 109 | header_threshhold = 0; |
| 110 | } |
| 111 | #else |
| 112 | if (argc <= optind + 1) { |
| 113 | fmt += 11; |
| 114 | } |
| 115 | /* Now define some things here to avoid #ifdefs in the code below. |
| 116 | * These should optimize out of the if conditions below. */ |
| 117 | #define header_threshhold 1 |
| 118 | #define count_bytes 0 |
| 119 | #endif |
| 120 | |
| 121 | do { |
| 122 | if ((fp = bb_wfopen_input(*argv)) != NULL) { |
| 123 | if (fp == stdin) { |
| 124 | *argv = (char *) bb_msg_standard_input; |
| 125 | } |
| 126 | if (header_threshhold) { |
| 127 | bb_printf(fmt, *argv); |
| 128 | } |
| 129 | i = count; |
| 130 | while (i && ((c = getc(fp)) != EOF)) { |
| 131 | if (count_bytes || (c == '\n')) { |
| 132 | --i; |
| 133 | } |
| 134 | putchar(c); |
| 135 | } |
| 136 | if (bb_fclose_nonstdin(fp)) { |
| 137 | bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ |
| 138 | retval = EXIT_FAILURE; |
| 139 | } |
| 140 | bb_xferror_stdout(); |
| 141 | } |
| 142 | fmt = header_fmt_str; |
| 143 | } while (*++argv); |
| 144 | |
| 145 | bb_fflush_stdout_and_exit(retval); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 146 | } |