parse_size(): replace atoll() with strtoull()

Replace the function atoll with strtoull(); Check that the suffix for the
parse_size() input is of only one character.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
This commit is contained in:
Goffredo Baroncelli 2012-10-29 18:53:18 +01:00 committed by David Sterba
parent 8f76aee6bc
commit 6318867602
1 changed files with 19 additions and 6 deletions

25
utils.c
View File

@ -31,6 +31,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <mntent.h> #include <mntent.h>
#include <ctype.h>
#include <linux/loop.h> #include <linux/loop.h>
#include <linux/major.h> #include <linux/major.h>
#include <linux/kdev_t.h> #include <linux/kdev_t.h>
@ -1222,12 +1223,18 @@ scan_again:
u64 parse_size(char *s) u64 parse_size(char *s)
{ {
int len = strlen(s); int i;
char c; char c;
u64 mult = 1; u64 mult = 1;
if (!isdigit(s[len - 1])) { for (i=0 ; s[i] && isdigit(s[i]) ; i++) ;
c = tolower(s[len - 1]); if (!i) {
fprintf(stderr, "ERROR: size value is empty\n");
exit(50);
}
if (s[i]) {
c = tolower(s[i]);
switch (c) { switch (c) {
case 'g': case 'g':
mult *= 1024; mult *= 1024;
@ -1238,11 +1245,17 @@ u64 parse_size(char *s)
case 'b': case 'b':
break; break;
default: default:
fprintf(stderr, "Unknown size descriptor %c\n", c); fprintf(stderr, "ERROR: Unknown size descriptor "
"'%c'\n", c);
exit(1); exit(1);
} }
s[len - 1] = '\0';
} }
return atoll(s) * mult; if (s[i] && s[i+1]) {
fprintf(stderr, "ERROR: Illegal suffix contains "
"character '%c' in wrong position\n",
s[i+1]);
exit(51);
}
return strtoull(s, NULL, 10) * mult;
} }