aboutsummaryrefslogtreecommitdiffstats
path: root/lib/vsprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vsprintf.c')
-rw-r--r--lib/vsprintf.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 7b481cea54ae..419993f58c6b 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -126,6 +126,129 @@ long long simple_strtoll(const char *cp,char **endp,unsigned int base)
126 return simple_strtoull(cp,endp,base); 126 return simple_strtoull(cp,endp,base);
127} 127}
128 128
129
130/**
131 * strict_strtoul - convert a string to an unsigned long strictly
132 * @cp: The string to be converted
133 * @base: The number base to use
134 * @res: The converted result value
135 *
136 * strict_strtoul converts a string to an unsigned long only if the
137 * string is really an unsigned long string, any string containing
138 * any invalid char at the tail will be rejected and -EINVAL is returned,
139 * only a newline char at the tail is acceptible because people generally
140 * change a module parameter in the following way:
141 *
142 * echo 1024 > /sys/module/e1000/parameters/copybreak
143 *
144 * echo will append a newline to the tail.
145 *
146 * It returns 0 if conversion is successful and *res is set to the converted
147 * value, otherwise it returns -EINVAL and *res is set to 0.
148 *
149 * simple_strtoul just ignores the successive invalid characters and
150 * return the converted value of prefix part of the string.
151 */
152int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
153
154/**
155 * strict_strtol - convert a string to a long strictly
156 * @cp: The string to be converted
157 * @base: The number base to use
158 * @res: The converted result value
159 *
160 * strict_strtol is similiar to strict_strtoul, but it allows the first
161 * character of a string is '-'.
162 *
163 * It returns 0 if conversion is successful and *res is set to the converted
164 * value, otherwise it returns -EINVAL and *res is set to 0.
165 */
166int strict_strtol(const char *cp, unsigned int base, long *res);
167
168/**
169 * strict_strtoull - convert a string to an unsigned long long strictly
170 * @cp: The string to be converted
171 * @base: The number base to use
172 * @res: The converted result value
173 *
174 * strict_strtoull converts a string to an unsigned long long only if the
175 * string is really an unsigned long long string, any string containing
176 * any invalid char at the tail will be rejected and -EINVAL is returned,
177 * only a newline char at the tail is acceptible because people generally
178 * change a module parameter in the following way:
179 *
180 * echo 1024 > /sys/module/e1000/parameters/copybreak
181 *
182 * echo will append a newline to the tail of the string.
183 *
184 * It returns 0 if conversion is successful and *res is set to the converted
185 * value, otherwise it returns -EINVAL and *res is set to 0.
186 *
187 * simple_strtoull just ignores the successive invalid characters and
188 * return the converted value of prefix part of the string.
189 */
190int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
191
192/**
193 * strict_strtoll - convert a string to a long long strictly
194 * @cp: The string to be converted
195 * @base: The number base to use
196 * @res: The converted result value
197 *
198 * strict_strtoll is similiar to strict_strtoull, but it allows the first
199 * character of a string is '-'.
200 *
201 * It returns 0 if conversion is successful and *res is set to the converted
202 * value, otherwise it returns -EINVAL and *res is set to 0.
203 */
204int strict_strtoll(const char *cp, unsigned int base, long long *res);
205
206#define define_strict_strtoux(type, valtype) \
207int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
208{ \
209 char *tail; \
210 valtype val; \
211 size_t len; \
212 \
213 *res = 0; \
214 len = strlen(cp); \
215 if (len == 0) \
216 return -EINVAL; \
217 \
218 val = simple_strtoul(cp, &tail, base); \
219 if ((*tail == '\0') || \
220 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
221 *res = val; \
222 return 0; \
223 } \
224 \
225 return -EINVAL; \
226} \
227
228#define define_strict_strtox(type, valtype) \
229int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
230{ \
231 int ret; \
232 if (*cp == '-') { \
233 ret = strict_strtou##type(cp+1, base, res); \
234 if (ret != 0) \
235 *res = -(*res); \
236 } else \
237 ret = strict_strtou##type(cp, base, res); \
238 \
239 return ret; \
240} \
241
242define_strict_strtoux(l, unsigned long)
243define_strict_strtox(l, long)
244define_strict_strtoux(ll, unsigned long long)
245define_strict_strtox(ll, long long)
246
247EXPORT_SYMBOL(strict_strtoul);
248EXPORT_SYMBOL(strict_strtol);
249EXPORT_SYMBOL(strict_strtoll);
250EXPORT_SYMBOL(strict_strtoull);
251
129static int skip_atoi(const char **s) 252static int skip_atoi(const char **s)
130{ 253{
131 int i=0; 254 int i=0;