4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
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
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)
39 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
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;
59 base = simple_guess_base(cp);
61 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
64 while (isxdigit(*cp)) {
67 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
70 result = result * base + value;
78 EXPORT_SYMBOL(simple_strtoul);
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)
89 return -simple_strtoul(cp + 1, endp, base);
90 return simple_strtoul(cp, endp, base);
92 EXPORT_SYMBOL(simple_strtol);
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;
105 base = simple_guess_base(cp);
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
110 while (isxdigit(*cp)) {
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
116 result = result * base + value;
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)
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)
172 val = simple_strtoul(cp, &tail, base);
175 if ((*tail == '\0') ||
176 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
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)
201 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
205 ret = strict_strtoul(cp, base, (unsigned long *)res);
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)
237 unsigned long long val;
245 val = simple_strtoull(cp, &tail, base);
248 if ((*tail == '\0') ||
249 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
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)
274 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
278 ret = strict_strtoull(cp, base, (unsigned long long *)res);
283 EXPORT_SYMBOL(strict_strtoll);
285 static int skip_atoi(const char **s)
290 i = i*10 + *((*s)++) - '0';
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
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;
311 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
312 q = (d0 * 0xcd) >> 11;
314 *buf++ = d0 + '0'; /* least significant digit */
315 d1 = q + 9*d3 + 5*d2 + d1;
317 q = (d1 * 0xcd) >> 11;
319 *buf++ = d1 + '0'; /* next digit */
322 if ((d2 != 0) || (d3 != 0)) {
325 *buf++ = d2 + '0'; /* next digit */
329 q = (d3 * 0xcd) >> 11;
331 *buf++ = d3 + '0'; /* next digit */
333 *buf++ = q + '0'; /* most sign. digit */
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;
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;
361 d1 = q + 9*d3 + 5*d2 + d1;
362 q = (d1 * 0xcd) >> 11;
372 q = (d3 * 0xcd) >> 11; /* - shorter code */
373 /* q = (d3 * 0x67) >> 10; - would also work */
379 /* No inlining helps gcc to use registers better */
380 static noinline char* put_dec(char *buf, unsigned long long num)
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 */
400 FORMAT_TYPE_NONE, /* Just a string part */
402 FORMAT_TYPE_PRECISION,
406 FORMAT_TYPE_PERCENT_CHAR,
408 FORMAT_TYPE_LONG_LONG,
423 enum format_type type;
424 int flags; /* flags to number() */
425 int field_width; /* width of output field */
427 int precision; /* # of digits/chars */
431 static char *number(char *buf, char *end, unsigned long long num,
432 struct printf_spec spec)
434 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
435 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
440 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
443 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
444 * produces same digits or (maybe lowercased) letters */
445 locase = (spec.flags & SMALL);
446 if (spec.flags & LEFT)
447 spec.flags &= ~ZEROPAD;
449 if (spec.flags & SIGN) {
450 if ((signed long long) num < 0) {
452 num = - (signed long long) num;
454 } else if (spec.flags & PLUS) {
457 } else if (spec.flags & SPACE) {
468 /* generate full string in tmp[], in reverse order */
472 /* Generic code, for any base:
474 tmp[i++] = (digits[do_div(num,base)] | locase);
477 else if (spec.base != 10) { /* 8 or 16 */
478 int mask = spec.base - 1;
480 if (spec.base == 16) shift = 4;
482 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
485 } else { /* base 10 */
486 i = put_dec(tmp, num) - tmp;
489 /* printing 100 using %2d gives "100", not "00" */
490 if (i > spec.precision)
492 /* leading space padding */
493 spec.field_width -= spec.precision;
494 if (!(spec.flags & (ZEROPAD+LEFT))) {
495 while(--spec.field_width >= 0) {
507 /* "0x" / "0" prefix */
512 if (spec.base == 16) {
514 *buf = ('X' | locase);
518 /* zero or space padding */
519 if (!(spec.flags & LEFT)) {
520 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
521 while (--spec.field_width >= 0) {
527 /* hmm even more zero padding? */
528 while (i <= --spec.precision) {
533 /* actual digits of result */
539 /* trailing space padding */
540 while (--spec.field_width >= 0) {
548 static char *string(char *buf, char *end, char *s, struct printf_spec spec)
552 if ((unsigned long)s < PAGE_SIZE)
555 len = strnlen(s, spec.precision);
557 if (!(spec.flags & LEFT)) {
558 while (len < spec.field_width--) {
564 for (i = 0; i < len; ++i) {
569 while (len < spec.field_width--) {
577 static char *symbol_string(char *buf, char *end, void *ptr,
578 struct printf_spec spec, char ext)
580 unsigned long value = (unsigned long) ptr;
581 #ifdef CONFIG_KALLSYMS
582 char sym[KSYM_SYMBOL_LEN];
584 sprint_symbol(sym, value);
586 kallsyms_lookup(value, NULL, NULL, NULL, sym);
587 return string(buf, end, sym, spec);
589 spec.field_width = 2*sizeof(void *);
590 spec.flags |= SPECIAL | SMALL | ZEROPAD;
592 return number(buf, end, value, spec);
596 static char *resource_string(char *buf, char *end, struct resource *res,
597 struct printf_spec spec)
599 #ifndef IO_RSRC_PRINTK_SIZE
600 #define IO_RSRC_PRINTK_SIZE 4
603 #ifndef MEM_RSRC_PRINTK_SIZE
604 #define MEM_RSRC_PRINTK_SIZE 8
606 struct printf_spec num_spec = {
609 .flags = SPECIAL | SMALL | ZEROPAD,
611 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
612 char sym[4*sizeof(resource_size_t) + 8];
613 char *p = sym, *pend = sym + sizeof(sym);
616 if (res->flags & IORESOURCE_IO)
617 size = IO_RSRC_PRINTK_SIZE;
618 else if (res->flags & IORESOURCE_MEM)
619 size = MEM_RSRC_PRINTK_SIZE;
622 num_spec.field_width = size;
623 p = number(p, pend, res->start, num_spec);
625 p = number(p, pend, res->end, num_spec);
629 return string(buf, end, sym, spec);
632 static char *mac_address_string(char *buf, char *end, u8 *addr,
633 struct printf_spec spec)
635 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
639 for (i = 0; i < 6; i++) {
640 p = pack_hex_byte(p, addr[i]);
641 if (!(spec.flags & SPECIAL) && i != 5)
645 spec.flags &= ~SPECIAL;
647 return string(buf, end, mac_addr, spec);
650 static char *ip6_addr_string(char *buf, char *end, u8 *addr,
651 struct printf_spec spec)
653 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
657 for (i = 0; i < 8; i++) {
658 p = pack_hex_byte(p, addr[2 * i]);
659 p = pack_hex_byte(p, addr[2 * i + 1]);
660 if (!(spec.flags & SPECIAL) && i != 7)
664 spec.flags &= ~SPECIAL;
666 return string(buf, end, ip6_addr, spec);
669 static char *ip4_addr_string(char *buf, char *end, u8 *addr,
670 struct printf_spec spec)
672 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
673 char temp[3]; /* hold each IP quad in reverse order */
677 for (i = 0; i < 4; i++) {
678 digits = put_dec_trunc(temp, addr[i]) - temp;
679 /* reverse the digits in the quad */
686 spec.flags &= ~SPECIAL;
688 return string(buf, end, ip4_addr, spec);
692 * Show a '%p' thing. A kernel extension is that the '%p' is followed
693 * by an extra set of alphanumeric characters that are extended format
696 * Right now we handle:
698 * - 'F' For symbolic function descriptor pointers with offset
699 * - 'f' For simple symbolic function names without offset
700 * - 'S' For symbolic direct pointers
701 * - 'R' For a struct resource pointer, it prints the range of
702 * addresses (not the name nor the flags)
703 * - 'M' For a 6-byte MAC address, it prints the address in the
704 * usual colon-separated hex notation
705 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
706 * decimal for v4 and colon separated network-order 16 bit hex for v6)
707 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
710 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
711 * function pointers are really function descriptors, which contain a
712 * pointer to the real address.
714 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
715 struct printf_spec spec)
718 return string(buf, end, "(null)", spec);
723 ptr = dereference_function_descriptor(ptr);
726 return symbol_string(buf, end, ptr, spec, *fmt);
728 return resource_string(buf, end, ptr, spec);
730 spec.flags |= SPECIAL;
733 return mac_address_string(buf, end, ptr, spec);
735 spec.flags |= SPECIAL;
739 return ip6_addr_string(buf, end, ptr, spec);
741 return ip4_addr_string(buf, end, ptr, spec);
742 spec.flags &= ~SPECIAL;
746 if (spec.field_width == -1) {
747 spec.field_width = 2*sizeof(void *);
748 spec.flags |= ZEROPAD;
752 return number(buf, end, (unsigned long) ptr, spec);
756 * Helper function to decode printf style format.
757 * Each call decode a token from the format and return the
758 * number of characters read (or likely the delta where it wants
759 * to go on the next call).
760 * The decoded token is returned through the parameters
762 * 'h', 'l', or 'L' for integer fields
763 * 'z' support added 23/7/1999 S.H.
764 * 'z' changed to 'Z' --davidm 1/25/99
765 * 't' added for ptrdiff_t
767 * @fmt: the format string
768 * @type of the token returned
769 * @flags: various flags such as +, -, # tokens..
770 * @field_width: overwritten width
771 * @base: base of the number (octal, hex, ...)
772 * @precision: precision of a number
773 * @qualifier: qualifier of a number (long, size_t, ...)
775 static int format_decode(const char *fmt, struct printf_spec *spec)
777 const char *start = fmt;
779 /* we finished early by reading the field width */
780 if (spec->type == FORMAT_TYPE_WIDTH) {
781 if (spec->field_width < 0) {
782 spec->field_width = -spec->field_width;
785 spec->type = FORMAT_TYPE_NONE;
789 /* we finished early by reading the precision */
790 if (spec->type == FORMAT_TYPE_PRECISION) {
791 if (spec->precision < 0)
794 spec->type = FORMAT_TYPE_NONE;
799 spec->type = FORMAT_TYPE_NONE;
801 for (; *fmt ; ++fmt) {
806 /* Return the current non-format string */
807 if (fmt != start || !*fmt)
813 while (1) { /* this also skips first '%' */
819 case '-': spec->flags |= LEFT; break;
820 case '+': spec->flags |= PLUS; break;
821 case ' ': spec->flags |= SPACE; break;
822 case '#': spec->flags |= SPECIAL; break;
823 case '0': spec->flags |= ZEROPAD; break;
824 default: found = false;
831 /* get field width */
832 spec->field_width = -1;
835 spec->field_width = skip_atoi(&fmt);
836 else if (*fmt == '*') {
837 /* it's the next argument */
838 spec->type = FORMAT_TYPE_WIDTH;
839 return ++fmt - start;
843 /* get the precision */
844 spec->precision = -1;
848 spec->precision = skip_atoi(&fmt);
849 if (spec->precision < 0)
851 } else if (*fmt == '*') {
852 /* it's the next argument */
853 spec->type = FORMAT_TYPE_PRECISION;
854 return ++fmt - start;
859 /* get the conversion qualifier */
860 spec->qualifier = -1;
861 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
862 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
863 spec->qualifier = *fmt++;
864 if (unlikely(spec->qualifier == *fmt)) {
865 if (spec->qualifier == 'l') {
866 spec->qualifier = 'L';
868 } else if (spec->qualifier == 'h') {
869 spec->qualifier = 'H';
879 spec->type = FORMAT_TYPE_CHAR;
880 return ++fmt - start;
883 spec->type = FORMAT_TYPE_STR;
884 return ++fmt - start;
887 spec->type = FORMAT_TYPE_PTR;
892 spec->type = FORMAT_TYPE_NRCHARS;
893 return ++fmt - start;
896 spec->type = FORMAT_TYPE_PERCENT_CHAR;
897 return ++fmt - start;
899 /* integer number formats - set up the flags and "break" */
905 spec->flags |= SMALL;
918 spec->type = FORMAT_TYPE_INVALID;
922 if (spec->qualifier == 'L')
923 spec->type = FORMAT_TYPE_LONG_LONG;
924 else if (spec->qualifier == 'l') {
925 if (spec->flags & SIGN)
926 spec->type = FORMAT_TYPE_LONG;
928 spec->type = FORMAT_TYPE_ULONG;
929 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
930 spec->type = FORMAT_TYPE_SIZE_T;
931 } else if (spec->qualifier == 't') {
932 spec->type = FORMAT_TYPE_PTRDIFF;
933 } else if (spec->qualifier == 'H') {
934 if (spec->flags & SIGN)
935 spec->type = FORMAT_TYPE_BYTE;
937 spec->type = FORMAT_TYPE_UBYTE;
938 } else if (spec->qualifier == 'h') {
939 if (spec->flags & SIGN)
940 spec->type = FORMAT_TYPE_SHORT;
942 spec->type = FORMAT_TYPE_USHORT;
944 if (spec->flags & SIGN)
945 spec->type = FORMAT_TYPE_INT;
947 spec->type = FORMAT_TYPE_UINT;
950 return ++fmt - start;
954 * vsnprintf - Format a string and place it in a buffer
955 * @buf: The buffer to place the result into
956 * @size: The size of the buffer, including the trailing null space
957 * @fmt: The format string to use
958 * @args: Arguments for the format string
960 * This function follows C99 vsnprintf, but has some extensions:
961 * %pS output the name of a text symbol
962 * %pF output the name of a function pointer with its offset
963 * %pf output the name of a function pointer without its offset
964 * %pR output the address range in a struct resource
966 * The return value is the number of characters which would
967 * be generated for the given input, excluding the trailing
968 * '\0', as per ISO C99. If you want to have the exact
969 * number of characters written into @buf as return value
970 * (not including the trailing '\0'), use vscnprintf(). If the
971 * return is greater than or equal to @size, the resulting
972 * string is truncated.
974 * Call this function if you are already dealing with a va_list.
975 * You probably want snprintf() instead.
977 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
979 unsigned long long num;
982 struct printf_spec spec = {0};
984 /* Reject out-of-range values early. Large positive sizes are
985 used for unknown buffer sizes. */
986 if (unlikely((int) size < 0)) {
987 /* There can be only one.. */
988 static char warn = 1;
997 /* Make sure end is always >= buf */
1004 const char *old_fmt = fmt;
1006 read = format_decode(fmt, &spec);
1010 switch (spec.type) {
1011 case FORMAT_TYPE_NONE: {
1014 if (copy > end - str)
1016 memcpy(str, old_fmt, copy);
1022 case FORMAT_TYPE_WIDTH:
1023 spec.field_width = va_arg(args, int);
1026 case FORMAT_TYPE_PRECISION:
1027 spec.precision = va_arg(args, int);
1030 case FORMAT_TYPE_CHAR:
1031 if (!(spec.flags & LEFT)) {
1032 while (--spec.field_width > 0) {
1039 c = (unsigned char) va_arg(args, int);
1043 while (--spec.field_width > 0) {
1050 case FORMAT_TYPE_STR:
1051 str = string(str, end, va_arg(args, char *), spec);
1054 case FORMAT_TYPE_PTR:
1055 str = pointer(fmt+1, str, end, va_arg(args, void *),
1057 while (isalnum(*fmt))
1061 case FORMAT_TYPE_PERCENT_CHAR:
1067 case FORMAT_TYPE_INVALID:
1073 case FORMAT_TYPE_NRCHARS: {
1074 int qualifier = spec.qualifier;
1076 if (qualifier == 'l') {
1077 long *ip = va_arg(args, long *);
1079 } else if (qualifier == 'Z' ||
1081 size_t *ip = va_arg(args, size_t *);
1084 int *ip = va_arg(args, int *);
1091 switch (spec.type) {
1092 case FORMAT_TYPE_LONG_LONG:
1093 num = va_arg(args, long long);
1095 case FORMAT_TYPE_ULONG:
1096 num = va_arg(args, unsigned long);
1098 case FORMAT_TYPE_LONG:
1099 num = va_arg(args, long);
1101 case FORMAT_TYPE_SIZE_T:
1102 num = va_arg(args, size_t);
1104 case FORMAT_TYPE_PTRDIFF:
1105 num = va_arg(args, ptrdiff_t);
1107 case FORMAT_TYPE_UBYTE:
1108 num = (unsigned char) va_arg(args, int);
1110 case FORMAT_TYPE_BYTE:
1111 num = (signed char) va_arg(args, int);
1113 case FORMAT_TYPE_USHORT:
1114 num = (unsigned short) va_arg(args, int);
1116 case FORMAT_TYPE_SHORT:
1117 num = (short) va_arg(args, int);
1119 case FORMAT_TYPE_INT:
1120 num = (int) va_arg(args, int);
1123 num = va_arg(args, unsigned int);
1126 str = number(str, end, num, spec);
1137 /* the trailing null byte doesn't count towards the total */
1141 EXPORT_SYMBOL(vsnprintf);
1144 * vscnprintf - Format a string and place it in a buffer
1145 * @buf: The buffer to place the result into
1146 * @size: The size of the buffer, including the trailing null space
1147 * @fmt: The format string to use
1148 * @args: Arguments for the format string
1150 * The return value is the number of characters which have been written into
1151 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1154 * Call this function if you are already dealing with a va_list.
1155 * You probably want scnprintf() instead.
1157 * See the vsnprintf() documentation for format string extensions over C99.
1159 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1163 i=vsnprintf(buf,size,fmt,args);
1164 return (i >= size) ? (size - 1) : i;
1166 EXPORT_SYMBOL(vscnprintf);
1169 * snprintf - Format a string and place it in a buffer
1170 * @buf: The buffer to place the result into
1171 * @size: The size of the buffer, including the trailing null space
1172 * @fmt: The format string to use
1173 * @...: Arguments for the format string
1175 * The return value is the number of characters which would be
1176 * generated for the given input, excluding the trailing null,
1177 * as per ISO C99. If the return is greater than or equal to
1178 * @size, the resulting string is truncated.
1180 * See the vsnprintf() documentation for format string extensions over C99.
1182 int snprintf(char * buf, size_t size, const char *fmt, ...)
1187 va_start(args, fmt);
1188 i=vsnprintf(buf,size,fmt,args);
1192 EXPORT_SYMBOL(snprintf);
1195 * scnprintf - Format a string and place it in a buffer
1196 * @buf: The buffer to place the result into
1197 * @size: The size of the buffer, including the trailing null space
1198 * @fmt: The format string to use
1199 * @...: Arguments for the format string
1201 * The return value is the number of characters written into @buf not including
1202 * the trailing '\0'. If @size is <= 0 the function returns 0.
1205 int scnprintf(char * buf, size_t size, const char *fmt, ...)
1210 va_start(args, fmt);
1211 i = vsnprintf(buf, size, fmt, args);
1213 return (i >= size) ? (size - 1) : i;
1215 EXPORT_SYMBOL(scnprintf);
1218 * vsprintf - Format a string and place it in a buffer
1219 * @buf: The buffer to place the result into
1220 * @fmt: The format string to use
1221 * @args: Arguments for the format string
1223 * The function returns the number of characters written
1224 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1227 * Call this function if you are already dealing with a va_list.
1228 * You probably want sprintf() instead.
1230 * See the vsnprintf() documentation for format string extensions over C99.
1232 int vsprintf(char *buf, const char *fmt, va_list args)
1234 return vsnprintf(buf, INT_MAX, fmt, args);
1236 EXPORT_SYMBOL(vsprintf);
1239 * sprintf - Format a string and place it in a buffer
1240 * @buf: The buffer to place the result into
1241 * @fmt: The format string to use
1242 * @...: Arguments for the format string
1244 * The function returns the number of characters written
1245 * into @buf. Use snprintf() or scnprintf() in order to avoid
1248 * See the vsnprintf() documentation for format string extensions over C99.
1250 int sprintf(char * buf, const char *fmt, ...)
1255 va_start(args, fmt);
1256 i=vsnprintf(buf, INT_MAX, fmt, args);
1260 EXPORT_SYMBOL(sprintf);
1262 #ifdef CONFIG_BINARY_PRINTF
1265 * vbin_printf() - VA arguments to binary data
1266 * bstr_printf() - Binary data to text string
1270 * vbin_printf - Parse a format string and place args' binary value in a buffer
1271 * @bin_buf: The buffer to place args' binary value
1272 * @size: The size of the buffer(by words(32bits), not characters)
1273 * @fmt: The format string to use
1274 * @args: Arguments for the format string
1276 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1279 * The return value is the number of words(32bits) which would be generated for
1283 * If the return value is greater than @size, the resulting bin_buf is NOT
1284 * valid for bstr_printf().
1286 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1288 struct printf_spec spec = {0};
1292 str = (char *)bin_buf;
1293 end = (char *)(bin_buf + size);
1295 #define save_arg(type) \
1297 if (sizeof(type) == 8) { \
1298 unsigned long long value; \
1299 str = PTR_ALIGN(str, sizeof(u32)); \
1300 value = va_arg(args, unsigned long long); \
1301 if (str + sizeof(type) <= end) { \
1302 *(u32 *)str = *(u32 *)&value; \
1303 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1306 unsigned long value; \
1307 str = PTR_ALIGN(str, sizeof(type)); \
1308 value = va_arg(args, int); \
1309 if (str + sizeof(type) <= end) \
1310 *(typeof(type) *)str = (type)value; \
1312 str += sizeof(type); \
1317 read = format_decode(fmt, &spec);
1321 switch (spec.type) {
1322 case FORMAT_TYPE_NONE:
1325 case FORMAT_TYPE_WIDTH:
1326 case FORMAT_TYPE_PRECISION:
1330 case FORMAT_TYPE_CHAR:
1334 case FORMAT_TYPE_STR: {
1335 const char *save_str = va_arg(args, char *);
1337 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1338 || (unsigned long)save_str < PAGE_SIZE)
1339 save_str = "<NULL>";
1340 len = strlen(save_str);
1341 if (str + len + 1 < end)
1342 memcpy(str, save_str, len + 1);
1347 case FORMAT_TYPE_PTR:
1349 /* skip all alphanumeric pointer suffixes */
1350 while (isalnum(*fmt))
1354 case FORMAT_TYPE_PERCENT_CHAR:
1357 case FORMAT_TYPE_INVALID:
1360 case FORMAT_TYPE_NRCHARS: {
1361 /* skip %n 's argument */
1362 int qualifier = spec.qualifier;
1364 if (qualifier == 'l')
1365 skip_arg = va_arg(args, long *);
1366 else if (qualifier == 'Z' || qualifier == 'z')
1367 skip_arg = va_arg(args, size_t *);
1369 skip_arg = va_arg(args, int *);
1374 switch (spec.type) {
1376 case FORMAT_TYPE_LONG_LONG:
1377 save_arg(long long);
1379 case FORMAT_TYPE_ULONG:
1380 case FORMAT_TYPE_LONG:
1381 save_arg(unsigned long);
1383 case FORMAT_TYPE_SIZE_T:
1386 case FORMAT_TYPE_PTRDIFF:
1387 save_arg(ptrdiff_t);
1389 case FORMAT_TYPE_UBYTE:
1390 case FORMAT_TYPE_BYTE:
1393 case FORMAT_TYPE_USHORT:
1394 case FORMAT_TYPE_SHORT:
1402 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1406 EXPORT_SYMBOL_GPL(vbin_printf);
1409 * bstr_printf - Format a string from binary arguments and place it in a buffer
1410 * @buf: The buffer to place the result into
1411 * @size: The size of the buffer, including the trailing null space
1412 * @fmt: The format string to use
1413 * @bin_buf: Binary arguments for the format string
1415 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1416 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1417 * a binary buffer that generated by vbin_printf.
1419 * The format follows C99 vsnprintf, but has some extensions:
1420 * %pS output the name of a text symbol
1421 * %pF output the name of a function pointer with its offset
1422 * %pf output the name of a function pointer without its offset
1423 * %pR output the address range in a struct resource
1426 * The return value is the number of characters which would
1427 * be generated for the given input, excluding the trailing
1428 * '\0', as per ISO C99. If you want to have the exact
1429 * number of characters written into @buf as return value
1430 * (not including the trailing '\0'), use vscnprintf(). If the
1431 * return is greater than or equal to @size, the resulting
1432 * string is truncated.
1434 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1436 unsigned long long num;
1438 const char *args = (const char *)bin_buf;
1440 struct printf_spec spec = {0};
1442 if (unlikely((int) size < 0)) {
1443 /* There can be only one.. */
1444 static char warn = 1;
1453 #define get_arg(type) \
1455 typeof(type) value; \
1456 if (sizeof(type) == 8) { \
1457 args = PTR_ALIGN(args, sizeof(u32)); \
1458 *(u32 *)&value = *(u32 *)args; \
1459 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1461 args = PTR_ALIGN(args, sizeof(type)); \
1462 value = *(typeof(type) *)args; \
1464 args += sizeof(type); \
1468 /* Make sure end is always >= buf */
1476 const char *old_fmt = fmt;
1478 read = format_decode(fmt, &spec);
1482 switch (spec.type) {
1483 case FORMAT_TYPE_NONE: {
1486 if (copy > end - str)
1488 memcpy(str, old_fmt, copy);
1494 case FORMAT_TYPE_WIDTH:
1495 spec.field_width = get_arg(int);
1498 case FORMAT_TYPE_PRECISION:
1499 spec.precision = get_arg(int);
1502 case FORMAT_TYPE_CHAR:
1503 if (!(spec.flags & LEFT)) {
1504 while (--spec.field_width > 0) {
1510 c = (unsigned char) get_arg(char);
1514 while (--spec.field_width > 0) {
1521 case FORMAT_TYPE_STR: {
1522 const char *str_arg = args;
1523 size_t len = strlen(str_arg);
1525 str = string(str, end, (char *)str_arg, spec);
1529 case FORMAT_TYPE_PTR:
1530 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1531 while (isalnum(*fmt))
1535 case FORMAT_TYPE_PERCENT_CHAR:
1541 case FORMAT_TYPE_INVALID:
1547 case FORMAT_TYPE_NRCHARS:
1552 switch (spec.type) {
1554 case FORMAT_TYPE_LONG_LONG:
1555 num = get_arg(long long);
1557 case FORMAT_TYPE_ULONG:
1558 num = get_arg(unsigned long);
1560 case FORMAT_TYPE_LONG:
1561 num = get_arg(unsigned long);
1563 case FORMAT_TYPE_SIZE_T:
1564 num = get_arg(size_t);
1566 case FORMAT_TYPE_PTRDIFF:
1567 num = get_arg(ptrdiff_t);
1569 case FORMAT_TYPE_UBYTE:
1570 num = get_arg(unsigned char);
1572 case FORMAT_TYPE_BYTE:
1573 num = get_arg(signed char);
1575 case FORMAT_TYPE_USHORT:
1576 num = get_arg(unsigned short);
1578 case FORMAT_TYPE_SHORT:
1579 num = get_arg(short);
1581 case FORMAT_TYPE_UINT:
1582 num = get_arg(unsigned int);
1588 str = number(str, end, num, spec);
1601 /* the trailing null byte doesn't count towards the total */
1604 EXPORT_SYMBOL_GPL(bstr_printf);
1607 * bprintf - Parse a format string and place args' binary value in a buffer
1608 * @bin_buf: The buffer to place args' binary value
1609 * @size: The size of the buffer(by words(32bits), not characters)
1610 * @fmt: The format string to use
1611 * @...: Arguments for the format string
1613 * The function returns the number of words(u32) written
1616 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1621 va_start(args, fmt);
1622 ret = vbin_printf(bin_buf, size, fmt, args);
1626 EXPORT_SYMBOL_GPL(bprintf);
1628 #endif /* CONFIG_BINARY_PRINTF */
1631 * vsscanf - Unformat a buffer into a list of arguments
1632 * @buf: input buffer
1633 * @fmt: format of buffer
1636 int vsscanf(const char * buf, const char * fmt, va_list args)
1638 const char *str = buf;
1647 while(*fmt && *str) {
1648 /* skip any white space in format */
1649 /* white space in format matchs any amount of
1650 * white space, including none, in the input.
1652 if (isspace(*fmt)) {
1653 while (isspace(*fmt))
1655 while (isspace(*str))
1659 /* anything that is not a conversion must match exactly */
1660 if (*fmt != '%' && *fmt) {
1661 if (*fmt++ != *str++)
1670 /* skip this conversion.
1671 * advance both strings to next white space
1674 while (!isspace(*fmt) && *fmt)
1676 while (!isspace(*str) && *str)
1681 /* get field width */
1684 field_width = skip_atoi(&fmt);
1686 /* get conversion qualifier */
1688 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1689 *fmt == 'Z' || *fmt == 'z') {
1691 if (unlikely(qualifier == *fmt)) {
1692 if (qualifier == 'h') {
1695 } else if (qualifier == 'l') {
1710 char *s = (char *) va_arg(args,char*);
1711 if (field_width == -1)
1715 } while (--field_width > 0 && *str);
1721 char *s = (char *) va_arg(args, char *);
1722 if(field_width == -1)
1723 field_width = INT_MAX;
1724 /* first, skip leading white space in buffer */
1725 while (isspace(*str))
1728 /* now copy until next white space */
1729 while (*str && !isspace(*str) && field_width--) {
1737 /* return number of characters read so far */
1739 int *i = (int *)va_arg(args,int*);
1757 /* looking for '%' in str */
1762 /* invalid format; stop here */
1766 /* have some sort of integer conversion.
1767 * first, skip white space in buffer.
1769 while (isspace(*str))
1773 if (is_sign && digit == '-')
1777 || (base == 16 && !isxdigit(digit))
1778 || (base == 10 && !isdigit(digit))
1779 || (base == 8 && (!isdigit(digit) || digit > '7'))
1780 || (base == 0 && !isdigit(digit)))
1784 case 'H': /* that's 'hh' in format */
1786 signed char *s = (signed char *) va_arg(args,signed char *);
1787 *s = (signed char) simple_strtol(str,&next,base);
1789 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1790 *s = (unsigned char) simple_strtoul(str, &next, base);
1795 short *s = (short *) va_arg(args,short *);
1796 *s = (short) simple_strtol(str,&next,base);
1798 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1799 *s = (unsigned short) simple_strtoul(str, &next, base);
1804 long *l = (long *) va_arg(args,long *);
1805 *l = simple_strtol(str,&next,base);
1807 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1808 *l = simple_strtoul(str,&next,base);
1813 long long *l = (long long*) va_arg(args,long long *);
1814 *l = simple_strtoll(str,&next,base);
1816 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1817 *l = simple_strtoull(str,&next,base);
1823 size_t *s = (size_t*) va_arg(args,size_t*);
1824 *s = (size_t) simple_strtoul(str,&next,base);
1829 int *i = (int *) va_arg(args, int*);
1830 *i = (int) simple_strtol(str,&next,base);
1832 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1833 *i = (unsigned int) simple_strtoul(str,&next,base);
1845 * Now we've come all the way through so either the input string or the
1846 * format ended. In the former case, there can be a %n at the current
1847 * position in the format that needs to be filled.
1849 if (*fmt == '%' && *(fmt + 1) == 'n') {
1850 int *p = (int *)va_arg(args, int *);
1856 EXPORT_SYMBOL(vsscanf);
1859 * sscanf - Unformat a buffer into a list of arguments
1860 * @buf: input buffer
1861 * @fmt: formatting of buffer
1862 * @...: resulting arguments
1864 int sscanf(const char * buf, const char * fmt, ...)
1870 i = vsscanf(buf,fmt,args);
1874 EXPORT_SYMBOL(sscanf);