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