Commit 8ba7f6c2 authored by Al Viro's avatar Al Viro

saner perf_atoll()

That loop in there is both anti-idiomatic *and* completely pointless.
strtoll() is there for purpose; use it and compare what's left with
acceptable suffices.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 849f3127
...@@ -9,78 +9,48 @@ ...@@ -9,78 +9,48 @@
*/ */
s64 perf_atoll(const char *str) s64 perf_atoll(const char *str)
{ {
unsigned int i; s64 length;
s64 length = -1, unit = 1; char *p;
char c;
if (!isdigit(str[0])) if (!isdigit(str[0]))
goto out_err; goto out_err;
for (i = 1; i < strlen(str); i++) { length = strtoll(str, &p, 10);
switch (str[i]) { switch (c = *p++) {
case 'B': case 'b': case 'B':
case 'b': if (*p)
break;
case 'K':
if (str[i + 1] != 'B')
goto out_err;
else
goto kilo;
case 'k':
if (str[i + 1] != 'b')
goto out_err;
kilo:
unit = K;
break;
case 'M':
if (str[i + 1] != 'B')
goto out_err;
else
goto mega;
case 'm':
if (str[i + 1] != 'b')
goto out_err; goto out_err;
mega: case '\0':
unit = K * K; return length;
break; default:
case 'G':
if (str[i + 1] != 'B')
goto out_err;
else
goto giga;
case 'g':
if (str[i + 1] != 'b')
goto out_err; goto out_err;
giga: /* two-letter suffices */
unit = K * K * K; case 'k': case 'K':
length <<= 10;
break; break;
case 'T': case 'm': case 'M':
if (str[i + 1] != 'B') length <<= 20;
goto out_err;
else
goto tera;
case 't':
if (str[i + 1] != 'b')
goto out_err;
tera:
unit = K * K * K * K;
break; break;
case '\0': /* only specified figures */ case 'g': case 'G':
unit = 1; length <<= 30;
break; break;
default: case 't': case 'T':
if (!isdigit(str[i])) length <<= 40;
goto out_err;
break; break;
} }
/* we want the cases to match */
if (islower(c)) {
if (strcmp(p, "b") != 0)
goto out_err;
} else {
if (strcmp(p, "B") != 0)
goto out_err;
} }
return length;
length = atoll(str) * unit;
goto out;
out_err: out_err:
length = -1; return -1;
out:
return length;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment