From adb8aaec84aab8259a9bd4b9c26b860254d9e249 Mon Sep 17 00:00:00 2001 From: Michel Machado Date: Tue, 16 Dec 2014 08:52:42 -0500 Subject: [PATCH] f3probe: add a friendly way to enter sizes This patch allows any number passed as parameters to use a unit. For example, the parameter --debug-real-size=1G sets a real size of 1GB. --- f3probe.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/f3probe.c b/f3probe.c index 2802295..ed382ca 100644 --- a/f3probe.c +++ b/f3probe.c @@ -79,14 +79,47 @@ static long long arg_to_long_long(const struct argp_state *state, { char *end; long long ll = strtoll(arg, &end, 0); - if (!arg) + if (end == arg) argp_error(state, "An integer must be provided"); - if (!*arg || *end) + + /* Deal with units. */ + switch (*end) { + case 's': + case 'S': /* Sectors */ + ll <<= 9; + end++; + break; + + case 'k': + case 'K': /* KB */ + ll <<= 10; + end++; + break; + + case 'm': + case 'M': /* MB */ + ll <<= 20; + end++; + break; + + case 'g': + case 'G': /* GB */ + ll <<= 30; + end++; + break; + + case 't': + case 'T': /* TB */ + ll <<= 40; + end++; + break; + } + + if (*end) argp_error(state, "`%s' is not an integer", arg); return ll; } -/* XXX Add a friendly way to enter real and fake sizes: 1, 1k, 1m, 1g... */ static error_t parse_opt(int key, char *arg, struct argp_state *state) { struct args *args = state->input;