blob: fa98e8b389e18fa33ae5a07739bcdb29024ee05f [file] [log] [blame]
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathe9080c92003-11-14 10:04:33 +00002/*
3 * Monitor a pipe with a simple progress display.
4 *
5 * Copyright (C) 2003 by Rob Landley <rob@landley.net>, Joey Hess
6 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathe9080c92003-11-14 10:04:33 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Glenn L McGrathe9080c92003-11-14 10:04:33 +000011
Glenn L McGrathe9080c92003-11-14 10:04:33 +000012#define PIPE_PROGRESS_SIZE 4096
13
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000014/* Read a block of data from stdin, write it to stdout.
15 * Activity is indicated by a '.' to stderr
Glenn L McGrathe9080c92003-11-14 10:04:33 +000016 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000017int pipe_progress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000018int pipe_progress_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Glenn L McGrathe9080c92003-11-14 10:04:33 +000019{
20 RESERVE_CONFIG_BUFFER(buf, PIPE_PROGRESS_SIZE);
21 time_t t = time(NULL);
22 size_t len;
23
24 while ((len = fread(buf, 1, PIPE_PROGRESS_SIZE, stdin)) > 0) {
25 time_t new_time = time(NULL);
26 if (new_time != t) {
27 t = new_time;
28 fputc('.', stderr);
29 }
30 fwrite(buf, len, 1, stdout);
31 }
32
33 fputc('\n', stderr);
34
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000035 if (ENABLE_FEATURE_CLEAN_UP)
36 RELEASE_CONFIG_BUFFER(buf);
37
Glenn L McGrathe9080c92003-11-14 10:04:33 +000038 return 0;
39}