Commit fd193829744bc77392395cf8f47889235c97f0a3

Authored by Robert P. J. Day
Committed by Linus Torvalds
1 parent cb345d7352

lib: allow memparse() to accept a NULL and ignorable second parm

Extend memparse() to allow the caller to use a NULL second parameter, which
would represent no interest in returning the address of the end of the parsed
string.

In numerous cases, callers invoke memparse() to parse a possibly-suffixed
string (such as "64K" or "2G" or whatever) and define a character pointer to
accept the end pointer being returned by memparse() even though they have no
interest in it and promptly throw it away.

This (backward-compatible) enhancement allows callers to use NULL in the cases
where they just don't care about getting back that end pointer.

[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 1 changed file with 11 additions and 5 deletions Side-by-side Diff

... ... @@ -116,7 +116,7 @@
116 116 /**
117 117 * memparse - parse a string with mem suffixes into a number
118 118 * @ptr: Where parse begins
119   - * @retptr: (output) Pointer to next char after parse completes
  119 + * @retptr: (output) Optional pointer to next char after parse completes
120 120 *
121 121 * Parses a string into a number. The number stored at @ptr is
122 122 * potentially suffixed with %K (for kilobytes, or 1024 bytes),
123 123  
124 124  
... ... @@ -126,11 +126,13 @@
126 126 * megabyte, or one gigabyte, respectively.
127 127 */
128 128  
129   -unsigned long long memparse (char *ptr, char **retptr)
  129 +unsigned long long memparse(char *ptr, char **retptr)
130 130 {
131   - unsigned long long ret = simple_strtoull (ptr, retptr, 0);
  131 + char *endptr; /* local pointer to end of parsed string */
132 132  
133   - switch (**retptr) {
  133 + unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
  134 +
  135 + switch (*endptr) {
134 136 case 'G':
135 137 case 'g':
136 138 ret <<= 10;
137 139  
... ... @@ -140,10 +142,14 @@
140 142 case 'K':
141 143 case 'k':
142 144 ret <<= 10;
143   - (*retptr)++;
  145 + endptr++;
144 146 default:
145 147 break;
146 148 }
  149 +
  150 + if (retptr)
  151 + *retptr = endptr;
  152 +
147 153 return ret;
148 154 }
149 155