From 4ff6a08d73f0ef1e2fcb7a93a74cc6fa66c24e2b Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 7 Apr 2024 13:31:07 +0300 Subject: Bigger `bytes_read` & removed redundant code handling impossible signed `bytes_read` --- stdu.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'stdu.c') diff --git a/stdu.c b/stdu.c index f2273f7..64f19a5 100644 --- a/stdu.c +++ b/stdu.c @@ -34,10 +34,11 @@ int main (int argc, char* argv[]) { int blocksize = 1; size_t bufsize = 1; - unsigned int bytes_read = 0; + int bufsize_tries = 10; + unsigned long long bytes_read = 0; size_t new_read_bytes = 0; if (conf->precision != 0) { - blocksize = exp_notated_to_int(int_ceiled_exponent_notation_base( + blocksize = exp_notated_to_ull(ull_ceiled_exponent_notation_base( bytes_read + 1, conf->precision, base)) @@ -55,14 +56,14 @@ int main (int argc, char* argv[]) { while (1) { /* output */ if (conf->human_readable && base == 10) { - int res = int_floored_with_prefix( + int res = ull_floored_with_prefix( &stdout_buffer, &stdout_buffer_size, bytes_read, conf->precision ); if (res < 0) { - printf("\r%u \ + printf("\r%llu \ (error when getting prefix)", bytes_read); continue; @@ -75,13 +76,13 @@ int main (int argc, char* argv[]) { previous_line_strlen = int_max(res, previous_line_strlen); } else if (conf->human_readable && base == 1024) { - int success = int_floored_with_binary_prefix( + int success = ull_floored_with_binary_prefix( &stdout_buffer, &stdout_buffer_size, bytes_read ); if (success < 0) { - printf("\r%u \ + printf("\r%llu \ (error when getting prefix)", bytes_read); } @@ -91,7 +92,7 @@ int main (int argc, char* argv[]) { stdout_buffer ); } else { - printf("\r%u", bytes_read); + printf("\r%llu", bytes_read); } if (fflush(stdout) == EOF) { printf("\n"); @@ -120,8 +121,9 @@ int main (int argc, char* argv[]) { /* resizing buffer and blocksize to read as much as possible * at once */ + if (bufsize_tries <= 0) continue; if (conf->precision == 0) continue; - blocksize = exp_notated_to_int(int_ceiled_exponent_notation_base( + blocksize = exp_notated_to_ull(ull_ceiled_exponent_notation_base( bytes_read + 1, conf->precision, base)) @@ -129,6 +131,7 @@ int main (int argc, char* argv[]) { if (blocksize > bufsize) { tmpbuf = malloc(bufsize * 2); if (tmpbuf == NULL) { + bufsize_tries--; free(tmpbuf); } else { free(buf); -- cgit v1.2.3