lib/strutils: follow const in parse_size()
* don't cast from char to const char * don't share endptr from strtoxxx() with rest of the code as the end pointer is char, but code works with const chars Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
8a7aeeda67
commit
d54b83156f
|
@ -62,7 +62,8 @@ static int do_scale_by_power (uintmax_t *x, int base, int power)
|
||||||
*/
|
*/
|
||||||
int parse_size(const char *str, uintmax_t *res, int *power)
|
int parse_size(const char *str, uintmax_t *res, int *power)
|
||||||
{
|
{
|
||||||
char *p;
|
const char *p;
|
||||||
|
char *end;
|
||||||
uintmax_t x, frac = 0;
|
uintmax_t x, frac = 0;
|
||||||
int base = 1024, rc = 0, pwr = 0, frac_zeros = 0;
|
int base = 1024, rc = 0, pwr = 0, frac_zeros = 0;
|
||||||
|
|
||||||
|
@ -83,25 +84,25 @@ int parse_size(const char *str, uintmax_t *res, int *power)
|
||||||
* use lconv->negative_sign. But coreutils use the same solution,
|
* use lconv->negative_sign. But coreutils use the same solution,
|
||||||
* so it's probably good enough...
|
* so it's probably good enough...
|
||||||
*/
|
*/
|
||||||
p = (char *) str;
|
p = str;
|
||||||
while (isspace((unsigned char) *p))
|
while (isspace((unsigned char) *p))
|
||||||
p++;
|
p++;
|
||||||
if (*p == '-') {
|
if (*p == '-') {
|
||||||
rc = -EINVAL;
|
rc = -EINVAL;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
p = NULL;
|
|
||||||
|
|
||||||
errno = 0;
|
errno = 0, end = NULL;
|
||||||
x = strtoumax(str, &p, 0);
|
x = strtoumax(str, &end, 0);
|
||||||
|
|
||||||
if (p == str ||
|
if (end == str ||
|
||||||
(errno != 0 && (x == UINTMAX_MAX || x == 0))) {
|
(errno != 0 && (x == UINTMAX_MAX || x == 0))) {
|
||||||
rc = errno ? -errno : -EINVAL;
|
rc = errno ? -errno : -EINVAL;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
if (!p || !*p)
|
if (!end || !*end)
|
||||||
goto done; /* without suffix */
|
goto done; /* without suffix */
|
||||||
|
p = end;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check size suffixes
|
* Check size suffixes
|
||||||
|
@ -113,25 +114,26 @@ check_suffix:
|
||||||
base = 1000; /* XB, 10^N */
|
base = 1000; /* XB, 10^N */
|
||||||
else if (*(p + 1)) {
|
else if (*(p + 1)) {
|
||||||
struct lconv const *l = localeconv();
|
struct lconv const *l = localeconv();
|
||||||
char *dp = l ? l->decimal_point : NULL;
|
const char *dp = l ? l->decimal_point : NULL;
|
||||||
size_t dpsz = dp ? strlen(dp) : 0;
|
size_t dpsz = dp ? strlen(dp) : 0;
|
||||||
|
|
||||||
if (frac == 0 && *p && dp && strncmp(dp, p, dpsz) == 0) {
|
if (frac == 0 && *p && dp && strncmp(dp, p, dpsz) == 0) {
|
||||||
char *fstr = p + dpsz;
|
const char *fstr = p + dpsz;
|
||||||
|
|
||||||
for (p = fstr; *p == '0'; p++)
|
for (p = fstr; *p == '0'; p++)
|
||||||
frac_zeros++;
|
frac_zeros++;
|
||||||
errno = 0, p = NULL;
|
errno = 0, end = NULL;
|
||||||
frac = strtoumax(fstr, &p, 0);
|
frac = strtoumax(fstr, &end, 0);
|
||||||
if (p == fstr ||
|
if (end == fstr ||
|
||||||
(errno != 0 && (frac == UINTMAX_MAX || frac == 0))) {
|
(errno != 0 && (frac == UINTMAX_MAX || frac == 0))) {
|
||||||
rc = errno ? -errno : -EINVAL;
|
rc = errno ? -errno : -EINVAL;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
if (frac && (!p || !*p)) {
|
if (frac && (!end || !*end)) {
|
||||||
rc = -EINVAL;
|
rc = -EINVAL;
|
||||||
goto err; /* without suffix, but with frac */
|
goto err; /* without suffix, but with frac */
|
||||||
}
|
}
|
||||||
|
p = end;
|
||||||
goto check_suffix;
|
goto check_suffix;
|
||||||
}
|
}
|
||||||
rc = -EINVAL;
|
rc = -EINVAL;
|
||||||
|
|
Loading…
Reference in New Issue