aboutsummaryrefslogtreecommitdiff
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
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>
-rw-r--r--coreutils/Kbuild.src2
-rw-r--r--coreutils/head.c27
-rw-r--r--coreutils/head_tail.c14
-rw-r--r--coreutils/head_tail.h6
-rw-r--r--coreutils/tail.c18
5 files changed, 40 insertions, 27 deletions
diff --git a/coreutils/Kbuild.src b/coreutils/Kbuild.src
index b715b9c47..ec4ef7df2 100644
--- a/coreutils/Kbuild.src
+++ b/coreutils/Kbuild.src
@@ -35,7 +35,6 @@ lib-$(CONFIG_EXPAND) += expand.o
35lib-$(CONFIG_FALSE) += false.o 35lib-$(CONFIG_FALSE) += false.o
36lib-$(CONFIG_FOLD) += fold.o 36lib-$(CONFIG_FOLD) += fold.o
37lib-$(CONFIG_FSYNC) += fsync.o 37lib-$(CONFIG_FSYNC) += fsync.o
38lib-$(CONFIG_HEAD) += head.o
39lib-$(CONFIG_INSTALL) += install.o 38lib-$(CONFIG_INSTALL) += install.o
40#lib-$(CONFIG_LENGTH) += length.o 39#lib-$(CONFIG_LENGTH) += length.o
41lib-$(CONFIG_LN) += ln.o 40lib-$(CONFIG_LN) += ln.o
@@ -71,7 +70,6 @@ lib-$(CONFIG_STTY) += stty.o
71lib-$(CONFIG_SUM) += sum.o 70lib-$(CONFIG_SUM) += sum.o
72lib-$(CONFIG_SYNC) += sync.o 71lib-$(CONFIG_SYNC) += sync.o
73lib-$(CONFIG_TAC) += tac.o 72lib-$(CONFIG_TAC) += tac.o
74lib-$(CONFIG_TAIL) += tail.o
75lib-$(CONFIG_TEE) += tee.o 73lib-$(CONFIG_TEE) += tee.o
76lib-$(CONFIG_TRUE) += true.o 74lib-$(CONFIG_TRUE) += true.o
77lib-$(CONFIG_TTY) += tty.o 75lib-$(CONFIG_TTY) += tty.o
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 }
diff --git a/coreutils/head_tail.c b/coreutils/head_tail.c
new file mode 100644
index 000000000..1658c0d1b
--- /dev/null
+++ b/coreutils/head_tail.c
@@ -0,0 +1,14 @@
1/*
2 * Copyright (C) 2013 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6#include "libbb.h"
7#include "head_tail.h"
8
9const struct suffix_mult head_tail_suffixes[] = {
10 { "b", 512 },
11 { "k", 1024 },
12 { "m", 1024*1024 },
13 { "", 0 }
14};
diff --git a/coreutils/head_tail.h b/coreutils/head_tail.h
new file mode 100644
index 000000000..df19e41e0
--- /dev/null
+++ b/coreutils/head_tail.h
@@ -0,0 +1,6 @@
1/*
2 * Copyright (C) 2013 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6extern const struct suffix_mult head_tail_suffixes[];
diff --git a/coreutils/tail.c b/coreutils/tail.c
index b376ec863..87251da83 100644
--- a/coreutils/tail.c
+++ b/coreutils/tail.c
@@ -24,6 +24,9 @@
24 * 7) lseek attempted when count==0 even if arg was +0 (from top) 24 * 7) lseek attempted when count==0 even if arg was +0 (from top)
25 */ 25 */
26 26
27//kbuild:lib-$(CONFIG_TAIL) += tail.o
28//kbuild:lib-$(CONFIG_TAIL) += head_tail.o
29
27//usage:#define tail_trivial_usage 30//usage:#define tail_trivial_usage
28//usage: "[OPTIONS] [FILE]..." 31//usage: "[OPTIONS] [FILE]..."
29//usage:#define tail_full_usage "\n\n" 32//usage:#define tail_full_usage "\n\n"
@@ -34,14 +37,13 @@
34//usage: "\n -s SECONDS Wait SECONDS between reads with -f" 37//usage: "\n -s SECONDS Wait SECONDS between reads with -f"
35//usage: ) 38//usage: )
36//usage: "\n -n N[kbm] Print last N lines" 39//usage: "\n -n N[kbm] Print last N lines"
40//usage: "\n -n +N[kbm] Skip N lines and print the rest"
37//usage: IF_FEATURE_FANCY_TAIL( 41//usage: IF_FEATURE_FANCY_TAIL(
38//usage: "\n -c N[kbm] Print last N bytes" 42//usage: "\n -c [+]N[kbm] Print last N bytes"
39//usage: "\n -q Never print headers" 43//usage: "\n -q Never print headers"
40//usage: "\n -v Always print headers" 44//usage: "\n -v Always print headers"
41//usage: "\n" 45//usage: "\n"
42//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)." 46//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
43//usage: "\nIf N starts with a '+', output begins with the Nth item from the start"
44//usage: "\nof each file, not from the end."
45//usage: ) 47//usage: )
46//usage: 48//usage:
47//usage:#define tail_example_usage 49//usage:#define tail_example_usage
@@ -49,13 +51,7 @@
49//usage: "nameserver 10.0.0.1\n" 51//usage: "nameserver 10.0.0.1\n"
50 52
51#include "libbb.h" 53#include "libbb.h"
52 54#include "head_tail.h"
53static const struct suffix_mult tail_suffixes[] = {
54 { "b", 512 },
55 { "k", 1024 },
56 { "m", 1024*1024 },
57 { "", 0 }
58};
59 55
60struct globals { 56struct globals {
61 bool from_top; 57 bool from_top;
@@ -102,7 +98,7 @@ static unsigned eat_num(const char *p)
102 p++; 98 p++;
103 G.from_top = 1; 99 G.from_top = 1;
104 } 100 }
105 return xatou_sfx(p, tail_suffixes); 101 return xatou_sfx(p, head_tail_suffixes);
106} 102}
107 103
108int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 104int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;