xfs: switch to NOFS allocation under i_lock in xfs_da_buf_make
[linux-2.6/linux-2.6-openrd.git] / lib / vsprintf.c
blob7536acea135ba4069c1dc04ad36d5024ac747d8c
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>
29 #include <asm/page.h> /* for PAGE_SIZE */
30 #include <asm/div64.h>
31 #include <asm/sections.h> /* for dereference_function_descriptor() */
33 /* Works only for digits and letters, but small and fast */
34 #define TOLOWER(x) ((x) | 0x20)
36 static unsigned int simple_guess_base(const char *cp)
38 if (cp[0] == '0') {
39 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
40 return 16;
41 else
42 return 8;
43 } else {
44 return 10;
48 /**
49 * simple_strtoul - convert a string to an unsigned long
50 * @cp: The start of the string
51 * @endp: A pointer to the end of the parsed string will be placed here
52 * @base: The number base to use
54 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
56 unsigned long result = 0;
58 if (!base)
59 base = simple_guess_base(cp);
61 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
62 cp += 2;
64 while (isxdigit(*cp)) {
65 unsigned int value;
67 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
68 if (value >= base)
69 break;
70 result = result * base + value;
71 cp++;
74 if (endp)
75 *endp = (char *)cp;
76 return result;
78 EXPORT_SYMBOL(simple_strtoul);
80 /**
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
86 long simple_strtol(const char *cp, char **endp, unsigned int base)
88 if(*cp == '-')
89 return -simple_strtoul(cp + 1, endp, base);
90 return simple_strtoul(cp, endp, base);
92 EXPORT_SYMBOL(simple_strtol);
94 /**
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
100 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
102 unsigned long long result = 0;
104 if (!base)
105 base = simple_guess_base(cp);
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
110 while (isxdigit(*cp)) {
111 unsigned int value;
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
114 if (value >= base)
115 break;
116 result = result * base + value;
117 cp++;
120 if (endp)
121 *endp = (char *)cp;
122 return result;
124 EXPORT_SYMBOL(simple_strtoull);
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
132 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
134 if(*cp=='-')
135 return -simple_strtoull(cp + 1, endp, base);
136 return simple_strtoull(cp, endp, base);
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
153 * echo will append a newline to the tail.
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
161 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
163 char *tail;
164 unsigned long val;
165 size_t len;
167 *res = 0;
168 len = strlen(cp);
169 if (len == 0)
170 return -EINVAL;
172 val = simple_strtoul(cp, &tail, base);
173 if (tail == cp)
174 return -EINVAL;
175 if ((*tail == '\0') ||
176 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
177 *res = val;
178 return 0;
181 return -EINVAL;
183 EXPORT_SYMBOL(strict_strtoul);
186 * strict_strtol - convert a string to a long strictly
187 * @cp: The string to be converted
188 * @base: The number base to use
189 * @res: The converted result value
191 * strict_strtol is similiar to strict_strtoul, but it allows the first
192 * character of a string is '-'.
194 * It returns 0 if conversion is successful and *res is set to the converted
195 * value, otherwise it returns -EINVAL and *res is set to 0.
197 int strict_strtol(const char *cp, unsigned int base, long *res)
199 int ret;
200 if (*cp == '-') {
201 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
202 if (!ret)
203 *res = -(*res);
204 } else {
205 ret = strict_strtoul(cp, base, (unsigned long *)res);
208 return ret;
210 EXPORT_SYMBOL(strict_strtol);
213 * strict_strtoull - convert a string to an unsigned long long strictly
214 * @cp: The string to be converted
215 * @base: The number base to use
216 * @res: The converted result value
218 * strict_strtoull converts a string to an unsigned long long only if the
219 * string is really an unsigned long long string, any string containing
220 * any invalid char at the tail will be rejected and -EINVAL is returned,
221 * only a newline char at the tail is acceptible because people generally
222 * change a module parameter in the following way:
224 * echo 1024 > /sys/module/e1000/parameters/copybreak
226 * echo will append a newline to the tail of the string.
228 * It returns 0 if conversion is successful and *res is set to the converted
229 * value, otherwise it returns -EINVAL and *res is set to 0.
231 * simple_strtoull just ignores the successive invalid characters and
232 * return the converted value of prefix part of the string.
234 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
236 char *tail;
237 unsigned long long val;
238 size_t len;
240 *res = 0;
241 len = strlen(cp);
242 if (len == 0)
243 return -EINVAL;
245 val = simple_strtoull(cp, &tail, base);
246 if (tail == cp)
247 return -EINVAL;
248 if ((*tail == '\0') ||
249 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
250 *res = val;
251 return 0;
254 return -EINVAL;
256 EXPORT_SYMBOL(strict_strtoull);
259 * strict_strtoll - convert a string to a long long strictly
260 * @cp: The string to be converted
261 * @base: The number base to use
262 * @res: The converted result value
264 * strict_strtoll is similiar to strict_strtoull, but it allows the first
265 * character of a string is '-'.
267 * It returns 0 if conversion is successful and *res is set to the converted
268 * value, otherwise it returns -EINVAL and *res is set to 0.
270 int strict_strtoll(const char *cp, unsigned int base, long long *res)
272 int ret;
273 if (*cp == '-') {
274 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
275 if (!ret)
276 *res = -(*res);
277 } else {
278 ret = strict_strtoull(cp, base, (unsigned long long *)res);
281 return ret;
283 EXPORT_SYMBOL(strict_strtoll);
285 static int skip_atoi(const char **s)
287 int i=0;
289 while (isdigit(**s))
290 i = i*10 + *((*s)++) - '0';
291 return i;
294 /* Decimal conversion is by far the most typical, and is used
295 * for /proc and /sys data. This directly impacts e.g. top performance
296 * with many processes running. We optimize it for speed
297 * using code from
298 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
299 * (with permission from the author, Douglas W. Jones). */
301 /* Formats correctly any integer in [0,99999].
302 * Outputs from one to five digits depending on input.
303 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
304 static char* put_dec_trunc(char *buf, unsigned q)
306 unsigned d3, d2, d1, d0;
307 d1 = (q>>4) & 0xf;
308 d2 = (q>>8) & 0xf;
309 d3 = (q>>12);
311 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
312 q = (d0 * 0xcd) >> 11;
313 d0 = d0 - 10*q;
314 *buf++ = d0 + '0'; /* least significant digit */
315 d1 = q + 9*d3 + 5*d2 + d1;
316 if (d1 != 0) {
317 q = (d1 * 0xcd) >> 11;
318 d1 = d1 - 10*q;
319 *buf++ = d1 + '0'; /* next digit */
321 d2 = q + 2*d2;
322 if ((d2 != 0) || (d3 != 0)) {
323 q = (d2 * 0xd) >> 7;
324 d2 = d2 - 10*q;
325 *buf++ = d2 + '0'; /* next digit */
327 d3 = q + 4*d3;
328 if (d3 != 0) {
329 q = (d3 * 0xcd) >> 11;
330 d3 = d3 - 10*q;
331 *buf++ = d3 + '0'; /* next digit */
332 if (q != 0)
333 *buf++ = q + '0'; /* most sign. digit */
337 return buf;
339 /* Same with if's removed. Always emits five digits */
340 static char* put_dec_full(char *buf, unsigned q)
342 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
343 /* but anyway, gcc produces better code with full-sized ints */
344 unsigned d3, d2, d1, d0;
345 d1 = (q>>4) & 0xf;
346 d2 = (q>>8) & 0xf;
347 d3 = (q>>12);
349 /* Possible ways to approx. divide by 10 */
350 /* gcc -O2 replaces multiply with shifts and adds */
351 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
352 // (x * 0x67) >> 10: 1100111
353 // (x * 0x34) >> 9: 110100 - same
354 // (x * 0x1a) >> 8: 11010 - same
355 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
357 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
358 q = (d0 * 0xcd) >> 11;
359 d0 = d0 - 10*q;
360 *buf++ = d0 + '0';
361 d1 = q + 9*d3 + 5*d2 + d1;
362 q = (d1 * 0xcd) >> 11;
363 d1 = d1 - 10*q;
364 *buf++ = d1 + '0';
366 d2 = q + 2*d2;
367 q = (d2 * 0xd) >> 7;
368 d2 = d2 - 10*q;
369 *buf++ = d2 + '0';
371 d3 = q + 4*d3;
372 q = (d3 * 0xcd) >> 11; /* - shorter code */
373 /* q = (d3 * 0x67) >> 10; - would also work */
374 d3 = d3 - 10*q;
375 *buf++ = d3 + '0';
376 *buf++ = q + '0';
377 return buf;
379 /* No inlining helps gcc to use registers better */
380 static noinline char* put_dec(char *buf, unsigned long long num)
382 while (1) {
383 unsigned rem;
384 if (num < 100000)
385 return put_dec_trunc(buf, num);
386 rem = do_div(num, 100000);
387 buf = put_dec_full(buf, rem);
391 #define ZEROPAD 1 /* pad with zero */
392 #define SIGN 2 /* unsigned/signed long */
393 #define PLUS 4 /* show plus */
394 #define SPACE 8 /* space if plus */
395 #define LEFT 16 /* left justified */
396 #define SMALL 32 /* Must be 32 == 0x20 */
397 #define SPECIAL 64 /* 0x */
399 enum format_type {
400 FORMAT_TYPE_NONE, /* Just a string part */
401 FORMAT_TYPE_WIDTH,
402 FORMAT_TYPE_PRECISION,
403 FORMAT_TYPE_CHAR,
404 FORMAT_TYPE_STR,
405 FORMAT_TYPE_PTR,
406 FORMAT_TYPE_PERCENT_CHAR,
407 FORMAT_TYPE_INVALID,
408 FORMAT_TYPE_LONG_LONG,
409 FORMAT_TYPE_ULONG,
410 FORMAT_TYPE_LONG,
411 FORMAT_TYPE_USHORT,
412 FORMAT_TYPE_SHORT,
413 FORMAT_TYPE_UINT,
414 FORMAT_TYPE_INT,
415 FORMAT_TYPE_NRCHARS,
416 FORMAT_TYPE_SIZE_T,
417 FORMAT_TYPE_PTRDIFF
420 struct printf_spec {
421 enum format_type type;
422 int flags; /* flags to number() */
423 int field_width; /* width of output field */
424 int base;
425 int precision; /* # of digits/chars */
426 int qualifier;
429 static char *number(char *buf, char *end, unsigned long long num,
430 struct printf_spec spec)
432 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
433 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
435 char tmp[66];
436 char sign;
437 char locase;
438 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
439 int i;
441 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
442 * produces same digits or (maybe lowercased) letters */
443 locase = (spec.flags & SMALL);
444 if (spec.flags & LEFT)
445 spec.flags &= ~ZEROPAD;
446 sign = 0;
447 if (spec.flags & SIGN) {
448 if ((signed long long) num < 0) {
449 sign = '-';
450 num = - (signed long long) num;
451 spec.field_width--;
452 } else if (spec.flags & PLUS) {
453 sign = '+';
454 spec.field_width--;
455 } else if (spec.flags & SPACE) {
456 sign = ' ';
457 spec.field_width--;
460 if (need_pfx) {
461 spec.field_width--;
462 if (spec.base == 16)
463 spec.field_width--;
466 /* generate full string in tmp[], in reverse order */
467 i = 0;
468 if (num == 0)
469 tmp[i++] = '0';
470 /* Generic code, for any base:
471 else do {
472 tmp[i++] = (digits[do_div(num,base)] | locase);
473 } while (num != 0);
475 else if (spec.base != 10) { /* 8 or 16 */
476 int mask = spec.base - 1;
477 int shift = 3;
478 if (spec.base == 16) shift = 4;
479 do {
480 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
481 num >>= shift;
482 } while (num);
483 } else { /* base 10 */
484 i = put_dec(tmp, num) - tmp;
487 /* printing 100 using %2d gives "100", not "00" */
488 if (i > spec.precision)
489 spec.precision = i;
490 /* leading space padding */
491 spec.field_width -= spec.precision;
492 if (!(spec.flags & (ZEROPAD+LEFT))) {
493 while(--spec.field_width >= 0) {
494 if (buf < end)
495 *buf = ' ';
496 ++buf;
499 /* sign */
500 if (sign) {
501 if (buf < end)
502 *buf = sign;
503 ++buf;
505 /* "0x" / "0" prefix */
506 if (need_pfx) {
507 if (buf < end)
508 *buf = '0';
509 ++buf;
510 if (spec.base == 16) {
511 if (buf < end)
512 *buf = ('X' | locase);
513 ++buf;
516 /* zero or space padding */
517 if (!(spec.flags & LEFT)) {
518 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
519 while (--spec.field_width >= 0) {
520 if (buf < end)
521 *buf = c;
522 ++buf;
525 /* hmm even more zero padding? */
526 while (i <= --spec.precision) {
527 if (buf < end)
528 *buf = '0';
529 ++buf;
531 /* actual digits of result */
532 while (--i >= 0) {
533 if (buf < end)
534 *buf = tmp[i];
535 ++buf;
537 /* trailing space padding */
538 while (--spec.field_width >= 0) {
539 if (buf < end)
540 *buf = ' ';
541 ++buf;
543 return buf;
546 static char *string(char *buf, char *end, char *s, struct printf_spec spec)
548 int len, i;
550 if ((unsigned long)s < PAGE_SIZE)
551 s = "<NULL>";
553 len = strnlen(s, spec.precision);
555 if (!(spec.flags & LEFT)) {
556 while (len < spec.field_width--) {
557 if (buf < end)
558 *buf = ' ';
559 ++buf;
562 for (i = 0; i < len; ++i) {
563 if (buf < end)
564 *buf = *s;
565 ++buf; ++s;
567 while (len < spec.field_width--) {
568 if (buf < end)
569 *buf = ' ';
570 ++buf;
572 return buf;
575 static char *symbol_string(char *buf, char *end, void *ptr,
576 struct printf_spec spec)
578 unsigned long value = (unsigned long) ptr;
579 #ifdef CONFIG_KALLSYMS
580 char sym[KSYM_SYMBOL_LEN];
581 sprint_symbol(sym, value);
582 return string(buf, end, sym, spec);
583 #else
584 spec.field_width = 2*sizeof(void *);
585 spec.flags |= SPECIAL | SMALL | ZEROPAD;
586 spec.base = 16;
587 return number(buf, end, value, spec);
588 #endif
591 static char *resource_string(char *buf, char *end, struct resource *res,
592 struct printf_spec spec)
594 #ifndef IO_RSRC_PRINTK_SIZE
595 #define IO_RSRC_PRINTK_SIZE 4
596 #endif
598 #ifndef MEM_RSRC_PRINTK_SIZE
599 #define MEM_RSRC_PRINTK_SIZE 8
600 #endif
601 struct printf_spec num_spec = {
602 .base = 16,
603 .precision = -1,
604 .flags = SPECIAL | SMALL | ZEROPAD,
606 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
607 char sym[4*sizeof(resource_size_t) + 8];
608 char *p = sym, *pend = sym + sizeof(sym);
609 int size = -1;
611 if (res->flags & IORESOURCE_IO)
612 size = IO_RSRC_PRINTK_SIZE;
613 else if (res->flags & IORESOURCE_MEM)
614 size = MEM_RSRC_PRINTK_SIZE;
616 *p++ = '[';
617 num_spec.field_width = size;
618 p = number(p, pend, res->start, num_spec);
619 *p++ = '-';
620 p = number(p, pend, res->end, num_spec);
621 *p++ = ']';
622 *p = 0;
624 return string(buf, end, sym, spec);
627 static char *mac_address_string(char *buf, char *end, u8 *addr,
628 struct printf_spec spec)
630 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
631 char *p = mac_addr;
632 int i;
634 for (i = 0; i < 6; i++) {
635 p = pack_hex_byte(p, addr[i]);
636 if (!(spec.flags & SPECIAL) && i != 5)
637 *p++ = ':';
639 *p = '\0';
640 spec.flags &= ~SPECIAL;
642 return string(buf, end, mac_addr, spec);
645 static char *ip6_addr_string(char *buf, char *end, u8 *addr,
646 struct printf_spec spec)
648 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
649 char *p = ip6_addr;
650 int i;
652 for (i = 0; i < 8; i++) {
653 p = pack_hex_byte(p, addr[2 * i]);
654 p = pack_hex_byte(p, addr[2 * i + 1]);
655 if (!(spec.flags & SPECIAL) && i != 7)
656 *p++ = ':';
658 *p = '\0';
659 spec.flags &= ~SPECIAL;
661 return string(buf, end, ip6_addr, spec);
664 static char *ip4_addr_string(char *buf, char *end, u8 *addr,
665 struct printf_spec spec)
667 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
668 char temp[3]; /* hold each IP quad in reverse order */
669 char *p = ip4_addr;
670 int i, digits;
672 for (i = 0; i < 4; i++) {
673 digits = put_dec_trunc(temp, addr[i]) - temp;
674 /* reverse the digits in the quad */
675 while (digits--)
676 *p++ = temp[digits];
677 if (i != 3)
678 *p++ = '.';
680 *p = '\0';
681 spec.flags &= ~SPECIAL;
683 return string(buf, end, ip4_addr, spec);
687 * Show a '%p' thing. A kernel extension is that the '%p' is followed
688 * by an extra set of alphanumeric characters that are extended format
689 * specifiers.
691 * Right now we handle:
693 * - 'F' For symbolic function descriptor pointers
694 * - 'S' For symbolic direct pointers
695 * - 'R' For a struct resource pointer, it prints the range of
696 * addresses (not the name nor the flags)
697 * - 'M' For a 6-byte MAC address, it prints the address in the
698 * usual colon-separated hex notation
699 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
700 * decimal for v4 and colon separated network-order 16 bit hex for v6)
701 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
702 * currently the same
704 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
705 * function pointers are really function descriptors, which contain a
706 * pointer to the real address.
708 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
709 struct printf_spec spec)
711 if (!ptr)
712 return string(buf, end, "(null)", spec);
714 switch (*fmt) {
715 case 'F':
716 ptr = dereference_function_descriptor(ptr);
717 /* Fallthrough */
718 case 'S':
719 return symbol_string(buf, end, ptr, spec);
720 case 'R':
721 return resource_string(buf, end, ptr, spec);
722 case 'm':
723 spec.flags |= SPECIAL;
724 /* Fallthrough */
725 case 'M':
726 return mac_address_string(buf, end, ptr, spec);
727 case 'i':
728 spec.flags |= SPECIAL;
729 /* Fallthrough */
730 case 'I':
731 if (fmt[1] == '6')
732 return ip6_addr_string(buf, end, ptr, spec);
733 if (fmt[1] == '4')
734 return ip4_addr_string(buf, end, ptr, spec);
735 spec.flags &= ~SPECIAL;
736 break;
738 spec.flags |= SMALL;
739 if (spec.field_width == -1) {
740 spec.field_width = 2*sizeof(void *);
741 spec.flags |= ZEROPAD;
743 spec.base = 16;
745 return number(buf, end, (unsigned long) ptr, spec);
749 * Helper function to decode printf style format.
750 * Each call decode a token from the format and return the
751 * number of characters read (or likely the delta where it wants
752 * to go on the next call).
753 * The decoded token is returned through the parameters
755 * 'h', 'l', or 'L' for integer fields
756 * 'z' support added 23/7/1999 S.H.
757 * 'z' changed to 'Z' --davidm 1/25/99
758 * 't' added for ptrdiff_t
760 * @fmt: the format string
761 * @type of the token returned
762 * @flags: various flags such as +, -, # tokens..
763 * @field_width: overwritten width
764 * @base: base of the number (octal, hex, ...)
765 * @precision: precision of a number
766 * @qualifier: qualifier of a number (long, size_t, ...)
768 static int format_decode(const char *fmt, struct printf_spec *spec)
770 const char *start = fmt;
772 /* we finished early by reading the field width */
773 if (spec->type == FORMAT_TYPE_WIDTH) {
774 if (spec->field_width < 0) {
775 spec->field_width = -spec->field_width;
776 spec->flags |= LEFT;
778 spec->type = FORMAT_TYPE_NONE;
779 goto precision;
782 /* we finished early by reading the precision */
783 if (spec->type == FORMAT_TYPE_PRECISION) {
784 if (spec->precision < 0)
785 spec->precision = 0;
787 spec->type = FORMAT_TYPE_NONE;
788 goto qualifier;
791 /* By default */
792 spec->type = FORMAT_TYPE_NONE;
794 for (; *fmt ; ++fmt) {
795 if (*fmt == '%')
796 break;
799 /* Return the current non-format string */
800 if (fmt != start || !*fmt)
801 return fmt - start;
803 /* Process flags */
804 spec->flags = 0;
806 while (1) { /* this also skips first '%' */
807 bool found = true;
809 ++fmt;
811 switch (*fmt) {
812 case '-': spec->flags |= LEFT; break;
813 case '+': spec->flags |= PLUS; break;
814 case ' ': spec->flags |= SPACE; break;
815 case '#': spec->flags |= SPECIAL; break;
816 case '0': spec->flags |= ZEROPAD; break;
817 default: found = false;
820 if (!found)
821 break;
824 /* get field width */
825 spec->field_width = -1;
827 if (isdigit(*fmt))
828 spec->field_width = skip_atoi(&fmt);
829 else if (*fmt == '*') {
830 /* it's the next argument */
831 spec->type = FORMAT_TYPE_WIDTH;
832 return ++fmt - start;
835 precision:
836 /* get the precision */
837 spec->precision = -1;
838 if (*fmt == '.') {
839 ++fmt;
840 if (isdigit(*fmt)) {
841 spec->precision = skip_atoi(&fmt);
842 if (spec->precision < 0)
843 spec->precision = 0;
844 } else if (*fmt == '*') {
845 /* it's the next argument */
846 spec->type = FORMAT_TYPE_PRECISION;
847 return ++fmt - start;
851 qualifier:
852 /* get the conversion qualifier */
853 spec->qualifier = -1;
854 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
855 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
856 spec->qualifier = *fmt;
857 ++fmt;
858 if (spec->qualifier == 'l' && *fmt == 'l') {
859 spec->qualifier = 'L';
860 ++fmt;
864 /* default base */
865 spec->base = 10;
866 switch (*fmt) {
867 case 'c':
868 spec->type = FORMAT_TYPE_CHAR;
869 return ++fmt - start;
871 case 's':
872 spec->type = FORMAT_TYPE_STR;
873 return ++fmt - start;
875 case 'p':
876 spec->type = FORMAT_TYPE_PTR;
877 return fmt - start;
878 /* skip alnum */
880 case 'n':
881 spec->type = FORMAT_TYPE_NRCHARS;
882 return ++fmt - start;
884 case '%':
885 spec->type = FORMAT_TYPE_PERCENT_CHAR;
886 return ++fmt - start;
888 /* integer number formats - set up the flags and "break" */
889 case 'o':
890 spec->base = 8;
891 break;
893 case 'x':
894 spec->flags |= SMALL;
896 case 'X':
897 spec->base = 16;
898 break;
900 case 'd':
901 case 'i':
902 spec->flags |= SIGN;
903 case 'u':
904 break;
906 default:
907 spec->type = FORMAT_TYPE_INVALID;
908 return fmt - start;
911 if (spec->qualifier == 'L')
912 spec->type = FORMAT_TYPE_LONG_LONG;
913 else if (spec->qualifier == 'l') {
914 if (spec->flags & SIGN)
915 spec->type = FORMAT_TYPE_LONG;
916 else
917 spec->type = FORMAT_TYPE_ULONG;
918 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
919 spec->type = FORMAT_TYPE_SIZE_T;
920 } else if (spec->qualifier == 't') {
921 spec->type = FORMAT_TYPE_PTRDIFF;
922 } else if (spec->qualifier == 'h') {
923 if (spec->flags & SIGN)
924 spec->type = FORMAT_TYPE_SHORT;
925 else
926 spec->type = FORMAT_TYPE_USHORT;
927 } else {
928 if (spec->flags & SIGN)
929 spec->type = FORMAT_TYPE_INT;
930 else
931 spec->type = FORMAT_TYPE_UINT;
934 return ++fmt - start;
938 * vsnprintf - Format a string and place it in a buffer
939 * @buf: The buffer to place the result into
940 * @size: The size of the buffer, including the trailing null space
941 * @fmt: The format string to use
942 * @args: Arguments for the format string
944 * This function follows C99 vsnprintf, but has some extensions:
945 * %pS output the name of a text symbol
946 * %pF output the name of a function pointer
947 * %pR output the address range in a struct resource
949 * The return value is the number of characters which would
950 * be generated for the given input, excluding the trailing
951 * '\0', as per ISO C99. If you want to have the exact
952 * number of characters written into @buf as return value
953 * (not including the trailing '\0'), use vscnprintf(). If the
954 * return is greater than or equal to @size, the resulting
955 * string is truncated.
957 * Call this function if you are already dealing with a va_list.
958 * You probably want snprintf() instead.
960 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
962 unsigned long long num;
963 char *str, *end, c;
964 int read;
965 struct printf_spec spec = {0};
967 /* Reject out-of-range values early. Large positive sizes are
968 used for unknown buffer sizes. */
969 if (unlikely((int) size < 0)) {
970 /* There can be only one.. */
971 static char warn = 1;
972 WARN_ON(warn);
973 warn = 0;
974 return 0;
977 str = buf;
978 end = buf + size;
980 /* Make sure end is always >= buf */
981 if (end < buf) {
982 end = ((void *)-1);
983 size = end - buf;
986 while (*fmt) {
987 const char *old_fmt = fmt;
989 read = format_decode(fmt, &spec);
991 fmt += read;
993 switch (spec.type) {
994 case FORMAT_TYPE_NONE: {
995 int copy = read;
996 if (str < end) {
997 if (copy > end - str)
998 copy = end - str;
999 memcpy(str, old_fmt, copy);
1001 str += read;
1002 break;
1005 case FORMAT_TYPE_WIDTH:
1006 spec.field_width = va_arg(args, int);
1007 break;
1009 case FORMAT_TYPE_PRECISION:
1010 spec.precision = va_arg(args, int);
1011 break;
1013 case FORMAT_TYPE_CHAR:
1014 if (!(spec.flags & LEFT)) {
1015 while (--spec.field_width > 0) {
1016 if (str < end)
1017 *str = ' ';
1018 ++str;
1022 c = (unsigned char) va_arg(args, int);
1023 if (str < end)
1024 *str = c;
1025 ++str;
1026 while (--spec.field_width > 0) {
1027 if (str < end)
1028 *str = ' ';
1029 ++str;
1031 break;
1033 case FORMAT_TYPE_STR:
1034 str = string(str, end, va_arg(args, char *), spec);
1035 break;
1037 case FORMAT_TYPE_PTR:
1038 str = pointer(fmt+1, str, end, va_arg(args, void *),
1039 spec);
1040 while (isalnum(*fmt))
1041 fmt++;
1042 break;
1044 case FORMAT_TYPE_PERCENT_CHAR:
1045 if (str < end)
1046 *str = '%';
1047 ++str;
1048 break;
1050 case FORMAT_TYPE_INVALID:
1051 if (str < end)
1052 *str = '%';
1053 ++str;
1054 break;
1056 case FORMAT_TYPE_NRCHARS: {
1057 int qualifier = spec.qualifier;
1059 if (qualifier == 'l') {
1060 long *ip = va_arg(args, long *);
1061 *ip = (str - buf);
1062 } else if (qualifier == 'Z' ||
1063 qualifier == 'z') {
1064 size_t *ip = va_arg(args, size_t *);
1065 *ip = (str - buf);
1066 } else {
1067 int *ip = va_arg(args, int *);
1068 *ip = (str - buf);
1070 break;
1073 default:
1074 switch (spec.type) {
1075 case FORMAT_TYPE_LONG_LONG:
1076 num = va_arg(args, long long);
1077 break;
1078 case FORMAT_TYPE_ULONG:
1079 num = va_arg(args, unsigned long);
1080 break;
1081 case FORMAT_TYPE_LONG:
1082 num = va_arg(args, long);
1083 break;
1084 case FORMAT_TYPE_SIZE_T:
1085 num = va_arg(args, size_t);
1086 break;
1087 case FORMAT_TYPE_PTRDIFF:
1088 num = va_arg(args, ptrdiff_t);
1089 break;
1090 case FORMAT_TYPE_USHORT:
1091 num = (unsigned short) va_arg(args, int);
1092 break;
1093 case FORMAT_TYPE_SHORT:
1094 num = (short) va_arg(args, int);
1095 break;
1096 case FORMAT_TYPE_INT:
1097 num = (int) va_arg(args, int);
1098 break;
1099 default:
1100 num = va_arg(args, unsigned int);
1103 str = number(str, end, num, spec);
1107 if (size > 0) {
1108 if (str < end)
1109 *str = '\0';
1110 else
1111 end[-1] = '\0';
1114 /* the trailing null byte doesn't count towards the total */
1115 return str-buf;
1118 EXPORT_SYMBOL(vsnprintf);
1121 * vscnprintf - Format a string and place it in a buffer
1122 * @buf: The buffer to place the result into
1123 * @size: The size of the buffer, including the trailing null space
1124 * @fmt: The format string to use
1125 * @args: Arguments for the format string
1127 * The return value is the number of characters which have been written into
1128 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1129 * returns 0.
1131 * Call this function if you are already dealing with a va_list.
1132 * You probably want scnprintf() instead.
1134 * See the vsnprintf() documentation for format string extensions over C99.
1136 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1138 int i;
1140 i=vsnprintf(buf,size,fmt,args);
1141 return (i >= size) ? (size - 1) : i;
1143 EXPORT_SYMBOL(vscnprintf);
1146 * snprintf - Format a string and place it in a buffer
1147 * @buf: The buffer to place the result into
1148 * @size: The size of the buffer, including the trailing null space
1149 * @fmt: The format string to use
1150 * @...: Arguments for the format string
1152 * The return value is the number of characters which would be
1153 * generated for the given input, excluding the trailing null,
1154 * as per ISO C99. If the return is greater than or equal to
1155 * @size, the resulting string is truncated.
1157 * See the vsnprintf() documentation for format string extensions over C99.
1159 int snprintf(char * buf, size_t size, const char *fmt, ...)
1161 va_list args;
1162 int i;
1164 va_start(args, fmt);
1165 i=vsnprintf(buf,size,fmt,args);
1166 va_end(args);
1167 return i;
1169 EXPORT_SYMBOL(snprintf);
1172 * scnprintf - Format a string and place it in a buffer
1173 * @buf: The buffer to place the result into
1174 * @size: The size of the buffer, including the trailing null space
1175 * @fmt: The format string to use
1176 * @...: Arguments for the format string
1178 * The return value is the number of characters written into @buf not including
1179 * the trailing '\0'. If @size is <= 0 the function returns 0.
1182 int scnprintf(char * buf, size_t size, const char *fmt, ...)
1184 va_list args;
1185 int i;
1187 va_start(args, fmt);
1188 i = vsnprintf(buf, size, fmt, args);
1189 va_end(args);
1190 return (i >= size) ? (size - 1) : i;
1192 EXPORT_SYMBOL(scnprintf);
1195 * vsprintf - Format a string and place it in a buffer
1196 * @buf: The buffer to place the result into
1197 * @fmt: The format string to use
1198 * @args: Arguments for the format string
1200 * The function returns the number of characters written
1201 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1202 * buffer overflows.
1204 * Call this function if you are already dealing with a va_list.
1205 * You probably want sprintf() instead.
1207 * See the vsnprintf() documentation for format string extensions over C99.
1209 int vsprintf(char *buf, const char *fmt, va_list args)
1211 return vsnprintf(buf, INT_MAX, fmt, args);
1213 EXPORT_SYMBOL(vsprintf);
1216 * sprintf - Format a string and place it in a buffer
1217 * @buf: The buffer to place the result into
1218 * @fmt: The format string to use
1219 * @...: Arguments for the format string
1221 * The function returns the number of characters written
1222 * into @buf. Use snprintf() or scnprintf() in order to avoid
1223 * buffer overflows.
1225 * See the vsnprintf() documentation for format string extensions over C99.
1227 int sprintf(char * buf, const char *fmt, ...)
1229 va_list args;
1230 int i;
1232 va_start(args, fmt);
1233 i=vsnprintf(buf, INT_MAX, fmt, args);
1234 va_end(args);
1235 return i;
1237 EXPORT_SYMBOL(sprintf);
1239 #ifdef CONFIG_BINARY_PRINTF
1241 * bprintf service:
1242 * vbin_printf() - VA arguments to binary data
1243 * bstr_printf() - Binary data to text string
1247 * vbin_printf - Parse a format string and place args' binary value in a buffer
1248 * @bin_buf: The buffer to place args' binary value
1249 * @size: The size of the buffer(by words(32bits), not characters)
1250 * @fmt: The format string to use
1251 * @args: Arguments for the format string
1253 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1254 * is skiped.
1256 * The return value is the number of words(32bits) which would be generated for
1257 * the given input.
1259 * NOTE:
1260 * If the return value is greater than @size, the resulting bin_buf is NOT
1261 * valid for bstr_printf().
1263 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1265 struct printf_spec spec = {0};
1266 char *str, *end;
1267 int read;
1269 str = (char *)bin_buf;
1270 end = (char *)(bin_buf + size);
1272 #define save_arg(type) \
1273 do { \
1274 if (sizeof(type) == 8) { \
1275 unsigned long long value; \
1276 str = PTR_ALIGN(str, sizeof(u32)); \
1277 value = va_arg(args, unsigned long long); \
1278 if (str + sizeof(type) <= end) { \
1279 *(u32 *)str = *(u32 *)&value; \
1280 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1282 } else { \
1283 unsigned long value; \
1284 str = PTR_ALIGN(str, sizeof(type)); \
1285 value = va_arg(args, int); \
1286 if (str + sizeof(type) <= end) \
1287 *(typeof(type) *)str = (type)value; \
1289 str += sizeof(type); \
1290 } while (0)
1293 while (*fmt) {
1294 read = format_decode(fmt, &spec);
1296 fmt += read;
1298 switch (spec.type) {
1299 case FORMAT_TYPE_NONE:
1300 break;
1302 case FORMAT_TYPE_WIDTH:
1303 case FORMAT_TYPE_PRECISION:
1304 save_arg(int);
1305 break;
1307 case FORMAT_TYPE_CHAR:
1308 save_arg(char);
1309 break;
1311 case FORMAT_TYPE_STR: {
1312 const char *save_str = va_arg(args, char *);
1313 size_t len;
1314 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1315 || (unsigned long)save_str < PAGE_SIZE)
1316 save_str = "<NULL>";
1317 len = strlen(save_str);
1318 if (str + len + 1 < end)
1319 memcpy(str, save_str, len + 1);
1320 str += len + 1;
1321 break;
1324 case FORMAT_TYPE_PTR:
1325 save_arg(void *);
1326 /* skip all alphanumeric pointer suffixes */
1327 while (isalnum(*fmt))
1328 fmt++;
1329 break;
1331 case FORMAT_TYPE_PERCENT_CHAR:
1332 break;
1334 case FORMAT_TYPE_INVALID:
1335 break;
1337 case FORMAT_TYPE_NRCHARS: {
1338 /* skip %n 's argument */
1339 int qualifier = spec.qualifier;
1340 void *skip_arg;
1341 if (qualifier == 'l')
1342 skip_arg = va_arg(args, long *);
1343 else if (qualifier == 'Z' || qualifier == 'z')
1344 skip_arg = va_arg(args, size_t *);
1345 else
1346 skip_arg = va_arg(args, int *);
1347 break;
1350 default:
1351 switch (spec.type) {
1353 case FORMAT_TYPE_LONG_LONG:
1354 save_arg(long long);
1355 break;
1356 case FORMAT_TYPE_ULONG:
1357 case FORMAT_TYPE_LONG:
1358 save_arg(unsigned long);
1359 break;
1360 case FORMAT_TYPE_SIZE_T:
1361 save_arg(size_t);
1362 break;
1363 case FORMAT_TYPE_PTRDIFF:
1364 save_arg(ptrdiff_t);
1365 break;
1366 case FORMAT_TYPE_USHORT:
1367 case FORMAT_TYPE_SHORT:
1368 save_arg(short);
1369 break;
1370 default:
1371 save_arg(int);
1375 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1377 #undef save_arg
1379 EXPORT_SYMBOL_GPL(vbin_printf);
1382 * bstr_printf - Format a string from binary arguments and place it in a buffer
1383 * @buf: The buffer to place the result into
1384 * @size: The size of the buffer, including the trailing null space
1385 * @fmt: The format string to use
1386 * @bin_buf: Binary arguments for the format string
1388 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1389 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1390 * a binary buffer that generated by vbin_printf.
1392 * The format follows C99 vsnprintf, but has some extensions:
1393 * %pS output the name of a text symbol
1394 * %pF output the name of a function pointer
1395 * %pR output the address range in a struct resource
1396 * %n is ignored
1398 * The return value is the number of characters which would
1399 * be generated for the given input, excluding the trailing
1400 * '\0', as per ISO C99. If you want to have the exact
1401 * number of characters written into @buf as return value
1402 * (not including the trailing '\0'), use vscnprintf(). If the
1403 * return is greater than or equal to @size, the resulting
1404 * string is truncated.
1406 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1408 unsigned long long num;
1409 char *str, *end, c;
1410 const char *args = (const char *)bin_buf;
1412 struct printf_spec spec = {0};
1414 if (unlikely((int) size < 0)) {
1415 /* There can be only one.. */
1416 static char warn = 1;
1417 WARN_ON(warn);
1418 warn = 0;
1419 return 0;
1422 str = buf;
1423 end = buf + size;
1425 #define get_arg(type) \
1426 ({ \
1427 typeof(type) value; \
1428 if (sizeof(type) == 8) { \
1429 args = PTR_ALIGN(args, sizeof(u32)); \
1430 *(u32 *)&value = *(u32 *)args; \
1431 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1432 } else { \
1433 args = PTR_ALIGN(args, sizeof(type)); \
1434 value = *(typeof(type) *)args; \
1436 args += sizeof(type); \
1437 value; \
1440 /* Make sure end is always >= buf */
1441 if (end < buf) {
1442 end = ((void *)-1);
1443 size = end - buf;
1446 while (*fmt) {
1447 int read;
1448 const char *old_fmt = fmt;
1450 read = format_decode(fmt, &spec);
1452 fmt += read;
1454 switch (spec.type) {
1455 case FORMAT_TYPE_NONE: {
1456 int copy = read;
1457 if (str < end) {
1458 if (copy > end - str)
1459 copy = end - str;
1460 memcpy(str, old_fmt, copy);
1462 str += read;
1463 break;
1466 case FORMAT_TYPE_WIDTH:
1467 spec.field_width = get_arg(int);
1468 break;
1470 case FORMAT_TYPE_PRECISION:
1471 spec.precision = get_arg(int);
1472 break;
1474 case FORMAT_TYPE_CHAR:
1475 if (!(spec.flags & LEFT)) {
1476 while (--spec.field_width > 0) {
1477 if (str < end)
1478 *str = ' ';
1479 ++str;
1482 c = (unsigned char) get_arg(char);
1483 if (str < end)
1484 *str = c;
1485 ++str;
1486 while (--spec.field_width > 0) {
1487 if (str < end)
1488 *str = ' ';
1489 ++str;
1491 break;
1493 case FORMAT_TYPE_STR: {
1494 const char *str_arg = args;
1495 size_t len = strlen(str_arg);
1496 args += len + 1;
1497 str = string(str, end, (char *)str_arg, spec);
1498 break;
1501 case FORMAT_TYPE_PTR:
1502 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1503 while (isalnum(*fmt))
1504 fmt++;
1505 break;
1507 case FORMAT_TYPE_PERCENT_CHAR:
1508 if (str < end)
1509 *str = '%';
1510 ++str;
1511 break;
1513 case FORMAT_TYPE_INVALID:
1514 if (str < end)
1515 *str = '%';
1516 ++str;
1517 break;
1519 case FORMAT_TYPE_NRCHARS:
1520 /* skip */
1521 break;
1523 default:
1524 switch (spec.type) {
1526 case FORMAT_TYPE_LONG_LONG:
1527 num = get_arg(long long);
1528 break;
1529 case FORMAT_TYPE_ULONG:
1530 num = get_arg(unsigned long);
1531 break;
1532 case FORMAT_TYPE_LONG:
1533 num = get_arg(unsigned long);
1534 break;
1535 case FORMAT_TYPE_SIZE_T:
1536 num = get_arg(size_t);
1537 break;
1538 case FORMAT_TYPE_PTRDIFF:
1539 num = get_arg(ptrdiff_t);
1540 break;
1541 case FORMAT_TYPE_USHORT:
1542 num = get_arg(unsigned short);
1543 break;
1544 case FORMAT_TYPE_SHORT:
1545 num = get_arg(short);
1546 break;
1547 case FORMAT_TYPE_UINT:
1548 num = get_arg(unsigned int);
1549 break;
1550 default:
1551 num = get_arg(int);
1554 str = number(str, end, num, spec);
1558 if (size > 0) {
1559 if (str < end)
1560 *str = '\0';
1561 else
1562 end[-1] = '\0';
1565 #undef get_arg
1567 /* the trailing null byte doesn't count towards the total */
1568 return str - buf;
1570 EXPORT_SYMBOL_GPL(bstr_printf);
1573 * bprintf - Parse a format string and place args' binary value in a buffer
1574 * @bin_buf: The buffer to place args' binary value
1575 * @size: The size of the buffer(by words(32bits), not characters)
1576 * @fmt: The format string to use
1577 * @...: Arguments for the format string
1579 * The function returns the number of words(u32) written
1580 * into @bin_buf.
1582 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1584 va_list args;
1585 int ret;
1587 va_start(args, fmt);
1588 ret = vbin_printf(bin_buf, size, fmt, args);
1589 va_end(args);
1590 return ret;
1592 EXPORT_SYMBOL_GPL(bprintf);
1594 #endif /* CONFIG_BINARY_PRINTF */
1597 * vsscanf - Unformat a buffer into a list of arguments
1598 * @buf: input buffer
1599 * @fmt: format of buffer
1600 * @args: arguments
1602 int vsscanf(const char * buf, const char * fmt, va_list args)
1604 const char *str = buf;
1605 char *next;
1606 char digit;
1607 int num = 0;
1608 int qualifier;
1609 int base;
1610 int field_width;
1611 int is_sign = 0;
1613 while(*fmt && *str) {
1614 /* skip any white space in format */
1615 /* white space in format matchs any amount of
1616 * white space, including none, in the input.
1618 if (isspace(*fmt)) {
1619 while (isspace(*fmt))
1620 ++fmt;
1621 while (isspace(*str))
1622 ++str;
1625 /* anything that is not a conversion must match exactly */
1626 if (*fmt != '%' && *fmt) {
1627 if (*fmt++ != *str++)
1628 break;
1629 continue;
1632 if (!*fmt)
1633 break;
1634 ++fmt;
1636 /* skip this conversion.
1637 * advance both strings to next white space
1639 if (*fmt == '*') {
1640 while (!isspace(*fmt) && *fmt)
1641 fmt++;
1642 while (!isspace(*str) && *str)
1643 str++;
1644 continue;
1647 /* get field width */
1648 field_width = -1;
1649 if (isdigit(*fmt))
1650 field_width = skip_atoi(&fmt);
1652 /* get conversion qualifier */
1653 qualifier = -1;
1654 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1655 *fmt == 'Z' || *fmt == 'z') {
1656 qualifier = *fmt++;
1657 if (unlikely(qualifier == *fmt)) {
1658 if (qualifier == 'h') {
1659 qualifier = 'H';
1660 fmt++;
1661 } else if (qualifier == 'l') {
1662 qualifier = 'L';
1663 fmt++;
1667 base = 10;
1668 is_sign = 0;
1670 if (!*fmt || !*str)
1671 break;
1673 switch(*fmt++) {
1674 case 'c':
1676 char *s = (char *) va_arg(args,char*);
1677 if (field_width == -1)
1678 field_width = 1;
1679 do {
1680 *s++ = *str++;
1681 } while (--field_width > 0 && *str);
1682 num++;
1684 continue;
1685 case 's':
1687 char *s = (char *) va_arg(args, char *);
1688 if(field_width == -1)
1689 field_width = INT_MAX;
1690 /* first, skip leading white space in buffer */
1691 while (isspace(*str))
1692 str++;
1694 /* now copy until next white space */
1695 while (*str && !isspace(*str) && field_width--) {
1696 *s++ = *str++;
1698 *s = '\0';
1699 num++;
1701 continue;
1702 case 'n':
1703 /* return number of characters read so far */
1705 int *i = (int *)va_arg(args,int*);
1706 *i = str - buf;
1708 continue;
1709 case 'o':
1710 base = 8;
1711 break;
1712 case 'x':
1713 case 'X':
1714 base = 16;
1715 break;
1716 case 'i':
1717 base = 0;
1718 case 'd':
1719 is_sign = 1;
1720 case 'u':
1721 break;
1722 case '%':
1723 /* looking for '%' in str */
1724 if (*str++ != '%')
1725 return num;
1726 continue;
1727 default:
1728 /* invalid format; stop here */
1729 return num;
1732 /* have some sort of integer conversion.
1733 * first, skip white space in buffer.
1735 while (isspace(*str))
1736 str++;
1738 digit = *str;
1739 if (is_sign && digit == '-')
1740 digit = *(str + 1);
1742 if (!digit
1743 || (base == 16 && !isxdigit(digit))
1744 || (base == 10 && !isdigit(digit))
1745 || (base == 8 && (!isdigit(digit) || digit > '7'))
1746 || (base == 0 && !isdigit(digit)))
1747 break;
1749 switch(qualifier) {
1750 case 'H': /* that's 'hh' in format */
1751 if (is_sign) {
1752 signed char *s = (signed char *) va_arg(args,signed char *);
1753 *s = (signed char) simple_strtol(str,&next,base);
1754 } else {
1755 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1756 *s = (unsigned char) simple_strtoul(str, &next, base);
1758 break;
1759 case 'h':
1760 if (is_sign) {
1761 short *s = (short *) va_arg(args,short *);
1762 *s = (short) simple_strtol(str,&next,base);
1763 } else {
1764 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1765 *s = (unsigned short) simple_strtoul(str, &next, base);
1767 break;
1768 case 'l':
1769 if (is_sign) {
1770 long *l = (long *) va_arg(args,long *);
1771 *l = simple_strtol(str,&next,base);
1772 } else {
1773 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1774 *l = simple_strtoul(str,&next,base);
1776 break;
1777 case 'L':
1778 if (is_sign) {
1779 long long *l = (long long*) va_arg(args,long long *);
1780 *l = simple_strtoll(str,&next,base);
1781 } else {
1782 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1783 *l = simple_strtoull(str,&next,base);
1785 break;
1786 case 'Z':
1787 case 'z':
1789 size_t *s = (size_t*) va_arg(args,size_t*);
1790 *s = (size_t) simple_strtoul(str,&next,base);
1792 break;
1793 default:
1794 if (is_sign) {
1795 int *i = (int *) va_arg(args, int*);
1796 *i = (int) simple_strtol(str,&next,base);
1797 } else {
1798 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1799 *i = (unsigned int) simple_strtoul(str,&next,base);
1801 break;
1803 num++;
1805 if (!next)
1806 break;
1807 str = next;
1811 * Now we've come all the way through so either the input string or the
1812 * format ended. In the former case, there can be a %n at the current
1813 * position in the format that needs to be filled.
1815 if (*fmt == '%' && *(fmt + 1) == 'n') {
1816 int *p = (int *)va_arg(args, int *);
1817 *p = str - buf;
1820 return num;
1822 EXPORT_SYMBOL(vsscanf);
1825 * sscanf - Unformat a buffer into a list of arguments
1826 * @buf: input buffer
1827 * @fmt: formatting of buffer
1828 * @...: resulting arguments
1830 int sscanf(const char * buf, const char * fmt, ...)
1832 va_list args;
1833 int i;
1835 va_start(args,fmt);
1836 i = vsscanf(buf,fmt,args);
1837 va_end(args);
1838 return i;
1840 EXPORT_SYMBOL(sscanf);