From 8fbff9dc2e8f3f444437c82835f22ba5d6a73367 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 8 Feb 2024 05:43:52 -0600 Subject: [PATCH] Add wc -L and tests. --- tests/wc.test | 18 ++++++++++++++++++ toys/posix/wc.c | 32 +++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/tests/wc.test b/tests/wc.test index 66cf48be..c751cce8 100755 --- a/tests/wc.test +++ b/tests/wc.test @@ -21,6 +21,7 @@ testing "standard input -cl" "wc -cl" " 1 5\n" "" "a b\nc" testing "-c" "wc -c file1" "26 file1\n" "" "" testing "-l" "wc -l file1" "4 file1\n" "" "" testing "-w" "wc -w file1" "5 file1\n" "" "" +testing "-L" "wc -L file1" "18 file1\n" "" "" NOSPACE=1 testing "one file" "wc file1" "4 5 26 file1\n" "" "" testing "multiple files" "wc input - file1" \ " 1 2 3 input\n 0 2 3 -\n 4 5 26 file1\n 5 9 32 total\n" "a\nb" "a b" @@ -33,3 +34,20 @@ testing "-m 2" 'cat "$FILES/utf8/test2.txt" | wc -m' "169\n" "" "" echo -n " " > file1 NOSPACE=1 testing "-mlw" "wc -mlw input" "1 2 11 input\n" "hello, 世界!\n" "" rm file1 + +# Corner cases for "line length". Not QUITE fold because \b doesn't subtract + +testcmd '-L 2' '-L' '1\n' '' 'a\n' +testcmd '-L 3' '-L' '1\n' '' 'a' +testcmd '-L 4' '-L' '6\n' '' 'first\rsecond\n' +testcmd '-L 5' '-L' '9\n' '' '\ta\n' +testcmd '-L 6' '-L' '8\n' '' '\t\b\b\b\n' +testcmd '-L 7' '-L' '9\n' '' 'abc\td\n' +testcmd '-L 8' '-L' '4\n' '' 'abc\bd\n' +testcmd '-L 9' '-L' '0\n' '' '\x01\n' +testcmd '-L 10' '-L' '1\n' '' 'w\xcc\x88\n' +NOSPACE=1 testcmd '-Lm incoherence' '-Lm' '2 0\n' '' '\x01\n' + +# 25 wide chars, also testing field order +NOSPACE=1 testcmd '-cmlLw wide' '-cmlLw' '0 1 25 75 50\n' '' \ + "$(<"$FILES"/utf8/japan.txt)" diff --git a/toys/posix/wc.c b/toys/posix/wc.c index 0fd9b550..1762d879 100644 --- a/toys/posix/wc.c +++ b/toys/posix/wc.c @@ -4,16 +4,17 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/wc.html -USE_WC(NEWTOY(wc, "mcwl", TOYFLAG_USR|TOYFLAG_BIN)) +USE_WC(NEWTOY(wc, "Lcmwl", TOYFLAG_USR|TOYFLAG_BIN)) config WC bool "wc" default y help - usage: wc [-lwcm] [FILE...] + usage: wc [-Llwcm] [FILE...] Count lines, words, and characters in input. + -L Show max line length -l Show lines -w Show words -c Show bytes @@ -28,7 +29,7 @@ config WC #include "toys.h" GLOBALS( - unsigned long totals[4]; + unsigned long totals[5]; ) static void show_lengths(unsigned long *lengths, char *name) @@ -45,12 +46,13 @@ static void show_lengths(unsigned long *lengths, char *name) if (!(!toys.optc && !(toys.optflags & (toys.optflags-1))) && toys.optc!=1) space = 7; - for (i = 0; i<4; i++) { + for (i = 0; ilengths[4]) lengths[4] = line; + line = 0; + } + space = iswspace(wchar); } } else space = isspace(toybuf[pos]); @@ -106,6 +115,7 @@ static void do_wc(int fd, char *name) if (pos != len) memmove(toybuf, toybuf+pos, len-pos); len -= pos; } + if (line>lengths[4]) lengths[4] = line; show: show_lengths(lengths, name); -- 2.39.2