diff options
author | David S. Miller <davem@davemloft.net> | 2010-01-23 01:45:46 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2010-01-23 01:45:46 -0500 |
commit | 6be325719b3e54624397e413efd4b33a997e55a3 (patch) | |
tree | 57f321a56794cab2222e179b16731e0d76a4a68a /lib/vsprintf.c | |
parent | 26d92f9276a56d55511a427fb70bd70886af647a (diff) | |
parent | 92dcffb916d309aa01778bf8963a6932e4014d07 (diff) |
Merge branch 'master' of /home/davem/src/GIT/linux-2.6/
Diffstat (limited to 'lib/vsprintf.c')
-rw-r--r-- | lib/vsprintf.c | 410 |
1 files changed, 231 insertions, 179 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 6438cd5599ee..3b8aeec4e327 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c | |||
@@ -9,7 +9,7 @@ | |||
9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) | 9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
10 | */ | 10 | */ |
11 | 11 | ||
12 | /* | 12 | /* |
13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> | 13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
14 | * - changed to provide snprintf and vsnprintf functions | 14 | * - changed to provide snprintf and vsnprintf functions |
15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> | 15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> |
@@ -47,14 +47,14 @@ static unsigned int simple_guess_base(const char *cp) | |||
47 | } | 47 | } |
48 | 48 | ||
49 | /** | 49 | /** |
50 | * simple_strtoul - convert a string to an unsigned long | 50 | * simple_strtoull - convert a string to an unsigned long long |
51 | * @cp: The start of the string | 51 | * @cp: The start of the string |
52 | * @endp: A pointer to the end of the parsed string will be placed here | 52 | * @endp: A pointer to the end of the parsed string will be placed here |
53 | * @base: The number base to use | 53 | * @base: The number base to use |
54 | */ | 54 | */ |
55 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) | 55 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
56 | { | 56 | { |
57 | unsigned long result = 0; | 57 | unsigned long long result = 0; |
58 | 58 | ||
59 | if (!base) | 59 | if (!base) |
60 | base = simple_guess_base(cp); | 60 | base = simple_guess_base(cp); |
@@ -71,58 +71,39 @@ unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) | |||
71 | result = result * base + value; | 71 | result = result * base + value; |
72 | cp++; | 72 | cp++; |
73 | } | 73 | } |
74 | |||
75 | if (endp) | 74 | if (endp) |
76 | *endp = (char *)cp; | 75 | *endp = (char *)cp; |
76 | |||
77 | return result; | 77 | return result; |
78 | } | 78 | } |
79 | EXPORT_SYMBOL(simple_strtoul); | 79 | EXPORT_SYMBOL(simple_strtoull); |
80 | 80 | ||
81 | /** | 81 | /** |
82 | * simple_strtol - convert a string to a signed long | 82 | * simple_strtoul - convert a string to an unsigned long |
83 | * @cp: The start of the string | 83 | * @cp: The start of the string |
84 | * @endp: A pointer to the end of the parsed string will be placed here | 84 | * @endp: A pointer to the end of the parsed string will be placed here |
85 | * @base: The number base to use | 85 | * @base: The number base to use |
86 | */ | 86 | */ |
87 | long simple_strtol(const char *cp, char **endp, unsigned int base) | 87 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
88 | { | 88 | { |
89 | if(*cp == '-') | 89 | return simple_strtoull(cp, endp, base); |
90 | return -simple_strtoul(cp + 1, endp, base); | ||
91 | return simple_strtoul(cp, endp, base); | ||
92 | } | 90 | } |
93 | EXPORT_SYMBOL(simple_strtol); | 91 | EXPORT_SYMBOL(simple_strtoul); |
94 | 92 | ||
95 | /** | 93 | /** |
96 | * simple_strtoull - convert a string to an unsigned long long | 94 | * simple_strtol - convert a string to a signed long |
97 | * @cp: The start of the string | 95 | * @cp: The start of the string |
98 | * @endp: A pointer to the end of the parsed string will be placed here | 96 | * @endp: A pointer to the end of the parsed string will be placed here |
99 | * @base: The number base to use | 97 | * @base: The number base to use |
100 | */ | 98 | */ |
101 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) | 99 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
102 | { | 100 | { |
103 | unsigned long long result = 0; | 101 | if (*cp == '-') |
104 | 102 | return -simple_strtoul(cp + 1, endp, base); | |
105 | if (!base) | ||
106 | base = simple_guess_base(cp); | ||
107 | |||
108 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') | ||
109 | cp += 2; | ||
110 | |||
111 | while (isxdigit(*cp)) { | ||
112 | unsigned int value; | ||
113 | |||
114 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; | ||
115 | if (value >= base) | ||
116 | break; | ||
117 | result = result * base + value; | ||
118 | cp++; | ||
119 | } | ||
120 | 103 | ||
121 | if (endp) | 104 | return simple_strtoul(cp, endp, base); |
122 | *endp = (char *)cp; | ||
123 | return result; | ||
124 | } | 105 | } |
125 | EXPORT_SYMBOL(simple_strtoull); | 106 | EXPORT_SYMBOL(simple_strtol); |
126 | 107 | ||
127 | /** | 108 | /** |
128 | * simple_strtoll - convert a string to a signed long long | 109 | * simple_strtoll - convert a string to a signed long long |
@@ -132,8 +113,9 @@ EXPORT_SYMBOL(simple_strtoull); | |||
132 | */ | 113 | */ |
133 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) | 114 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
134 | { | 115 | { |
135 | if(*cp=='-') | 116 | if (*cp == '-') |
136 | return -simple_strtoull(cp + 1, endp, base); | 117 | return -simple_strtoull(cp + 1, endp, base); |
118 | |||
137 | return simple_strtoull(cp, endp, base); | 119 | return simple_strtoull(cp, endp, base); |
138 | } | 120 | } |
139 | 121 | ||
@@ -173,6 +155,7 @@ int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) | |||
173 | val = simple_strtoul(cp, &tail, base); | 155 | val = simple_strtoul(cp, &tail, base); |
174 | if (tail == cp) | 156 | if (tail == cp) |
175 | return -EINVAL; | 157 | return -EINVAL; |
158 | |||
176 | if ((*tail == '\0') || | 159 | if ((*tail == '\0') || |
177 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { | 160 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { |
178 | *res = val; | 161 | *res = val; |
@@ -285,10 +268,11 @@ EXPORT_SYMBOL(strict_strtoll); | |||
285 | 268 | ||
286 | static int skip_atoi(const char **s) | 269 | static int skip_atoi(const char **s) |
287 | { | 270 | { |
288 | int i=0; | 271 | int i = 0; |
289 | 272 | ||
290 | while (isdigit(**s)) | 273 | while (isdigit(**s)) |
291 | i = i*10 + *((*s)++) - '0'; | 274 | i = i*10 + *((*s)++) - '0'; |
275 | |||
292 | return i; | 276 | return i; |
293 | } | 277 | } |
294 | 278 | ||
@@ -302,7 +286,7 @@ static int skip_atoi(const char **s) | |||
302 | /* Formats correctly any integer in [0,99999]. | 286 | /* Formats correctly any integer in [0,99999]. |
303 | * Outputs from one to five digits depending on input. | 287 | * Outputs from one to five digits depending on input. |
304 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ | 288 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
305 | static char* put_dec_trunc(char *buf, unsigned q) | 289 | static char *put_dec_trunc(char *buf, unsigned q) |
306 | { | 290 | { |
307 | unsigned d3, d2, d1, d0; | 291 | unsigned d3, d2, d1, d0; |
308 | d1 = (q>>4) & 0xf; | 292 | d1 = (q>>4) & 0xf; |
@@ -331,14 +315,15 @@ static char* put_dec_trunc(char *buf, unsigned q) | |||
331 | d3 = d3 - 10*q; | 315 | d3 = d3 - 10*q; |
332 | *buf++ = d3 + '0'; /* next digit */ | 316 | *buf++ = d3 + '0'; /* next digit */ |
333 | if (q != 0) | 317 | if (q != 0) |
334 | *buf++ = q + '0'; /* most sign. digit */ | 318 | *buf++ = q + '0'; /* most sign. digit */ |
335 | } | 319 | } |
336 | } | 320 | } |
337 | } | 321 | } |
322 | |||
338 | return buf; | 323 | return buf; |
339 | } | 324 | } |
340 | /* Same with if's removed. Always emits five digits */ | 325 | /* Same with if's removed. Always emits five digits */ |
341 | static char* put_dec_full(char *buf, unsigned q) | 326 | static char *put_dec_full(char *buf, unsigned q) |
342 | { | 327 | { |
343 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ | 328 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
344 | /* but anyway, gcc produces better code with full-sized ints */ | 329 | /* but anyway, gcc produces better code with full-sized ints */ |
@@ -347,14 +332,15 @@ static char* put_dec_full(char *buf, unsigned q) | |||
347 | d2 = (q>>8) & 0xf; | 332 | d2 = (q>>8) & 0xf; |
348 | d3 = (q>>12); | 333 | d3 = (q>>12); |
349 | 334 | ||
350 | /* Possible ways to approx. divide by 10 */ | 335 | /* |
351 | /* gcc -O2 replaces multiply with shifts and adds */ | 336 | * Possible ways to approx. divide by 10 |
352 | // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) | 337 | * gcc -O2 replaces multiply with shifts and adds |
353 | // (x * 0x67) >> 10: 1100111 | 338 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
354 | // (x * 0x34) >> 9: 110100 - same | 339 | * (x * 0x67) >> 10: 1100111 |
355 | // (x * 0x1a) >> 8: 11010 - same | 340 | * (x * 0x34) >> 9: 110100 - same |
356 | // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) | 341 | * (x * 0x1a) >> 8: 11010 - same |
357 | 342 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) | |
343 | */ | ||
358 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); | 344 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
359 | q = (d0 * 0xcd) >> 11; | 345 | q = (d0 * 0xcd) >> 11; |
360 | d0 = d0 - 10*q; | 346 | d0 = d0 - 10*q; |
@@ -375,10 +361,11 @@ static char* put_dec_full(char *buf, unsigned q) | |||
375 | d3 = d3 - 10*q; | 361 | d3 = d3 - 10*q; |
376 | *buf++ = d3 + '0'; | 362 | *buf++ = d3 + '0'; |
377 | *buf++ = q + '0'; | 363 | *buf++ = q + '0'; |
364 | |||
378 | return buf; | 365 | return buf; |
379 | } | 366 | } |
380 | /* No inlining helps gcc to use registers better */ | 367 | /* No inlining helps gcc to use registers better */ |
381 | static noinline char* put_dec(char *buf, unsigned long long num) | 368 | static noinline char *put_dec(char *buf, unsigned long long num) |
382 | { | 369 | { |
383 | while (1) { | 370 | while (1) { |
384 | unsigned rem; | 371 | unsigned rem; |
@@ -448,9 +435,9 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
448 | spec.flags &= ~ZEROPAD; | 435 | spec.flags &= ~ZEROPAD; |
449 | sign = 0; | 436 | sign = 0; |
450 | if (spec.flags & SIGN) { | 437 | if (spec.flags & SIGN) { |
451 | if ((signed long long) num < 0) { | 438 | if ((signed long long)num < 0) { |
452 | sign = '-'; | 439 | sign = '-'; |
453 | num = - (signed long long) num; | 440 | num = -(signed long long)num; |
454 | spec.field_width--; | 441 | spec.field_width--; |
455 | } else if (spec.flags & PLUS) { | 442 | } else if (spec.flags & PLUS) { |
456 | sign = '+'; | 443 | sign = '+'; |
@@ -478,7 +465,9 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
478 | else if (spec.base != 10) { /* 8 or 16 */ | 465 | else if (spec.base != 10) { /* 8 or 16 */ |
479 | int mask = spec.base - 1; | 466 | int mask = spec.base - 1; |
480 | int shift = 3; | 467 | int shift = 3; |
481 | if (spec.base == 16) shift = 4; | 468 | |
469 | if (spec.base == 16) | ||
470 | shift = 4; | ||
482 | do { | 471 | do { |
483 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); | 472 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
484 | num >>= shift; | 473 | num >>= shift; |
@@ -493,7 +482,7 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
493 | /* leading space padding */ | 482 | /* leading space padding */ |
494 | spec.field_width -= spec.precision; | 483 | spec.field_width -= spec.precision; |
495 | if (!(spec.flags & (ZEROPAD+LEFT))) { | 484 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
496 | while(--spec.field_width >= 0) { | 485 | while (--spec.field_width >= 0) { |
497 | if (buf < end) | 486 | if (buf < end) |
498 | *buf = ' '; | 487 | *buf = ' '; |
499 | ++buf; | 488 | ++buf; |
@@ -543,15 +532,16 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
543 | *buf = ' '; | 532 | *buf = ' '; |
544 | ++buf; | 533 | ++buf; |
545 | } | 534 | } |
535 | |||
546 | return buf; | 536 | return buf; |
547 | } | 537 | } |
548 | 538 | ||
549 | static char *string(char *buf, char *end, char *s, struct printf_spec spec) | 539 | static char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
550 | { | 540 | { |
551 | int len, i; | 541 | int len, i; |
552 | 542 | ||
553 | if ((unsigned long)s < PAGE_SIZE) | 543 | if ((unsigned long)s < PAGE_SIZE) |
554 | s = "<NULL>"; | 544 | s = "(null)"; |
555 | 545 | ||
556 | len = strnlen(s, spec.precision); | 546 | len = strnlen(s, spec.precision); |
557 | 547 | ||
@@ -572,6 +562,7 @@ static char *string(char *buf, char *end, char *s, struct printf_spec spec) | |||
572 | *buf = ' '; | 562 | *buf = ' '; |
573 | ++buf; | 563 | ++buf; |
574 | } | 564 | } |
565 | |||
575 | return buf; | 566 | return buf; |
576 | } | 567 | } |
577 | 568 | ||
@@ -585,11 +576,13 @@ static char *symbol_string(char *buf, char *end, void *ptr, | |||
585 | sprint_symbol(sym, value); | 576 | sprint_symbol(sym, value); |
586 | else | 577 | else |
587 | kallsyms_lookup(value, NULL, NULL, NULL, sym); | 578 | kallsyms_lookup(value, NULL, NULL, NULL, sym); |
579 | |||
588 | return string(buf, end, sym, spec); | 580 | return string(buf, end, sym, spec); |
589 | #else | 581 | #else |
590 | spec.field_width = 2*sizeof(void *); | 582 | spec.field_width = 2 * sizeof(void *); |
591 | spec.flags |= SPECIAL | SMALL | ZEROPAD; | 583 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
592 | spec.base = 16; | 584 | spec.base = 16; |
585 | |||
593 | return number(buf, end, value, spec); | 586 | return number(buf, end, value, spec); |
594 | #endif | 587 | #endif |
595 | } | 588 | } |
@@ -718,22 +711,19 @@ static char *ip4_string(char *p, const u8 *addr, bool leading_zeros) | |||
718 | if (i < 3) | 711 | if (i < 3) |
719 | *p++ = '.'; | 712 | *p++ = '.'; |
720 | } | 713 | } |
721 | |||
722 | *p = '\0'; | 714 | *p = '\0'; |
715 | |||
723 | return p; | 716 | return p; |
724 | } | 717 | } |
725 | 718 | ||
726 | static char *ip6_compressed_string(char *p, const char *addr) | 719 | static char *ip6_compressed_string(char *p, const char *addr) |
727 | { | 720 | { |
728 | int i; | 721 | int i, j, range; |
729 | int j; | ||
730 | int range; | ||
731 | unsigned char zerolength[8]; | 722 | unsigned char zerolength[8]; |
732 | int longest = 1; | 723 | int longest = 1; |
733 | int colonpos = -1; | 724 | int colonpos = -1; |
734 | u16 word; | 725 | u16 word; |
735 | u8 hi; | 726 | u8 hi, lo; |
736 | u8 lo; | ||
737 | bool needcolon = false; | 727 | bool needcolon = false; |
738 | bool useIPv4; | 728 | bool useIPv4; |
739 | struct in6_addr in6; | 729 | struct in6_addr in6; |
@@ -787,8 +777,9 @@ static char *ip6_compressed_string(char *p, const char *addr) | |||
787 | p = pack_hex_byte(p, hi); | 777 | p = pack_hex_byte(p, hi); |
788 | else | 778 | else |
789 | *p++ = hex_asc_lo(hi); | 779 | *p++ = hex_asc_lo(hi); |
780 | p = pack_hex_byte(p, lo); | ||
790 | } | 781 | } |
791 | if (hi || lo > 0x0f) | 782 | else if (lo > 0x0f) |
792 | p = pack_hex_byte(p, lo); | 783 | p = pack_hex_byte(p, lo); |
793 | else | 784 | else |
794 | *p++ = hex_asc_lo(lo); | 785 | *p++ = hex_asc_lo(lo); |
@@ -800,22 +791,23 @@ static char *ip6_compressed_string(char *p, const char *addr) | |||
800 | *p++ = ':'; | 791 | *p++ = ':'; |
801 | p = ip4_string(p, &in6.s6_addr[12], false); | 792 | p = ip4_string(p, &in6.s6_addr[12], false); |
802 | } | 793 | } |
803 | |||
804 | *p = '\0'; | 794 | *p = '\0'; |
795 | |||
805 | return p; | 796 | return p; |
806 | } | 797 | } |
807 | 798 | ||
808 | static char *ip6_string(char *p, const char *addr, const char *fmt) | 799 | static char *ip6_string(char *p, const char *addr, const char *fmt) |
809 | { | 800 | { |
810 | int i; | 801 | int i; |
802 | |||
811 | for (i = 0; i < 8; i++) { | 803 | for (i = 0; i < 8; i++) { |
812 | p = pack_hex_byte(p, *addr++); | 804 | p = pack_hex_byte(p, *addr++); |
813 | p = pack_hex_byte(p, *addr++); | 805 | p = pack_hex_byte(p, *addr++); |
814 | if (fmt[0] == 'I' && i != 7) | 806 | if (fmt[0] == 'I' && i != 7) |
815 | *p++ = ':'; | 807 | *p++ = ':'; |
816 | } | 808 | } |
817 | |||
818 | *p = '\0'; | 809 | *p = '\0'; |
810 | |||
819 | return p; | 811 | return p; |
820 | } | 812 | } |
821 | 813 | ||
@@ -842,6 +834,52 @@ static char *ip4_addr_string(char *buf, char *end, const u8 *addr, | |||
842 | return string(buf, end, ip4_addr, spec); | 834 | return string(buf, end, ip4_addr, spec); |
843 | } | 835 | } |
844 | 836 | ||
837 | static char *uuid_string(char *buf, char *end, const u8 *addr, | ||
838 | struct printf_spec spec, const char *fmt) | ||
839 | { | ||
840 | char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; | ||
841 | char *p = uuid; | ||
842 | int i; | ||
843 | static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | ||
844 | static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; | ||
845 | const u8 *index = be; | ||
846 | bool uc = false; | ||
847 | |||
848 | switch (*(++fmt)) { | ||
849 | case 'L': | ||
850 | uc = true; /* fall-through */ | ||
851 | case 'l': | ||
852 | index = le; | ||
853 | break; | ||
854 | case 'B': | ||
855 | uc = true; | ||
856 | break; | ||
857 | } | ||
858 | |||
859 | for (i = 0; i < 16; i++) { | ||
860 | p = pack_hex_byte(p, addr[index[i]]); | ||
861 | switch (i) { | ||
862 | case 3: | ||
863 | case 5: | ||
864 | case 7: | ||
865 | case 9: | ||
866 | *p++ = '-'; | ||
867 | break; | ||
868 | } | ||
869 | } | ||
870 | |||
871 | *p = 0; | ||
872 | |||
873 | if (uc) { | ||
874 | p = uuid; | ||
875 | do { | ||
876 | *p = toupper(*p); | ||
877 | } while (*(++p)); | ||
878 | } | ||
879 | |||
880 | return string(buf, end, uuid, spec); | ||
881 | } | ||
882 | |||
845 | /* | 883 | /* |
846 | * Show a '%p' thing. A kernel extension is that the '%p' is followed | 884 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
847 | * by an extra set of alphanumeric characters that are extended format | 885 | * by an extra set of alphanumeric characters that are extended format |
@@ -865,7 +903,19 @@ static char *ip4_addr_string(char *buf, char *end, const u8 *addr, | |||
865 | * IPv6 omits the colons (01020304...0f) | 903 | * IPv6 omits the colons (01020304...0f) |
866 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) | 904 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
867 | * - 'I6c' for IPv6 addresses printed as specified by | 905 | * - 'I6c' for IPv6 addresses printed as specified by |
868 | * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt | 906 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 |
907 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form | ||
908 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
909 | * Options for %pU are: | ||
910 | * b big endian lower case hex (default) | ||
911 | * B big endian UPPER case hex | ||
912 | * l little endian lower case hex | ||
913 | * L little endian UPPER case hex | ||
914 | * big endian output byte order is: | ||
915 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] | ||
916 | * little endian output byte order is: | ||
917 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] | ||
918 | * | ||
869 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 | 919 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
870 | * function pointers are really function descriptors, which contain a | 920 | * function pointers are really function descriptors, which contain a |
871 | * pointer to the real address. | 921 | * pointer to the real address. |
@@ -880,9 +930,9 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |||
880 | case 'F': | 930 | case 'F': |
881 | case 'f': | 931 | case 'f': |
882 | ptr = dereference_function_descriptor(ptr); | 932 | ptr = dereference_function_descriptor(ptr); |
883 | case 's': | ||
884 | /* Fallthrough */ | 933 | /* Fallthrough */ |
885 | case 'S': | 934 | case 'S': |
935 | case 's': | ||
886 | return symbol_string(buf, end, ptr, spec, *fmt); | 936 | return symbol_string(buf, end, ptr, spec, *fmt); |
887 | case 'R': | 937 | case 'R': |
888 | case 'r': | 938 | case 'r': |
@@ -906,6 +956,8 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |||
906 | return ip4_addr_string(buf, end, ptr, spec, fmt); | 956 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
907 | } | 957 | } |
908 | break; | 958 | break; |
959 | case 'U': | ||
960 | return uuid_string(buf, end, ptr, spec, fmt); | ||
909 | } | 961 | } |
910 | spec.flags |= SMALL; | 962 | spec.flags |= SMALL; |
911 | if (spec.field_width == -1) { | 963 | if (spec.field_width == -1) { |
@@ -1023,8 +1075,8 @@ precision: | |||
1023 | qualifier: | 1075 | qualifier: |
1024 | /* get the conversion qualifier */ | 1076 | /* get the conversion qualifier */ |
1025 | spec->qualifier = -1; | 1077 | spec->qualifier = -1; |
1026 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | 1078 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
1027 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { | 1079 | TOLOWER(*fmt) == 'z' || *fmt == 't') { |
1028 | spec->qualifier = *fmt++; | 1080 | spec->qualifier = *fmt++; |
1029 | if (unlikely(spec->qualifier == *fmt)) { | 1081 | if (unlikely(spec->qualifier == *fmt)) { |
1030 | if (spec->qualifier == 'l') { | 1082 | if (spec->qualifier == 'l') { |
@@ -1091,7 +1143,7 @@ qualifier: | |||
1091 | spec->type = FORMAT_TYPE_LONG; | 1143 | spec->type = FORMAT_TYPE_LONG; |
1092 | else | 1144 | else |
1093 | spec->type = FORMAT_TYPE_ULONG; | 1145 | spec->type = FORMAT_TYPE_ULONG; |
1094 | } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') { | 1146 | } else if (TOLOWER(spec->qualifier) == 'z') { |
1095 | spec->type = FORMAT_TYPE_SIZE_T; | 1147 | spec->type = FORMAT_TYPE_SIZE_T; |
1096 | } else if (spec->qualifier == 't') { | 1148 | } else if (spec->qualifier == 't') { |
1097 | spec->type = FORMAT_TYPE_PTRDIFF; | 1149 | spec->type = FORMAT_TYPE_PTRDIFF; |
@@ -1127,7 +1179,18 @@ qualifier: | |||
1127 | * %ps output the name of a text symbol without offset | 1179 | * %ps output the name of a text symbol without offset |
1128 | * %pF output the name of a function pointer with its offset | 1180 | * %pF output the name of a function pointer with its offset |
1129 | * %pf output the name of a function pointer without its offset | 1181 | * %pf output the name of a function pointer without its offset |
1130 | * %pR output the address range in a struct resource | 1182 | * %pR output the address range in a struct resource with decoded flags |
1183 | * %pr output the address range in a struct resource with raw flags | ||
1184 | * %pM output a 6-byte MAC address with colons | ||
1185 | * %pm output a 6-byte MAC address without colons | ||
1186 | * %pI4 print an IPv4 address without leading zeros | ||
1187 | * %pi4 print an IPv4 address with leading zeros | ||
1188 | * %pI6 print an IPv6 address with colons | ||
1189 | * %pi6 print an IPv6 address without colons | ||
1190 | * %pI6c print an IPv6 address as specified by | ||
1191 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 | ||
1192 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper | ||
1193 | * case. | ||
1131 | * %n is ignored | 1194 | * %n is ignored |
1132 | * | 1195 | * |
1133 | * The return value is the number of characters which would | 1196 | * The return value is the number of characters which would |
@@ -1144,8 +1207,7 @@ qualifier: | |||
1144 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | 1207 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
1145 | { | 1208 | { |
1146 | unsigned long long num; | 1209 | unsigned long long num; |
1147 | char *str, *end, c; | 1210 | char *str, *end; |
1148 | int read; | ||
1149 | struct printf_spec spec = {0}; | 1211 | struct printf_spec spec = {0}; |
1150 | 1212 | ||
1151 | /* Reject out-of-range values early. Large positive sizes are | 1213 | /* Reject out-of-range values early. Large positive sizes are |
@@ -1164,8 +1226,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1164 | 1226 | ||
1165 | while (*fmt) { | 1227 | while (*fmt) { |
1166 | const char *old_fmt = fmt; | 1228 | const char *old_fmt = fmt; |
1167 | 1229 | int read = format_decode(fmt, &spec); | |
1168 | read = format_decode(fmt, &spec); | ||
1169 | 1230 | ||
1170 | fmt += read; | 1231 | fmt += read; |
1171 | 1232 | ||
@@ -1189,7 +1250,9 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1189 | spec.precision = va_arg(args, int); | 1250 | spec.precision = va_arg(args, int); |
1190 | break; | 1251 | break; |
1191 | 1252 | ||
1192 | case FORMAT_TYPE_CHAR: | 1253 | case FORMAT_TYPE_CHAR: { |
1254 | char c; | ||
1255 | |||
1193 | if (!(spec.flags & LEFT)) { | 1256 | if (!(spec.flags & LEFT)) { |
1194 | while (--spec.field_width > 0) { | 1257 | while (--spec.field_width > 0) { |
1195 | if (str < end) | 1258 | if (str < end) |
@@ -1208,6 +1271,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1208 | ++str; | 1271 | ++str; |
1209 | } | 1272 | } |
1210 | break; | 1273 | break; |
1274 | } | ||
1211 | 1275 | ||
1212 | case FORMAT_TYPE_STR: | 1276 | case FORMAT_TYPE_STR: |
1213 | str = string(str, end, va_arg(args, char *), spec); | 1277 | str = string(str, end, va_arg(args, char *), spec); |
@@ -1238,8 +1302,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1238 | if (qualifier == 'l') { | 1302 | if (qualifier == 'l') { |
1239 | long *ip = va_arg(args, long *); | 1303 | long *ip = va_arg(args, long *); |
1240 | *ip = (str - buf); | 1304 | *ip = (str - buf); |
1241 | } else if (qualifier == 'Z' || | 1305 | } else if (TOLOWER(qualifier) == 'z') { |
1242 | qualifier == 'z') { | ||
1243 | size_t *ip = va_arg(args, size_t *); | 1306 | size_t *ip = va_arg(args, size_t *); |
1244 | *ip = (str - buf); | 1307 | *ip = (str - buf); |
1245 | } else { | 1308 | } else { |
@@ -1322,7 +1385,8 @@ int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1322 | { | 1385 | { |
1323 | int i; | 1386 | int i; |
1324 | 1387 | ||
1325 | i=vsnprintf(buf,size,fmt,args); | 1388 | i = vsnprintf(buf, size, fmt, args); |
1389 | |||
1326 | return (i >= size) ? (size - 1) : i; | 1390 | return (i >= size) ? (size - 1) : i; |
1327 | } | 1391 | } |
1328 | EXPORT_SYMBOL(vscnprintf); | 1392 | EXPORT_SYMBOL(vscnprintf); |
@@ -1341,14 +1405,15 @@ EXPORT_SYMBOL(vscnprintf); | |||
1341 | * | 1405 | * |
1342 | * See the vsnprintf() documentation for format string extensions over C99. | 1406 | * See the vsnprintf() documentation for format string extensions over C99. |
1343 | */ | 1407 | */ |
1344 | int snprintf(char * buf, size_t size, const char *fmt, ...) | 1408 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
1345 | { | 1409 | { |
1346 | va_list args; | 1410 | va_list args; |
1347 | int i; | 1411 | int i; |
1348 | 1412 | ||
1349 | va_start(args, fmt); | 1413 | va_start(args, fmt); |
1350 | i=vsnprintf(buf,size,fmt,args); | 1414 | i = vsnprintf(buf, size, fmt, args); |
1351 | va_end(args); | 1415 | va_end(args); |
1416 | |||
1352 | return i; | 1417 | return i; |
1353 | } | 1418 | } |
1354 | EXPORT_SYMBOL(snprintf); | 1419 | EXPORT_SYMBOL(snprintf); |
@@ -1364,7 +1429,7 @@ EXPORT_SYMBOL(snprintf); | |||
1364 | * the trailing '\0'. If @size is <= 0 the function returns 0. | 1429 | * the trailing '\0'. If @size is <= 0 the function returns 0. |
1365 | */ | 1430 | */ |
1366 | 1431 | ||
1367 | int scnprintf(char * buf, size_t size, const char *fmt, ...) | 1432 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
1368 | { | 1433 | { |
1369 | va_list args; | 1434 | va_list args; |
1370 | int i; | 1435 | int i; |
@@ -1372,6 +1437,7 @@ int scnprintf(char * buf, size_t size, const char *fmt, ...) | |||
1372 | va_start(args, fmt); | 1437 | va_start(args, fmt); |
1373 | i = vsnprintf(buf, size, fmt, args); | 1438 | i = vsnprintf(buf, size, fmt, args); |
1374 | va_end(args); | 1439 | va_end(args); |
1440 | |||
1375 | return (i >= size) ? (size - 1) : i; | 1441 | return (i >= size) ? (size - 1) : i; |
1376 | } | 1442 | } |
1377 | EXPORT_SYMBOL(scnprintf); | 1443 | EXPORT_SYMBOL(scnprintf); |
@@ -1409,14 +1475,15 @@ EXPORT_SYMBOL(vsprintf); | |||
1409 | * | 1475 | * |
1410 | * See the vsnprintf() documentation for format string extensions over C99. | 1476 | * See the vsnprintf() documentation for format string extensions over C99. |
1411 | */ | 1477 | */ |
1412 | int sprintf(char * buf, const char *fmt, ...) | 1478 | int sprintf(char *buf, const char *fmt, ...) |
1413 | { | 1479 | { |
1414 | va_list args; | 1480 | va_list args; |
1415 | int i; | 1481 | int i; |
1416 | 1482 | ||
1417 | va_start(args, fmt); | 1483 | va_start(args, fmt); |
1418 | i=vsnprintf(buf, INT_MAX, fmt, args); | 1484 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1419 | va_end(args); | 1485 | va_end(args); |
1486 | |||
1420 | return i; | 1487 | return i; |
1421 | } | 1488 | } |
1422 | EXPORT_SYMBOL(sprintf); | 1489 | EXPORT_SYMBOL(sprintf); |
@@ -1449,7 +1516,6 @@ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) | |||
1449 | { | 1516 | { |
1450 | struct printf_spec spec = {0}; | 1517 | struct printf_spec spec = {0}; |
1451 | char *str, *end; | 1518 | char *str, *end; |
1452 | int read; | ||
1453 | 1519 | ||
1454 | str = (char *)bin_buf; | 1520 | str = (char *)bin_buf; |
1455 | end = (char *)(bin_buf + size); | 1521 | end = (char *)(bin_buf + size); |
@@ -1474,14 +1540,15 @@ do { \ | |||
1474 | str += sizeof(type); \ | 1540 | str += sizeof(type); \ |
1475 | } while (0) | 1541 | } while (0) |
1476 | 1542 | ||
1477 | |||
1478 | while (*fmt) { | 1543 | while (*fmt) { |
1479 | read = format_decode(fmt, &spec); | 1544 | int read = format_decode(fmt, &spec); |
1480 | 1545 | ||
1481 | fmt += read; | 1546 | fmt += read; |
1482 | 1547 | ||
1483 | switch (spec.type) { | 1548 | switch (spec.type) { |
1484 | case FORMAT_TYPE_NONE: | 1549 | case FORMAT_TYPE_NONE: |
1550 | case FORMAT_TYPE_INVALID: | ||
1551 | case FORMAT_TYPE_PERCENT_CHAR: | ||
1485 | break; | 1552 | break; |
1486 | 1553 | ||
1487 | case FORMAT_TYPE_WIDTH: | 1554 | case FORMAT_TYPE_WIDTH: |
@@ -1496,13 +1563,14 @@ do { \ | |||
1496 | case FORMAT_TYPE_STR: { | 1563 | case FORMAT_TYPE_STR: { |
1497 | const char *save_str = va_arg(args, char *); | 1564 | const char *save_str = va_arg(args, char *); |
1498 | size_t len; | 1565 | size_t len; |
1566 | |||
1499 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE | 1567 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
1500 | || (unsigned long)save_str < PAGE_SIZE) | 1568 | || (unsigned long)save_str < PAGE_SIZE) |
1501 | save_str = "<NULL>"; | 1569 | save_str = "(null)"; |
1502 | len = strlen(save_str); | 1570 | len = strlen(save_str) + 1; |
1503 | if (str + len + 1 < end) | 1571 | if (str + len < end) |
1504 | memcpy(str, save_str, len + 1); | 1572 | memcpy(str, save_str, len); |
1505 | str += len + 1; | 1573 | str += len; |
1506 | break; | 1574 | break; |
1507 | } | 1575 | } |
1508 | 1576 | ||
@@ -1513,19 +1581,13 @@ do { \ | |||
1513 | fmt++; | 1581 | fmt++; |
1514 | break; | 1582 | break; |
1515 | 1583 | ||
1516 | case FORMAT_TYPE_PERCENT_CHAR: | ||
1517 | break; | ||
1518 | |||
1519 | case FORMAT_TYPE_INVALID: | ||
1520 | break; | ||
1521 | |||
1522 | case FORMAT_TYPE_NRCHARS: { | 1584 | case FORMAT_TYPE_NRCHARS: { |
1523 | /* skip %n 's argument */ | 1585 | /* skip %n 's argument */ |
1524 | int qualifier = spec.qualifier; | 1586 | int qualifier = spec.qualifier; |
1525 | void *skip_arg; | 1587 | void *skip_arg; |
1526 | if (qualifier == 'l') | 1588 | if (qualifier == 'l') |
1527 | skip_arg = va_arg(args, long *); | 1589 | skip_arg = va_arg(args, long *); |
1528 | else if (qualifier == 'Z' || qualifier == 'z') | 1590 | else if (TOLOWER(qualifier) == 'z') |
1529 | skip_arg = va_arg(args, size_t *); | 1591 | skip_arg = va_arg(args, size_t *); |
1530 | else | 1592 | else |
1531 | skip_arg = va_arg(args, int *); | 1593 | skip_arg = va_arg(args, int *); |
@@ -1561,8 +1623,8 @@ do { \ | |||
1561 | } | 1623 | } |
1562 | } | 1624 | } |
1563 | } | 1625 | } |
1564 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; | ||
1565 | 1626 | ||
1627 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; | ||
1566 | #undef save_arg | 1628 | #undef save_arg |
1567 | } | 1629 | } |
1568 | EXPORT_SYMBOL_GPL(vbin_printf); | 1630 | EXPORT_SYMBOL_GPL(vbin_printf); |
@@ -1591,11 +1653,9 @@ EXPORT_SYMBOL_GPL(vbin_printf); | |||
1591 | */ | 1653 | */ |
1592 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | 1654 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
1593 | { | 1655 | { |
1594 | unsigned long long num; | ||
1595 | char *str, *end, c; | ||
1596 | const char *args = (const char *)bin_buf; | ||
1597 | |||
1598 | struct printf_spec spec = {0}; | 1656 | struct printf_spec spec = {0}; |
1657 | char *str, *end; | ||
1658 | const char *args = (const char *)bin_buf; | ||
1599 | 1659 | ||
1600 | if (WARN_ON_ONCE((int) size < 0)) | 1660 | if (WARN_ON_ONCE((int) size < 0)) |
1601 | return 0; | 1661 | return 0; |
@@ -1625,10 +1685,8 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1625 | } | 1685 | } |
1626 | 1686 | ||
1627 | while (*fmt) { | 1687 | while (*fmt) { |
1628 | int read; | ||
1629 | const char *old_fmt = fmt; | 1688 | const char *old_fmt = fmt; |
1630 | 1689 | int read = format_decode(fmt, &spec); | |
1631 | read = format_decode(fmt, &spec); | ||
1632 | 1690 | ||
1633 | fmt += read; | 1691 | fmt += read; |
1634 | 1692 | ||
@@ -1652,7 +1710,9 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1652 | spec.precision = get_arg(int); | 1710 | spec.precision = get_arg(int); |
1653 | break; | 1711 | break; |
1654 | 1712 | ||
1655 | case FORMAT_TYPE_CHAR: | 1713 | case FORMAT_TYPE_CHAR: { |
1714 | char c; | ||
1715 | |||
1656 | if (!(spec.flags & LEFT)) { | 1716 | if (!(spec.flags & LEFT)) { |
1657 | while (--spec.field_width > 0) { | 1717 | while (--spec.field_width > 0) { |
1658 | if (str < end) | 1718 | if (str < end) |
@@ -1670,11 +1730,11 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1670 | ++str; | 1730 | ++str; |
1671 | } | 1731 | } |
1672 | break; | 1732 | break; |
1733 | } | ||
1673 | 1734 | ||
1674 | case FORMAT_TYPE_STR: { | 1735 | case FORMAT_TYPE_STR: { |
1675 | const char *str_arg = args; | 1736 | const char *str_arg = args; |
1676 | size_t len = strlen(str_arg); | 1737 | args += strlen(str_arg) + 1; |
1677 | args += len + 1; | ||
1678 | str = string(str, end, (char *)str_arg, spec); | 1738 | str = string(str, end, (char *)str_arg, spec); |
1679 | break; | 1739 | break; |
1680 | } | 1740 | } |
@@ -1686,11 +1746,6 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1686 | break; | 1746 | break; |
1687 | 1747 | ||
1688 | case FORMAT_TYPE_PERCENT_CHAR: | 1748 | case FORMAT_TYPE_PERCENT_CHAR: |
1689 | if (str < end) | ||
1690 | *str = '%'; | ||
1691 | ++str; | ||
1692 | break; | ||
1693 | |||
1694 | case FORMAT_TYPE_INVALID: | 1749 | case FORMAT_TYPE_INVALID: |
1695 | if (str < end) | 1750 | if (str < end) |
1696 | *str = '%'; | 1751 | *str = '%'; |
@@ -1701,15 +1756,15 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1701 | /* skip */ | 1756 | /* skip */ |
1702 | break; | 1757 | break; |
1703 | 1758 | ||
1704 | default: | 1759 | default: { |
1760 | unsigned long long num; | ||
1761 | |||
1705 | switch (spec.type) { | 1762 | switch (spec.type) { |
1706 | 1763 | ||
1707 | case FORMAT_TYPE_LONG_LONG: | 1764 | case FORMAT_TYPE_LONG_LONG: |
1708 | num = get_arg(long long); | 1765 | num = get_arg(long long); |
1709 | break; | 1766 | break; |
1710 | case FORMAT_TYPE_ULONG: | 1767 | case FORMAT_TYPE_ULONG: |
1711 | num = get_arg(unsigned long); | ||
1712 | break; | ||
1713 | case FORMAT_TYPE_LONG: | 1768 | case FORMAT_TYPE_LONG: |
1714 | num = get_arg(unsigned long); | 1769 | num = get_arg(unsigned long); |
1715 | break; | 1770 | break; |
@@ -1739,8 +1794,9 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1739 | } | 1794 | } |
1740 | 1795 | ||
1741 | str = number(str, end, num, spec); | 1796 | str = number(str, end, num, spec); |
1742 | } | 1797 | } /* default: */ |
1743 | } | 1798 | } /* switch(spec.type) */ |
1799 | } /* while(*fmt) */ | ||
1744 | 1800 | ||
1745 | if (size > 0) { | 1801 | if (size > 0) { |
1746 | if (str < end) | 1802 | if (str < end) |
@@ -1774,6 +1830,7 @@ int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) | |||
1774 | va_start(args, fmt); | 1830 | va_start(args, fmt); |
1775 | ret = vbin_printf(bin_buf, size, fmt, args); | 1831 | ret = vbin_printf(bin_buf, size, fmt, args); |
1776 | va_end(args); | 1832 | va_end(args); |
1833 | |||
1777 | return ret; | 1834 | return ret; |
1778 | } | 1835 | } |
1779 | EXPORT_SYMBOL_GPL(bprintf); | 1836 | EXPORT_SYMBOL_GPL(bprintf); |
@@ -1786,27 +1843,23 @@ EXPORT_SYMBOL_GPL(bprintf); | |||
1786 | * @fmt: format of buffer | 1843 | * @fmt: format of buffer |
1787 | * @args: arguments | 1844 | * @args: arguments |
1788 | */ | 1845 | */ |
1789 | int vsscanf(const char * buf, const char * fmt, va_list args) | 1846 | int vsscanf(const char *buf, const char *fmt, va_list args) |
1790 | { | 1847 | { |
1791 | const char *str = buf; | 1848 | const char *str = buf; |
1792 | char *next; | 1849 | char *next; |
1793 | char digit; | 1850 | char digit; |
1794 | int num = 0; | 1851 | int num = 0; |
1795 | int qualifier; | 1852 | int qualifier, base, field_width; |
1796 | int base; | 1853 | bool is_sign; |
1797 | int field_width; | ||
1798 | int is_sign = 0; | ||
1799 | 1854 | ||
1800 | while(*fmt && *str) { | 1855 | while (*fmt && *str) { |
1801 | /* skip any white space in format */ | 1856 | /* skip any white space in format */ |
1802 | /* white space in format matchs any amount of | 1857 | /* white space in format matchs any amount of |
1803 | * white space, including none, in the input. | 1858 | * white space, including none, in the input. |
1804 | */ | 1859 | */ |
1805 | if (isspace(*fmt)) { | 1860 | if (isspace(*fmt)) { |
1806 | while (isspace(*fmt)) | 1861 | fmt = skip_spaces(++fmt); |
1807 | ++fmt; | 1862 | str = skip_spaces(str); |
1808 | while (isspace(*str)) | ||
1809 | ++str; | ||
1810 | } | 1863 | } |
1811 | 1864 | ||
1812 | /* anything that is not a conversion must match exactly */ | 1865 | /* anything that is not a conversion must match exactly */ |
@@ -1819,7 +1872,7 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1819 | if (!*fmt) | 1872 | if (!*fmt) |
1820 | break; | 1873 | break; |
1821 | ++fmt; | 1874 | ++fmt; |
1822 | 1875 | ||
1823 | /* skip this conversion. | 1876 | /* skip this conversion. |
1824 | * advance both strings to next white space | 1877 | * advance both strings to next white space |
1825 | */ | 1878 | */ |
@@ -1838,8 +1891,8 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1838 | 1891 | ||
1839 | /* get conversion qualifier */ | 1892 | /* get conversion qualifier */ |
1840 | qualifier = -1; | 1893 | qualifier = -1; |
1841 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | 1894 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
1842 | *fmt == 'Z' || *fmt == 'z') { | 1895 | TOLOWER(*fmt) == 'z') { |
1843 | qualifier = *fmt++; | 1896 | qualifier = *fmt++; |
1844 | if (unlikely(qualifier == *fmt)) { | 1897 | if (unlikely(qualifier == *fmt)) { |
1845 | if (qualifier == 'h') { | 1898 | if (qualifier == 'h') { |
@@ -1851,16 +1904,17 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1851 | } | 1904 | } |
1852 | } | 1905 | } |
1853 | } | 1906 | } |
1854 | base = 10; | ||
1855 | is_sign = 0; | ||
1856 | 1907 | ||
1857 | if (!*fmt || !*str) | 1908 | if (!*fmt || !*str) |
1858 | break; | 1909 | break; |
1859 | 1910 | ||
1860 | switch(*fmt++) { | 1911 | base = 10; |
1912 | is_sign = 0; | ||
1913 | |||
1914 | switch (*fmt++) { | ||
1861 | case 'c': | 1915 | case 'c': |
1862 | { | 1916 | { |
1863 | char *s = (char *) va_arg(args,char*); | 1917 | char *s = (char *)va_arg(args, char*); |
1864 | if (field_width == -1) | 1918 | if (field_width == -1) |
1865 | field_width = 1; | 1919 | field_width = 1; |
1866 | do { | 1920 | do { |
@@ -1871,17 +1925,15 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1871 | continue; | 1925 | continue; |
1872 | case 's': | 1926 | case 's': |
1873 | { | 1927 | { |
1874 | char *s = (char *) va_arg(args, char *); | 1928 | char *s = (char *)va_arg(args, char *); |
1875 | if(field_width == -1) | 1929 | if (field_width == -1) |
1876 | field_width = INT_MAX; | 1930 | field_width = INT_MAX; |
1877 | /* first, skip leading white space in buffer */ | 1931 | /* first, skip leading white space in buffer */ |
1878 | while (isspace(*str)) | 1932 | str = skip_spaces(str); |
1879 | str++; | ||
1880 | 1933 | ||
1881 | /* now copy until next white space */ | 1934 | /* now copy until next white space */ |
1882 | while (*str && !isspace(*str) && field_width--) { | 1935 | while (*str && !isspace(*str) && field_width--) |
1883 | *s++ = *str++; | 1936 | *s++ = *str++; |
1884 | } | ||
1885 | *s = '\0'; | 1937 | *s = '\0'; |
1886 | num++; | 1938 | num++; |
1887 | } | 1939 | } |
@@ -1889,7 +1941,7 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1889 | case 'n': | 1941 | case 'n': |
1890 | /* return number of characters read so far */ | 1942 | /* return number of characters read so far */ |
1891 | { | 1943 | { |
1892 | int *i = (int *)va_arg(args,int*); | 1944 | int *i = (int *)va_arg(args, int*); |
1893 | *i = str - buf; | 1945 | *i = str - buf; |
1894 | } | 1946 | } |
1895 | continue; | 1947 | continue; |
@@ -1901,14 +1953,14 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1901 | base = 16; | 1953 | base = 16; |
1902 | break; | 1954 | break; |
1903 | case 'i': | 1955 | case 'i': |
1904 | base = 0; | 1956 | base = 0; |
1905 | case 'd': | 1957 | case 'd': |
1906 | is_sign = 1; | 1958 | is_sign = 1; |
1907 | case 'u': | 1959 | case 'u': |
1908 | break; | 1960 | break; |
1909 | case '%': | 1961 | case '%': |
1910 | /* looking for '%' in str */ | 1962 | /* looking for '%' in str */ |
1911 | if (*str++ != '%') | 1963 | if (*str++ != '%') |
1912 | return num; | 1964 | return num; |
1913 | continue; | 1965 | continue; |
1914 | default: | 1966 | default: |
@@ -1919,71 +1971,70 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1919 | /* have some sort of integer conversion. | 1971 | /* have some sort of integer conversion. |
1920 | * first, skip white space in buffer. | 1972 | * first, skip white space in buffer. |
1921 | */ | 1973 | */ |
1922 | while (isspace(*str)) | 1974 | str = skip_spaces(str); |
1923 | str++; | ||
1924 | 1975 | ||
1925 | digit = *str; | 1976 | digit = *str; |
1926 | if (is_sign && digit == '-') | 1977 | if (is_sign && digit == '-') |
1927 | digit = *(str + 1); | 1978 | digit = *(str + 1); |
1928 | 1979 | ||
1929 | if (!digit | 1980 | if (!digit |
1930 | || (base == 16 && !isxdigit(digit)) | 1981 | || (base == 16 && !isxdigit(digit)) |
1931 | || (base == 10 && !isdigit(digit)) | 1982 | || (base == 10 && !isdigit(digit)) |
1932 | || (base == 8 && (!isdigit(digit) || digit > '7')) | 1983 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
1933 | || (base == 0 && !isdigit(digit))) | 1984 | || (base == 0 && !isdigit(digit))) |
1934 | break; | 1985 | break; |
1935 | 1986 | ||
1936 | switch(qualifier) { | 1987 | switch (qualifier) { |
1937 | case 'H': /* that's 'hh' in format */ | 1988 | case 'H': /* that's 'hh' in format */ |
1938 | if (is_sign) { | 1989 | if (is_sign) { |
1939 | signed char *s = (signed char *) va_arg(args,signed char *); | 1990 | signed char *s = (signed char *)va_arg(args, signed char *); |
1940 | *s = (signed char) simple_strtol(str,&next,base); | 1991 | *s = (signed char)simple_strtol(str, &next, base); |
1941 | } else { | 1992 | } else { |
1942 | unsigned char *s = (unsigned char *) va_arg(args, unsigned char *); | 1993 | unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); |
1943 | *s = (unsigned char) simple_strtoul(str, &next, base); | 1994 | *s = (unsigned char)simple_strtoul(str, &next, base); |
1944 | } | 1995 | } |
1945 | break; | 1996 | break; |
1946 | case 'h': | 1997 | case 'h': |
1947 | if (is_sign) { | 1998 | if (is_sign) { |
1948 | short *s = (short *) va_arg(args,short *); | 1999 | short *s = (short *)va_arg(args, short *); |
1949 | *s = (short) simple_strtol(str,&next,base); | 2000 | *s = (short)simple_strtol(str, &next, base); |
1950 | } else { | 2001 | } else { |
1951 | unsigned short *s = (unsigned short *) va_arg(args, unsigned short *); | 2002 | unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); |
1952 | *s = (unsigned short) simple_strtoul(str, &next, base); | 2003 | *s = (unsigned short)simple_strtoul(str, &next, base); |
1953 | } | 2004 | } |
1954 | break; | 2005 | break; |
1955 | case 'l': | 2006 | case 'l': |
1956 | if (is_sign) { | 2007 | if (is_sign) { |
1957 | long *l = (long *) va_arg(args,long *); | 2008 | long *l = (long *)va_arg(args, long *); |
1958 | *l = simple_strtol(str,&next,base); | 2009 | *l = simple_strtol(str, &next, base); |
1959 | } else { | 2010 | } else { |
1960 | unsigned long *l = (unsigned long*) va_arg(args,unsigned long*); | 2011 | unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); |
1961 | *l = simple_strtoul(str,&next,base); | 2012 | *l = simple_strtoul(str, &next, base); |
1962 | } | 2013 | } |
1963 | break; | 2014 | break; |
1964 | case 'L': | 2015 | case 'L': |
1965 | if (is_sign) { | 2016 | if (is_sign) { |
1966 | long long *l = (long long*) va_arg(args,long long *); | 2017 | long long *l = (long long *)va_arg(args, long long *); |
1967 | *l = simple_strtoll(str,&next,base); | 2018 | *l = simple_strtoll(str, &next, base); |
1968 | } else { | 2019 | } else { |
1969 | unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*); | 2020 | unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); |
1970 | *l = simple_strtoull(str,&next,base); | 2021 | *l = simple_strtoull(str, &next, base); |
1971 | } | 2022 | } |
1972 | break; | 2023 | break; |
1973 | case 'Z': | 2024 | case 'Z': |
1974 | case 'z': | 2025 | case 'z': |
1975 | { | 2026 | { |
1976 | size_t *s = (size_t*) va_arg(args,size_t*); | 2027 | size_t *s = (size_t *)va_arg(args, size_t *); |
1977 | *s = (size_t) simple_strtoul(str,&next,base); | 2028 | *s = (size_t)simple_strtoul(str, &next, base); |
1978 | } | 2029 | } |
1979 | break; | 2030 | break; |
1980 | default: | 2031 | default: |
1981 | if (is_sign) { | 2032 | if (is_sign) { |
1982 | int *i = (int *) va_arg(args, int*); | 2033 | int *i = (int *)va_arg(args, int *); |
1983 | *i = (int) simple_strtol(str,&next,base); | 2034 | *i = (int)simple_strtol(str, &next, base); |
1984 | } else { | 2035 | } else { |
1985 | unsigned int *i = (unsigned int*) va_arg(args, unsigned int*); | 2036 | unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); |
1986 | *i = (unsigned int) simple_strtoul(str,&next,base); | 2037 | *i = (unsigned int)simple_strtoul(str, &next, base); |
1987 | } | 2038 | } |
1988 | break; | 2039 | break; |
1989 | } | 2040 | } |
@@ -2014,14 +2065,15 @@ EXPORT_SYMBOL(vsscanf); | |||
2014 | * @fmt: formatting of buffer | 2065 | * @fmt: formatting of buffer |
2015 | * @...: resulting arguments | 2066 | * @...: resulting arguments |
2016 | */ | 2067 | */ |
2017 | int sscanf(const char * buf, const char * fmt, ...) | 2068 | int sscanf(const char *buf, const char *fmt, ...) |
2018 | { | 2069 | { |
2019 | va_list args; | 2070 | va_list args; |
2020 | int i; | 2071 | int i; |
2021 | 2072 | ||
2022 | va_start(args,fmt); | 2073 | va_start(args, fmt); |
2023 | i = vsscanf(buf,fmt,args); | 2074 | i = vsscanf(buf, fmt, args); |
2024 | va_end(args); | 2075 | va_end(args); |
2076 | |||
2025 | return i; | 2077 | return i; |
2026 | } | 2078 | } |
2027 | EXPORT_SYMBOL(sscanf); | 2079 | EXPORT_SYMBOL(sscanf); |