]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/lib/vsprintf.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ | |
8 | /* | |
9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) | |
10 | */ | |
11 | ||
7b9186f5 | 12 | /* |
1da177e4 LT |
13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
14 | * - changed to provide snprintf and vsnprintf functions | |
15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> | |
16 | * - scnprintf and vscnprintf | |
17 | */ | |
18 | ||
19 | #include <stdarg.h> | |
8bc3bcc9 | 20 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ |
1da177e4 LT |
21 | #include <linux/types.h> |
22 | #include <linux/string.h> | |
23 | #include <linux/ctype.h> | |
24 | #include <linux/kernel.h> | |
0fe1ef24 LT |
25 | #include <linux/kallsyms.h> |
26 | #include <linux/uaccess.h> | |
332d2e78 | 27 | #include <linux/ioport.h> |
8a27f7c9 | 28 | #include <net/addrconf.h> |
1da177e4 | 29 | |
4e57b681 | 30 | #include <asm/page.h> /* for PAGE_SIZE */ |
1da177e4 | 31 | #include <asm/div64.h> |
deac93df | 32 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
1da177e4 | 33 | |
1dff46d6 | 34 | #include "kstrtox.h" |
aa46a63e | 35 | |
1da177e4 | 36 | /** |
922ac25c | 37 | * simple_strtoull - convert a string to an unsigned long long |
1da177e4 LT |
38 | * @cp: The start of the string |
39 | * @endp: A pointer to the end of the parsed string will be placed here | |
40 | * @base: The number base to use | |
41 | */ | |
922ac25c | 42 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
1da177e4 | 43 | { |
1dff46d6 AD |
44 | unsigned long long result; |
45 | unsigned int rv; | |
aa46a63e | 46 | |
1dff46d6 AD |
47 | cp = _parse_integer_fixup_radix(cp, &base); |
48 | rv = _parse_integer(cp, base, &result); | |
49 | /* FIXME */ | |
50 | cp += (rv & ~KSTRTOX_OVERFLOW); | |
aa46a63e | 51 | |
1da177e4 LT |
52 | if (endp) |
53 | *endp = (char *)cp; | |
7b9186f5 | 54 | |
1da177e4 LT |
55 | return result; |
56 | } | |
922ac25c | 57 | EXPORT_SYMBOL(simple_strtoull); |
1da177e4 LT |
58 | |
59 | /** | |
922ac25c | 60 | * simple_strtoul - convert a string to an unsigned long |
1da177e4 LT |
61 | * @cp: The start of the string |
62 | * @endp: A pointer to the end of the parsed string will be placed here | |
63 | * @base: The number base to use | |
64 | */ | |
922ac25c | 65 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
1da177e4 | 66 | { |
922ac25c | 67 | return simple_strtoull(cp, endp, base); |
1da177e4 | 68 | } |
922ac25c | 69 | EXPORT_SYMBOL(simple_strtoul); |
1da177e4 LT |
70 | |
71 | /** | |
922ac25c | 72 | * simple_strtol - convert a string to a signed long |
1da177e4 LT |
73 | * @cp: The start of the string |
74 | * @endp: A pointer to the end of the parsed string will be placed here | |
75 | * @base: The number base to use | |
76 | */ | |
922ac25c | 77 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
1da177e4 | 78 | { |
922ac25c AGR |
79 | if (*cp == '-') |
80 | return -simple_strtoul(cp + 1, endp, base); | |
7b9186f5 | 81 | |
922ac25c | 82 | return simple_strtoul(cp, endp, base); |
1da177e4 | 83 | } |
922ac25c | 84 | EXPORT_SYMBOL(simple_strtol); |
1da177e4 LT |
85 | |
86 | /** | |
87 | * simple_strtoll - convert a string to a signed long long | |
88 | * @cp: The start of the string | |
89 | * @endp: A pointer to the end of the parsed string will be placed here | |
90 | * @base: The number base to use | |
91 | */ | |
22d27051 | 92 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
1da177e4 | 93 | { |
7b9186f5 | 94 | if (*cp == '-') |
22d27051 | 95 | return -simple_strtoull(cp + 1, endp, base); |
7b9186f5 | 96 | |
22d27051 | 97 | return simple_strtoull(cp, endp, base); |
1da177e4 | 98 | } |
98d5ce0d | 99 | EXPORT_SYMBOL(simple_strtoll); |
1da177e4 | 100 | |
cf3b429b JP |
101 | static noinline_for_stack |
102 | int skip_atoi(const char **s) | |
1da177e4 | 103 | { |
7b9186f5 | 104 | int i = 0; |
1da177e4 LT |
105 | |
106 | while (isdigit(**s)) | |
107 | i = i*10 + *((*s)++) - '0'; | |
7b9186f5 | 108 | |
1da177e4 LT |
109 | return i; |
110 | } | |
111 | ||
4277eedd DV |
112 | /* Decimal conversion is by far the most typical, and is used |
113 | * for /proc and /sys data. This directly impacts e.g. top performance | |
114 | * with many processes running. We optimize it for speed | |
133fd9f5 DV |
115 | * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html> |
116 | * (with permission from the author, Douglas W. Jones). | |
117 | */ | |
4277eedd | 118 | |
133fd9f5 DV |
119 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
120 | /* Formats correctly any integer in [0, 999999999] */ | |
cf3b429b | 121 | static noinline_for_stack |
133fd9f5 | 122 | char *put_dec_full9(char *buf, unsigned q) |
4277eedd | 123 | { |
133fd9f5 | 124 | unsigned r; |
7b9186f5 | 125 | |
133fd9f5 DV |
126 | /* |
127 | * Possible ways to approx. divide by 10 | |
128 | * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) | |
129 | * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) | |
130 | * (x * 0x6667) >> 18 x < 43699 | |
131 | * (x * 0x3334) >> 17 x < 16389 | |
132 | * (x * 0x199a) >> 16 x < 16389 | |
133 | * (x * 0x0ccd) >> 15 x < 16389 | |
134 | * (x * 0x0667) >> 14 x < 2739 | |
135 | * (x * 0x0334) >> 13 x < 1029 | |
136 | * (x * 0x019a) >> 12 x < 1029 | |
137 | * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) | |
138 | * (x * 0x0067) >> 10 x < 179 | |
139 | * (x * 0x0034) >> 9 x < 69 same | |
140 | * (x * 0x001a) >> 8 x < 69 same | |
141 | * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) | |
142 | * (x * 0x0007) >> 6 x < 19 | |
143 | * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html> | |
144 | */ | |
145 | r = (q * (uint64_t)0x1999999a) >> 32; | |
146 | *buf++ = (q - 10 * r) + '0'; /* 1 */ | |
147 | q = (r * (uint64_t)0x1999999a) >> 32; | |
148 | *buf++ = (r - 10 * q) + '0'; /* 2 */ | |
149 | r = (q * (uint64_t)0x1999999a) >> 32; | |
150 | *buf++ = (q - 10 * r) + '0'; /* 3 */ | |
151 | q = (r * (uint64_t)0x1999999a) >> 32; | |
152 | *buf++ = (r - 10 * q) + '0'; /* 4 */ | |
153 | r = (q * (uint64_t)0x1999999a) >> 32; | |
154 | *buf++ = (q - 10 * r) + '0'; /* 5 */ | |
155 | /* Now value is under 10000, can avoid 64-bit multiply */ | |
156 | q = (r * 0x199a) >> 16; | |
157 | *buf++ = (r - 10 * q) + '0'; /* 6 */ | |
158 | r = (q * 0xcd) >> 11; | |
159 | *buf++ = (q - 10 * r) + '0'; /* 7 */ | |
160 | q = (r * 0xcd) >> 11; | |
161 | *buf++ = (r - 10 * q) + '0'; /* 8 */ | |
162 | *buf++ = q + '0'; /* 9 */ | |
4277eedd DV |
163 | return buf; |
164 | } | |
133fd9f5 DV |
165 | #endif |
166 | ||
167 | /* Similar to above but do not pad with zeros. | |
168 | * Code can be easily arranged to print 9 digits too, but our callers | |
169 | * always call put_dec_full9() instead when the number has 9 decimal digits. | |
170 | */ | |
cf3b429b | 171 | static noinline_for_stack |
133fd9f5 | 172 | char *put_dec_trunc8(char *buf, unsigned r) |
4277eedd | 173 | { |
133fd9f5 DV |
174 | unsigned q; |
175 | ||
176 | /* Copy of previous function's body with added early returns */ | |
177 | q = (r * (uint64_t)0x1999999a) >> 32; | |
178 | *buf++ = (r - 10 * q) + '0'; /* 2 */ | |
179 | if (q == 0) | |
180 | return buf; | |
181 | r = (q * (uint64_t)0x1999999a) >> 32; | |
182 | *buf++ = (q - 10 * r) + '0'; /* 3 */ | |
183 | if (r == 0) | |
184 | return buf; | |
185 | q = (r * (uint64_t)0x1999999a) >> 32; | |
186 | *buf++ = (r - 10 * q) + '0'; /* 4 */ | |
187 | if (q == 0) | |
188 | return buf; | |
189 | r = (q * (uint64_t)0x1999999a) >> 32; | |
190 | *buf++ = (q - 10 * r) + '0'; /* 5 */ | |
191 | if (r == 0) | |
192 | return buf; | |
193 | q = (r * 0x199a) >> 16; | |
194 | *buf++ = (r - 10 * q) + '0'; /* 6 */ | |
195 | if (q == 0) | |
196 | return buf; | |
197 | r = (q * 0xcd) >> 11; | |
198 | *buf++ = (q - 10 * r) + '0'; /* 7 */ | |
199 | if (r == 0) | |
200 | return buf; | |
201 | q = (r * 0xcd) >> 11; | |
202 | *buf++ = (r - 10 * q) + '0'; /* 8 */ | |
203 | if (q == 0) | |
204 | return buf; | |
205 | *buf++ = q + '0'; /* 9 */ | |
206 | return buf; | |
207 | } | |
4277eedd | 208 | |
133fd9f5 DV |
209 | /* There are two algorithms to print larger numbers. |
210 | * One is generic: divide by 1000000000 and repeatedly print | |
211 | * groups of (up to) 9 digits. It's conceptually simple, | |
212 | * but requires a (unsigned long long) / 1000000000 division. | |
213 | * | |
214 | * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, | |
215 | * manipulates them cleverly and generates groups of 4 decimal digits. | |
216 | * It so happens that it does NOT require long long division. | |
217 | * | |
218 | * If long is > 32 bits, division of 64-bit values is relatively easy, | |
219 | * and we will use the first algorithm. | |
220 | * If long long is > 64 bits (strange architecture with VERY large long long), | |
221 | * second algorithm can't be used, and we again use the first one. | |
222 | * | |
223 | * Else (if long is 32 bits and long long is 64 bits) we use second one. | |
224 | */ | |
7b9186f5 | 225 | |
133fd9f5 DV |
226 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
227 | ||
228 | /* First algorithm: generic */ | |
229 | ||
230 | static | |
231 | char *put_dec(char *buf, unsigned long long n) | |
232 | { | |
233 | if (n >= 100*1000*1000) { | |
234 | while (n >= 1000*1000*1000) | |
235 | buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); | |
236 | if (n >= 100*1000*1000) | |
237 | return put_dec_full9(buf, n); | |
238 | } | |
239 | return put_dec_trunc8(buf, n); | |
4277eedd | 240 | } |
133fd9f5 DV |
241 | |
242 | #else | |
243 | ||
244 | /* Second algorithm: valid only for 64-bit long longs */ | |
245 | ||
cf3b429b | 246 | static noinline_for_stack |
133fd9f5 | 247 | char *put_dec_full4(char *buf, unsigned q) |
4277eedd | 248 | { |
133fd9f5 DV |
249 | unsigned r; |
250 | r = (q * 0xcccd) >> 19; | |
251 | *buf++ = (q - 10 * r) + '0'; | |
252 | q = (r * 0x199a) >> 16; | |
253 | *buf++ = (r - 10 * q) + '0'; | |
254 | r = (q * 0xcd) >> 11; | |
255 | *buf++ = (q - 10 * r) + '0'; | |
256 | *buf++ = r + '0'; | |
257 | return buf; | |
4277eedd DV |
258 | } |
259 | ||
133fd9f5 DV |
260 | /* Based on code by Douglas W. Jones found at |
261 | * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> | |
262 | * (with permission from the author). | |
263 | * Performs no 64-bit division and hence should be fast on 32-bit machines. | |
264 | */ | |
265 | static | |
266 | char *put_dec(char *buf, unsigned long long n) | |
267 | { | |
268 | uint32_t d3, d2, d1, q, h; | |
269 | ||
270 | if (n < 100*1000*1000) | |
271 | return put_dec_trunc8(buf, n); | |
272 | ||
273 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ | |
274 | h = (n >> 32); | |
275 | d2 = (h ) & 0xffff; | |
276 | d3 = (h >> 16); /* implicit "& 0xffff" */ | |
277 | ||
278 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); | |
279 | ||
280 | buf = put_dec_full4(buf, q % 10000); | |
281 | q = q / 10000; | |
282 | ||
283 | d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1; | |
284 | buf = put_dec_full4(buf, d1 % 10000); | |
285 | q = d1 / 10000; | |
286 | ||
287 | d2 = q + 4749 * d3 + 42 * d2; | |
288 | buf = put_dec_full4(buf, d2 % 10000); | |
289 | q = d2 / 10000; | |
290 | ||
291 | d3 = q + 281 * d3; | |
292 | if (!d3) | |
293 | goto done; | |
294 | buf = put_dec_full4(buf, d3 % 10000); | |
295 | q = d3 / 10000; | |
296 | if (!q) | |
297 | goto done; | |
298 | buf = put_dec_full4(buf, q); | |
299 | done: | |
300 | while (buf[-1] == '0') | |
301 | --buf; | |
302 | ||
303 | return buf; | |
304 | } | |
305 | ||
306 | #endif | |
307 | ||
1ac101a5 KH |
308 | /* |
309 | * Convert passed number to decimal string. | |
310 | * Returns the length of string. On buffer overflow, returns 0. | |
311 | * | |
312 | * If speed is not important, use snprintf(). It's easy to read the code. | |
313 | */ | |
314 | int num_to_str(char *buf, int size, unsigned long long num) | |
315 | { | |
133fd9f5 | 316 | char tmp[sizeof(num) * 3]; |
1ac101a5 KH |
317 | int idx, len; |
318 | ||
133fd9f5 DV |
319 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
320 | if (num <= 9) { | |
321 | tmp[0] = '0' + num; | |
322 | len = 1; | |
323 | } else { | |
324 | len = put_dec(tmp, num) - tmp; | |
325 | } | |
1ac101a5 KH |
326 | |
327 | if (len > size) | |
328 | return 0; | |
329 | for (idx = 0; idx < len; ++idx) | |
330 | buf[idx] = tmp[len - idx - 1]; | |
133fd9f5 | 331 | return len; |
1ac101a5 KH |
332 | } |
333 | ||
1da177e4 LT |
334 | #define ZEROPAD 1 /* pad with zero */ |
335 | #define SIGN 2 /* unsigned/signed long */ | |
336 | #define PLUS 4 /* show plus */ | |
337 | #define SPACE 8 /* space if plus */ | |
338 | #define LEFT 16 /* left justified */ | |
b89dc5d6 BH |
339 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
340 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ | |
1da177e4 | 341 | |
fef20d9c FW |
342 | enum format_type { |
343 | FORMAT_TYPE_NONE, /* Just a string part */ | |
ed681a91 | 344 | FORMAT_TYPE_WIDTH, |
fef20d9c FW |
345 | FORMAT_TYPE_PRECISION, |
346 | FORMAT_TYPE_CHAR, | |
347 | FORMAT_TYPE_STR, | |
348 | FORMAT_TYPE_PTR, | |
349 | FORMAT_TYPE_PERCENT_CHAR, | |
350 | FORMAT_TYPE_INVALID, | |
351 | FORMAT_TYPE_LONG_LONG, | |
352 | FORMAT_TYPE_ULONG, | |
353 | FORMAT_TYPE_LONG, | |
a4e94ef0 Z |
354 | FORMAT_TYPE_UBYTE, |
355 | FORMAT_TYPE_BYTE, | |
fef20d9c FW |
356 | FORMAT_TYPE_USHORT, |
357 | FORMAT_TYPE_SHORT, | |
358 | FORMAT_TYPE_UINT, | |
359 | FORMAT_TYPE_INT, | |
360 | FORMAT_TYPE_NRCHARS, | |
361 | FORMAT_TYPE_SIZE_T, | |
362 | FORMAT_TYPE_PTRDIFF | |
363 | }; | |
364 | ||
365 | struct printf_spec { | |
4e310fda | 366 | u8 type; /* format_type enum */ |
ef0658f3 | 367 | u8 flags; /* flags to number() */ |
4e310fda JP |
368 | u8 base; /* number base, 8, 10 or 16 only */ |
369 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ | |
370 | s16 field_width; /* width of output field */ | |
371 | s16 precision; /* # of digits/chars */ | |
fef20d9c FW |
372 | }; |
373 | ||
cf3b429b JP |
374 | static noinline_for_stack |
375 | char *number(char *buf, char *end, unsigned long long num, | |
376 | struct printf_spec spec) | |
1da177e4 | 377 | { |
9b706aee DV |
378 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
379 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ | |
380 | ||
381 | char tmp[66]; | |
382 | char sign; | |
383 | char locase; | |
fef20d9c | 384 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
1da177e4 | 385 | int i; |
7c203422 | 386 | bool is_zero = num == 0LL; |
1da177e4 | 387 | |
9b706aee DV |
388 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
389 | * produces same digits or (maybe lowercased) letters */ | |
fef20d9c FW |
390 | locase = (spec.flags & SMALL); |
391 | if (spec.flags & LEFT) | |
392 | spec.flags &= ~ZEROPAD; | |
1da177e4 | 393 | sign = 0; |
fef20d9c | 394 | if (spec.flags & SIGN) { |
7b9186f5 | 395 | if ((signed long long)num < 0) { |
1da177e4 | 396 | sign = '-'; |
7b9186f5 | 397 | num = -(signed long long)num; |
fef20d9c FW |
398 | spec.field_width--; |
399 | } else if (spec.flags & PLUS) { | |
1da177e4 | 400 | sign = '+'; |
fef20d9c FW |
401 | spec.field_width--; |
402 | } else if (spec.flags & SPACE) { | |
1da177e4 | 403 | sign = ' '; |
fef20d9c | 404 | spec.field_width--; |
1da177e4 LT |
405 | } |
406 | } | |
b39a7340 | 407 | if (need_pfx) { |
fef20d9c | 408 | if (spec.base == 16) |
7c203422 PC |
409 | spec.field_width -= 2; |
410 | else if (!is_zero) | |
fef20d9c | 411 | spec.field_width--; |
1da177e4 | 412 | } |
b39a7340 DV |
413 | |
414 | /* generate full string in tmp[], in reverse order */ | |
1da177e4 | 415 | i = 0; |
133fd9f5 DV |
416 | if (num < spec.base) |
417 | tmp[i++] = digits[num] | locase; | |
4277eedd DV |
418 | /* Generic code, for any base: |
419 | else do { | |
9b706aee | 420 | tmp[i++] = (digits[do_div(num,base)] | locase); |
4277eedd DV |
421 | } while (num != 0); |
422 | */ | |
fef20d9c FW |
423 | else if (spec.base != 10) { /* 8 or 16 */ |
424 | int mask = spec.base - 1; | |
b39a7340 | 425 | int shift = 3; |
7b9186f5 AGR |
426 | |
427 | if (spec.base == 16) | |
428 | shift = 4; | |
b39a7340 | 429 | do { |
9b706aee | 430 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
b39a7340 DV |
431 | num >>= shift; |
432 | } while (num); | |
4277eedd DV |
433 | } else { /* base 10 */ |
434 | i = put_dec(tmp, num) - tmp; | |
435 | } | |
b39a7340 DV |
436 | |
437 | /* printing 100 using %2d gives "100", not "00" */ | |
fef20d9c FW |
438 | if (i > spec.precision) |
439 | spec.precision = i; | |
b39a7340 | 440 | /* leading space padding */ |
fef20d9c FW |
441 | spec.field_width -= spec.precision; |
442 | if (!(spec.flags & (ZEROPAD+LEFT))) { | |
7b9186f5 | 443 | while (--spec.field_width >= 0) { |
f796937a | 444 | if (buf < end) |
1da177e4 LT |
445 | *buf = ' '; |
446 | ++buf; | |
447 | } | |
448 | } | |
b39a7340 | 449 | /* sign */ |
1da177e4 | 450 | if (sign) { |
f796937a | 451 | if (buf < end) |
1da177e4 LT |
452 | *buf = sign; |
453 | ++buf; | |
454 | } | |
b39a7340 DV |
455 | /* "0x" / "0" prefix */ |
456 | if (need_pfx) { | |
7c203422 PC |
457 | if (spec.base == 16 || !is_zero) { |
458 | if (buf < end) | |
459 | *buf = '0'; | |
460 | ++buf; | |
461 | } | |
fef20d9c | 462 | if (spec.base == 16) { |
f796937a | 463 | if (buf < end) |
9b706aee | 464 | *buf = ('X' | locase); |
1da177e4 LT |
465 | ++buf; |
466 | } | |
467 | } | |
b39a7340 | 468 | /* zero or space padding */ |
fef20d9c FW |
469 | if (!(spec.flags & LEFT)) { |
470 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; | |
471 | while (--spec.field_width >= 0) { | |
f796937a | 472 | if (buf < end) |
1da177e4 LT |
473 | *buf = c; |
474 | ++buf; | |
475 | } | |
476 | } | |
b39a7340 | 477 | /* hmm even more zero padding? */ |
fef20d9c | 478 | while (i <= --spec.precision) { |
f796937a | 479 | if (buf < end) |
1da177e4 LT |
480 | *buf = '0'; |
481 | ++buf; | |
482 | } | |
b39a7340 DV |
483 | /* actual digits of result */ |
484 | while (--i >= 0) { | |
f796937a | 485 | if (buf < end) |
1da177e4 LT |
486 | *buf = tmp[i]; |
487 | ++buf; | |
488 | } | |
b39a7340 | 489 | /* trailing space padding */ |
fef20d9c | 490 | while (--spec.field_width >= 0) { |
f796937a | 491 | if (buf < end) |
1da177e4 LT |
492 | *buf = ' '; |
493 | ++buf; | |
494 | } | |
7b9186f5 | 495 | |
1da177e4 LT |
496 | return buf; |
497 | } | |
498 | ||
cf3b429b JP |
499 | static noinline_for_stack |
500 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) | |
0f9bfa56 LT |
501 | { |
502 | int len, i; | |
503 | ||
504 | if ((unsigned long)s < PAGE_SIZE) | |
0f4f81dc | 505 | s = "(null)"; |
0f9bfa56 | 506 | |
fef20d9c | 507 | len = strnlen(s, spec.precision); |
0f9bfa56 | 508 | |
fef20d9c FW |
509 | if (!(spec.flags & LEFT)) { |
510 | while (len < spec.field_width--) { | |
0f9bfa56 LT |
511 | if (buf < end) |
512 | *buf = ' '; | |
513 | ++buf; | |
514 | } | |
515 | } | |
516 | for (i = 0; i < len; ++i) { | |
517 | if (buf < end) | |
518 | *buf = *s; | |
519 | ++buf; ++s; | |
520 | } | |
fef20d9c | 521 | while (len < spec.field_width--) { |
0f9bfa56 LT |
522 | if (buf < end) |
523 | *buf = ' '; | |
524 | ++buf; | |
525 | } | |
7b9186f5 | 526 | |
0f9bfa56 LT |
527 | return buf; |
528 | } | |
529 | ||
cf3b429b JP |
530 | static noinline_for_stack |
531 | char *symbol_string(char *buf, char *end, void *ptr, | |
532 | struct printf_spec spec, char ext) | |
0fe1ef24 LT |
533 | { |
534 | unsigned long value = (unsigned long) ptr; | |
535 | #ifdef CONFIG_KALLSYMS | |
536 | char sym[KSYM_SYMBOL_LEN]; | |
0f77a8d3 NK |
537 | if (ext == 'B') |
538 | sprint_backtrace(sym, value); | |
539 | else if (ext != 'f' && ext != 's') | |
0c8b946e FW |
540 | sprint_symbol(sym, value); |
541 | else | |
4796dd20 | 542 | sprint_symbol_no_offset(sym, value); |
7b9186f5 | 543 | |
fef20d9c | 544 | return string(buf, end, sym, spec); |
0fe1ef24 | 545 | #else |
7b9186f5 | 546 | spec.field_width = 2 * sizeof(void *); |
fef20d9c FW |
547 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
548 | spec.base = 16; | |
7b9186f5 | 549 | |
fef20d9c | 550 | return number(buf, end, value, spec); |
0fe1ef24 LT |
551 | #endif |
552 | } | |
553 | ||
cf3b429b JP |
554 | static noinline_for_stack |
555 | char *resource_string(char *buf, char *end, struct resource *res, | |
556 | struct printf_spec spec, const char *fmt) | |
332d2e78 LT |
557 | { |
558 | #ifndef IO_RSRC_PRINTK_SIZE | |
28405372 | 559 | #define IO_RSRC_PRINTK_SIZE 6 |
332d2e78 LT |
560 | #endif |
561 | ||
562 | #ifndef MEM_RSRC_PRINTK_SIZE | |
28405372 | 563 | #define MEM_RSRC_PRINTK_SIZE 10 |
332d2e78 | 564 | #endif |
4da0b66c | 565 | static const struct printf_spec io_spec = { |
fef20d9c | 566 | .base = 16, |
4da0b66c | 567 | .field_width = IO_RSRC_PRINTK_SIZE, |
fef20d9c FW |
568 | .precision = -1, |
569 | .flags = SPECIAL | SMALL | ZEROPAD, | |
570 | }; | |
4da0b66c BH |
571 | static const struct printf_spec mem_spec = { |
572 | .base = 16, | |
573 | .field_width = MEM_RSRC_PRINTK_SIZE, | |
574 | .precision = -1, | |
575 | .flags = SPECIAL | SMALL | ZEROPAD, | |
576 | }; | |
0f4050c7 BH |
577 | static const struct printf_spec bus_spec = { |
578 | .base = 16, | |
579 | .field_width = 2, | |
580 | .precision = -1, | |
581 | .flags = SMALL | ZEROPAD, | |
582 | }; | |
4da0b66c | 583 | static const struct printf_spec dec_spec = { |
c91d3376 BH |
584 | .base = 10, |
585 | .precision = -1, | |
586 | .flags = 0, | |
587 | }; | |
4da0b66c | 588 | static const struct printf_spec str_spec = { |
fd95541e BH |
589 | .field_width = -1, |
590 | .precision = 10, | |
591 | .flags = LEFT, | |
592 | }; | |
4da0b66c | 593 | static const struct printf_spec flag_spec = { |
fd95541e BH |
594 | .base = 16, |
595 | .precision = -1, | |
596 | .flags = SPECIAL | SMALL, | |
597 | }; | |
c7dabef8 BH |
598 | |
599 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) | |
600 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ | |
601 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) | |
602 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) | |
9d7cca04 | 603 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
c7dabef8 BH |
604 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
605 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, | |
606 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; | |
607 | ||
332d2e78 | 608 | char *p = sym, *pend = sym + sizeof(sym); |
c7dabef8 | 609 | int decode = (fmt[0] == 'R') ? 1 : 0; |
4da0b66c | 610 | const struct printf_spec *specp; |
332d2e78 LT |
611 | |
612 | *p++ = '['; | |
4da0b66c | 613 | if (res->flags & IORESOURCE_IO) { |
c7dabef8 | 614 | p = string(p, pend, "io ", str_spec); |
4da0b66c BH |
615 | specp = &io_spec; |
616 | } else if (res->flags & IORESOURCE_MEM) { | |
c7dabef8 | 617 | p = string(p, pend, "mem ", str_spec); |
4da0b66c BH |
618 | specp = &mem_spec; |
619 | } else if (res->flags & IORESOURCE_IRQ) { | |
c7dabef8 | 620 | p = string(p, pend, "irq ", str_spec); |
4da0b66c BH |
621 | specp = &dec_spec; |
622 | } else if (res->flags & IORESOURCE_DMA) { | |
c7dabef8 | 623 | p = string(p, pend, "dma ", str_spec); |
4da0b66c | 624 | specp = &dec_spec; |
0f4050c7 BH |
625 | } else if (res->flags & IORESOURCE_BUS) { |
626 | p = string(p, pend, "bus ", str_spec); | |
627 | specp = &bus_spec; | |
4da0b66c | 628 | } else { |
c7dabef8 | 629 | p = string(p, pend, "??? ", str_spec); |
4da0b66c | 630 | specp = &mem_spec; |
c7dabef8 | 631 | decode = 0; |
fd95541e | 632 | } |
4da0b66c | 633 | p = number(p, pend, res->start, *specp); |
c91d3376 BH |
634 | if (res->start != res->end) { |
635 | *p++ = '-'; | |
4da0b66c | 636 | p = number(p, pend, res->end, *specp); |
c91d3376 | 637 | } |
c7dabef8 | 638 | if (decode) { |
fd95541e BH |
639 | if (res->flags & IORESOURCE_MEM_64) |
640 | p = string(p, pend, " 64bit", str_spec); | |
641 | if (res->flags & IORESOURCE_PREFETCH) | |
642 | p = string(p, pend, " pref", str_spec); | |
9d7cca04 BH |
643 | if (res->flags & IORESOURCE_WINDOW) |
644 | p = string(p, pend, " window", str_spec); | |
fd95541e BH |
645 | if (res->flags & IORESOURCE_DISABLED) |
646 | p = string(p, pend, " disabled", str_spec); | |
c7dabef8 BH |
647 | } else { |
648 | p = string(p, pend, " flags ", str_spec); | |
649 | p = number(p, pend, res->flags, flag_spec); | |
fd95541e | 650 | } |
332d2e78 | 651 | *p++ = ']'; |
c7dabef8 | 652 | *p = '\0'; |
332d2e78 | 653 | |
fef20d9c | 654 | return string(buf, end, sym, spec); |
332d2e78 LT |
655 | } |
656 | ||
cf3b429b JP |
657 | static noinline_for_stack |
658 | char *mac_address_string(char *buf, char *end, u8 *addr, | |
659 | struct printf_spec spec, const char *fmt) | |
dd45c9cf | 660 | { |
8a27f7c9 | 661 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
dd45c9cf HH |
662 | char *p = mac_addr; |
663 | int i; | |
bc7259a2 | 664 | char separator; |
76597ff9 | 665 | bool reversed = false; |
bc7259a2 | 666 | |
76597ff9 AE |
667 | switch (fmt[1]) { |
668 | case 'F': | |
bc7259a2 | 669 | separator = '-'; |
76597ff9 AE |
670 | break; |
671 | ||
672 | case 'R': | |
673 | reversed = true; | |
674 | /* fall through */ | |
675 | ||
676 | default: | |
bc7259a2 | 677 | separator = ':'; |
76597ff9 | 678 | break; |
bc7259a2 | 679 | } |
dd45c9cf HH |
680 | |
681 | for (i = 0; i < 6; i++) { | |
76597ff9 AE |
682 | if (reversed) |
683 | p = hex_byte_pack(p, addr[5 - i]); | |
684 | else | |
685 | p = hex_byte_pack(p, addr[i]); | |
686 | ||
8a27f7c9 | 687 | if (fmt[0] == 'M' && i != 5) |
bc7259a2 | 688 | *p++ = separator; |
dd45c9cf HH |
689 | } |
690 | *p = '\0'; | |
691 | ||
fef20d9c | 692 | return string(buf, end, mac_addr, spec); |
dd45c9cf HH |
693 | } |
694 | ||
cf3b429b JP |
695 | static noinline_for_stack |
696 | char *ip4_string(char *p, const u8 *addr, const char *fmt) | |
8a27f7c9 JP |
697 | { |
698 | int i; | |
0159f24e JP |
699 | bool leading_zeros = (fmt[0] == 'i'); |
700 | int index; | |
701 | int step; | |
702 | ||
703 | switch (fmt[2]) { | |
704 | case 'h': | |
705 | #ifdef __BIG_ENDIAN | |
706 | index = 0; | |
707 | step = 1; | |
708 | #else | |
709 | index = 3; | |
710 | step = -1; | |
711 | #endif | |
712 | break; | |
713 | case 'l': | |
714 | index = 3; | |
715 | step = -1; | |
716 | break; | |
717 | case 'n': | |
718 | case 'b': | |
719 | default: | |
720 | index = 0; | |
721 | step = 1; | |
722 | break; | |
723 | } | |
8a27f7c9 JP |
724 | for (i = 0; i < 4; i++) { |
725 | char temp[3]; /* hold each IP quad in reverse order */ | |
133fd9f5 | 726 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
8a27f7c9 JP |
727 | if (leading_zeros) { |
728 | if (digits < 3) | |
729 | *p++ = '0'; | |
730 | if (digits < 2) | |
731 | *p++ = '0'; | |
732 | } | |
733 | /* reverse the digits in the quad */ | |
734 | while (digits--) | |
735 | *p++ = temp[digits]; | |
736 | if (i < 3) | |
737 | *p++ = '.'; | |
0159f24e | 738 | index += step; |
8a27f7c9 | 739 | } |
8a27f7c9 | 740 | *p = '\0'; |
7b9186f5 | 741 | |
8a27f7c9 JP |
742 | return p; |
743 | } | |
744 | ||
cf3b429b JP |
745 | static noinline_for_stack |
746 | char *ip6_compressed_string(char *p, const char *addr) | |
689afa7d | 747 | { |
7b9186f5 | 748 | int i, j, range; |
8a27f7c9 JP |
749 | unsigned char zerolength[8]; |
750 | int longest = 1; | |
751 | int colonpos = -1; | |
752 | u16 word; | |
7b9186f5 | 753 | u8 hi, lo; |
8a27f7c9 | 754 | bool needcolon = false; |
eb78cd26 JP |
755 | bool useIPv4; |
756 | struct in6_addr in6; | |
757 | ||
758 | memcpy(&in6, addr, sizeof(struct in6_addr)); | |
759 | ||
760 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); | |
8a27f7c9 JP |
761 | |
762 | memset(zerolength, 0, sizeof(zerolength)); | |
763 | ||
764 | if (useIPv4) | |
765 | range = 6; | |
766 | else | |
767 | range = 8; | |
768 | ||
769 | /* find position of longest 0 run */ | |
770 | for (i = 0; i < range; i++) { | |
771 | for (j = i; j < range; j++) { | |
eb78cd26 | 772 | if (in6.s6_addr16[j] != 0) |
8a27f7c9 JP |
773 | break; |
774 | zerolength[i]++; | |
775 | } | |
776 | } | |
777 | for (i = 0; i < range; i++) { | |
778 | if (zerolength[i] > longest) { | |
779 | longest = zerolength[i]; | |
780 | colonpos = i; | |
781 | } | |
782 | } | |
29cf519e JP |
783 | if (longest == 1) /* don't compress a single 0 */ |
784 | colonpos = -1; | |
689afa7d | 785 | |
8a27f7c9 JP |
786 | /* emit address */ |
787 | for (i = 0; i < range; i++) { | |
788 | if (i == colonpos) { | |
789 | if (needcolon || i == 0) | |
790 | *p++ = ':'; | |
791 | *p++ = ':'; | |
792 | needcolon = false; | |
793 | i += longest - 1; | |
794 | continue; | |
795 | } | |
796 | if (needcolon) { | |
797 | *p++ = ':'; | |
798 | needcolon = false; | |
799 | } | |
800 | /* hex u16 without leading 0s */ | |
eb78cd26 | 801 | word = ntohs(in6.s6_addr16[i]); |
8a27f7c9 JP |
802 | hi = word >> 8; |
803 | lo = word & 0xff; | |
804 | if (hi) { | |
805 | if (hi > 0x0f) | |
55036ba7 | 806 | p = hex_byte_pack(p, hi); |
8a27f7c9 JP |
807 | else |
808 | *p++ = hex_asc_lo(hi); | |
55036ba7 | 809 | p = hex_byte_pack(p, lo); |
8a27f7c9 | 810 | } |
b5ff992b | 811 | else if (lo > 0x0f) |
55036ba7 | 812 | p = hex_byte_pack(p, lo); |
8a27f7c9 JP |
813 | else |
814 | *p++ = hex_asc_lo(lo); | |
815 | needcolon = true; | |
816 | } | |
817 | ||
818 | if (useIPv4) { | |
819 | if (needcolon) | |
820 | *p++ = ':'; | |
0159f24e | 821 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
8a27f7c9 | 822 | } |
8a27f7c9 | 823 | *p = '\0'; |
7b9186f5 | 824 | |
8a27f7c9 JP |
825 | return p; |
826 | } | |
827 | ||
cf3b429b JP |
828 | static noinline_for_stack |
829 | char *ip6_string(char *p, const char *addr, const char *fmt) | |
8a27f7c9 JP |
830 | { |
831 | int i; | |
7b9186f5 | 832 | |
689afa7d | 833 | for (i = 0; i < 8; i++) { |
55036ba7 AS |
834 | p = hex_byte_pack(p, *addr++); |
835 | p = hex_byte_pack(p, *addr++); | |
8a27f7c9 | 836 | if (fmt[0] == 'I' && i != 7) |
689afa7d HH |
837 | *p++ = ':'; |
838 | } | |
839 | *p = '\0'; | |
7b9186f5 | 840 | |
8a27f7c9 JP |
841 | return p; |
842 | } | |
843 | ||
cf3b429b JP |
844 | static noinline_for_stack |
845 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, | |
846 | struct printf_spec spec, const char *fmt) | |
8a27f7c9 JP |
847 | { |
848 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; | |
849 | ||
850 | if (fmt[0] == 'I' && fmt[2] == 'c') | |
eb78cd26 | 851 | ip6_compressed_string(ip6_addr, addr); |
8a27f7c9 | 852 | else |
eb78cd26 | 853 | ip6_string(ip6_addr, addr, fmt); |
689afa7d | 854 | |
fef20d9c | 855 | return string(buf, end, ip6_addr, spec); |
689afa7d HH |
856 | } |
857 | ||
cf3b429b JP |
858 | static noinline_for_stack |
859 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, | |
860 | struct printf_spec spec, const char *fmt) | |
4aa99606 | 861 | { |
8a27f7c9 | 862 | char ip4_addr[sizeof("255.255.255.255")]; |
4aa99606 | 863 | |
0159f24e | 864 | ip4_string(ip4_addr, addr, fmt); |
4aa99606 | 865 | |
fef20d9c | 866 | return string(buf, end, ip4_addr, spec); |
4aa99606 HH |
867 | } |
868 | ||
cf3b429b JP |
869 | static noinline_for_stack |
870 | char *uuid_string(char *buf, char *end, const u8 *addr, | |
871 | struct printf_spec spec, const char *fmt) | |
9ac6e44e JP |
872 | { |
873 | char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; | |
874 | char *p = uuid; | |
875 | int i; | |
876 | static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | |
877 | static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; | |
878 | const u8 *index = be; | |
879 | bool uc = false; | |
880 | ||
881 | switch (*(++fmt)) { | |
882 | case 'L': | |
883 | uc = true; /* fall-through */ | |
884 | case 'l': | |
885 | index = le; | |
886 | break; | |
887 | case 'B': | |
888 | uc = true; | |
889 | break; | |
890 | } | |
891 | ||
892 | for (i = 0; i < 16; i++) { | |
55036ba7 | 893 | p = hex_byte_pack(p, addr[index[i]]); |
9ac6e44e JP |
894 | switch (i) { |
895 | case 3: | |
896 | case 5: | |
897 | case 7: | |
898 | case 9: | |
899 | *p++ = '-'; | |
900 | break; | |
901 | } | |
902 | } | |
903 | ||
904 | *p = 0; | |
905 | ||
906 | if (uc) { | |
907 | p = uuid; | |
908 | do { | |
909 | *p = toupper(*p); | |
910 | } while (*(++p)); | |
911 | } | |
912 | ||
913 | return string(buf, end, uuid, spec); | |
914 | } | |
915 | ||
c8f44aff MM |
916 | static |
917 | char *netdev_feature_string(char *buf, char *end, const u8 *addr, | |
918 | struct printf_spec spec) | |
919 | { | |
920 | spec.flags |= SPECIAL | SMALL | ZEROPAD; | |
921 | if (spec.field_width == -1) | |
922 | spec.field_width = 2 + 2 * sizeof(netdev_features_t); | |
923 | spec.base = 16; | |
924 | ||
925 | return number(buf, end, *(const netdev_features_t *)addr, spec); | |
926 | } | |
927 | ||
411f05f1 | 928 | int kptr_restrict __read_mostly; |
455cd5ab | 929 | |
4d8a743c LT |
930 | /* |
931 | * Show a '%p' thing. A kernel extension is that the '%p' is followed | |
932 | * by an extra set of alphanumeric characters that are extended format | |
933 | * specifiers. | |
934 | * | |
332d2e78 LT |
935 | * Right now we handle: |
936 | * | |
0c8b946e FW |
937 | * - 'F' For symbolic function descriptor pointers with offset |
938 | * - 'f' For simple symbolic function names without offset | |
0efb4d20 SR |
939 | * - 'S' For symbolic direct pointers with offset |
940 | * - 's' For symbolic direct pointers without offset | |
0f77a8d3 | 941 | * - 'B' For backtraced symbolic direct pointers with offset |
c7dabef8 BH |
942 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
943 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] | |
dd45c9cf HH |
944 | * - 'M' For a 6-byte MAC address, it prints the address in the |
945 | * usual colon-separated hex notation | |
8a27f7c9 | 946 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
bc7259a2 | 947 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
c8e00060 | 948 | * with a dash-separated hex notation |
76597ff9 | 949 | * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth) |
8a27f7c9 JP |
950 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
951 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) | |
952 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's | |
953 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses | |
954 | * IPv6 omits the colons (01020304...0f) | |
955 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) | |
0159f24e | 956 | * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order |
8a27f7c9 | 957 | * - 'I6c' for IPv6 addresses printed as specified by |
29cf519e | 958 | * http://tools.ietf.org/html/rfc5952 |
9ac6e44e JP |
959 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
960 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
961 | * Options for %pU are: | |
962 | * b big endian lower case hex (default) | |
963 | * B big endian UPPER case hex | |
964 | * l little endian lower case hex | |
965 | * L little endian UPPER case hex | |
966 | * big endian output byte order is: | |
967 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] | |
968 | * little endian output byte order is: | |
969 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] | |
7db6f5fb JP |
970 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
971 | * call vsnprintf(->format, *->va_list). | |
972 | * Implements a "recursive vsnprintf". | |
973 | * Do not use this feature without some mechanism to verify the | |
974 | * correctness of the format string and va_list arguments. | |
455cd5ab | 975 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
c8f44aff | 976 | * - 'NF' For a netdev_features_t |
9ac6e44e | 977 | * |
332d2e78 LT |
978 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
979 | * function pointers are really function descriptors, which contain a | |
980 | * pointer to the real address. | |
4d8a743c | 981 | */ |
cf3b429b JP |
982 | static noinline_for_stack |
983 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |
984 | struct printf_spec spec) | |
78a8bf69 | 985 | { |
725fe002 GL |
986 | int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); |
987 | ||
9f36e2c4 | 988 | if (!ptr && *fmt != 'K') { |
5e057981 JP |
989 | /* |
990 | * Print (null) with the same width as a pointer so it makes | |
991 | * tabular output look nice. | |
992 | */ | |
993 | if (spec.field_width == -1) | |
725fe002 | 994 | spec.field_width = default_width; |
fef20d9c | 995 | return string(buf, end, "(null)", spec); |
5e057981 | 996 | } |
d97106ab | 997 | |
0fe1ef24 LT |
998 | switch (*fmt) { |
999 | case 'F': | |
0c8b946e | 1000 | case 'f': |
0fe1ef24 LT |
1001 | ptr = dereference_function_descriptor(ptr); |
1002 | /* Fallthrough */ | |
1003 | case 'S': | |
9ac6e44e | 1004 | case 's': |
0f77a8d3 | 1005 | case 'B': |
0c8b946e | 1006 | return symbol_string(buf, end, ptr, spec, *fmt); |
332d2e78 | 1007 | case 'R': |
c7dabef8 | 1008 | case 'r': |
fd95541e | 1009 | return resource_string(buf, end, ptr, spec, fmt); |
8a27f7c9 JP |
1010 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
1011 | case 'm': /* Contiguous: 000102030405 */ | |
76597ff9 AE |
1012 | /* [mM]F (FDDI) */ |
1013 | /* [mM]R (Reverse order; Bluetooth) */ | |
8a27f7c9 JP |
1014 | return mac_address_string(buf, end, ptr, spec, fmt); |
1015 | case 'I': /* Formatted IP supported | |
1016 | * 4: 1.2.3.4 | |
1017 | * 6: 0001:0203:...:0708 | |
1018 | * 6c: 1::708 or 1::1.2.3.4 | |
1019 | */ | |
1020 | case 'i': /* Contiguous: | |
1021 | * 4: 001.002.003.004 | |
1022 | * 6: 000102...0f | |
1023 | */ | |
1024 | switch (fmt[1]) { | |
1025 | case '6': | |
1026 | return ip6_addr_string(buf, end, ptr, spec, fmt); | |
1027 | case '4': | |
1028 | return ip4_addr_string(buf, end, ptr, spec, fmt); | |
1029 | } | |
fef20d9c | 1030 | break; |
9ac6e44e JP |
1031 | case 'U': |
1032 | return uuid_string(buf, end, ptr, spec, fmt); | |
7db6f5fb | 1033 | case 'V': |
5756b76e JB |
1034 | { |
1035 | va_list va; | |
1036 | ||
1037 | va_copy(va, *((struct va_format *)ptr)->va); | |
1038 | buf += vsnprintf(buf, end > buf ? end - buf : 0, | |
1039 | ((struct va_format *)ptr)->fmt, va); | |
1040 | va_end(va); | |
1041 | return buf; | |
1042 | } | |
455cd5ab DR |
1043 | case 'K': |
1044 | /* | |
1045 | * %pK cannot be used in IRQ context because its test | |
1046 | * for CAP_SYSLOG would be meaningless. | |
1047 | */ | |
3715c530 DR |
1048 | if (kptr_restrict && (in_irq() || in_serving_softirq() || |
1049 | in_nmi())) { | |
455cd5ab | 1050 | if (spec.field_width == -1) |
725fe002 | 1051 | spec.field_width = default_width; |
455cd5ab | 1052 | return string(buf, end, "pK-error", spec); |
455cd5ab | 1053 | } |
26297607 JP |
1054 | if (!((kptr_restrict == 0) || |
1055 | (kptr_restrict == 1 && | |
1056 | has_capability_noaudit(current, CAP_SYSLOG)))) | |
1057 | ptr = NULL; | |
1058 | break; | |
c8f44aff MM |
1059 | case 'N': |
1060 | switch (fmt[1]) { | |
1061 | case 'F': | |
1062 | return netdev_feature_string(buf, end, ptr, spec); | |
1063 | } | |
1064 | break; | |
fef20d9c FW |
1065 | } |
1066 | spec.flags |= SMALL; | |
1067 | if (spec.field_width == -1) { | |
725fe002 | 1068 | spec.field_width = default_width; |
fef20d9c FW |
1069 | spec.flags |= ZEROPAD; |
1070 | } | |
1071 | spec.base = 16; | |
1072 | ||
1073 | return number(buf, end, (unsigned long) ptr, spec); | |
1074 | } | |
1075 | ||
1076 | /* | |
1077 | * Helper function to decode printf style format. | |
1078 | * Each call decode a token from the format and return the | |
1079 | * number of characters read (or likely the delta where it wants | |
1080 | * to go on the next call). | |
1081 | * The decoded token is returned through the parameters | |
1082 | * | |
1083 | * 'h', 'l', or 'L' for integer fields | |
1084 | * 'z' support added 23/7/1999 S.H. | |
1085 | * 'z' changed to 'Z' --davidm 1/25/99 | |
1086 | * 't' added for ptrdiff_t | |
1087 | * | |
1088 | * @fmt: the format string | |
1089 | * @type of the token returned | |
1090 | * @flags: various flags such as +, -, # tokens.. | |
1091 | * @field_width: overwritten width | |
1092 | * @base: base of the number (octal, hex, ...) | |
1093 | * @precision: precision of a number | |
1094 | * @qualifier: qualifier of a number (long, size_t, ...) | |
1095 | */ | |
cf3b429b JP |
1096 | static noinline_for_stack |
1097 | int format_decode(const char *fmt, struct printf_spec *spec) | |
fef20d9c FW |
1098 | { |
1099 | const char *start = fmt; | |
fef20d9c FW |
1100 | |
1101 | /* we finished early by reading the field width */ | |
ed681a91 | 1102 | if (spec->type == FORMAT_TYPE_WIDTH) { |
fef20d9c FW |
1103 | if (spec->field_width < 0) { |
1104 | spec->field_width = -spec->field_width; | |
1105 | spec->flags |= LEFT; | |
1106 | } | |
1107 | spec->type = FORMAT_TYPE_NONE; | |
1108 | goto precision; | |
1109 | } | |
1110 | ||
1111 | /* we finished early by reading the precision */ | |
1112 | if (spec->type == FORMAT_TYPE_PRECISION) { | |
1113 | if (spec->precision < 0) | |
1114 | spec->precision = 0; | |
1115 | ||
1116 | spec->type = FORMAT_TYPE_NONE; | |
1117 | goto qualifier; | |
1118 | } | |
1119 | ||
1120 | /* By default */ | |
1121 | spec->type = FORMAT_TYPE_NONE; | |
1122 | ||
1123 | for (; *fmt ; ++fmt) { | |
1124 | if (*fmt == '%') | |
1125 | break; | |
1126 | } | |
1127 | ||
1128 | /* Return the current non-format string */ | |
1129 | if (fmt != start || !*fmt) | |
1130 | return fmt - start; | |
1131 | ||
1132 | /* Process flags */ | |
1133 | spec->flags = 0; | |
1134 | ||
1135 | while (1) { /* this also skips first '%' */ | |
1136 | bool found = true; | |
1137 | ||
1138 | ++fmt; | |
1139 | ||
1140 | switch (*fmt) { | |
1141 | case '-': spec->flags |= LEFT; break; | |
1142 | case '+': spec->flags |= PLUS; break; | |
1143 | case ' ': spec->flags |= SPACE; break; | |
1144 | case '#': spec->flags |= SPECIAL; break; | |
1145 | case '0': spec->flags |= ZEROPAD; break; | |
1146 | default: found = false; | |
1147 | } | |
1148 | ||
1149 | if (!found) | |
1150 | break; | |
1151 | } | |
1152 | ||
1153 | /* get field width */ | |
1154 | spec->field_width = -1; | |
1155 | ||
1156 | if (isdigit(*fmt)) | |
1157 | spec->field_width = skip_atoi(&fmt); | |
1158 | else if (*fmt == '*') { | |
1159 | /* it's the next argument */ | |
ed681a91 | 1160 | spec->type = FORMAT_TYPE_WIDTH; |
fef20d9c FW |
1161 | return ++fmt - start; |
1162 | } | |
1163 | ||
1164 | precision: | |
1165 | /* get the precision */ | |
1166 | spec->precision = -1; | |
1167 | if (*fmt == '.') { | |
1168 | ++fmt; | |
1169 | if (isdigit(*fmt)) { | |
1170 | spec->precision = skip_atoi(&fmt); | |
1171 | if (spec->precision < 0) | |
1172 | spec->precision = 0; | |
1173 | } else if (*fmt == '*') { | |
1174 | /* it's the next argument */ | |
adf26f84 | 1175 | spec->type = FORMAT_TYPE_PRECISION; |
fef20d9c FW |
1176 | return ++fmt - start; |
1177 | } | |
1178 | } | |
1179 | ||
1180 | qualifier: | |
1181 | /* get the conversion qualifier */ | |
1182 | spec->qualifier = -1; | |
75fb8f26 AS |
1183 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
1184 | _tolower(*fmt) == 'z' || *fmt == 't') { | |
a4e94ef0 Z |
1185 | spec->qualifier = *fmt++; |
1186 | if (unlikely(spec->qualifier == *fmt)) { | |
1187 | if (spec->qualifier == 'l') { | |
1188 | spec->qualifier = 'L'; | |
1189 | ++fmt; | |
1190 | } else if (spec->qualifier == 'h') { | |
1191 | spec->qualifier = 'H'; | |
1192 | ++fmt; | |
1193 | } | |
fef20d9c FW |
1194 | } |
1195 | } | |
1196 | ||
1197 | /* default base */ | |
1198 | spec->base = 10; | |
1199 | switch (*fmt) { | |
1200 | case 'c': | |
1201 | spec->type = FORMAT_TYPE_CHAR; | |
1202 | return ++fmt - start; | |
1203 | ||
1204 | case 's': | |
1205 | spec->type = FORMAT_TYPE_STR; | |
1206 | return ++fmt - start; | |
1207 | ||
1208 | case 'p': | |
1209 | spec->type = FORMAT_TYPE_PTR; | |
1210 | return fmt - start; | |
1211 | /* skip alnum */ | |
1212 | ||
1213 | case 'n': | |
1214 | spec->type = FORMAT_TYPE_NRCHARS; | |
1215 | return ++fmt - start; | |
1216 | ||
1217 | case '%': | |
1218 | spec->type = FORMAT_TYPE_PERCENT_CHAR; | |
1219 | return ++fmt - start; | |
1220 | ||
1221 | /* integer number formats - set up the flags and "break" */ | |
1222 | case 'o': | |
1223 | spec->base = 8; | |
1224 | break; | |
1225 | ||
1226 | case 'x': | |
1227 | spec->flags |= SMALL; | |
1228 | ||
1229 | case 'X': | |
1230 | spec->base = 16; | |
1231 | break; | |
1232 | ||
1233 | case 'd': | |
1234 | case 'i': | |
39e874f8 | 1235 | spec->flags |= SIGN; |
fef20d9c | 1236 | case 'u': |
4aa99606 | 1237 | break; |
fef20d9c FW |
1238 | |
1239 | default: | |
1240 | spec->type = FORMAT_TYPE_INVALID; | |
1241 | return fmt - start; | |
0fe1ef24 | 1242 | } |
fef20d9c FW |
1243 | |
1244 | if (spec->qualifier == 'L') | |
1245 | spec->type = FORMAT_TYPE_LONG_LONG; | |
1246 | else if (spec->qualifier == 'l') { | |
39e874f8 | 1247 | if (spec->flags & SIGN) |
fef20d9c FW |
1248 | spec->type = FORMAT_TYPE_LONG; |
1249 | else | |
1250 | spec->type = FORMAT_TYPE_ULONG; | |
75fb8f26 | 1251 | } else if (_tolower(spec->qualifier) == 'z') { |
fef20d9c FW |
1252 | spec->type = FORMAT_TYPE_SIZE_T; |
1253 | } else if (spec->qualifier == 't') { | |
1254 | spec->type = FORMAT_TYPE_PTRDIFF; | |
a4e94ef0 Z |
1255 | } else if (spec->qualifier == 'H') { |
1256 | if (spec->flags & SIGN) | |
1257 | spec->type = FORMAT_TYPE_BYTE; | |
1258 | else | |
1259 | spec->type = FORMAT_TYPE_UBYTE; | |
fef20d9c | 1260 | } else if (spec->qualifier == 'h') { |
39e874f8 | 1261 | if (spec->flags & SIGN) |
fef20d9c FW |
1262 | spec->type = FORMAT_TYPE_SHORT; |
1263 | else | |
1264 | spec->type = FORMAT_TYPE_USHORT; | |
1265 | } else { | |
39e874f8 | 1266 | if (spec->flags & SIGN) |
fef20d9c FW |
1267 | spec->type = FORMAT_TYPE_INT; |
1268 | else | |
1269 | spec->type = FORMAT_TYPE_UINT; | |
78a8bf69 | 1270 | } |
fef20d9c FW |
1271 | |
1272 | return ++fmt - start; | |
78a8bf69 LT |
1273 | } |
1274 | ||
1da177e4 LT |
1275 | /** |
1276 | * vsnprintf - Format a string and place it in a buffer | |
1277 | * @buf: The buffer to place the result into | |
1278 | * @size: The size of the buffer, including the trailing null space | |
1279 | * @fmt: The format string to use | |
1280 | * @args: Arguments for the format string | |
1281 | * | |
20036fdc | 1282 | * This function follows C99 vsnprintf, but has some extensions: |
91adcd2c SR |
1283 | * %pS output the name of a text symbol with offset |
1284 | * %ps output the name of a text symbol without offset | |
0c8b946e FW |
1285 | * %pF output the name of a function pointer with its offset |
1286 | * %pf output the name of a function pointer without its offset | |
0f77a8d3 | 1287 | * %pB output the name of a backtrace symbol with its offset |
8a79503a UKK |
1288 | * %pR output the address range in a struct resource with decoded flags |
1289 | * %pr output the address range in a struct resource with raw flags | |
1290 | * %pM output a 6-byte MAC address with colons | |
1291 | * %pm output a 6-byte MAC address without colons | |
1292 | * %pI4 print an IPv4 address without leading zeros | |
1293 | * %pi4 print an IPv4 address with leading zeros | |
1294 | * %pI6 print an IPv6 address with colons | |
1295 | * %pi6 print an IPv6 address without colons | |
f996f208 | 1296 | * %pI6c print an IPv6 address as specified by RFC 5952 |
8a79503a UKK |
1297 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
1298 | * case. | |
0efb4d20 | 1299 | * %n is ignored |
20036fdc | 1300 | * |
80f548e0 AM |
1301 | * ** Please update Documentation/printk-formats.txt when making changes ** |
1302 | * | |
1da177e4 LT |
1303 | * The return value is the number of characters which would |
1304 | * be generated for the given input, excluding the trailing | |
1305 | * '\0', as per ISO C99. If you want to have the exact | |
1306 | * number of characters written into @buf as return value | |
72fd4a35 | 1307 | * (not including the trailing '\0'), use vscnprintf(). If the |
1da177e4 LT |
1308 | * return is greater than or equal to @size, the resulting |
1309 | * string is truncated. | |
1310 | * | |
ba1835eb | 1311 | * If you're not already dealing with a va_list consider using snprintf(). |
1da177e4 LT |
1312 | */ |
1313 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |
1314 | { | |
1da177e4 | 1315 | unsigned long long num; |
d4be151b | 1316 | char *str, *end; |
fef20d9c | 1317 | struct printf_spec spec = {0}; |
1da177e4 | 1318 | |
f796937a JF |
1319 | /* Reject out-of-range values early. Large positive sizes are |
1320 | used for unknown buffer sizes. */ | |
2f30b1f9 | 1321 | if (WARN_ON_ONCE((int) size < 0)) |
1da177e4 | 1322 | return 0; |
1da177e4 LT |
1323 | |
1324 | str = buf; | |
f796937a | 1325 | end = buf + size; |
1da177e4 | 1326 | |
f796937a JF |
1327 | /* Make sure end is always >= buf */ |
1328 | if (end < buf) { | |
1329 | end = ((void *)-1); | |
1330 | size = end - buf; | |
1da177e4 LT |
1331 | } |
1332 | ||
fef20d9c FW |
1333 | while (*fmt) { |
1334 | const char *old_fmt = fmt; | |
d4be151b | 1335 | int read = format_decode(fmt, &spec); |
1da177e4 | 1336 | |
fef20d9c | 1337 | fmt += read; |
1da177e4 | 1338 | |
fef20d9c FW |
1339 | switch (spec.type) { |
1340 | case FORMAT_TYPE_NONE: { | |
1341 | int copy = read; | |
1342 | if (str < end) { | |
1343 | if (copy > end - str) | |
1344 | copy = end - str; | |
1345 | memcpy(str, old_fmt, copy); | |
1da177e4 | 1346 | } |
fef20d9c FW |
1347 | str += read; |
1348 | break; | |
1da177e4 LT |
1349 | } |
1350 | ||
ed681a91 | 1351 | case FORMAT_TYPE_WIDTH: |
fef20d9c FW |
1352 | spec.field_width = va_arg(args, int); |
1353 | break; | |
1da177e4 | 1354 | |
fef20d9c FW |
1355 | case FORMAT_TYPE_PRECISION: |
1356 | spec.precision = va_arg(args, int); | |
1357 | break; | |
1da177e4 | 1358 | |
d4be151b AGR |
1359 | case FORMAT_TYPE_CHAR: { |
1360 | char c; | |
1361 | ||
fef20d9c FW |
1362 | if (!(spec.flags & LEFT)) { |
1363 | while (--spec.field_width > 0) { | |
f796937a | 1364 | if (str < end) |
1da177e4 LT |
1365 | *str = ' '; |
1366 | ++str; | |
1da177e4 | 1367 | |
fef20d9c FW |
1368 | } |
1369 | } | |
1370 | c = (unsigned char) va_arg(args, int); | |
1371 | if (str < end) | |
1372 | *str = c; | |
1373 | ++str; | |
1374 | while (--spec.field_width > 0) { | |
f796937a | 1375 | if (str < end) |
fef20d9c | 1376 | *str = ' '; |
1da177e4 | 1377 | ++str; |
fef20d9c FW |
1378 | } |
1379 | break; | |
d4be151b | 1380 | } |
1da177e4 | 1381 | |
fef20d9c FW |
1382 | case FORMAT_TYPE_STR: |
1383 | str = string(str, end, va_arg(args, char *), spec); | |
1384 | break; | |
1da177e4 | 1385 | |
fef20d9c FW |
1386 | case FORMAT_TYPE_PTR: |
1387 | str = pointer(fmt+1, str, end, va_arg(args, void *), | |
1388 | spec); | |
1389 | while (isalnum(*fmt)) | |
1390 | fmt++; | |
1391 | break; | |
1da177e4 | 1392 | |
fef20d9c FW |
1393 | case FORMAT_TYPE_PERCENT_CHAR: |
1394 | if (str < end) | |
1395 | *str = '%'; | |
1396 | ++str; | |
1397 | break; | |
1da177e4 | 1398 | |
fef20d9c FW |
1399 | case FORMAT_TYPE_INVALID: |
1400 | if (str < end) | |
1401 | *str = '%'; | |
1402 | ++str; | |
fef20d9c FW |
1403 | break; |
1404 | ||
1405 | case FORMAT_TYPE_NRCHARS: { | |
ef0658f3 | 1406 | u8 qualifier = spec.qualifier; |
fef20d9c FW |
1407 | |
1408 | if (qualifier == 'l') { | |
1409 | long *ip = va_arg(args, long *); | |
1410 | *ip = (str - buf); | |
75fb8f26 | 1411 | } else if (_tolower(qualifier) == 'z') { |
fef20d9c FW |
1412 | size_t *ip = va_arg(args, size_t *); |
1413 | *ip = (str - buf); | |
1414 | } else { | |
1415 | int *ip = va_arg(args, int *); | |
1416 | *ip = (str - buf); | |
1417 | } | |
1418 | break; | |
1da177e4 | 1419 | } |
fef20d9c FW |
1420 | |
1421 | default: | |
1422 | switch (spec.type) { | |
1423 | case FORMAT_TYPE_LONG_LONG: | |
1424 | num = va_arg(args, long long); | |
1425 | break; | |
1426 | case FORMAT_TYPE_ULONG: | |
1427 | num = va_arg(args, unsigned long); | |
1428 | break; | |
1429 | case FORMAT_TYPE_LONG: | |
1430 | num = va_arg(args, long); | |
1431 | break; | |
1432 | case FORMAT_TYPE_SIZE_T: | |
1433 | num = va_arg(args, size_t); | |
1434 | break; | |
1435 | case FORMAT_TYPE_PTRDIFF: | |
1436 | num = va_arg(args, ptrdiff_t); | |
1437 | break; | |
a4e94ef0 Z |
1438 | case FORMAT_TYPE_UBYTE: |
1439 | num = (unsigned char) va_arg(args, int); | |
1440 | break; | |
1441 | case FORMAT_TYPE_BYTE: | |
1442 | num = (signed char) va_arg(args, int); | |
1443 | break; | |
fef20d9c FW |
1444 | case FORMAT_TYPE_USHORT: |
1445 | num = (unsigned short) va_arg(args, int); | |
1446 | break; | |
1447 | case FORMAT_TYPE_SHORT: | |
1448 | num = (short) va_arg(args, int); | |
1449 | break; | |
39e874f8 FW |
1450 | case FORMAT_TYPE_INT: |
1451 | num = (int) va_arg(args, int); | |
fef20d9c FW |
1452 | break; |
1453 | default: | |
1454 | num = va_arg(args, unsigned int); | |
1455 | } | |
1456 | ||
1457 | str = number(str, end, num, spec); | |
1da177e4 | 1458 | } |
1da177e4 | 1459 | } |
fef20d9c | 1460 | |
f796937a JF |
1461 | if (size > 0) { |
1462 | if (str < end) | |
1463 | *str = '\0'; | |
1464 | else | |
0a6047ee | 1465 | end[-1] = '\0'; |
f796937a | 1466 | } |
fef20d9c | 1467 | |
f796937a | 1468 | /* the trailing null byte doesn't count towards the total */ |
1da177e4 | 1469 | return str-buf; |
fef20d9c | 1470 | |
1da177e4 | 1471 | } |
1da177e4 LT |
1472 | EXPORT_SYMBOL(vsnprintf); |
1473 | ||
1474 | /** | |
1475 | * vscnprintf - Format a string and place it in a buffer | |
1476 | * @buf: The buffer to place the result into | |
1477 | * @size: The size of the buffer, including the trailing null space | |
1478 | * @fmt: The format string to use | |
1479 | * @args: Arguments for the format string | |
1480 | * | |
1481 | * The return value is the number of characters which have been written into | |
b921c69f | 1482 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
1da177e4 LT |
1483 | * returns 0. |
1484 | * | |
ba1835eb | 1485 | * If you're not already dealing with a va_list consider using scnprintf(). |
20036fdc AK |
1486 | * |
1487 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 LT |
1488 | */ |
1489 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) | |
1490 | { | |
1491 | int i; | |
1492 | ||
7b9186f5 AGR |
1493 | i = vsnprintf(buf, size, fmt, args); |
1494 | ||
b921c69f AA |
1495 | if (likely(i < size)) |
1496 | return i; | |
1497 | if (size != 0) | |
1498 | return size - 1; | |
1499 | return 0; | |
1da177e4 | 1500 | } |
1da177e4 LT |
1501 | EXPORT_SYMBOL(vscnprintf); |
1502 | ||
1503 | /** | |
1504 | * snprintf - Format a string and place it in a buffer | |
1505 | * @buf: The buffer to place the result into | |
1506 | * @size: The size of the buffer, including the trailing null space | |
1507 | * @fmt: The format string to use | |
1508 | * @...: Arguments for the format string | |
1509 | * | |
1510 | * The return value is the number of characters which would be | |
1511 | * generated for the given input, excluding the trailing null, | |
1512 | * as per ISO C99. If the return is greater than or equal to | |
1513 | * @size, the resulting string is truncated. | |
20036fdc AK |
1514 | * |
1515 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 | 1516 | */ |
7b9186f5 | 1517 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
1da177e4 LT |
1518 | { |
1519 | va_list args; | |
1520 | int i; | |
1521 | ||
1522 | va_start(args, fmt); | |
7b9186f5 | 1523 | i = vsnprintf(buf, size, fmt, args); |
1da177e4 | 1524 | va_end(args); |
7b9186f5 | 1525 | |
1da177e4 LT |
1526 | return i; |
1527 | } | |
1da177e4 LT |
1528 | EXPORT_SYMBOL(snprintf); |
1529 | ||
1530 | /** | |
1531 | * scnprintf - Format a string and place it in a buffer | |
1532 | * @buf: The buffer to place the result into | |
1533 | * @size: The size of the buffer, including the trailing null space | |
1534 | * @fmt: The format string to use | |
1535 | * @...: Arguments for the format string | |
1536 | * | |
1537 | * The return value is the number of characters written into @buf not including | |
b903c0b8 | 1538 | * the trailing '\0'. If @size is == 0 the function returns 0. |
1da177e4 LT |
1539 | */ |
1540 | ||
7b9186f5 | 1541 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
1da177e4 LT |
1542 | { |
1543 | va_list args; | |
1544 | int i; | |
1545 | ||
1546 | va_start(args, fmt); | |
b921c69f | 1547 | i = vscnprintf(buf, size, fmt, args); |
1da177e4 | 1548 | va_end(args); |
7b9186f5 | 1549 | |
b921c69f | 1550 | return i; |
1da177e4 LT |
1551 | } |
1552 | EXPORT_SYMBOL(scnprintf); | |
1553 | ||
1554 | /** | |
1555 | * vsprintf - Format a string and place it in a buffer | |
1556 | * @buf: The buffer to place the result into | |
1557 | * @fmt: The format string to use | |
1558 | * @args: Arguments for the format string | |
1559 | * | |
1560 | * The function returns the number of characters written | |
72fd4a35 | 1561 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
1da177e4 LT |
1562 | * buffer overflows. |
1563 | * | |
ba1835eb | 1564 | * If you're not already dealing with a va_list consider using sprintf(). |
20036fdc AK |
1565 | * |
1566 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 LT |
1567 | */ |
1568 | int vsprintf(char *buf, const char *fmt, va_list args) | |
1569 | { | |
1570 | return vsnprintf(buf, INT_MAX, fmt, args); | |
1571 | } | |
1da177e4 LT |
1572 | EXPORT_SYMBOL(vsprintf); |
1573 | ||
1574 | /** | |
1575 | * sprintf - Format a string and place it in a buffer | |
1576 | * @buf: The buffer to place the result into | |
1577 | * @fmt: The format string to use | |
1578 | * @...: Arguments for the format string | |
1579 | * | |
1580 | * The function returns the number of characters written | |
72fd4a35 | 1581 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
1da177e4 | 1582 | * buffer overflows. |
20036fdc AK |
1583 | * |
1584 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 | 1585 | */ |
7b9186f5 | 1586 | int sprintf(char *buf, const char *fmt, ...) |
1da177e4 LT |
1587 | { |
1588 | va_list args; | |
1589 | int i; | |
1590 | ||
1591 | va_start(args, fmt); | |
7b9186f5 | 1592 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1da177e4 | 1593 | va_end(args); |
7b9186f5 | 1594 | |
1da177e4 LT |
1595 | return i; |
1596 | } | |
1da177e4 LT |
1597 | EXPORT_SYMBOL(sprintf); |
1598 | ||
4370aa4a LJ |
1599 | #ifdef CONFIG_BINARY_PRINTF |
1600 | /* | |
1601 | * bprintf service: | |
1602 | * vbin_printf() - VA arguments to binary data | |
1603 | * bstr_printf() - Binary data to text string | |
1604 | */ | |
1605 | ||
1606 | /** | |
1607 | * vbin_printf - Parse a format string and place args' binary value in a buffer | |
1608 | * @bin_buf: The buffer to place args' binary value | |
1609 | * @size: The size of the buffer(by words(32bits), not characters) | |
1610 | * @fmt: The format string to use | |
1611 | * @args: Arguments for the format string | |
1612 | * | |
1613 | * The format follows C99 vsnprintf, except %n is ignored, and its argument | |
1614 | * is skiped. | |
1615 | * | |
1616 | * The return value is the number of words(32bits) which would be generated for | |
1617 | * the given input. | |
1618 | * | |
1619 | * NOTE: | |
1620 | * If the return value is greater than @size, the resulting bin_buf is NOT | |
1621 | * valid for bstr_printf(). | |
1622 | */ | |
1623 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) | |
1624 | { | |
fef20d9c | 1625 | struct printf_spec spec = {0}; |
4370aa4a | 1626 | char *str, *end; |
4370aa4a LJ |
1627 | |
1628 | str = (char *)bin_buf; | |
1629 | end = (char *)(bin_buf + size); | |
1630 | ||
1631 | #define save_arg(type) \ | |
1632 | do { \ | |
1633 | if (sizeof(type) == 8) { \ | |
1634 | unsigned long long value; \ | |
1635 | str = PTR_ALIGN(str, sizeof(u32)); \ | |
1636 | value = va_arg(args, unsigned long long); \ | |
1637 | if (str + sizeof(type) <= end) { \ | |
1638 | *(u32 *)str = *(u32 *)&value; \ | |
1639 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ | |
1640 | } \ | |
1641 | } else { \ | |
1642 | unsigned long value; \ | |
1643 | str = PTR_ALIGN(str, sizeof(type)); \ | |
1644 | value = va_arg(args, int); \ | |
1645 | if (str + sizeof(type) <= end) \ | |
1646 | *(typeof(type) *)str = (type)value; \ | |
1647 | } \ | |
1648 | str += sizeof(type); \ | |
1649 | } while (0) | |
1650 | ||
fef20d9c | 1651 | while (*fmt) { |
d4be151b | 1652 | int read = format_decode(fmt, &spec); |
4370aa4a | 1653 | |
fef20d9c | 1654 | fmt += read; |
4370aa4a | 1655 | |
fef20d9c FW |
1656 | switch (spec.type) { |
1657 | case FORMAT_TYPE_NONE: | |
d4be151b AGR |
1658 | case FORMAT_TYPE_INVALID: |
1659 | case FORMAT_TYPE_PERCENT_CHAR: | |
fef20d9c FW |
1660 | break; |
1661 | ||
ed681a91 | 1662 | case FORMAT_TYPE_WIDTH: |
fef20d9c FW |
1663 | case FORMAT_TYPE_PRECISION: |
1664 | save_arg(int); | |
1665 | break; | |
1666 | ||
1667 | case FORMAT_TYPE_CHAR: | |
4370aa4a | 1668 | save_arg(char); |
fef20d9c FW |
1669 | break; |
1670 | ||
1671 | case FORMAT_TYPE_STR: { | |
4370aa4a LJ |
1672 | const char *save_str = va_arg(args, char *); |
1673 | size_t len; | |
6c356634 | 1674 | |
4370aa4a LJ |
1675 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
1676 | || (unsigned long)save_str < PAGE_SIZE) | |
0f4f81dc | 1677 | save_str = "(null)"; |
6c356634 AGR |
1678 | len = strlen(save_str) + 1; |
1679 | if (str + len < end) | |
1680 | memcpy(str, save_str, len); | |
1681 | str += len; | |
fef20d9c | 1682 | break; |
4370aa4a | 1683 | } |
fef20d9c FW |
1684 | |
1685 | case FORMAT_TYPE_PTR: | |
4370aa4a LJ |
1686 | save_arg(void *); |
1687 | /* skip all alphanumeric pointer suffixes */ | |
fef20d9c | 1688 | while (isalnum(*fmt)) |
4370aa4a | 1689 | fmt++; |
fef20d9c FW |
1690 | break; |
1691 | ||
fef20d9c | 1692 | case FORMAT_TYPE_NRCHARS: { |
4370aa4a | 1693 | /* skip %n 's argument */ |
ef0658f3 | 1694 | u8 qualifier = spec.qualifier; |
4370aa4a LJ |
1695 | void *skip_arg; |
1696 | if (qualifier == 'l') | |
1697 | skip_arg = va_arg(args, long *); | |
75fb8f26 | 1698 | else if (_tolower(qualifier) == 'z') |
4370aa4a LJ |
1699 | skip_arg = va_arg(args, size_t *); |
1700 | else | |
1701 | skip_arg = va_arg(args, int *); | |
fef20d9c | 1702 | break; |
4370aa4a | 1703 | } |
fef20d9c FW |
1704 | |
1705 | default: | |
1706 | switch (spec.type) { | |
1707 | ||
1708 | case FORMAT_TYPE_LONG_LONG: | |
4370aa4a | 1709 | save_arg(long long); |
fef20d9c FW |
1710 | break; |
1711 | case FORMAT_TYPE_ULONG: | |
1712 | case FORMAT_TYPE_LONG: | |
4370aa4a | 1713 | save_arg(unsigned long); |
fef20d9c FW |
1714 | break; |
1715 | case FORMAT_TYPE_SIZE_T: | |
4370aa4a | 1716 | save_arg(size_t); |
fef20d9c FW |
1717 | break; |
1718 | case FORMAT_TYPE_PTRDIFF: | |
4370aa4a | 1719 | save_arg(ptrdiff_t); |
fef20d9c | 1720 | break; |
a4e94ef0 Z |
1721 | case FORMAT_TYPE_UBYTE: |
1722 | case FORMAT_TYPE_BYTE: | |
1723 | save_arg(char); | |
1724 | break; | |
fef20d9c FW |
1725 | case FORMAT_TYPE_USHORT: |
1726 | case FORMAT_TYPE_SHORT: | |
4370aa4a | 1727 | save_arg(short); |
fef20d9c FW |
1728 | break; |
1729 | default: | |
4370aa4a | 1730 | save_arg(int); |
fef20d9c | 1731 | } |
4370aa4a LJ |
1732 | } |
1733 | } | |
fef20d9c | 1734 | |
7b9186f5 | 1735 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
fef20d9c | 1736 | #undef save_arg |
4370aa4a LJ |
1737 | } |
1738 | EXPORT_SYMBOL_GPL(vbin_printf); | |
1739 | ||
1740 | /** | |
1741 | * bstr_printf - Format a string from binary arguments and place it in a buffer | |
1742 | * @buf: The buffer to place the result into | |
1743 | * @size: The size of the buffer, including the trailing null space | |
1744 | * @fmt: The format string to use | |
1745 | * @bin_buf: Binary arguments for the format string | |
1746 | * | |
1747 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets | |
1748 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is | |
1749 | * a binary buffer that generated by vbin_printf. | |
1750 | * | |
1751 | * The format follows C99 vsnprintf, but has some extensions: | |
0efb4d20 | 1752 | * see vsnprintf comment for details. |
4370aa4a LJ |
1753 | * |
1754 | * The return value is the number of characters which would | |
1755 | * be generated for the given input, excluding the trailing | |
1756 | * '\0', as per ISO C99. If you want to have the exact | |
1757 | * number of characters written into @buf as return value | |
1758 | * (not including the trailing '\0'), use vscnprintf(). If the | |
1759 | * return is greater than or equal to @size, the resulting | |
1760 | * string is truncated. | |
1761 | */ | |
1762 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |
1763 | { | |
fef20d9c | 1764 | struct printf_spec spec = {0}; |
d4be151b AGR |
1765 | char *str, *end; |
1766 | const char *args = (const char *)bin_buf; | |
4370aa4a | 1767 | |
2f30b1f9 | 1768 | if (WARN_ON_ONCE((int) size < 0)) |
4370aa4a | 1769 | return 0; |
4370aa4a LJ |
1770 | |
1771 | str = buf; | |
1772 | end = buf + size; | |
1773 | ||
1774 | #define get_arg(type) \ | |
1775 | ({ \ | |
1776 | typeof(type) value; \ | |
1777 | if (sizeof(type) == 8) { \ | |
1778 | args = PTR_ALIGN(args, sizeof(u32)); \ | |
1779 | *(u32 *)&value = *(u32 *)args; \ | |
1780 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ | |
1781 | } else { \ | |
1782 | args = PTR_ALIGN(args, sizeof(type)); \ | |
1783 | value = *(typeof(type) *)args; \ | |
1784 | } \ | |
1785 | args += sizeof(type); \ | |
1786 | value; \ | |
1787 | }) | |
1788 | ||
1789 | /* Make sure end is always >= buf */ | |
1790 | if (end < buf) { | |
1791 | end = ((void *)-1); | |
1792 | size = end - buf; | |
1793 | } | |
1794 | ||
fef20d9c | 1795 | while (*fmt) { |
fef20d9c | 1796 | const char *old_fmt = fmt; |
d4be151b | 1797 | int read = format_decode(fmt, &spec); |
4370aa4a | 1798 | |
fef20d9c | 1799 | fmt += read; |
4370aa4a | 1800 | |
fef20d9c FW |
1801 | switch (spec.type) { |
1802 | case FORMAT_TYPE_NONE: { | |
1803 | int copy = read; | |
1804 | if (str < end) { | |
1805 | if (copy > end - str) | |
1806 | copy = end - str; | |
1807 | memcpy(str, old_fmt, copy); | |
4370aa4a | 1808 | } |
fef20d9c FW |
1809 | str += read; |
1810 | break; | |
4370aa4a LJ |
1811 | } |
1812 | ||
ed681a91 | 1813 | case FORMAT_TYPE_WIDTH: |
fef20d9c FW |
1814 | spec.field_width = get_arg(int); |
1815 | break; | |
4370aa4a | 1816 | |
fef20d9c FW |
1817 | case FORMAT_TYPE_PRECISION: |
1818 | spec.precision = get_arg(int); | |
1819 | break; | |
4370aa4a | 1820 | |
d4be151b AGR |
1821 | case FORMAT_TYPE_CHAR: { |
1822 | char c; | |
1823 | ||
fef20d9c FW |
1824 | if (!(spec.flags & LEFT)) { |
1825 | while (--spec.field_width > 0) { | |
4370aa4a LJ |
1826 | if (str < end) |
1827 | *str = ' '; | |
1828 | ++str; | |
1829 | } | |
1830 | } | |
1831 | c = (unsigned char) get_arg(char); | |
1832 | if (str < end) | |
1833 | *str = c; | |
1834 | ++str; | |
fef20d9c | 1835 | while (--spec.field_width > 0) { |
4370aa4a LJ |
1836 | if (str < end) |
1837 | *str = ' '; | |
1838 | ++str; | |
1839 | } | |
fef20d9c | 1840 | break; |
d4be151b | 1841 | } |
4370aa4a | 1842 | |
fef20d9c | 1843 | case FORMAT_TYPE_STR: { |
4370aa4a | 1844 | const char *str_arg = args; |
d4be151b | 1845 | args += strlen(str_arg) + 1; |
fef20d9c FW |
1846 | str = string(str, end, (char *)str_arg, spec); |
1847 | break; | |
4370aa4a LJ |
1848 | } |
1849 | ||
fef20d9c FW |
1850 | case FORMAT_TYPE_PTR: |
1851 | str = pointer(fmt+1, str, end, get_arg(void *), spec); | |
1852 | while (isalnum(*fmt)) | |
4370aa4a | 1853 | fmt++; |
fef20d9c | 1854 | break; |
4370aa4a | 1855 | |
fef20d9c | 1856 | case FORMAT_TYPE_PERCENT_CHAR: |
fef20d9c | 1857 | case FORMAT_TYPE_INVALID: |
4370aa4a LJ |
1858 | if (str < end) |
1859 | *str = '%'; | |
1860 | ++str; | |
fef20d9c FW |
1861 | break; |
1862 | ||
1863 | case FORMAT_TYPE_NRCHARS: | |
1864 | /* skip */ | |
1865 | break; | |
1866 | ||
d4be151b AGR |
1867 | default: { |
1868 | unsigned long long num; | |
1869 | ||
fef20d9c FW |
1870 | switch (spec.type) { |
1871 | ||
1872 | case FORMAT_TYPE_LONG_LONG: | |
1873 | num = get_arg(long long); | |
1874 | break; | |
1875 | case FORMAT_TYPE_ULONG: | |
fef20d9c FW |
1876 | case FORMAT_TYPE_LONG: |
1877 | num = get_arg(unsigned long); | |
1878 | break; | |
1879 | case FORMAT_TYPE_SIZE_T: | |
1880 | num = get_arg(size_t); | |
1881 | break; | |
1882 | case FORMAT_TYPE_PTRDIFF: | |
1883 | num = get_arg(ptrdiff_t); | |
1884 | break; | |
a4e94ef0 Z |
1885 | case FORMAT_TYPE_UBYTE: |
1886 | num = get_arg(unsigned char); | |
1887 | break; | |
1888 | case FORMAT_TYPE_BYTE: | |
1889 | num = get_arg(signed char); | |
1890 | break; | |
fef20d9c FW |
1891 | case FORMAT_TYPE_USHORT: |
1892 | num = get_arg(unsigned short); | |
1893 | break; | |
1894 | case FORMAT_TYPE_SHORT: | |
1895 | num = get_arg(short); | |
1896 | break; | |
1897 | case FORMAT_TYPE_UINT: | |
1898 | num = get_arg(unsigned int); | |
1899 | break; | |
1900 | default: | |
1901 | num = get_arg(int); | |
1902 | } | |
1903 | ||
1904 | str = number(str, end, num, spec); | |
d4be151b AGR |
1905 | } /* default: */ |
1906 | } /* switch(spec.type) */ | |
1907 | } /* while(*fmt) */ | |
fef20d9c | 1908 | |
4370aa4a LJ |
1909 | if (size > 0) { |
1910 | if (str < end) | |
1911 | *str = '\0'; | |
1912 | else | |
1913 | end[-1] = '\0'; | |
1914 | } | |
fef20d9c | 1915 | |
4370aa4a LJ |
1916 | #undef get_arg |
1917 | ||
1918 | /* the trailing null byte doesn't count towards the total */ | |
1919 | return str - buf; | |
1920 | } | |
1921 | EXPORT_SYMBOL_GPL(bstr_printf); | |
1922 | ||
1923 | /** | |
1924 | * bprintf - Parse a format string and place args' binary value in a buffer | |
1925 | * @bin_buf: The buffer to place args' binary value | |
1926 | * @size: The size of the buffer(by words(32bits), not characters) | |
1927 | * @fmt: The format string to use | |
1928 | * @...: Arguments for the format string | |
1929 | * | |
1930 | * The function returns the number of words(u32) written | |
1931 | * into @bin_buf. | |
1932 | */ | |
1933 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) | |
1934 | { | |
1935 | va_list args; | |
1936 | int ret; | |
1937 | ||
1938 | va_start(args, fmt); | |
1939 | ret = vbin_printf(bin_buf, size, fmt, args); | |
1940 | va_end(args); | |
7b9186f5 | 1941 | |
4370aa4a LJ |
1942 | return ret; |
1943 | } | |
1944 | EXPORT_SYMBOL_GPL(bprintf); | |
1945 | ||
1946 | #endif /* CONFIG_BINARY_PRINTF */ | |
1947 | ||
1da177e4 LT |
1948 | /** |
1949 | * vsscanf - Unformat a buffer into a list of arguments | |
1950 | * @buf: input buffer | |
1951 | * @fmt: format of buffer | |
1952 | * @args: arguments | |
1953 | */ | |
7b9186f5 | 1954 | int vsscanf(const char *buf, const char *fmt, va_list args) |
1da177e4 LT |
1955 | { |
1956 | const char *str = buf; | |
1957 | char *next; | |
1958 | char digit; | |
1959 | int num = 0; | |
ef0658f3 JP |
1960 | u8 qualifier; |
1961 | u8 base; | |
1962 | s16 field_width; | |
d4be151b | 1963 | bool is_sign; |
1da177e4 | 1964 | |
7b9186f5 | 1965 | while (*fmt && *str) { |
1da177e4 LT |
1966 | /* skip any white space in format */ |
1967 | /* white space in format matchs any amount of | |
1968 | * white space, including none, in the input. | |
1969 | */ | |
1970 | if (isspace(*fmt)) { | |
e7d2860b AGR |
1971 | fmt = skip_spaces(++fmt); |
1972 | str = skip_spaces(str); | |
1da177e4 LT |
1973 | } |
1974 | ||
1975 | /* anything that is not a conversion must match exactly */ | |
1976 | if (*fmt != '%' && *fmt) { | |
1977 | if (*fmt++ != *str++) | |
1978 | break; | |
1979 | continue; | |
1980 | } | |
1981 | ||
1982 | if (!*fmt) | |
1983 | break; | |
1984 | ++fmt; | |
7b9186f5 | 1985 | |
1da177e4 LT |
1986 | /* skip this conversion. |
1987 | * advance both strings to next white space | |
1988 | */ | |
1989 | if (*fmt == '*') { | |
8fccae2c | 1990 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
1da177e4 LT |
1991 | fmt++; |
1992 | while (!isspace(*str) && *str) | |
1993 | str++; | |
1994 | continue; | |
1995 | } | |
1996 | ||
1997 | /* get field width */ | |
1998 | field_width = -1; | |
1999 | if (isdigit(*fmt)) | |
2000 | field_width = skip_atoi(&fmt); | |
2001 | ||
2002 | /* get conversion qualifier */ | |
2003 | qualifier = -1; | |
75fb8f26 AS |
2004 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
2005 | _tolower(*fmt) == 'z') { | |
1da177e4 LT |
2006 | qualifier = *fmt++; |
2007 | if (unlikely(qualifier == *fmt)) { | |
2008 | if (qualifier == 'h') { | |
2009 | qualifier = 'H'; | |
2010 | fmt++; | |
2011 | } else if (qualifier == 'l') { | |
2012 | qualifier = 'L'; | |
2013 | fmt++; | |
2014 | } | |
2015 | } | |
2016 | } | |
1da177e4 LT |
2017 | |
2018 | if (!*fmt || !*str) | |
2019 | break; | |
2020 | ||
d4be151b AGR |
2021 | base = 10; |
2022 | is_sign = 0; | |
2023 | ||
7b9186f5 | 2024 | switch (*fmt++) { |
1da177e4 LT |
2025 | case 'c': |
2026 | { | |
7b9186f5 | 2027 | char *s = (char *)va_arg(args, char*); |
1da177e4 LT |
2028 | if (field_width == -1) |
2029 | field_width = 1; | |
2030 | do { | |
2031 | *s++ = *str++; | |
2032 | } while (--field_width > 0 && *str); | |
2033 | num++; | |
2034 | } | |
2035 | continue; | |
2036 | case 's': | |
2037 | { | |
7b9186f5 AGR |
2038 | char *s = (char *)va_arg(args, char *); |
2039 | if (field_width == -1) | |
4be929be | 2040 | field_width = SHRT_MAX; |
1da177e4 | 2041 | /* first, skip leading white space in buffer */ |
e7d2860b | 2042 | str = skip_spaces(str); |
1da177e4 LT |
2043 | |
2044 | /* now copy until next white space */ | |
7b9186f5 | 2045 | while (*str && !isspace(*str) && field_width--) |
1da177e4 | 2046 | *s++ = *str++; |
1da177e4 LT |
2047 | *s = '\0'; |
2048 | num++; | |
2049 | } | |
2050 | continue; | |
2051 | case 'n': | |
2052 | /* return number of characters read so far */ | |
2053 | { | |
7b9186f5 | 2054 | int *i = (int *)va_arg(args, int*); |
1da177e4 LT |
2055 | *i = str - buf; |
2056 | } | |
2057 | continue; | |
2058 | case 'o': | |
2059 | base = 8; | |
2060 | break; | |
2061 | case 'x': | |
2062 | case 'X': | |
2063 | base = 16; | |
2064 | break; | |
2065 | case 'i': | |
7b9186f5 | 2066 | base = 0; |
1da177e4 LT |
2067 | case 'd': |
2068 | is_sign = 1; | |
2069 | case 'u': | |
2070 | break; | |
2071 | case '%': | |
2072 | /* looking for '%' in str */ | |
7b9186f5 | 2073 | if (*str++ != '%') |
1da177e4 LT |
2074 | return num; |
2075 | continue; | |
2076 | default: | |
2077 | /* invalid format; stop here */ | |
2078 | return num; | |
2079 | } | |
2080 | ||
2081 | /* have some sort of integer conversion. | |
2082 | * first, skip white space in buffer. | |
2083 | */ | |
e7d2860b | 2084 | str = skip_spaces(str); |
1da177e4 LT |
2085 | |
2086 | digit = *str; | |
2087 | if (is_sign && digit == '-') | |
2088 | digit = *(str + 1); | |
2089 | ||
2090 | if (!digit | |
7b9186f5 AGR |
2091 | || (base == 16 && !isxdigit(digit)) |
2092 | || (base == 10 && !isdigit(digit)) | |
2093 | || (base == 8 && (!isdigit(digit) || digit > '7')) | |
2094 | || (base == 0 && !isdigit(digit))) | |
2095 | break; | |
1da177e4 | 2096 | |
7b9186f5 | 2097 | switch (qualifier) { |
1da177e4 LT |
2098 | case 'H': /* that's 'hh' in format */ |
2099 | if (is_sign) { | |
7b9186f5 AGR |
2100 | signed char *s = (signed char *)va_arg(args, signed char *); |
2101 | *s = (signed char)simple_strtol(str, &next, base); | |
1da177e4 | 2102 | } else { |
7b9186f5 AGR |
2103 | unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); |
2104 | *s = (unsigned char)simple_strtoul(str, &next, base); | |
1da177e4 LT |
2105 | } |
2106 | break; | |
2107 | case 'h': | |
2108 | if (is_sign) { | |
7b9186f5 AGR |
2109 | short *s = (short *)va_arg(args, short *); |
2110 | *s = (short)simple_strtol(str, &next, base); | |
1da177e4 | 2111 | } else { |
7b9186f5 AGR |
2112 | unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); |
2113 | *s = (unsigned short)simple_strtoul(str, &next, base); | |
1da177e4 LT |
2114 | } |
2115 | break; | |
2116 | case 'l': | |
2117 | if (is_sign) { | |
7b9186f5 AGR |
2118 | long *l = (long *)va_arg(args, long *); |
2119 | *l = simple_strtol(str, &next, base); | |
1da177e4 | 2120 | } else { |
7b9186f5 AGR |
2121 | unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); |
2122 | *l = simple_strtoul(str, &next, base); | |
1da177e4 LT |
2123 | } |
2124 | break; | |
2125 | case 'L': | |
2126 | if (is_sign) { | |
7b9186f5 AGR |
2127 | long long *l = (long long *)va_arg(args, long long *); |
2128 | *l = simple_strtoll(str, &next, base); | |
1da177e4 | 2129 | } else { |
7b9186f5 AGR |
2130 | unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); |
2131 | *l = simple_strtoull(str, &next, base); | |
1da177e4 LT |
2132 | } |
2133 | break; | |
2134 | case 'Z': | |
2135 | case 'z': | |
2136 | { | |
7b9186f5 AGR |
2137 | size_t *s = (size_t *)va_arg(args, size_t *); |
2138 | *s = (size_t)simple_strtoul(str, &next, base); | |
1da177e4 LT |
2139 | } |
2140 | break; | |
2141 | default: | |
2142 | if (is_sign) { | |
7b9186f5 AGR |
2143 | int *i = (int *)va_arg(args, int *); |
2144 | *i = (int)simple_strtol(str, &next, base); | |
1da177e4 | 2145 | } else { |
7b9186f5 AGR |
2146 | unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); |
2147 | *i = (unsigned int)simple_strtoul(str, &next, base); | |
1da177e4 LT |
2148 | } |
2149 | break; | |
2150 | } | |
2151 | num++; | |
2152 | ||
2153 | if (!next) | |
2154 | break; | |
2155 | str = next; | |
2156 | } | |
c6b40d16 JB |
2157 | |
2158 | /* | |
2159 | * Now we've come all the way through so either the input string or the | |
2160 | * format ended. In the former case, there can be a %n at the current | |
2161 | * position in the format that needs to be filled. | |
2162 | */ | |
2163 | if (*fmt == '%' && *(fmt + 1) == 'n') { | |
2164 | int *p = (int *)va_arg(args, int *); | |
2165 | *p = str - buf; | |
2166 | } | |
2167 | ||
1da177e4 LT |
2168 | return num; |
2169 | } | |
1da177e4 LT |
2170 | EXPORT_SYMBOL(vsscanf); |
2171 | ||
2172 | /** | |
2173 | * sscanf - Unformat a buffer into a list of arguments | |
2174 | * @buf: input buffer | |
2175 | * @fmt: formatting of buffer | |
2176 | * @...: resulting arguments | |
2177 | */ | |
7b9186f5 | 2178 | int sscanf(const char *buf, const char *fmt, ...) |
1da177e4 LT |
2179 | { |
2180 | va_list args; | |
2181 | int i; | |
2182 | ||
7b9186f5 AGR |
2183 | va_start(args, fmt); |
2184 | i = vsscanf(buf, fmt, args); | |
1da177e4 | 2185 | va_end(args); |
7b9186f5 | 2186 | |
1da177e4 LT |
2187 | return i; |
2188 | } | |
1da177e4 | 2189 | EXPORT_SYMBOL(sscanf); |