[git commit] seq: accept negative parameters

Denys Vlasenko vda.linux at googlemail.com
Mon Apr 10 12:33:20 UTC 2023


commit: https://git.busybox.net/busybox/commit/?id=9bc2b6e88474a8ebb50c94f29320d343bd374928
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
seq_main                                             429     476     +47
packed_usage                                       34557   34538     -19
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 47/-19)             Total: 28 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/seq.c     | 23 +++++++++++++++++++++--
 testsuite/seq.tests |  3 +++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/coreutils/seq.c b/coreutils/seq.c
index beb339d1e..c0e2d1e06 100644
--- a/coreutils/seq.c
+++ b/coreutils/seq.c
@@ -22,7 +22,7 @@
 //usage:#define seq_full_usage "\n\n"
 //usage:       "Print numbers from FIRST to LAST, in steps of INC.\n"
 //usage:       "FIRST, INC default to 1.\n"
-//usage:     "\n	-w	Pad to last with leading zeros"
+//usage:     "\n	-w	Pad with leading zeros"
 //usage:     "\n	-s SEP	String separator"
 
 #include "libbb.h"
@@ -41,6 +41,7 @@ int seq_main(int argc, char **argv)
 	unsigned width;
 	unsigned frac_part;
 	const char *sep, *opt_s = "\n";
+	char *saved;
 	unsigned opt;
 
 #if ENABLE_LOCALE_SUPPORT
@@ -49,7 +50,25 @@ int seq_main(int argc, char **argv)
 	setlocale(LC_NUMERIC, "C");
 #endif
 
-	opt = getopt32(argv, "+ws:", &opt_s);
+	/* Cater for negative arguments: if we see one, truncate argv[] on it */
+	n = 0;
+	for (;;) {
+		char c;
+		saved = argv[++n];
+		if (!saved)
+			break;
+		if (saved[0] != '-')
+			break;
+		c = saved[1];
+		if (c == '.' || (c >= '0' && c <= '9')) {
+			argv[n] = NULL;
+			break;
+		}
+	}
+	opt = getopt32(argv, "+ws:", &opt_s); /* "+": stop at first non-option */
+	/* Restore possibly truncated argv[] */
+	argv[n] = saved;
+
 	argc -= optind;
 	argv += optind;
 	first = increment = 1;
diff --git a/testsuite/seq.tests b/testsuite/seq.tests
index 1e1116c7d..d414169c9 100755
--- a/testsuite/seq.tests
+++ b/testsuite/seq.tests
@@ -43,4 +43,7 @@ testing "seq count down by 3 with padding" "seq -w 8 -3 04" "08\n05\n" "" ""
 testing "seq count by .3 with padding 1" "seq -w 09 .3 11" "09.0\n09.3\n09.6\n09.9\n10.2\n10.5\n10.8\n" "" ""
 testing "seq count by .3 with padding 2" "seq -w 03 .3 0004" "0003.0\n0003.3\n0003.6\n0003.9\n" "" ""
 
+testing "seq from -4 count down by 2" "seq -4 -2 -8" "-4\n-6\n-8\n" "" ""
+testing "seq from -.0 count down by .25" "seq -.0 -.25 -.9" "-0.00\n-0.25\n-0.50\n-0.75\n" "" ""
+
 exit $FAILCOUNT


More information about the busybox-cvs mailing list