count.c 923 B

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