From 0e9e21139dc19f81d38957035a4ba603917cb77b Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 5 Jan 2025 14:48:44 +0200 Subject: Bug fix for `--forward` `--forward` used printf for outputting data to stdout, which failed with binary data that didn\'t first of all neccessarily contain null bytes and secondly could contain unexpected null bytes. Also fixed the trailing newline appended to files when using `--forward`. That bug in turn was caused by the newline getting sent to the wrong place - it was meant to the user. --- stdu.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'stdu.c') diff --git a/stdu.c b/stdu.c index e549440..24833a0 100644 --- a/stdu.c +++ b/stdu.c @@ -83,7 +83,7 @@ Print amount of data piped to stdin.\n\ int previous_line_strlen = 0; while (1) { - /* output */ + /* output to the user */ if (conf->human_readable && base == 10) { int res = ull_floored_with_prefix( &stdout_buffer, @@ -166,9 +166,9 @@ Print amount of data piped to stdin.\n\ } bytes_read += new_read_bytes; - /* writing */ + /* writing forwarded data */ if (conf->forward) { - printf("%s", buf); + fwrite(buf, 1, new_read_bytes, stdout); } /* resizing buffer and blocksize to read as much as possible @@ -176,11 +176,15 @@ Print amount of data piped to stdin.\n\ */ if (bufsize_tries <= 0) continue; if (conf->precision == 0) continue; - blocksize = exp_notated_to_ull(ull_ceiled_exponent_notation_base( - bytes_read + 1, - conf->precision, - base)) - - bytes_read; + + blocksize = exp_notated_to_ull( + ull_ceiled_exponent_notation_base( + bytes_read + 1, + conf->precision, + base + ) + ) - bytes_read; + if (blocksize > bufsize) { tmpbuf = malloc(bufsize * 2); if (tmpbuf == NULL) { @@ -193,7 +197,7 @@ Print amount of data piped to stdin.\n\ } } } - printf("\n"); + fprintf(progress_destination, "\n"); free(stdout_buffer); return 0; -- cgit v1.2.3