pipe_progress.c 927 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Monitor a pipe with a simple progress display.
  4. *
  5. * Copyright (C) 2003 by Rob Landley <rob@landley.net>, Joey Hess
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #define PIPE_PROGRESS_SIZE 4096
  11. /* Read a block of data from stdin, write it to stdout.
  12. * Activity is indicated by a '.' to stderr
  13. */
  14. int pipe_progress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  15. int pipe_progress_main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
  16. {
  17. RESERVE_CONFIG_BUFFER(buf, PIPE_PROGRESS_SIZE);
  18. time_t t = time(NULL);
  19. size_t len;
  20. while ((len = fread(buf, 1, PIPE_PROGRESS_SIZE, stdin)) > 0) {
  21. time_t new_time = time(NULL);
  22. if (new_time != t) {
  23. t = new_time;
  24. fputc('.', stderr);
  25. }
  26. fwrite(buf, len, 1, stdout);
  27. }
  28. fputc('\n', stderr);
  29. if (ENABLE_FEATURE_CLEAN_UP)
  30. RELEASE_CONFIG_BUFFER(buf);
  31. return 0;
  32. }