[git commit] head, tail: use common suffix struct. simplify help text.

Denys Vlasenko vda.linux at googlemail.com
Mon Feb 25 00:24:32 UTC 2013


commit: http://git.busybox.net/busybox/commit/?id=af0255f49681bcc78830a56af885e891eca210f4
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

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 at googlemail.com>
---
 coreutils/Kbuild.src  |    2 --
 coreutils/head.c      |   27 +++++++++++++--------------
 coreutils/head_tail.c |   14 ++++++++++++++
 coreutils/head_tail.h |    6 ++++++
 coreutils/tail.c      |   18 +++++++-----------
 5 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/coreutils/Kbuild.src b/coreutils/Kbuild.src
index b715b9c..ec4ef7d 100644
--- a/coreutils/Kbuild.src
+++ b/coreutils/Kbuild.src
@@ -35,7 +35,6 @@ lib-$(CONFIG_EXPAND)    += expand.o
 lib-$(CONFIG_FALSE)     += false.o
 lib-$(CONFIG_FOLD)      += fold.o
 lib-$(CONFIG_FSYNC)     += fsync.o
-lib-$(CONFIG_HEAD)      += head.o
 lib-$(CONFIG_INSTALL)   += install.o
 #lib-$(CONFIG_LENGTH)    += length.o
 lib-$(CONFIG_LN)        += ln.o
@@ -71,7 +70,6 @@ lib-$(CONFIG_STTY)      += stty.o
 lib-$(CONFIG_SUM)       += sum.o
 lib-$(CONFIG_SYNC)      += sync.o
 lib-$(CONFIG_TAC)       += tac.o
-lib-$(CONFIG_TAIL)      += tail.o
 lib-$(CONFIG_TEE)       += tee.o
 lib-$(CONFIG_TRUE)      += true.o
 lib-$(CONFIG_TTY)       += tty.o
diff --git a/coreutils/head.c b/coreutils/head.c
index ec45127..598fccb 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -11,6 +11,9 @@
 /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
 /* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
 
+//kbuild:lib-$(CONFIG_HEAD) += head.o
+//kbuild:lib-$(CONFIG_HEAD) += head_tail.o
+
 //usage:#define head_trivial_usage
 //usage:       "[OPTIONS] [FILE]..."
 //usage:#define head_full_usage "\n\n"
@@ -31,6 +34,7 @@
 //usage:       "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
 
 #include "libbb.h"
+#include "head_tail.h"
 
 /* This is a NOEXEC applet. Be very careful! */
 
@@ -41,20 +45,12 @@ static const char head_opts[] ALIGN1 =
 #endif
 	;
 
