blob: 0ec3919da7c6c136f5b4f49d46c30a703b868fd4 [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001/* vi: set sw=4 ts=4:
2 *
Rob Landleyc555a0c2007-01-31 12:27:10 -05003 * count.c - Progress indicator from stdin to stdout
Rob Landleyfece5cb2007-12-03 20:05:57 -06004 *
Rob Landley28964802008-01-19 17:08:39 -06005 * Copyright 2002 Rob Landley <rob@landley.net>
6 *
Rob Landleyfece5cb2007-12-03 20:05:57 -06007 * Not in SUSv3.
Rob Landley28964802008-01-19 17:08:39 -06008
Rob Landley55928b12008-01-19 17:43:27 -06009USE_COUNT(NEWTOY(count, NULL, TOYFLAG_USR|TOYFLAG_BIN))
10
Rob Landley28964802008-01-19 17:08:39 -060011config COUNT
12 bool "count"
13 default y
14 help
15 usage: count
16
17 Copy stdin to stdout, displaying simple progress indicator to stderr.
18*/
Rob Landleyc555a0c2007-01-31 12:27:10 -050019
20#include "toys.h"
21
Rob Landleyefda21c2007-11-29 18:14:37 -060022void count_main(void)
Rob Landleyc555a0c2007-01-31 12:27:10 -050023{
24 uint64_t size = 0;
25 int len;
Rob Landleyee00a7f2012-03-19 19:19:21 -050026 char buf[32];
Rob Landleyc555a0c2007-01-31 12:27:10 -050027
28 for (;;) {
29 len = xread(0, toybuf, sizeof(toybuf));
30 if (!len) break;
31 size += len;
Rob Landleycebe48a2008-08-28 17:32:05 -050032 xwrite(1, toybuf, len);
Rob Landleyee00a7f2012-03-19 19:19:21 -050033 xwrite(2, buf, sprintf(buf, "%"PRIu64" bytes\r", size));
Rob Landleyc555a0c2007-01-31 12:27:10 -050034 }
Rob Landleyee00a7f2012-03-19 19:19:21 -050035 xwrite(2, "\n", 1);
Rob Landleyc555a0c2007-01-31 12:27:10 -050036}