diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-05-31 15:29:42 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-06-30 08:31:37 +0300 |
commit | 576b7a70dbf6c43651dc77db3a0104c019eed018 (patch) | |
tree | 6ca087eb2e3bd27d876ad2842d67cee2d7e09cbf /stdu.c | |
parent | 41de123de0453274fb2e5b2192548ce274ad8deb (diff) | |
download | stdu-work/1.1.0.tar.gz stdu-work/1.1.0.zip |
Added `--forward`work/1.1.0
Diffstat (limited to 'stdu.c')
-rw-r--r-- | stdu.c | 39 |
1 files changed, 31 insertions, 8 deletions
@@ -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 */ |