Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
Rob Landley | c555a0c | 2007-01-31 12:27:10 -0500 | [diff] [blame] | 3 | * count.c - Progress indicator from stdin to stdout |
Rob Landley | fece5cb | 2007-12-03 20:05:57 -0600 | [diff] [blame] | 4 | * |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 5 | * Copyright 2002 Rob Landley <rob@landley.net> |
| 6 | * |
Rob Landley | fece5cb | 2007-12-03 20:05:57 -0600 | [diff] [blame] | 7 | * Not in SUSv3. |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 8 | |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 9 | USE_COUNT(NEWTOY(count, NULL, TOYFLAG_USR|TOYFLAG_BIN)) |
| 10 | |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 11 | config 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 Landley | c555a0c | 2007-01-31 12:27:10 -0500 | [diff] [blame] | 19 | |
| 20 | #include "toys.h" |
| 21 | |
Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 22 | void count_main(void) |
Rob Landley | c555a0c | 2007-01-31 12:27:10 -0500 | [diff] [blame] | 23 | { |
| 24 | uint64_t size = 0; |
| 25 | int len; |
| 26 | |
| 27 | for (;;) { |
| 28 | len = xread(0, toybuf, sizeof(toybuf)); |
| 29 | if (!len) break; |
| 30 | size += len; |
Rob Landley | cebe48a | 2008-08-28 17:32:05 -0500 | [diff] [blame] | 31 | xwrite(1, toybuf, len); |
Rob Landley | c555a0c | 2007-01-31 12:27:10 -0500 | [diff] [blame] | 32 | fdprintf(2, "%"PRIu64" bytes\r", size); |
| 33 | } |
| 34 | fdprintf(2,"\n"); |
Rob Landley | c555a0c | 2007-01-31 12:27:10 -0500 | [diff] [blame] | 35 | } |