9
0
Fork 0

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.
This commit is contained in:
Sascha Hauer 2007-10-09 12:56:20 +02:00
parent 90426cc3f1
commit 0fca6b006c
1 changed files with 25 additions and 15 deletions

View File

@ -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;
}