diff options
author | Joel Kronqvist <joelkronqvist@proton.me> | 2024-04-10 16:54:50 +0300 |
---|---|---|
committer | Joel Kronqvist <joelkronqvist@proton.me> | 2024-04-11 16:01:36 +0300 |
commit | a793df3e2a07c65f36fb2f5f43d2c51ab0c47bf2 (patch) | |
tree | 49858b1ef415ce16c843c4fbb4ab7eccaa73033d /stdu.c | |
parent | 4ff6a08d73f0ef1e2fcb7a93a74cc6fa66c24e2b (diff) | |
download | stdu-a793df3e2a07c65f36fb2f5f43d2c51ab0c47bf2.tar.gz stdu-a793df3e2a07c65f36fb2f5f43d2c51ab0c47bf2.zip |
Added help message & fixed blocksize bug
The bug was caused by integer overflow with too large precisions.
Diffstat (limited to 'stdu.c')
-rw-r--r-- | stdu.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -4,6 +4,7 @@ #include <string.h> #include <errno.h> #include <stdbool.h> +#include <limits.h> #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, |