From 576b7a70dbf6c43651dc77db3a0104c019eed018 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 31 May 2024 15:29:42 +0300 Subject: Added `--forward` --- stdu.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'stdu.c') diff --git a/stdu.c b/stdu.c index 2cd0aa4..e549440 100644 --- a/stdu.c +++ b/stdu.c @@ -27,6 +27,8 @@ int main (int argc, char* argv[]) { Config* conf = (Config*) res.result; + FILE* progress_destination = conf->forward ? stderr : stdout; + if (conf->help) { printf( "Usage: stdu [-hm?] [-p n]\n\ @@ -34,6 +36,7 @@ Print amount of data piped to stdin.\n\ --help | -?: print this help message\n\ --human-readable | -h: output in a human readable format\n\ --multiline | -m: output with line breaks\n\ +--forward | -o: forward STDIN to STDOUT and print amount of transferred data to STDERR\n\ --precision n | -p n | -pn: output with n significant digits\n" ); return 0; @@ -89,12 +92,17 @@ Print amount of data piped to stdin.\n\ conf->precision ); if (res < 0) { - printf("\r%llu \ + fprintf( + progress_destination, + "\r%llu \ (error when getting prefix)%s", - bytes_read, endl); + bytes_read, + endl + ); continue; } - printf( + fprintf( + progress_destination, "\r%sB%*s", stdout_buffer, int_max(previous_line_strlen - res, 0), @@ -109,11 +117,16 @@ Print amount of data piped to stdin.\n\ bytes_read ); if (res < 0) { - printf("\r%llu \ + fprintf( + progress_destination, + "\r%llu \ (error when getting prefix)%s", - bytes_read, endl); + bytes_read, + endl + ); } - printf( + fprintf( + progress_destination, "\r%sB%*s", stdout_buffer, int_max(previous_line_strlen - res, 0), @@ -122,9 +135,14 @@ Print amount of data piped to stdin.\n\ previous_line_strlen = int_max(res, previous_line_strlen); } else { - printf("\r%llu%s", bytes_read, endl); + fprintf( + progress_destination, + "\r%llu%s", + bytes_read, + endl + ); } - if (fflush(stdout) == EOF) { + if (fflush(progress_destination) == EOF) { printf("\n"); perror("error during fflush"); return 1; @@ -148,6 +166,11 @@ Print amount of data piped to stdin.\n\ } bytes_read += new_read_bytes; + /* writing */ + if (conf->forward) { + printf("%s", buf); + } + /* resizing buffer and blocksize to read as much as possible * at once */ -- cgit v1.2.3