From a793df3e2a07c65f36fb2f5f43d2c51ab0c47bf2 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Wed, 10 Apr 2024 16:54:50 +0300 Subject: Added help message & fixed blocksize bug The bug was caused by integer overflow with too large precisions. --- stdu.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'stdu.c') diff --git a/stdu.c b/stdu.c index 64f19a5..466d2f5 100644 --- a/stdu.c +++ b/stdu.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "intmath.h" #include "formatting.h" @@ -26,6 +27,17 @@ int main (int argc, char* argv[]) { Config* conf = (Config*) res.result; + if (conf->help) { + printf( +"Usage: stdu [-h] [-p n]\n\ +Print amount of data piped to stdin.\n\ +--help | -?: print this help message\n\ +--human-readable | -h: output in a human readable format\n\ +--precision n | -p n | -pn: output with n significant digits\n " + ); + return 0; + } + unsigned int base = 10; if (conf->human_readable && conf->precision == 0) { conf->precision = 1; @@ -38,6 +50,15 @@ int main (int argc, char* argv[]) { unsigned long long bytes_read = 0; size_t new_read_bytes = 0; if (conf->precision != 0) { + int max_precision = int_logn(base, (unsigned int) INT_MAX); + if (conf->precision > max_precision) { + fprintf( + stderr, + "Overflow error: precision may not be greater than %d\n", + max_precision + ); + return 1; + } blocksize = exp_notated_to_ull(ull_ceiled_exponent_notation_base( bytes_read + 1, conf->precision, -- cgit v1.2.3