fed up with those stupid warnings
[mmotm.git] / lib / vsprintf.c
blobee98ace795fca21ffff9595c42ab711f58d05a81
1 /*
2 * linux/lib/vsprintf.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
12 /*
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
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>
25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
27 #include <linux/ioport.h>
28 #include <net/addrconf.h>
30 #include <asm/page.h> /* for PAGE_SIZE */
31 #include <asm/div64.h>
32 #include <asm/sections.h> /* for dereference_function_descriptor() */
34 /* Works only for digits and letters, but small and fast */
35 #define TOLOWER(x) ((x) | 0x20)
37 static unsigned int simple_guess_base(const char *cp)
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;
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
55 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
57 unsigned long result = 0;
59 if (!base)
60 base = simple_guess_base(cp);
62 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
63 cp += 2;
65 while (isxdigit(*cp)) {
66 unsigned int value;
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69 if (value >= base)
70 break;
71 result = result * base + value;
72 cp++;
75 if (endp)
76 *endp = (char *)cp;
77 return result;
79 EXPORT_SYMBOL(simple_strtoul);
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
87 long simple_strtol(const char *cp, char **endp, unsigned int base)
89 if(*cp == '-')
90 return -simple_strtoul(cp + 1, endp, base);
91 return simple_strtoul(cp, endp, base);
93 EXPORT_SYMBOL(simple_strtol);
95 /**
96 * simple_strtoull - convert a string to an unsigned long long
97 * @cp: The start of the string
98 * @endp: A pointer to the end of the parsed string will be placed here
99 * @base: The number base to use
101 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
103 unsigned long long result = 0;
105 if (!base)
106 base = simple_guess_base(cp);
108 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
109 cp += 2;
111 while (isxdigit(*cp)) {
112 unsigned int value;
114 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
115 if (value >= base)
116 break;
117 result = result * base + value;
118 cp++;
121 if (endp)
122 *endp = (char *)cp;
123 return result;
125 EXPORT_SYMBOL(simple_strtoull);
128 * simple_strtoll - convert a string to a signed long long
129 * @cp: The start of the string
130 * @endp: A pointer to the end of the parsed string will be placed here
131 * @base: The number base to use
133 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
135 if(*cp=='-')
136 return -simple_strtoull(cp + 1, endp, base);
137 return simple_strtoull(cp, endp, base);
141 * strict_strtoul - convert a string to an unsigned long strictly
142 * @cp: The string to be converted
143 * @base: The number base to use
144 * @res: The converted result value
146 * strict_strtoul converts a string to an unsigned long only if the
147 * string is really an unsigned long string, any string containing
148 * any invalid char at the tail will be rejected and -EINVAL is returned,
149 * only a newline char at the tail is acceptible because people generally
150 * change a module parameter in the following way:
152 * echo 1024 > /sys/module/e1000/parameters/copybreak
154 * echo will append a newline to the tail.
156 * It returns 0 if conversion is successful and *res is set to the converted
157 * value, otherwise it returns -EINVAL and *res is set to 0.
159 * simple_strtoul just ignores the successive invalid characters and
160 * return the converted value of prefix part of the string.
162 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
164 char *tail;
165 unsigned long val;
166 size_t len;
168 *res = 0;
169 len = strlen(cp);
170 if (len == 0)
171 return -EINVAL;
173 val = simple_strtoul(cp, &tail, base);
174 if (tail == cp)
175 return -EINVAL;
176 if ((*tail == '\0') ||
177 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
178 *res = val;
179 return 0;
182 return -EINVAL;
184 EXPORT_SYMBOL(strict_strtoul);
187 * strict_strtol - convert a string to a long strictly
188 * @cp: The string to be converted
189 * @base: The number base to use
190 * @res: The converted result value
192 * strict_strtol is similiar to strict_strtoul, but it allows the first
193 * character of a string is '-'.
195 * It returns 0 if conversion is successful and *res is set to the converted
196 * value, otherwise it returns -EINVAL and *res is set to 0.
198 int strict_strtol(const char *cp, unsigned int base, long *res)
200 int ret;
201 if (*cp == '-') {
202 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
203 if (!ret)
204 *res = -(*res);
205 } else {
206 ret = strict_strtoul(cp, base, (unsigned long *)res);
209 return ret;
211 EXPORT_SYMBOL(strict_strtol);
214 * strict_strtoull - convert a string to an unsigned long long strictly
215 * @cp: The string to be converted
216 * @base: The number base to use
217 * @res: The converted result value
219 * strict_strtoull converts a string to an unsigned long long only if the
220 * string is really an unsigned long long string, any string containing
221 * any invalid char at the tail will be rejected and -EINVAL is returned,
222 * only a newline char at the tail is acceptible because people generally
223 * change a module parameter in the following way:
225 * echo 1024 > /sys/module/e1000/parameters/copybreak
227 * echo will append a newline to the tail of the string.
229 * It returns 0 if conversion is successful and *res is set to the converted
230 * value, otherwise it returns -EINVAL and *res is set to 0.
232 * simple_strtoull just ignores the successive invalid characters and
233 * return the converted value of prefix part of the string.
235 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
237 char *tail;
238 unsigned long long val;
239 size_t len;
241 *res = 0;
242 len = strlen(cp);
243 if (len == 0)
244 return -EINVAL;
246 val = simple_strtoull(cp, &tail, base);
247 if (tail == cp)
248 return -EINVAL;
249 if ((*tail == '\0') ||
250 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
251 *res = val;
252 return 0;
255 return -EINVAL;
257 EXPORT_SYMBOL(strict_strtoull);
260 * strict_strtoll - convert a string to a long long strictly
261 * @cp: The string to be converted
262 * @base: The number base to use
263 * @res: The converted result value
265 * strict_strtoll is similiar to strict_strtoull, but it allows the first
266 * character of a string is '-'.
268 * It returns 0 if conversion is successful and *res is set to the converted
269 * value, otherwise it returns -EINVAL and *res is set to 0.
271 int strict_strtoll(const char *cp, unsigned int base, long long *res)
273 int ret;
274 if (*cp == '-') {
275 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
276 if (!ret)
277 *res = -(*res);
278 } else {
279 ret = strict_strtoull(cp, base, (unsigned long long *)res);
282 return ret;
284 EXPORT_SYMBOL(strict_strtoll);
286 static int skip_atoi(const char **s)
288 int i=0;
290 while (isdigit(**s))
291 i = i*10 + *((*s)++) - '0';
292 return i;
295 /* Decimal conversion is by far the most typical, and is used
296 * for /proc and /sys data. This directly impacts e.g. top performance
297 * with many processes running. We optimize it for speed
298 * using code from
299 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
300 * (with permission from the author, Douglas W. Jones). */
302 /* Formats correctly any integer in [0,99999].
303 * Outputs from one to five digits depending on input.
304 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
305 static char* put_dec_trunc(char *buf, unsigned q)
307 unsigned d3, d2, d1, d0;
308 d1 = (q>>4) & 0xf;
309 d2 = (q>>8) & 0xf;
310 d3 = (q>>12);
312 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
313 q = (d0 * 0xcd) >> 11;
314 d0 = d0 - 10*q;
315 *buf++ = d0 + '0'; /* least significant digit */
316 d1 = q + 9*d3 + 5*d2 + d1;
317 if (d1 != 0) {
318 q = (d1 * 0xcd) >> 11;
319 d1 = d1 - 10*q;
320 *buf++ = d1 + '0'; /* next digit */
322 d2 = q + 2*d2;
323 if ((d2 != 0) || (d3 != 0)) {
324 q = (d2 * 0xd) >> 7;
325 d2 = d2 - 10*q;
326 *buf++ = d2 + '0'; /* next digit */
328 d3 = q + 4*d3;
329 if (d3 != 0) {
330 q = (d3 * 0xcd) >> 11;
331 d3 = d3 - 10*q;
332 *buf++ = d3 + '0'; /* next digit */
333 if (q != 0)
334 *buf++ = q + '0'; /* most sign. digit */
338 return buf;
340 /* Same with if's removed. Always emits five digits */
341 static char* put_dec_full(char *buf, unsigned q)
343 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
344 /* but anyway, gcc produces better code with full-sized ints */
345 unsigned d3, d2, d1, d0;
346 d1 = (q>>4) & 0xf;
347 d2 = (q>>8) & 0xf;
348 d3 = (q>>12);
350 /* Possible ways to approx. divide by 10 */
351 /* gcc -O2 replaces multiply with shifts and adds */
352 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
353 // (x * 0x67) >> 10: 1100111
354 // (x * 0x34) >> 9: 110100 - same
355 // (x * 0x1a) >> 8: 11010 - same
356 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
358 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
359 q = (d0 * 0xcd) >> 11;
360 d0 = d0 - 10*q;
361 *buf++ = d0 + '0';
362 d1 = q + 9*d3 + 5*d2 + d1;
363 q = (d1 * 0xcd) >> 11;
364 d1 = d1 - 10*q;
365 *buf++ = d1 + '0';
367 d2 = q + 2*d2;
368 q = (d2 * 0xd) >> 7;
369 d2 = d2 - 10*q;
370 *buf++ = d2 + '0';
372 d3 = q + 4*d3;
373 q = (d3 * 0xcd) >> 11; /* - shorter code */
374 /* q = (d3 * 0x67) >> 10; - would also work */
375 d3 = d3 - 10*q;
376 *buf++ = d3 + '0';
377 *buf++ = q + '0';
378 return buf;
380 /* No inlining helps gcc to use registers better */
381 static noinline char* put_dec(char *buf, unsigned long long num)
383 while (1) {
384 unsigned rem;
385 if (num < 100000)
386 return put_dec_trunc(buf, num);
387 rem = do_div(num, 100000);
388 buf = put_dec_full(buf, rem);
392 #define ZEROPAD 1 /* pad with zero */
393 #define SIGN 2 /* unsigned/signed long */
394 #define PLUS 4 /* show plus */
395 #define SPACE 8 /* space if plus */
396 #define LEFT 16 /* left justified */
397 #define SMALL 32 /* Must be 32 == 0x20 */
398 #define SPECIAL 64 /* 0x */
400 enum format_type {
401 FORMAT_TYPE_NONE, /* Just a string part */
402 FORMAT_TYPE_WIDTH,
403 FORMAT_TYPE_PRECISION,
404 FORMAT_TYPE_CHAR,
405 FORMAT_TYPE_STR,
406 FORMAT_TYPE_PTR,
407 FORMAT_TYPE_PERCENT_CHAR,
408 FORMAT_TYPE_INVALID,
409 FORMAT_TYPE_LONG_LONG,
410 FORMAT_TYPE_ULONG,
411 FORMAT_TYPE_LONG,
412 FORMAT_TYPE_UBYTE,
413 FORMAT_TYPE_BYTE,
414 FORMAT_TYPE_USHORT,
415 FORMAT_TYPE_SHORT,
416 FORMAT_TYPE_UINT,
417 FORMAT_TYPE_INT,
418 FORMAT_TYPE_NRCHARS,
419 FORMAT_TYPE_SIZE_T,
420 FORMAT_TYPE_PTRDIFF
423 struct printf_spec {
424 enum format_type type;
425 int flags; /* flags to number() */
426 int field_width; /* width of output field */
427 int base;
428 int precision; /* # of digits/chars */
429 int qualifier;
432 static char *number(char *buf, char *end, unsigned long long num,
433 struct printf_spec spec)
435 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
436 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
438 char tmp[66];
439 char sign;
440 char locase;
441 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
442 int i;
444 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
445 * produces same digits or (maybe lowercased) letters */
446 locase = (spec.flags & SMALL);
447 if (spec.flags & LEFT)
448 spec.flags &= ~ZEROPAD;
449 sign = 0;
450 if (spec.flags & SIGN) {
451 if ((signed long long) num < 0) {
452 sign = '-';
453 num = - (signed long long) num;
454 spec.field_width--;
455 } else if (spec.flags & PLUS) {
456 sign = '+';
457 spec.field_width--;
458 } else if (spec.flags & SPACE) {
459 sign = ' ';
460 spec.field_width--;
463 if (need_pfx) {
464 spec.field_width--;
465 if (spec.base == 16)
466 spec.field_width--;
469 /* generate full string in tmp[], in reverse order */
470 i = 0;
471 if (num == 0)
472 tmp[i++] = '0';
473 /* Generic code, for any base:
474 else do {
475 tmp[i++] = (digits[do_div(num,base)] | locase);
476 } while (num != 0);
478 else if (spec.base != 10) { /* 8 or 16 */
479 int mask = spec.base - 1;
480 int shift = 3;
481 if (spec.base == 16) shift = 4;
482 do {
483 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
484 num >>= shift;
485 } while (num);
486 } else { /* base 10 */
487 i = put_dec(tmp, num) - tmp;
490 /* printing 100 using %2d gives "100", not "00" */
491 if (i > spec.precision)
492 spec.precision = i;
493 /* leading space padding */
494 spec.field_width -= spec.precision;
495 if (!(spec.flags & (ZEROPAD+LEFT))) {
496 while(--spec.field_width >= 0) {
497 if (buf < end)
498 *buf = ' ';
499 ++buf;
502 /* sign */
503 if (sign) {
504 if (buf < end)
505 *buf = sign;
506 ++buf;
508 /* "0x" / "0" prefix */
509 if (need_pfx) {
510 if (buf < end)
511 *buf = '0';
512 ++buf;
513 if (spec.base == 16) {
514 if (buf < end)
515 *buf = ('X' | locase);
516 ++buf;
519 /* zero or space padding */
520 if (!(spec.flags & LEFT)) {
521 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
522 while (--spec.field_width >= 0) {
523 if (buf < end)
524 *buf = c;
525 ++buf;
528 /* hmm even more zero padding? */
529 while (i <= --spec.precision) {
530 if (buf < end)
531 *buf = '0';
532 ++buf;
534 /* actual digits of result */
535 while (--i >= 0) {
536 if (buf < end)
537 *buf = tmp[i];
538 ++buf;
540 /* trailing space padding */
541 while (--spec.field_width >= 0) {
542 if (buf < end)
543 *buf = ' ';
544 ++buf;
546 return buf;
549 static char *string(char *buf, char *end, char *s, struct printf_spec spec)
551 int len, i;
553 if ((unsigned long)s < PAGE_SIZE)
554 s = "<NULL>";
556 len = strnlen(s, spec.precision);
558 if (!(spec.flags & LEFT)) {
559 while (len < spec.field_width--) {
560 if (buf < end)
561 *buf = ' ';
562 ++buf;
565 for (i = 0; i < len; ++i) {
566 if (buf < end)
567 *buf = *s;
568 ++buf; ++s;
570 while (len < spec.field_width--) {
571 if (buf < end)
572 *buf = ' ';
573 ++buf;
575 return buf;
578 static char *symbol_string(char *buf, char *end, void *ptr,
579 struct printf_spec spec, char ext)
581 unsigned long value = (unsigned long) ptr;
582 #ifdef CONFIG_KALLSYMS
583 char sym[KSYM_SYMBOL_LEN];
584 if (ext != 'f' && ext != 's')
585 sprint_symbol(sym, value);
586 else
587 kallsyms_lookup(value, NULL, NULL, NULL, sym);
588 return string(buf, end, sym, spec);
589 #else
590 spec.field_width = 2*sizeof(void *);
591 spec.flags |= SPECIAL | SMALL | ZEROPAD;
592 spec.base = 16;
593 return number(buf, end, value, spec);
594 #endif
597 static char *resource_string(char *buf, char *end, struct resource *res,
598 struct printf_spec spec)
600 #ifndef IO_RSRC_PRINTK_SIZE
601 #define IO_RSRC_PRINTK_SIZE 4
602 #endif
604 #ifndef MEM_RSRC_PRINTK_SIZE
605 #define MEM_RSRC_PRINTK_SIZE 8
606 #endif
607 struct printf_spec num_spec = {
608 .base = 16,
609 .precision = -1,
610 .flags = SPECIAL | SMALL | ZEROPAD,
612 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
613 char sym[4*sizeof(resource_size_t) + 8];
614 char *p = sym, *pend = sym + sizeof(sym);
615 int size = -1;
617 if (res->flags & IORESOURCE_IO)
618 size = IO_RSRC_PRINTK_SIZE;
619 else if (res->flags & IORESOURCE_MEM)
620 size = MEM_RSRC_PRINTK_SIZE;
622 *p++ = '[';
623 num_spec.field_width = size;
624 p = number(p, pend, res->start, num_spec);
625 *p++ = '-';
626 p = number(p, pend, res->end, num_spec);
627 *p++ = ']';
628 *p = 0;
630 return string(buf, end, sym, spec);
633 static char *mac_address_string(char *buf, char *end, u8 *addr,
634 struct printf_spec spec, const char *fmt)
636 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
637 char *p = mac_addr;
638 int i;
640 for (i = 0; i < 6; i++) {
641 p = pack_hex_byte(p, addr[i]);
642 if (fmt[0] == 'M' && i != 5)
643 *p++ = ':';
645 *p = '\0';
647 return string(buf, end, mac_addr, spec);
650 static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
652 int i;
654 for (i = 0; i < 4; i++) {
655 char temp[3]; /* hold each IP quad in reverse order */
656 int digits = put_dec_trunc(temp, addr[i]) - temp;
657 if (leading_zeros) {
658 if (digits < 3)
659 *p++ = '0';
660 if (digits < 2)
661 *p++ = '0';
663 /* reverse the digits in the quad */
664 while (digits--)
665 *p++ = temp[digits];
666 if (i < 3)
667 *p++ = '.';
670 *p = '\0';
671 return p;
674 static char *ip6_compressed_string(char *p, const char *addr)
676 int i;
677 int j;
678 int range;
679 unsigned char zerolength[8];
680 int longest = 1;
681 int colonpos = -1;
682 u16 word;
683 u8 hi;
684 u8 lo;
685 bool needcolon = false;
686 bool useIPv4;
687 struct in6_addr in6;
689 memcpy(&in6, addr, sizeof(struct in6_addr));
691 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
693 memset(zerolength, 0, sizeof(zerolength));
695 if (useIPv4)
696 range = 6;
697 else
698 range = 8;
700 /* find position of longest 0 run */
701 for (i = 0; i < range; i++) {
702 for (j = i; j < range; j++) {
703 if (in6.s6_addr16[j] != 0)
704 break;
705 zerolength[i]++;
708 for (i = 0; i < range; i++) {
709 if (zerolength[i] > longest) {
710 longest = zerolength[i];
711 colonpos = i;
715 /* emit address */
716 for (i = 0; i < range; i++) {
717 if (i == colonpos) {
718 if (needcolon || i == 0)
719 *p++ = ':';
720 *p++ = ':';
721 needcolon = false;
722 i += longest - 1;
723 continue;
725 if (needcolon) {
726 *p++ = ':';
727 needcolon = false;
729 /* hex u16 without leading 0s */
730 word = ntohs(in6.s6_addr16[i]);
731 hi = word >> 8;
732 lo = word & 0xff;
733 if (hi) {
734 if (hi > 0x0f)
735 p = pack_hex_byte(p, hi);
736 else
737 *p++ = hex_asc_lo(hi);
739 if (hi || lo > 0x0f)
740 p = pack_hex_byte(p, lo);
741 else
742 *p++ = hex_asc_lo(lo);
743 needcolon = true;
746 if (useIPv4) {
747 if (needcolon)
748 *p++ = ':';
749 p = ip4_string(p, &in6.s6_addr[12], false);
752 *p = '\0';
753 return p;
756 static char *ip6_string(char *p, const char *addr, const char *fmt)
758 int i;
759 for (i = 0; i < 8; i++) {
760 p = pack_hex_byte(p, *addr++);
761 p = pack_hex_byte(p, *addr++);
762 if (fmt[0] == 'I' && i != 7)
763 *p++ = ':';
766 *p = '\0';
767 return p;
770 static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
771 struct printf_spec spec, const char *fmt)
773 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
775 if (fmt[0] == 'I' && fmt[2] == 'c')
776 ip6_compressed_string(ip6_addr, addr);
777 else
778 ip6_string(ip6_addr, addr, fmt);
780 return string(buf, end, ip6_addr, spec);
783 static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
784 struct printf_spec spec, const char *fmt)
786 char ip4_addr[sizeof("255.255.255.255")];
788 ip4_string(ip4_addr, addr, fmt[0] == 'i');
790 return string(buf, end, ip4_addr, spec);
793 static char *uuid_string(char *buf, char *end, const u8 *addr,
794 struct printf_spec spec, const char *fmt)
796 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
797 char *p = uuid;
798 int i;
799 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
800 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
801 const u8 *index = be;
802 bool uc = false;
804 switch (*(++fmt)) {
805 case 'L':
806 uc = true; /* fall-through */
807 case 'l':
808 index = le;
809 break;
810 case 'B':
811 uc = true;
812 break;
815 for (i = 0; i < 16; i++) {
816 p = pack_hex_byte(p, addr[index[i]]);
817 switch (i) {
818 case 3:
819 case 5:
820 case 7:
821 case 9:
822 *p++ = '-';
823 break;
827 *p = 0;
829 if (uc) {
830 p = uuid;
831 do {
832 *p = toupper(*p);
833 } while (*(++p));
836 return string(buf, end, uuid, spec);
840 * Show a '%p' thing. A kernel extension is that the '%p' is followed
841 * by an extra set of alphanumeric characters that are extended format
842 * specifiers.
844 * Right now we handle:
846 * - 'F' For symbolic function descriptor pointers with offset
847 * - 'f' For simple symbolic function names without offset
848 * - 'S' For symbolic direct pointers with offset
849 * - 's' For symbolic direct pointers without offset
850 * - 'R' For a struct resource pointer, it prints the range of
851 * addresses (not the name nor the flags)
852 * - 'M' For a 6-byte MAC address, it prints the address in the
853 * usual colon-separated hex notation
854 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
855 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
856 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
857 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
858 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
859 * IPv6 omits the colons (01020304...0f)
860 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
861 * - 'I6c' for IPv6 addresses printed as specified by
862 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
863 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
864 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
865 * Options for %pU are:
866 * b big endian lower case hex (default)
867 * B big endian UPPER case hex
868 * l little endian lower case hex
869 * L little endian UPPER case hex
870 * big endian output byte order is:
871 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
872 * little endian output byte order is:
873 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
875 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
876 * function pointers are really function descriptors, which contain a
877 * pointer to the real address.
879 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
880 struct printf_spec spec)
882 if (!ptr)
883 return string(buf, end, "(null)", spec);
885 switch (*fmt) {
886 case 'F':
887 case 'f':
888 ptr = dereference_function_descriptor(ptr);
889 /* Fallthrough */
890 case 'S':
891 case 's':
892 return symbol_string(buf, end, ptr, spec, *fmt);
893 case 'R':
894 return resource_string(buf, end, ptr, spec);
895 case 'M': /* Colon separated: 00:01:02:03:04:05 */
896 case 'm': /* Contiguous: 000102030405 */
897 return mac_address_string(buf, end, ptr, spec, fmt);
898 case 'I': /* Formatted IP supported
899 * 4: 1.2.3.4
900 * 6: 0001:0203:...:0708
901 * 6c: 1::708 or 1::1.2.3.4
903 case 'i': /* Contiguous:
904 * 4: 001.002.003.004
905 * 6: 000102...0f
907 switch (fmt[1]) {
908 case '6':
909 return ip6_addr_string(buf, end, ptr, spec, fmt);
910 case '4':
911 return ip4_addr_string(buf, end, ptr, spec, fmt);
913 break;
914 case 'U':
915 return uuid_string(buf, end, ptr, spec, fmt);
917 spec.flags |= SMALL;
918 if (spec.field_width == -1) {
919 spec.field_width = 2*sizeof(void *);
920 spec.flags |= ZEROPAD;
922 spec.base = 16;
924 return number(buf, end, (unsigned long) ptr, spec);
928 * Helper function to decode printf style format.
929 * Each call decode a token from the format and return the
930 * number of characters read (or likely the delta where it wants
931 * to go on the next call).
932 * The decoded token is returned through the parameters
934 * 'h', 'l', or 'L' for integer fields
935 * 'z' support added 23/7/1999 S.H.
936 * 'z' changed to 'Z' --davidm 1/25/99
937 * 't' added for ptrdiff_t
939 * @fmt: the format string
940 * @type of the token returned
941 * @flags: various flags such as +, -, # tokens..
942 * @field_width: overwritten width
943 * @base: base of the number (octal, hex, ...)
944 * @precision: precision of a number
945 * @qualifier: qualifier of a number (long, size_t, ...)
947 static int format_decode(const char *fmt, struct printf_spec *spec)
949 const char *start = fmt;
951 /* we finished early by reading the field width */
952 if (spec->type == FORMAT_TYPE_WIDTH) {
953 if (spec->field_width < 0) {
954 spec->field_width = -spec->field_width;
955 spec->flags |= LEFT;
957 spec->type = FORMAT_TYPE_NONE;
958 goto precision;
961 /* we finished early by reading the precision */
962 if (spec->type == FORMAT_TYPE_PRECISION) {
963 if (spec->precision < 0)
964 spec->precision = 0;
966 spec->type = FORMAT_TYPE_NONE;
967 goto qualifier;
970 /* By default */
971 spec->type = FORMAT_TYPE_NONE;
973 for (; *fmt ; ++fmt) {
974 if (*fmt == '%')
975 break;
978 /* Return the current non-format string */
979 if (fmt != start || !*fmt)
980 return fmt - start;
982 /* Process flags */
983 spec->flags = 0;
985 while (1) { /* this also skips first '%' */
986 bool found = true;
988 ++fmt;
990 switch (*fmt) {
991 case '-': spec->flags |= LEFT; break;
992 case '+': spec->flags |= PLUS; break;
993 case ' ': spec->flags |= SPACE; break;
994 case '#': spec->flags |= SPECIAL; break;
995 case '0': spec->flags |= ZEROPAD; break;
996 default: found = false;
999 if (!found)
1000 break;
1003 /* get field width */
1004 spec->field_width = -1;
1006 if (isdigit(*fmt))
1007 spec->field_width = skip_atoi(&fmt);
1008 else if (*fmt == '*') {
1009 /* it's the next argument */
1010 spec->type = FORMAT_TYPE_WIDTH;
1011 return ++fmt - start;
1014 precision:
1015 /* get the precision */
1016 spec->precision = -1;
1017 if (*fmt == '.') {
1018 ++fmt;
1019 if (isdigit(*fmt)) {
1020 spec->precision = skip_atoi(&fmt);
1021 if (spec->precision < 0)
1022 spec->precision = 0;
1023 } else if (*fmt == '*') {
1024 /* it's the next argument */
1025 spec->type = FORMAT_TYPE_PRECISION;
1026 return ++fmt - start;
1030 qualifier:
1031 /* get the conversion qualifier */
1032 spec->qualifier = -1;
1033 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1034 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
1035 spec->qualifier = *fmt++;
1036 if (unlikely(spec->qualifier == *fmt)) {
1037 if (spec->qualifier == 'l') {
1038 spec->qualifier = 'L';
1039 ++fmt;
1040 } else if (spec->qualifier == 'h') {
1041 spec->qualifier = 'H';
1042 ++fmt;
1047 /* default base */
1048 spec->base = 10;
1049 switch (*fmt) {
1050 case 'c':
1051 spec->type = FORMAT_TYPE_CHAR;
1052 return ++fmt - start;
1054 case 's':
1055 spec->type = FORMAT_TYPE_STR;
1056 return ++fmt - start;
1058 case 'p':
1059 spec->type = FORMAT_TYPE_PTR;
1060 return fmt - start;
1061 /* skip alnum */
1063 case 'n':
1064 spec->type = FORMAT_TYPE_NRCHARS;
1065 return ++fmt - start;
1067 case '%':
1068 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1069 return ++fmt - start;
1071 /* integer number formats - set up the flags and "break" */
1072 case 'o':
1073 spec->base = 8;
1074 break;
1076 case 'x':
1077 spec->flags |= SMALL;
1079 case 'X':
1080 spec->base = 16;
1081 break;
1083 case 'd':
1084 case 'i':
1085 spec->flags |= SIGN;
1086 case 'u':
1087 break;
1089 default:
1090 spec->type = FORMAT_TYPE_INVALID;
1091 return fmt - start;
1094 if (spec->qualifier == 'L')
1095 spec->type = FORMAT_TYPE_LONG_LONG;
1096 else if (spec->qualifier == 'l') {
1097 if (spec->flags & SIGN)
1098 spec->type = FORMAT_TYPE_LONG;
1099 else
1100 spec->type = FORMAT_TYPE_ULONG;
1101 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1102 spec->type = FORMAT_TYPE_SIZE_T;
1103 } else if (spec->qualifier == 't') {
1104 spec->type = FORMAT_TYPE_PTRDIFF;
1105 } else if (spec->qualifier == 'H') {
1106 if (spec->flags & SIGN)
1107 spec->type = FORMAT_TYPE_BYTE;
1108 else
1109 spec->type = FORMAT_TYPE_UBYTE;
1110 } else if (spec->qualifier == 'h') {
1111 if (spec->flags & SIGN)
1112 spec->type = FORMAT_TYPE_SHORT;
1113 else
1114 spec->type = FORMAT_TYPE_USHORT;
1115 } else {
1116 if (spec->flags & SIGN)
1117 spec->type = FORMAT_TYPE_INT;
1118 else
1119 spec->type = FORMAT_TYPE_UINT;
1122 return ++fmt - start;
1126 * vsnprintf - Format a string and place it in a buffer
1127 * @buf: The buffer to place the result into
1128 * @size: The size of the buffer, including the trailing null space
1129 * @fmt: The format string to use
1130 * @args: Arguments for the format string
1132 * This function follows C99 vsnprintf, but has some extensions:
1133 * %pS output the name of a text symbol with offset
1134 * %ps output the name of a text symbol without offset
1135 * %pF output the name of a function pointer with its offset
1136 * %pf output the name of a function pointer without its offset
1137 * %pR output the address range in a struct resource
1138 * %n is ignored
1140 * The return value is the number of characters which would
1141 * be generated for the given input, excluding the trailing
1142 * '\0', as per ISO C99. If you want to have the exact
1143 * number of characters written into @buf as return value
1144 * (not including the trailing '\0'), use vscnprintf(). If the
1145 * return is greater than or equal to @size, the resulting
1146 * string is truncated.
1148 * Call this function if you are already dealing with a va_list.
1149 * You probably want snprintf() instead.
1151 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1153 unsigned long long num;
1154 char *str, *end, c;
1155 int read;
1156 struct printf_spec spec = {0};
1158 /* Reject out-of-range values early. Large positive sizes are
1159 used for unknown buffer sizes. */
1160 if (WARN_ON_ONCE((int) size < 0))
1161 return 0;
1163 str = buf;
1164 end = buf + size;
1166 /* Make sure end is always >= buf */
1167 if (end < buf) {
1168 end = ((void *)-1);
1169 size = end - buf;
1172 while (*fmt) {
1173 const char *old_fmt = fmt;
1175 read = format_decode(fmt, &spec);
1177 fmt += read;
1179 switch (spec.type) {
1180 case FORMAT_TYPE_NONE: {
1181 int copy = read;
1182 if (str < end) {
1183 if (copy > end - str)
1184 copy = end - str;
1185 memcpy(str, old_fmt, copy);
1187 str += read;
1188 break;
1191 case FORMAT_TYPE_WIDTH:
1192 spec.field_width = va_arg(args, int);
1193 break;
1195 case FORMAT_TYPE_PRECISION:
1196 spec.precision = va_arg(args, int);
1197 break;
1199 case FORMAT_TYPE_CHAR:
1200 if (!(spec.flags & LEFT)) {
1201 while (--spec.field_width > 0) {
1202 if (str < end)
1203 *str = ' ';
1204 ++str;
1208 c = (unsigned char) va_arg(args, int);
1209 if (str < end)
1210 *str = c;
1211 ++str;
1212 while (--spec.field_width > 0) {
1213 if (str < end)
1214 *str = ' ';
1215 ++str;
1217 break;
1219 case FORMAT_TYPE_STR:
1220 str = string(str, end, va_arg(args, char *), spec);
1221 break;
1223 case FORMAT_TYPE_PTR:
1224 str = pointer(fmt+1, str, end, va_arg(args, void *),
1225 spec);
1226 while (isalnum(*fmt))
1227 fmt++;
1228 break;
1230 case FORMAT_TYPE_PERCENT_CHAR:
1231 if (str < end)
1232 *str = '%';
1233 ++str;
1234 break;
1236 case FORMAT_TYPE_INVALID:
1237 if (str < end)
1238 *str = '%';
1239 ++str;
1240 break;
1242 case FORMAT_TYPE_NRCHARS: {
1243 int qualifier = spec.qualifier;
1245 if (qualifier == 'l') {
1246 long *ip = va_arg(args, long *);
1247 *ip = (str - buf);
1248 } else if (qualifier == 'Z' ||
1249 qualifier == 'z') {
1250 size_t *ip = va_arg(args, size_t *);
1251 *ip = (str - buf);
1252 } else {
1253 int *ip = va_arg(args, int *);
1254 *ip = (str - buf);
1256 break;
1259 default:
1260 switch (spec.type) {
1261 case FORMAT_TYPE_LONG_LONG:
1262 num = va_arg(args, long long);
1263 break;
1264 case FORMAT_TYPE_ULONG:
1265 num = va_arg(args, unsigned long);
1266 break;
1267 case FORMAT_TYPE_LONG:
1268 num = va_arg(args, long);
1269 break;
1270 case FORMAT_TYPE_SIZE_T:
1271 num = va_arg(args, size_t);
1272 break;
1273 case FORMAT_TYPE_PTRDIFF:
1274 num = va_arg(args, ptrdiff_t);
1275 break;
1276 case FORMAT_TYPE_UBYTE:
1277 num = (unsigned char) va_arg(args, int);
1278 break;
1279 case FORMAT_TYPE_BYTE:
1280 num = (signed char) va_arg(args, int);
1281 break;
1282 case FORMAT_TYPE_USHORT:
1283 num = (unsigned short) va_arg(args, int);
1284 break;
1285 case FORMAT_TYPE_SHORT:
1286 num = (short) va_arg(args, int);
1287 break;
1288 case FORMAT_TYPE_INT:
1289 num = (int) va_arg(args, int);
1290 break;
1291 default:
1292 num = va_arg(args, unsigned int);
1295 str = number(str, end, num, spec);
1299 if (size > 0) {
1300 if (str < end)
1301 *str = '\0';
1302 else
1303 end[-1] = '\0';
1306 /* the trailing null byte doesn't count towards the total */
1307 return str-buf;
1310 EXPORT_SYMBOL(vsnprintf);
1313 * vscnprintf - Format a string and place it in a buffer
1314 * @buf: The buffer to place the result into
1315 * @size: The size of the buffer, including the trailing null space
1316 * @fmt: The format string to use
1317 * @args: Arguments for the format string
1319 * The return value is the number of characters which have been written into
1320 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1321 * returns 0.
1323 * Call this function if you are already dealing with a va_list.
1324 * You probably want scnprintf() instead.
1326 * See the vsnprintf() documentation for format string extensions over C99.
1328 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1330 int i;
1332 i=vsnprintf(buf,size,fmt,args);
1333 return (i >= size) ? (size - 1) : i;
1335 EXPORT_SYMBOL(vscnprintf);
1338 * snprintf - Format a string and place it in a buffer
1339 * @buf: The buffer to place the result into
1340 * @size: The size of the buffer, including the trailing null space
1341 * @fmt: The format string to use
1342 * @...: Arguments for the format string
1344 * The return value is the number of characters which would be
1345 * generated for the given input, excluding the trailing null,
1346 * as per ISO C99. If the return is greater than or equal to
1347 * @size, the resulting string is truncated.
1349 * See the vsnprintf() documentation for format string extensions over C99.
1351 int snprintf(char * buf, size_t size, const char *fmt, ...)
1353 va_list args;
1354 int i;
1356 va_start(args, fmt);
1357 i=vsnprintf(buf,size,fmt,args);
1358 va_end(args);
1359 return i;
1361 EXPORT_SYMBOL(snprintf);
1364 * scnprintf - Format a string and place it in a buffer
1365 * @buf: The buffer to place the result into
1366 * @size: The size of the buffer, including the trailing null space
1367 * @fmt: The format string to use
1368 * @...: Arguments for the format string
1370 * The return value is the number of characters written into @buf not including
1371 * the trailing '\0'. If @size is <= 0 the function returns 0.
1374 int scnprintf(char * buf, size_t size, const char *fmt, ...)
1376 va_list args;
1377 int i;
1379 va_start(args, fmt);
1380 i = vsnprintf(buf, size, fmt, args);
1381 va_end(args);
1382 return (i >= size) ? (size - 1) : i;
1384 EXPORT_SYMBOL(scnprintf);
1387 * vsprintf - Format a string and place it in a buffer
1388 * @buf: The buffer to place the result into
1389 * @fmt: The format string to use
1390 * @args: Arguments for the format string
1392 * The function returns the number of characters written
1393 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1394 * buffer overflows.
1396 * Call this function if you are already dealing with a va_list.
1397 * You probably want sprintf() instead.
1399 * See the vsnprintf() documentation for format string extensions over C99.
1401 int vsprintf(char *buf, const char *fmt, va_list args)
1403 return vsnprintf(buf, INT_MAX, fmt, args);
1405 EXPORT_SYMBOL(vsprintf);
1408 * sprintf - Format a string and place it in a buffer
1409 * @buf: The buffer to place the result into
1410 * @fmt: The format string to use
1411 * @...: Arguments for the format string
1413 * The function returns the number of characters written
1414 * into @buf. Use snprintf() or scnprintf() in order to avoid
1415 * buffer overflows.
1417 * See the vsnprintf() documentation for format string extensions over C99.
1419 int sprintf(char * buf, const char *fmt, ...)
1421 va_list args;
1422 int i;
1424 va_start(args, fmt);
1425 i=vsnprintf(buf, INT_MAX, fmt, args);
1426 va_end(args);
1427 return i;
1429 EXPORT_SYMBOL(sprintf);
1431 #ifdef CONFIG_BINARY_PRINTF
1433 * bprintf service:
1434 * vbin_printf() - VA arguments to binary data
1435 * bstr_printf() - Binary data to text string
1439 * vbin_printf - Parse a format string and place args' binary value in a buffer
1440 * @bin_buf: The buffer to place args' binary value
1441 * @size: The size of the buffer(by words(32bits), not characters)
1442 * @fmt: The format string to use
1443 * @args: Arguments for the format string
1445 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1446 * is skiped.
1448 * The return value is the number of words(32bits) which would be generated for
1449 * the given input.
1451 * NOTE:
1452 * If the return value is greater than @size, the resulting bin_buf is NOT
1453 * valid for bstr_printf().
1455 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1457 struct printf_spec spec = {0};
1458 char *str, *end;
1459 int read;
1461 str = (char *)bin_buf;
1462 end = (char *)(bin_buf + size);
1464 #define save_arg(type) \
1465 do { \
1466 if (sizeof(type) == 8) { \
1467 unsigned long long value; \
1468 str = PTR_ALIGN(str, sizeof(u32)); \
1469 value = va_arg(args, unsigned long long); \
1470 if (str + sizeof(type) <= end) { \
1471 *(u32 *)str = *(u32 *)&value; \
1472 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1474 } else { \
1475 unsigned long value; \
1476 str = PTR_ALIGN(str, sizeof(type)); \
1477 value = va_arg(args, int); \
1478 if (str + sizeof(type) <= end) \
1479 *(typeof(type) *)str = (type)value; \
1481 str += sizeof(type); \
1482 } while (0)
1485 while (*fmt) {
1486 read = format_decode(fmt, &spec);
1488 fmt += read;
1490 switch (spec.type) {
1491 case FORMAT_TYPE_NONE:
1492 break;
1494 case FORMAT_TYPE_WIDTH:
1495 case FORMAT_TYPE_PRECISION:
1496 save_arg(int);
1497 break;
1499 case FORMAT_TYPE_CHAR:
1500 save_arg(char);
1501 break;
1503 case FORMAT_TYPE_STR: {
1504 const char *save_str = va_arg(args, char *);
1505 size_t len;
1506 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1507 || (unsigned long)save_str < PAGE_SIZE)
1508 save_str = "<NULL>";
1509 len = strlen(save_str);
1510 if (str + len + 1 < end)
1511 memcpy(str, save_str, len + 1);
1512 str += len + 1;
1513 break;
1516 case FORMAT_TYPE_PTR:
1517 save_arg(void *);
1518 /* skip all alphanumeric pointer suffixes */
1519 while (isalnum(*fmt))
1520 fmt++;
1521 break;
1523 case FORMAT_TYPE_PERCENT_CHAR:
1524 break;
1526 case FORMAT_TYPE_INVALID:
1527 break;
1529 case FORMAT_TYPE_NRCHARS: {
1530 /* skip %n 's argument */
1531 int qualifier = spec.qualifier;
1532 void *skip_arg;
1533 if (qualifier == 'l')
1534 skip_arg = va_arg(args, long *);
1535 else if (qualifier == 'Z' || qualifier == 'z')
1536 skip_arg = va_arg(args, size_t *);
1537 else
1538 skip_arg = va_arg(args, int *);
1539 break;
1542 default:
1543 switch (spec.type) {
1545 case FORMAT_TYPE_LONG_LONG:
1546 save_arg(long long);
1547 break;
1548 case FORMAT_TYPE_ULONG:
1549 case FORMAT_TYPE_LONG:
1550 save_arg(unsigned long);
1551 break;
1552 case FORMAT_TYPE_SIZE_T:
1553 save_arg(size_t);
1554 break;
1555 case FORMAT_TYPE_PTRDIFF:
1556 save_arg(ptrdiff_t);
1557 break;
1558 case FORMAT_TYPE_UBYTE:
1559 case FORMAT_TYPE_BYTE:
1560 save_arg(char);
1561 break;
1562 case FORMAT_TYPE_USHORT:
1563 case FORMAT_TYPE_SHORT:
1564 save_arg(short);
1565 break;
1566 default:
1567 save_arg(int);
1571 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1573 #undef save_arg
1575 EXPORT_SYMBOL_GPL(vbin_printf);
1578 * bstr_printf - Format a string from binary arguments and place it in a buffer
1579 * @buf: The buffer to place the result into
1580 * @size: The size of the buffer, including the trailing null space
1581 * @fmt: The format string to use
1582 * @bin_buf: Binary arguments for the format string
1584 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1585 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1586 * a binary buffer that generated by vbin_printf.
1588 * The format follows C99 vsnprintf, but has some extensions:
1589 * see vsnprintf comment for details.
1591 * The return value is the number of characters which would
1592 * be generated for the given input, excluding the trailing
1593 * '\0', as per ISO C99. If you want to have the exact
1594 * number of characters written into @buf as return value
1595 * (not including the trailing '\0'), use vscnprintf(). If the
1596 * return is greater than or equal to @size, the resulting
1597 * string is truncated.
1599 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1601 unsigned long long num;
1602 char *str, *end, c;
1603 const char *args = (const char *)bin_buf;
1605 struct printf_spec spec = {0};
1607 if (WARN_ON_ONCE((int) size < 0))
1608 return 0;
1610 str = buf;
1611 end = buf + size;
1613 #define get_arg(type) \
1614 ({ \
1615 typeof(type) value; \
1616 if (sizeof(type) == 8) { \
1617 args = PTR_ALIGN(args, sizeof(u32)); \
1618 *(u32 *)&value = *(u32 *)args; \
1619 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1620 } else { \
1621 args = PTR_ALIGN(args, sizeof(type)); \
1622 value = *(typeof(type) *)args; \
1624 args += sizeof(type); \
1625 value; \
1628 /* Make sure end is always >= buf */
1629 if (end < buf) {
1630 end = ((void *)-1);
1631 size = end - buf;
1634 while (*fmt) {
1635 int read;
1636 const char *old_fmt = fmt;
1638 read = format_decode(fmt, &spec);
1640 fmt += read;
1642 switch (spec.type) {
1643 case FORMAT_TYPE_NONE: {
1644 int copy = read;
1645 if (str < end) {
1646 if (copy > end - str)
1647 copy = end - str;
1648 memcpy(str, old_fmt, copy);
1650 str += read;
1651 break;
1654 case FORMAT_TYPE_WIDTH:
1655 spec.field_width = get_arg(int);
1656 break;
1658 case FORMAT_TYPE_PRECISION:
1659 spec.precision = get_arg(int);
1660 break;
1662 case FORMAT_TYPE_CHAR:
1663 if (!(spec.flags & LEFT)) {
1664 while (--spec.field_width > 0) {
1665 if (str < end)
1666 *str = ' ';
1667 ++str;
1670 c = (unsigned char) get_arg(char);
1671 if (str < end)
1672 *str = c;
1673 ++str;
1674 while (--spec.field_width > 0) {
1675 if (str < end)
1676 *str = ' ';
1677 ++str;
1679 break;
1681 case FORMAT_TYPE_STR: {
1682 const char *str_arg = args;
1683 size_t len = strlen(str_arg);
1684 args += len + 1;
1685 str = string(str, end, (char *)str_arg, spec);
1686 break;
1689 case FORMAT_TYPE_PTR:
1690 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1691 while (isalnum(*fmt))
1692 fmt++;
1693 break;
1695 case FORMAT_TYPE_PERCENT_CHAR:
1696 if (str < end)
1697 *str = '%';
1698 ++str;
1699 break;
1701 case FORMAT_TYPE_INVALID:
1702 if (str < end)
1703 *str = '%';
1704 ++str;
1705 break;
1707 case FORMAT_TYPE_NRCHARS:
1708 /* skip */
1709 break;
1711 default:
1712 switch (spec.type) {
1714 case FORMAT_TYPE_LONG_LONG:
1715 num = get_arg(long long);
1716 break;
1717 case FORMAT_TYPE_ULONG:
1718 num = get_arg(unsigned long);
1719 break;
1720 case FORMAT_TYPE_LONG:
1721 num = get_arg(unsigned long);
1722 break;
1723 case FORMAT_TYPE_SIZE_T:
1724 num = get_arg(size_t);
1725 break;
1726 case FORMAT_TYPE_PTRDIFF:
1727 num = get_arg(ptrdiff_t);
1728 break;
1729 case FORMAT_TYPE_UBYTE:
1730 num = get_arg(unsigned char);
1731 break;
1732 case FORMAT_TYPE_BYTE:
1733 num = get_arg(signed char);
1734 break;
1735 case FORMAT_TYPE_USHORT:
1736 num = get_arg(unsigned short);
1737 break;
1738 case FORMAT_TYPE_SHORT:
1739 num = get_arg(short);
1740 break;
1741 case FORMAT_TYPE_UINT:
1742 num = get_arg(unsigned int);
1743 break;
1744 default:
1745 num = get_arg(int);
1748 str = number(str, end, num, spec);
1752 if (size > 0) {
1753 if (str < end)
1754 *str = '\0';
1755 else
1756 end[-1] = '\0';
1759 #undef get_arg
1761 /* the trailing null byte doesn't count towards the total */
1762 return str - buf;
1764 EXPORT_SYMBOL_GPL(bstr_printf);
1767 * bprintf - Parse a format string and place args' binary value in a buffer
1768 * @bin_buf: The buffer to place args' binary value
1769 * @size: The size of the buffer(by words(32bits), not characters)
1770 * @fmt: The format string to use
1771 * @...: Arguments for the format string
1773 * The function returns the number of words(u32) written
1774 * into @bin_buf.
1776 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1778 va_list args;
1779 int ret;
1781 va_start(args, fmt);
1782 ret = vbin_printf(bin_buf, size, fmt, args);
1783 va_end(args);
1784 return ret;
1786 EXPORT_SYMBOL_GPL(bprintf);
1788 #endif /* CONFIG_BINARY_PRINTF */
1791 * vsscanf - Unformat a buffer into a list of arguments
1792 * @buf: input buffer
1793 * @fmt: format of buffer
1794 * @args: arguments
1796 int vsscanf(const char * buf, const char * fmt, va_list args)
1798 const char *str = buf;
1799 char *next;
1800 char digit;
1801 int num = 0;
1802 int qualifier;
1803 int base;
1804 int field_width;
1805 int is_sign = 0;
1807 while(*fmt && *str) {
1808 /* skip any white space in format */
1809 /* white space in format matchs any amount of
1810 * white space, including none, in the input.
1812 if (isspace(*fmt)) {
1813 while (isspace(*fmt))
1814 ++fmt;
1815 while (isspace(*str))
1816 ++str;
1819 /* anything that is not a conversion must match exactly */
1820 if (*fmt != '%' && *fmt) {
1821 if (*fmt++ != *str++)
1822 break;
1823 continue;
1826 if (!*fmt)
1827 break;
1828 ++fmt;
1830 /* skip this conversion.
1831 * advance both strings to next white space
1833 if (*fmt == '*') {
1834 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1835 fmt++;
1836 while (!isspace(*str) && *str)
1837 str++;
1838 continue;
1841 /* get field width */
1842 field_width = -1;
1843 if (isdigit(*fmt))
1844 field_width = skip_atoi(&fmt);
1846 /* get conversion qualifier */
1847 qualifier = -1;
1848 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1849 *fmt == 'Z' || *fmt == 'z') {
1850 qualifier = *fmt++;
1851 if (unlikely(qualifier == *fmt)) {
1852 if (qualifier == 'h') {
1853 qualifier = 'H';
1854 fmt++;
1855 } else if (qualifier == 'l') {
1856 qualifier = 'L';
1857 fmt++;
1861 base = 10;
1862 is_sign = 0;
1864 if (!*fmt || !*str)
1865 break;
1867 switch(*fmt++) {
1868 case 'c':
1870 char *s = (char *) va_arg(args,char*);
1871 if (field_width == -1)
1872 field_width = 1;
1873 do {
1874 *s++ = *str++;
1875 } while (--field_width > 0 && *str);
1876 num++;
1878 continue;
1879 case 's':
1881 char *s = (char *) va_arg(args, char *);
1882 if(field_width == -1)
1883 field_width = INT_MAX;
1884 /* first, skip leading white space in buffer */
1885 while (isspace(*str))
1886 str++;
1888 /* now copy until next white space */
1889 while (*str && !isspace(*str) && field_width--) {
1890 *s++ = *str++;
1892 *s = '\0';
1893 num++;
1895 continue;
1896 case 'n':
1897 /* return number of characters read so far */
1899 int *i = (int *)va_arg(args,int*);
1900 *i = str - buf;
1902 continue;
1903 case 'o':
1904 base = 8;
1905 break;
1906 case 'x':
1907 case 'X':
1908 base = 16;
1909 break;
1910 case 'i':
1911 base = 0;
1912 case 'd':
1913 is_sign = 1;
1914 case 'u':
1915 break;
1916 case '%':
1917 /* looking for '%' in str */
1918 if (*str++ != '%')
1919 return num;
1920 continue;
1921 default:
1922 /* invalid format; stop here */
1923 return num;
1926 /* have some sort of integer conversion.
1927 * first, skip white space in buffer.
1929 while (isspace(*str))
1930 str++;
1932 digit = *str;
1933 if (is_sign && digit == '-')
1934 digit = *(str + 1);
1936 if (!digit
1937 || (base == 16 && !isxdigit(digit))
1938 || (base == 10 && !isdigit(digit))
1939 || (base == 8 && (!isdigit(digit) || digit > '7'))
1940 || (base == 0 && !isdigit(digit)))
1941 break;
1943 switch(qualifier) {
1944 case 'H': /* that's 'hh' in format */
1945 if (is_sign) {
1946 signed char *s = (signed char *) va_arg(args,signed char *);
1947 *s = (signed char) simple_strtol(str,&next,base);
1948 } else {
1949 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1950 *s = (unsigned char) simple_strtoul(str, &next, base);
1952 break;
1953 case 'h':
1954 if (is_sign) {
1955 short *s = (short *) va_arg(args,short *);
1956 *s = (short) simple_strtol(str,&next,base);
1957 } else {
1958 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1959 *s = (unsigned short) simple_strtoul(str, &next, base);
1961 break;
1962 case 'l':
1963 if (is_sign) {
1964 long *l = (long *) va_arg(args,long *);
1965 *l = simple_strtol(str,&next,base);
1966 } else {
1967 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1968 *l = simple_strtoul(str,&next,base);
1970 break;
1971 case 'L':
1972 if (is_sign) {
1973 long long *l = (long long*) va_arg(args,long long *);
1974 *l = simple_strtoll(str,&next,base);
1975 } else {
1976 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1977 *l = simple_strtoull(str,&next,base);
1979 break;
1980 case 'Z':
1981 case 'z':
1983 size_t *s = (size_t*) va_arg(args,size_t*);
1984 *s = (size_t) simple_strtoul(str,&next,base);
1986 break;
1987 default:
1988 if (is_sign) {
1989 int *i = (int *) va_arg(args, int*);
1990 *i = (int) simple_strtol(str,&next,base);
1991 } else {
1992 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1993 *i = (unsigned int) simple_strtoul(str,&next,base);
1995 break;
1997 num++;
1999 if (!next)
2000 break;
2001 str = next;
2005 * Now we've come all the way through so either the input string or the
2006 * format ended. In the former case, there can be a %n at the current
2007 * position in the format that needs to be filled.
2009 if (*fmt == '%' && *(fmt + 1) == 'n') {
2010 int *p = (int *)va_arg(args, int *);
2011 *p = str - buf;
2014 return num;
2016 EXPORT_SYMBOL(vsscanf);
2019 * sscanf - Unformat a buffer into a list of arguments
2020 * @buf: input buffer
2021 * @fmt: formatting of buffer
2022 * @...: resulting arguments
2024 int sscanf(const char * buf, const char * fmt, ...)
2026 va_list args;
2027 int i;
2029 va_start(args,fmt);
2030 i = vsscanf(buf,fmt,args);
2031 va_end(args);
2032 return i;
2034 EXPORT_SYMBOL(sscanf);