2015-09-29 23:19:01 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ctype.h>
|
2015-09-30 18:05:14 +00:00
|
|
|
#include <errno.h>
|
2015-09-29 23:19:01 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
off_t
|
|
|
|
parseoffset(const char *str)
|
|
|
|
{
|
2015-09-30 18:05:14 +00:00
|
|
|
off_t res, scale = 1;
|
2015-09-29 23:19:01 +00:00
|
|
|
char *end;
|
|
|
|
|
2015-09-30 18:05:14 +00:00
|
|
|
/* strictly check what strtol() usually would let pass */
|
|
|
|
if (!str || !*str || isspace(*str) || *str == '+' || *str == '-') {
|
|
|
|
weprintf("parseoffset %s: invalid value\n", str);
|
2015-09-30 17:14:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-30 18:05:14 +00:00
|
|
|
errno = 0;
|
|
|
|
res = strtol(str, &end, 0);
|
|
|
|
if (errno) {
|
|
|
|
weprintf("parseoffset %s: invalid value\n", str);
|
|
|
|
return -1;
|
2015-09-29 23:19:01 +00:00
|
|
|
}
|
|
|
|
if (res < 0) {
|
2015-09-30 17:14:14 +00:00
|
|
|
weprintf("parseoffset %s: negative value\n", str);
|
2015-09-29 23:19:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* suffix */
|
|
|
|
if (*end) {
|
|
|
|
switch (toupper((int)*end)) {
|
|
|
|
case 'B':
|
|
|
|
scale = 512L;
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
scale = 1024L;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
scale = 1024L * 1024L;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
scale = 1024L * 1024L * 1024L;
|
|
|
|
break;
|
|
|
|
default:
|
2015-09-30 18:05:14 +00:00
|
|
|
weprintf("parseoffset %s: invalid suffix '%s'\n", str, end);
|
2015-09-29 23:19:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prevent overflow */
|
2015-11-26 10:30:35 +00:00
|
|
|
if (res > (SSIZE_MAX / scale)) {
|
2015-09-30 17:14:14 +00:00
|
|
|
weprintf("parseoffset %s: out of range\n", str);
|
2015-09-29 23:19:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-29 23:26:27 +00:00
|
|
|
return res * scale;
|
2015-09-29 23:19:01 +00:00
|
|
|
}
|