config: allow parsing percentages

Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
This commit is contained in:
Michael Olbrich 2019-05-06 08:48:24 +02:00
parent 28152c298b
commit e2f88d5af0
3 changed files with 32 additions and 4 deletions

View File

@ -152,7 +152,22 @@ unsigned long long cfg_getint_suffix(cfg_t *sec, const char *name)
unsigned long long val = 0;
if (str)
val = strtoul_suffix(str, NULL, 0);
val = strtoul_suffix(str, NULL, NULL);
return val;
}
/*
* Like cfg_getint_suffix() but allow '%' as suffix as well
*/
unsigned long long cfg_getint_suffix_percent(cfg_t *sec, const char *name,
cfg_bool_t *percent)
{
const char *str = cfg_getstr(sec, name);
unsigned long long val = 0;
if (str)
val = strtoul_suffix(str, NULL, percent);
return val;
}

View File

@ -124,7 +124,8 @@ extern struct image_handler fit_handler;
(type *)( (char *)__mptr - offsetof(type,member) );})
void *xzalloc(size_t n);
unsigned long long strtoul_suffix(const char *str, char **endp, int base);
unsigned long long strtoul_suffix(const char *str, char **endp,
cfg_bool_t *percent);
int init_config(void);
cfg_opt_t *get_confuse_opts(void);
@ -144,6 +145,8 @@ int insert_data(struct image *image, const char *data, const char *outfile,
int reload_partitions(struct image *image);
unsigned long long cfg_getint_suffix(cfg_t *sec, const char *name);
unsigned long long cfg_getint_suffix_percent(cfg_t *sec, const char *name,
cfg_bool_t *percent);
static inline const char *imageoutfile(const struct image *image)
{

14
util.c
View File

@ -251,12 +251,16 @@ void *xzalloc(size_t n)
* Like simple_strtoul() but handles an optional G, M, K or k
* suffix for Gigabyte, Megabyte or Kilobyte
*/
unsigned long long strtoul_suffix(const char *str, char **endp, int base)
unsigned long long strtoul_suffix(const char *str, char **endp,
cfg_bool_t *percent)
{
unsigned long long val;
char *end;
val = strtoull(str, &end, base);
val = strtoull(str, &end, 0);
if (percent)
*percent = cfg_false;
switch (*end) {
case 'G':
@ -271,6 +275,12 @@ unsigned long long strtoul_suffix(const char *str, char **endp, int base)
end++;
case '\0':
break;
case '%':
if (percent) {
*percent = cfg_true;
break;
}
/* fall-through */
default:
error("Invalid size suffix '%s' in '%s'\n", end, str);
exit(1);