logo

utils-std

Collection of commonly available Unix tools
commit: 56aae959a7499c5703be088b875b85a9306ab19c
parent 9c1da8724df4dde8e0c7adbaa547a4b631c9a58b
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 16 Mar 2024 20:37:23 +0100

cmd/seq: Add -t option (NetBSD inspired)

Diffstat:

Mcmd/seq.13+++
Mcmd/seq.c13+++++++++----
Mtest-cmd/seq6++++++
3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/cmd/seq.1 b/cmd/seq.1 @@ -11,6 +11,7 @@ .Nm .Op Fl w .Op Fl s Ar separator +.Op Fl t Ar terminator .Op Ar first Op Ar step .Ar last .Sh DESCRIPTION @@ -33,6 +34,8 @@ number of the sequence. Should match the longest number in the sequence reasonably well. .It Fl s Ar separator Set the string used between each number (default: "\n" aka newline) +.It Fl t Ar terminator +Set the final string used at the end of the sequence (default: "\n" aka newline) .El .Sh EXIT STATUS .Ex -std diff --git a/cmd/seq.c b/cmd/seq.c @@ -12,8 +12,10 @@ #include <string.h> // strerror #include <unistd.h> // getopt, optarg, optind -char *sep = "\n"; -int len = 0; +char *sep = "\n"; +char *term = "\n"; + +int len = 0; bool opt_w = false; double g_last = 1.0; @@ -34,7 +36,7 @@ seq(double i, double step, double last) printf("%s%0*g", sep, len, i -= step); } - printf("\n"); + printf("%s", term); if(opt_w && i != g_last) fprintf(stderr, "seq: Failed to guess last, got: %g; expected: %g\n", i, g_last); @@ -79,7 +81,7 @@ main(int argc, char *argv[]) int c; /* flawfinder: ignore. Old implementations of getopt should fix themselves */ - while((c = getopt(argc, argv, ":ws:")) != -1) + while((c = getopt(argc, argv, ":ws:t:")) != -1) { switch(c) { @@ -89,6 +91,9 @@ main(int argc, char *argv[]) case 's': sep = optarg; break; + case 't': + term = optarg; + break; case ':': fprintf(stderr, "seq: Option -%c requires an operand\n", optopt); usage(); diff --git a/test-cmd/seq b/test-cmd/seq @@ -48,6 +48,11 @@ sep_body() { atf_check -o "inline:1-2-3\n" ../cmd/seq -s - 3 } +atf_test_case term +term_body() { + atf_check -o "inline:1-2-3:" ../cmd/seq -s - -t: 3 +} + atf_test_case noarg noarg_body() { atf_check -s exit:1 -e 'inline:usage: seq [-w] [-s separator] [first [step]] last\n' ../cmd/seq @@ -87,6 +92,7 @@ atf_init_test_cases() { atf_add_test_case three atf_add_test_case sep + atf_add_test_case term atf_add_test_case noarg atf_add_test_case badflag