Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Monitor a pipe with a simple progress display. |
| 4 | * |
| 5 | * Copyright (C) 2003 by Rob Landley <rob@landley.net>, Joey Hess |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 8 | */ |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 9 | |
| 10 | //usage:#define pipe_progress_trivial_usage NOUSAGE_STR |
| 11 | //usage:#define pipe_progress_full_usage "" |
| 12 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 14 | |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 15 | #define PIPE_PROGRESS_SIZE 4096 |
| 16 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 17 | /* Read a block of data from stdin, write it to stdout. |
| 18 | * Activity is indicated by a '.' to stderr |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 19 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 20 | int pipe_progress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 21 | int pipe_progress_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 22 | { |
Denys Vlasenko | 5f33037 | 2010-06-06 18:19:25 +0200 | [diff] [blame] | 23 | char buf[PIPE_PROGRESS_SIZE]; |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 24 | time_t t = time(NULL); |
Denys Vlasenko | 5f33037 | 2010-06-06 18:19:25 +0200 | [diff] [blame] | 25 | int len; |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 26 | |
Denys Vlasenko | 5f33037 | 2010-06-06 18:19:25 +0200 | [diff] [blame] | 27 | while ((len = safe_read(STDIN_FILENO, buf, PIPE_PROGRESS_SIZE)) > 0) { |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 28 | time_t new_time = time(NULL); |
| 29 | if (new_time != t) { |
| 30 | t = new_time; |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 31 | bb_putchar_stderr('.'); |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 32 | } |
Denys Vlasenko | 5f33037 | 2010-06-06 18:19:25 +0200 | [diff] [blame] | 33 | full_write(STDOUT_FILENO, buf, len); |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 36 | bb_putchar_stderr('\n'); |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 37 | |
Glenn L McGrath | e9080c9 | 2003-11-14 10:04:33 +0000 | [diff] [blame] | 38 | return 0; |
| 39 | } |