From 464ab9faa5c41889f217c56a7975912278a3ce97 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 17 Oct 2023 04:22:15 -0500 Subject: [PATCH] Add count -l, suggested by Oliver Webb. --- toys/other/count.c | 49 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/toys/other/count.c b/toys/other/count.c index 3d388e78..41fbb375 100644 --- a/toys/other/count.c +++ b/toys/other/count.c @@ -2,37 +2,74 @@ * * Copyright 2002 Rob Landley -USE_COUNT(NEWTOY(count, NULL, TOYFLAG_USR|TOYFLAG_BIN)) +USE_COUNT(NEWTOY(count, "<0>0l", TOYFLAG_USR|TOYFLAG_BIN)) config COUNT bool "count" default y help - usage: count + usage: count [-l] + + -l Long output (total bytes, human readable, transfer rate, elapsed time) Copy stdin to stdout, displaying simple progress indicator to stderr. */ +#define FOR_count #include "toys.h" +GLOBALS( + unsigned long long size, start; + unsigned tick, *slice; +) + +static void display(unsigned long long now) +{ + unsigned long long bb; + unsigned seconds, ii, len = TT.tick ? : 1; + + // raw number, human readable, time, and recent/total rate + if (FLAG(l)) { + human_readable(toybuf+256, TT.size, 0); + seconds = (now - TT.start)/1000; + for (bb = ii = 0, len = minof(len, 64); ii=then) { + display(now); + while (then<=now) { + then += 250; + if (FLAG(l)) TT.slice[63&++TT.tick] = 0; + } + } + if (!(len = poll(&pfd, 1, then-now))) continue; if (len<0 && errno != EINTR && errno != ENOMEM) perror_exit(0); if ((len = xread(0, buf, 65536))) { xwrite(1, buf, len); - size += len; + TT.size += len; + TT.slice[63&TT.tick] += len; if ((now = millitime())-then<250) continue; } - dprintf(2, "%llu bytes\r", size); if (!len) break; } + display(now); dprintf(2, "\n"); } -- 2.39.2