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