aboutsummaryrefslogtreecommitdiff
path: root/coreutils/head.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2013-02-25 01:24:32 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2013-02-25 01:24:32 +0100
commitaf0255f49681bcc78830a56af885e891eca210f4 (patch)
tree89fa748e057d2543c69205300f7614a4ef326fdf /coreutils/head.c
parent8e6a1ea8250137d1dd9b0a37b7a1d6d596a91099 (diff)
downloadbusybox-w32-af0255f49681bcc78830a56af885e891eca210f4.tar.gz
busybox-w32-af0255f49681bcc78830a56af885e891eca210f4.tar.bz2
busybox-w32-af0255f49681bcc78830a56af885e891eca210f4.zip
head,tail: use common suffix struct. simplify help text.
function old new delta head_tail_suffixes - 32 +32 head_main 415 406 -9 packed_usage 29252 29234 -18 tail_suffixes 32 - -32 head_suffixes 32 - -32 ------------------------------------------------------------------------------ (add/remove: 2/2 grow/shrink: 0/2 up/down: 32/-91) Total: -59 bytes text data bss dec hex filename 890474 497 7584 898555 db5fb busybox_old 890415 497 7584 898496 db5c0 busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/head.c')
-rw-r--r--coreutils/head.c27
1 files changed, 13 insertions, 14 deletions
diff --git a/coreutils/head.c b/coreutils/head.c
index ec4512765..598fccb64 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -11,6 +11,9 @@
11/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */ 11/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
12/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */ 12/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
13 13
14//kbuild:lib-$(CONFIG_HEAD) += head.o
15//kbuild:lib-$(CONFIG_HEAD) += head_tail.o
16
14//usage:#define head_trivial_usage 17//usage:#define head_trivial_usage
15//usage: "[OPTIONS] [FILE]..." 18//usage: "[OPTIONS] [FILE]..."
16//usage:#define head_full_usage "\n\n" 19//usage:#define head_full_usage "\n\n"
@@ -31,6 +34,7 @@
31//usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n" 34//usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
32 35
33#include "libbb.h" 36#include "libbb.h"
37#include "head_tail.h"
34 38
35/* This is a NOEXEC applet. Be very careful! */ 39/* This is a NOEXEC applet. Be very careful! */
36 40
@@ -41,20 +45,12 @@ static const char head_opts[] ALIGN1 =
41#endif 45#endif
42 ; 46 ;
43 47
44static const struct suffix_mult head_suffixes[] = {
45 { "b", 512 },
46 { "k", 1024 },
47 { "m", 1024*1024 },
48 { "", 0 }
49};
50
51#define header_fmt_str "\n==> %s <==\n" 48#define header_fmt_str "\n==> %s <==\n"
52 49
53int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 50int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54int head_main(int argc, char **argv) 51int head_main(int argc, char **argv)
55{ 52{
56 unsigned long count = 10; 53 unsigned long count = 10;
57 unsigned long i;
58#if ENABLE_FEATURE_FANCY_HEAD 54#if ENABLE_FEATURE_FANCY_HEAD
59 int count_bytes = 0; 55 int count_bytes = 0;
60 int header_threshhold = 1; 56 int header_threshhold = 1;
@@ -63,7 +59,6 @@ int head_main(int argc, char **argv)
63 const char *fmt; 59 const char *fmt;
64 char *p; 60 char *p;
65 int opt; 61 int opt;
66 int c;
67 int retval = EXIT_SUCCESS; 62 int retval = EXIT_SUCCESS;
68 63
69#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD 64#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
@@ -97,7 +92,7 @@ int head_main(int argc, char **argv)
97#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD 92#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
98 GET_COUNT: 93 GET_COUNT:
99#endif 94#endif
100 count = xatoul_sfx(p, head_suffixes); 95 count = xatoul_sfx(p, head_tail_suffixes);
101 break; 96 break;
102 default: 97 default:
103 bb_show_usage(); 98 bb_show_usage();
@@ -127,6 +122,8 @@ int head_main(int argc, char **argv)
127 do { 122 do {
128 fp = fopen_or_warn_stdin(*argv); 123 fp = fopen_or_warn_stdin(*argv);
129 if (fp) { 124 if (fp) {
125 unsigned long i;
126
130 if (fp == stdin) { 127 if (fp == stdin) {
131 *argv = (char *) bb_msg_standard_input; 128 *argv = (char *) bb_msg_standard_input;
132 } 129 }
@@ -134,17 +131,19 @@ int head_main(int argc, char **argv)
134 printf(fmt, *argv); 131 printf(fmt, *argv);
135 } 132 }
136 i = count; 133 i = count;
137 while (i && ((c = getc(fp)) != EOF)) { 134 while (i) {
138 if (count_bytes || (c == '\n')) { 135 int c = getc(fp);
136 if (c == EOF)
137 break;
138 if (count_bytes || (c == '\n'))
139 --i; 139 --i;
140 }
141 putchar(c); 140 putchar(c);
142 } 141 }
142 die_if_ferror_stdout();
143 if (fclose_if_not_stdin(fp)) { 143 if (fclose_if_not_stdin(fp)) {
144 bb_simple_perror_msg(*argv); 144 bb_simple_perror_msg(*argv);
145 retval = EXIT_FAILURE; 145 retval = EXIT_FAILURE;
146 } 146 }
147 die_if_ferror_stdout();
148 } else { 147 } else {
149 retval = EXIT_FAILURE; 148 retval = EXIT_FAILURE;
150 } 149 }