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