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