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.
This commit is contained in:
Michel Machado 2014-12-16 08:52:42 -05:00
parent 1db68aa268
commit adb8aaec84

View File

@ -79,14 +79,47 @@ static long long arg_to_long_long(const struct argp_state *state,
{ {
char *end; char *end;
long long ll = strtoll(arg, &end, 0); long long ll = strtoll(arg, &end, 0);
if (!arg) if (end == arg)
argp_error(state, "An integer must be provided"); 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); argp_error(state, "`%s' is not an integer", arg);
return ll; 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) static error_t parse_opt(int key, char *arg, struct argp_state *state)
{ {
struct args *args = state->input; struct args *args = state->input;