diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-09-29 20:53:05 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-09-29 22:04:48 +0200 |
commit | 3db442021a8af00ae0aba842c6ab294d25e9d626 (patch) | |
tree | 707ba5aec3a1cf44008bd90bffc3abc66100ada0 | |
parent | 8426f24779ff3ad41f867a4a84bd44852a08c394 (diff) |
toilet: print totals
-rw-r--r-- | src/toilet.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/toilet.c b/src/toilet.c index 9f45a7d..c66896a 100644 --- a/src/toilet.c +++ b/src/toilet.c @@ -19,6 +19,9 @@ static int modeMap; +// lines, words, bytes +static size_t totals[3] = {0}; + static void usage(FILE *f) { fprintf(f, "Gebruik: toilet [-nchV] [BESTAND]...\n" @@ -118,11 +121,24 @@ static size_t get_count(enum MODE mode, struct filebuf *fb) { } } +static int mlog2(int val) { +#define C(n) case (1 << (n)): return (n); + + switch (val) { + C(0) C(1) C(2) + } + assert(false); + +#undef C +} + static int process(struct filebuf *fb, char *fname, bool) { for (enum MODE mode = 1; mode <= M_BYTES; mode <<= 1) { if (mode & modeMap) { const size_t count = get_count(mode, fb); printf("% 10li ", count); + + totals[mlog2(mode)] += count; } } printf("%s\n", fname); @@ -140,5 +156,12 @@ int entry_toilet(int argc, char **argv) { modeMap = INT_MAX; } - return loop_files(args, process); + const int res = loop_files(args, process); + + for (size_t i = 0; i < 3; i++) { + printf("% 10li ", totals[i]); + } + puts("totaal"); + + return res; } |