logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 6283191de21fb1a57ed3059caf0d6efea2f91cba
parent 72fd9c6e03adfa4196a666478c09fafa12eea572
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 12 Mar 2022 14:21:52 +0100

seq: Fix support for negative integers

Diffstat:

Mbin/seq.c18+++++++++++++++++-
Mtest-bin/seq18++++++++++++++++++
2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/bin/seq.c b/bin/seq.c @@ -31,6 +31,19 @@ seq(long i, long step, long last) } } +static long unsigned int +safe_labs(long int a) +{ + if(a >= 0) + { + return (long unsigned int)a; + } + else + { + return (long unsigned int)-a; + } +} + long get_num(char *str) { @@ -69,6 +82,9 @@ main(int argc, char *argv[]) case 's': separator = optarg; break; + case '?': + usage(); + return 1; } } @@ -90,7 +106,7 @@ main(int argc, char *argv[]) break; case 3: first = get_num(argv[0]); - step = get_num(argv[1]); + step = (long)safe_labs(get_num(argv[1])); last = get_num(argv[2]); break; default: diff --git a/test-bin/seq b/test-bin/seq @@ -3,6 +3,9 @@ atf_test_case one one_body() { atf_check -o "inline:1\n" ../bin/seq 1 atf_check -o "inline:1\n2\n3\n4\n5\n" ../bin/seq 5 + + atf_check -o "inline:1\n0\n-1\n" -- ../bin/seq -- -1 + atf_check -o "inline:1\n0\n-1\n-2\n-3\n-4\n-5\n" -- ../bin/seq -- -5 } atf_test_case two @@ -10,6 +13,12 @@ two_body() { atf_check -o "inline:0\n1\n" ../bin/seq 0 1 atf_check -o "inline:0\n1\n2\n3\n4\n5\n" ../bin/seq 0 5 atf_check -o "inline:10\n11\n12\n13\n14\n15\n" ../bin/seq 10 15 + + atf_check -o "inline:0\n-1\n" -- ../bin/seq 0 -1 + atf_check -o "inline:0\n-1\n-2\n-3\n-4\n-5\n" -- ../bin/seq 0 -5 + atf_check -o "inline:-10\n-11\n-12\n-13\n-14\n-15\n" -- ../bin/seq -- -10 -15 + atf_check -o "inline:2\n1\n0\n-1\n-2\n" -- ../bin/seq 2 -2 + atf_check -o "inline:-2\n-1\n0\n1\n2\n" -- ../bin/seq -- -2 2 } atf_test_case three @@ -20,6 +29,15 @@ three_body() { atf_check -o "inline:0\n2\n4\n" ../bin/seq 0 2 5 atf_check -o "inline:10\n12\n14\n" ../bin/seq 10 2 15 + + atf_check -o "inline:0\n-1\n" -- ../bin/seq 0 1 -1 + atf_check -o "inline:0\n-1\n-2\n-3\n-4\n-5\n" -- ../bin/seq 0 1 -5 + atf_check -o "inline:-10\n-11\n-12\n-13\n-14\n-15\n" -- ../bin/seq -- -10 1 -15 + + atf_check -o "inline:0\n-1\n" -- ../bin/seq 0 -1 -1 + + atf_check -o "inline:0\n-2\n-4\n" -- ../bin/seq 0 2 -5 + atf_check -o "inline:-10\n-12\n-14\n" -- ../bin/seq -- -10 2 -15 } atf_test_case noarg