seq: accept negative parameters
No binary (build failed)
—
(log yok)
| @@ -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; | |
| @@ -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 | |