diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2007-10-09 12:56:20 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2007-10-09 12:56:20 +0200 |
commit | 0fca6b006c0482494cda0fde19d6929d9d9b3454 (patch) | |
tree | 539065ea490b44a854265510793e15786a153e5f /lib | |
parent | 90426cc3f1119e3d38109e85c94538599d5be2ec (diff) | |
download | barebox-0fca6b006c0482494cda0fde19d6929d9d9b3454.tar.gz barebox-0fca6b006c0482494cda0fde19d6929d9d9b3454.tar.xz |
parse_area_spec():
- No need to handle strings where only size or end is given. This
form is confusing and does not give real benefit.
- Fix start-end form. The calculation was wrong.
- Return an error if end < start.
- Add function description.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/misc.c | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/lib/misc.c b/lib/misc.c index cc4e49d9f2..ea0ffc2b19 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -26,6 +26,10 @@ #include <fs.h> #include <linux/ctype.h> +/* + * Like simple_strtoul() but handles an optional G, M, K or k + * suffix for Gigabyte, Megabyte or Kilobyte + */ unsigned long strtoul_suffix(const char *str, char **endp, int base) { unsigned long val; @@ -42,6 +46,8 @@ unsigned long strtoul_suffix(const char *str, char **endp, int base) case 'K': val *= 1024; end++; + default: + break; } if (endp) @@ -51,23 +57,22 @@ unsigned long strtoul_suffix(const char *str, char **endp, int base) } EXPORT_SYMBOL(strtoul_suffix); +/* + * This function parses strings in the form <startadr>[-endaddr] + * or <startadr>[+size] and fills in start and size accordingly. + * <startadr> and <endadr> can be given in decimal or hex (with 0x prefix) + * and can have an optional G, M, K or k suffix. + * + * examples: + * 0x1000-0x2000 -> start = 0x1000, size = 0x1001 + * 0x1000+0x1000 -> start = 0x1000, size = 0x1000 + * 0x1000 -> start = 0x1000, size = ~0 + * 1M+1k -> start = 0x100000, size = 0x400 + */ int parse_area_spec(const char *str, ulong *start, ulong *size) { char *endp; - - if (*str == '+') { - /* no beginning given but size so start is 0 */ - *start = 0; - *size = strtoul_suffix(str + 1, &endp, 0); - return 0; - } - - if (*str == '-') { - /* no beginning given but end, so start is 0 */ - *start = 0; - *size = strtoul_suffix(str + 1, &endp, 0) + 1; - return 0; - } + ulong end; if (!isdigit(*str)) return -1; @@ -84,7 +89,12 @@ int parse_area_spec(const char *str, ulong *start, ulong *size) if (*str == '-') { /* beginning and end given */ - *size = strtoul_suffix(str + 1, NULL, 0) + 1; + end = strtoul_suffix(str + 1, NULL, 0); + if (end < *start) { + printf("end < start\n"); + return -1; + } + *size = end - *start + 1; return 0; } |