1 /* count.c - Progress indicator from stdin to stdout
2  *
3  * Copyright 2002 Rob Landley <rob@landley.net>
4 
5 USE_COUNT(NEWTOY(count, NULL, TOYFLAG_USR|TOYFLAG_BIN))
6 
7 config COUNT
8   bool "count"
9   default y
10   help
11     usage: count
12 
13     Copy stdin to stdout, displaying simple progress indicator to stderr.
14 */
15 
16 #include "toys.h"
17 
count_main(void)18 void count_main(void)
19 {
20   struct pollfd pfd = {0, POLLIN, 0};
21   unsigned long long size = 0, last = 0, then = 0, now;
22   char *buf = xmalloc(65536);
23   int len;
24 
25   // poll, print if data not ready, update 4x/second otherwise
26   for (;;) {
27     if (!(len = poll(&pfd, 1, (last != size) ? 250 : 0))) continue;
28     if (len<0 && errno != EINTR && errno != ENOMEM) perror_exit(0);
29     if ((len = xread(0, buf, 65536))) {
30       xwrite(1, buf, len);
31       size += len;
32       if ((now = millitime())-then<250) continue;
33     }
34     dprintf(2, "%llu bytes\r", size);
35     if (!len) break;
36   }
37   dprintf(2, "\n");
38 }
39