/* wc.c - Word count * * Copyright 2011 Rob Landley * * See http://opengroup.org/onlinepubs/9699919799/utilities/wc.html USE_WC(NEWTOY(wc, "mcwl", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE)) config WC bool "wc" default y help usage: wc -lwcm [FILE...] Count lines, words, and characters in input. -l Show lines -w Show words -c Show bytes -m Show characters By default outputs lines, words, bytes, and filename for each argument (or from stdin if none). Displays only either bytes or characters. */ #define FOR_wc #include "toys.h" GLOBALS( unsigned long totals[4]; ) static void show_lengths(unsigned long *lengths, char *name) { int i, space = 0, first = 1; // POSIX says there should never be leading spaces, but accepts that // traditional implementations use 7 spaces, unless only one file (or // just stdin) is being counted, when there should be no leading spaces, // *except* for the case where we're going to output multiple numbers. // And, yes, folks have test scripts that rely on all this nonsense :-( // Note: sufficiently modern versions of coreutils wc will use the smallest // column width necessary to have all columns be equal width rather than 0. if (!(!toys.optc && !(toys.optflags & (toys.optflags-1))) && toys.optc!=1) space = 7; for (i = 0; i<4; i++) { if (toys.optflags&(1<1) show_lengths(TT.totals, "total"); }