-static const struct suffix_mult head_suffixes[] = {
-	{ "b", 512 },
-	{ "k", 1024 },
-	{ "m", 1024*1024 },
-	{ "", 0 }
-};
-
 #define header_fmt_str "\n==> %s <==\n"
 
 int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int head_main(int argc, char **argv)
 {
 	unsigned long count = 10;
-	unsigned long i;
 #if ENABLE_FEATURE_FANCY_HEAD
 	int count_bytes = 0;
 	int header_threshhold = 1;
@@ -63,7 +59,6 @@ int head_main(int argc, char **argv)
 	const char *fmt;
 	char *p;
 	int opt;
-	int c;
 	int retval = EXIT_SUCCESS;
 
 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
@@ -97,7 +92,7 @@ int head_main(int argc, char **argv)
 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
  GET_COUNT:
 #endif
-			count = xatoul_sfx(p, head_suffixes);
+			count = xatoul_sfx(p, head_tail_suffixes);
 			break;
 		default:
 			bb_show_usage();
@@ -127,6 +122,8 @@ int head_main(int argc, char **argv)
 	do {
 		fp = fopen_or_warn_stdin(*argv);
 		if (fp) {
+			unsigned long i;
+
 			if (fp == stdin) {
 				*argv = (char *) bb_msg_standard_input;
 			}
@@ -134,17 +131,19 @@ int head_main(int argc, char **argv)
 				printf(fmt, *argv);
 			}
 			i = count;
-			while (i && ((c = getc(fp)) != EOF)) {
-				if (count_bytes || (c == '\n')) {
+			while (i) {
+				int c = getc(fp);
+				if (c == EOF)
+					break;
+				if (count_bytes || (c == '\n'))
 					--i;
-				}
 				putchar(c);
 			}
+			die_if_ferror_stdout();
 			if (fclose_if_not_stdin(fp)) {
 				bb_simple_perror_msg(*argv);
 				retval = EXIT_FAILURE;
 			}
-			die_if_ferror_stdout();
 		} else {
 			retval = EXIT_FAILURE;
 		}
diff --git a/coreutils/head_tail.c b/coreutils/head_tail.c
new file mode 100644
index 0000000..1658c0d
--- /dev/null
+++ b/coreutils/head_tail.c
@@ -0,0 +1,14 @@
+/*
+ * Copyright (C) 2013 Denys Vlasenko
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ */
+#include "libbb.h"
+#include "head_tail.h"
+
+const struct suffix_mult head_tail_suffixes[] = {
+	{ "b", 512 },
+	{ "k", 1024 },
+	{ "m", 1024*1024 },
+	{ "", 0 }
+};
diff --git a/coreutils/head_tail.h b/coreutils/head_tail.h
new file mode 100644
index 0000000..df19e41
--- /dev/null
+++ b/coreutils/head_tail.h
@@ -0,0 +1,6 @@
+/*
+ * Copyright (C) 2013 Denys Vlasenko
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ */
+extern const struct suffix_mult head_tail_suffixes[];
diff --git a/coreutils/tail.c b/coreutils/tail.c
index b376ec8..87251da 100644
--- a/coreutils/tail.c
+++ b/coreutils/tail.c
@@ -24,6 +24,9 @@
  * 7) lseek attempted when count==0 even if arg was +0 (from top)
  */
 
+//kbuild:lib-$(CONFIG_TAIL) += tail.o
+//kbuild:lib-$(CONFIG_TAIL) += head_tail.o
+
 //usage:#define tail_trivial_usage
 //usage:       "[OPTIONS] [FILE]..."
 //usage:#define tail_full_usage "\n\n"
@@ -34,14 +37,13 @@
 //usage:     "\n	-s SECONDS	Wait SECONDS between reads with -f"
 //usage:	)
 //usage:     "\n	-n N[kbm]	Print last N lines"
+//usage:     "\n	-n +N[kbm]	Skip N lines and print the rest"
 //usage:	IF_FEATURE_FANCY_TAIL(
-//usage:     "\n	-c N[kbm]	Print last N bytes"
+//usage:     "\n	-c [+]N[kbm]	Print last N bytes"
 //usage:     "\n	-q		Never print headers"
 //usage:     "\n	-v		Always print headers"
 //usage:     "\n"
 //usage:     "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
-//usage:     "\nIf N starts with a '+', output begins with the Nth item from the start"
-//usage:     "\nof each file, not from the end."
 //usage:	)
 //usage:
 //usage:#define tail_example_usage
@@ -49,13 +51,7 @@
 //usage:       "nameserver 10.0.0.1\n"
 
 #include "libbb.h"
-
-static const struct suffix_mult tail_suffixes[] = {
-	{ "b", 512 },
-	{ "k", 1024 },
-	{ "m", 1024*1024 },
-	{ "", 0 }
-};
+#include "head_tail.h"
 
 struct globals {
 	bool from_top;
@@ -102,7 +98,7 @@ static unsigned eat_num(const char *p)
 		p++;
 		G.from_top = 1;
 	}
-	return xatou_sfx(p, tail_suffixes);
+	return xatou_sfx(p, head_tail_suffixes);
 }
 
 int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;


More information about the busybox-cvs mailing list