aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-04-10 14:33:20 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2023-04-10 14:33:20 +0200
commit9bc2b6e88474a8ebb50c94f29320d343bd374928 (patch)
tree0af73191b3842822d9d1bf7e8729ea49a4df1a2d
parenta26711a2d1464167be4ebc990fe21a3809a2da34 (diff)
downloadbusybox-w32-9bc2b6e88474a8ebb50c94f29320d343bd374928.tar.gz
busybox-w32-9bc2b6e88474a8ebb50c94f29320d343bd374928.tar.bz2
busybox-w32-9bc2b6e88474a8ebb50c94f29320d343bd374928.zip
seq: accept negative parameters
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@googlemail.com>
-rw-r--r--coreutils/seq.c23
-rwxr-xr-xtestsuite/seq.tests3
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 @@
22//usage:#define seq_full_usage "\n\n" 22//usage:#define seq_full_usage "\n\n"
23//usage: "Print numbers from FIRST to LAST, in steps of INC.\n" 23//usage: "Print numbers from FIRST to LAST, in steps of INC.\n"
24//usage: "FIRST, INC default to 1.\n" 24//usage: "FIRST, INC default to 1.\n"
25//usage: "\n -w Pad to last with leading zeros" 25//usage: "\n -w Pad with leading zeros"
26//usage: "\n -s SEP String separator" 26//usage: "\n -s SEP String separator"
27 27
28#include "libbb.h" 28#include "libbb.h"
@@ -41,6 +41,7 @@ int seq_main(int argc, char **argv)
41 unsigned width; 41 unsigned width;
42 unsigned frac_part; 42 unsigned frac_part;
43 const char *sep, *opt_s = "\n"; 43 const char *sep, *opt_s = "\n";
44 char *saved;
44 unsigned opt; 45 unsigned opt;
45 46
46#if ENABLE_LOCALE_SUPPORT 47#if ENABLE_LOCALE_SUPPORT
@@ -49,7 +50,25 @@ int seq_main(int argc, char **argv)
49 setlocale(LC_NUMERIC, "C"); 50 setlocale(LC_NUMERIC, "C");
50#endif 51#endif
51 52
52 opt = getopt32(argv, "+ws:", &opt_s); 53 /* Cater for negative arguments: if we see one, truncate argv[] on it */
54 n = 0;
55 for (;;) {
56 char c;
57 saved = argv[++n];
58 if (!saved)
59 break;
60 if (saved[0] != '-')
61 break;
62 c = saved[1];
63 if (c == '.' || (c >= '0' && c <= '9')) {
64 argv[n] = NULL;
65 break;
66 }
67 }
68 opt = getopt32(argv, "+ws:", &opt_s); /* "+": stop at first non-option */
69 /* Restore possibly truncated argv[] */
70 argv[n] = saved;
71
53 argc -= optind; 72 argc -= optind;
54 argv += optind; 73 argv += optind;
55 first = increment = 1; 74 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" "" ""
43testing "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" "" "" 43testing "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" "" ""
44testing "seq count by .3 with padding 2" "seq -w 03 .3 0004" "0003.0\n0003.3\n0003.6\n0003.9\n" "" "" 44testing "seq count by .3 with padding 2" "seq -w 03 .3 0004" "0003.0\n0003.3\n0003.6\n0003.9\n" "" ""
45 45
46testing "seq from -4 count down by 2" "seq -4 -2 -8" "-4\n-6\n-8\n" "" ""
47testing "seq from -.0 count down by .25" "seq -.0 -.25 -.9" "-0.00\n-0.25\n-0.50\n-0.75\n" "" ""
48
46exit $FAILCOUNT 49exit $FAILCOUNT