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 |
| 658 | char *mac_address_string(char *buf, char *end, u8 *addr, |
| 659 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 660 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 661 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 662 | char *p = mac_addr; |
| 663 | int i; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 664 | char separator; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 665 | bool reversed = false; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 666 | |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 667 | switch (fmt[1]) { |
| 668 | case 'F': |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 669 | separator = '-'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 670 | break; |
| 671 | |
| 672 | case 'R': |
| 673 | reversed = true; |
| 674 | /* fall through */ |
| 675 | |
| 676 | default: |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 677 | separator = ':'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 678 | break; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 679 | } |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 680 | |
| 681 | for (i = 0; i < 6; i++) { |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 682 | if (reversed) |
| 683 | p = hex_byte_pack(p, addr[5 - i]); |
| 684 | else |
| 685 | p = hex_byte_pack(p, addr[i]); |
| 686 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 687 | if (fmt[0] == 'M' && i != 5) |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 688 | *p++ = separator; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 689 | } |
| 690 | *p = '\0'; |
| 691 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 692 | return string(buf, end, mac_addr, spec); |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 695 | static noinline_for_stack |
| 696 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 697 | { |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 698 | int i; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 699 | bool leading_zeros = (fmt[0] == 'i'); |
| 700 | int index; |
| 701 | int step; |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 702 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 703 | switch (fmt[2]) { |
| 704 | case 'h': |
| 705 | #ifdef __BIG_ENDIAN |
| 706 | index = 0; |
| 707 | step = 1; |
| 708 | #else |
| 709 | index = 3; |
| 710 | step = -1; |
| 711 | #endif |
| 712 | break; |
| 713 | case 'l': |
| 714 | index = 3; |
| 715 | step = -1; |
| 716 | break; |
| 717 | case 'n': |
| 718 | case 'b': |
| 719 | default: |
| 720 | index = 0; |
| 721 | step = 1; |
| 722 | break; |
| 723 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 724 | for (i = 0; i < 4; i++) { |
| 725 | char temp[3]; /* hold each IP quad in reverse order */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 726 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 727 | if (leading_zeros) { |
| 728 | if (digits < 3) |
| 729 | *p++ = '0'; |
| 730 | if (digits < 2) |
| 731 | *p++ = '0'; |
| 732 | } |
| 733 | /* reverse the digits in the quad */ |
| 734 | while (digits--) |
| 735 | *p++ = temp[digits]; |
| 736 | if (i < 3) |
| 737 | *p++ = '.'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 738 | index += step; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 739 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 740 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 741 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 742 | return p; |
| 743 | } |
| 744 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 745 | static noinline_for_stack |
| 746 | char *ip6_compressed_string(char *p, const char *addr) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 747 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 748 | int i, j, range; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 749 | unsigned char zerolength[8]; |
| 750 | int longest = 1; |
| 751 | int colonpos = -1; |
| 752 | u16 word; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 753 | u8 hi, lo; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 754 | bool needcolon = false; |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 755 | bool useIPv4; |
| 756 | struct in6_addr in6; |
| 757 | |
| 758 | memcpy(&in6, addr, sizeof(struct in6_addr)); |
| 759 | |
| 760 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 761 | |
| 762 | memset(zerolength, 0, sizeof(zerolength)); |
| 763 | |
| 764 | if (useIPv4) |
| 765 | range = 6; |
| 766 | else |
| 767 | range = 8; |
| 768 | |
| 769 | /* find position of longest 0 run */ |
| 770 | for (i = 0; i < range; i++) { |
| 771 | for (j = i; j < range; j++) { |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 772 | if (in6.s6_addr16[j] != 0) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 773 | break; |
| 774 | zerolength[i]++; |
| 775 | } |
| 776 | } |
| 777 | for (i = 0; i < range; i++) { |
| 778 | if (zerolength[i] > longest) { |
| 779 | longest = zerolength[i]; |
| 780 | colonpos = i; |
| 781 | } |
| 782 | } |
Joe Perches | 29cf519 | 2011-06-09 11:23:37 -0700 | [diff] [blame] | 783 | if (longest == 1) /* don't compress a single 0 */ |
| 784 | colonpos = -1; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 785 | |
| 786 | /* emit address */ |
| 787 | for (i = 0; i < range; i++) { |
| 788 | if (i == colonpos) { |
| 789 | if (needcolon || i == 0) |
| 790 | *p++ = ':'; |
| 791 | *p++ = ':'; |
| 792 | needcolon = false; |
| 793 | i += longest - 1; |
| 794 | continue; |
| 795 | } |
| 796 | if (needcolon) { |
| 797 | *p++ = ':'; |
| 798 | needcolon = false; |
| 799 | } |
| 800 | /* hex u16 without leading 0s */ |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 801 | word = ntohs(in6.s6_addr16[i]); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 802 | hi = word >> 8; |
| 803 | lo = word & 0xff; |
| 804 | if (hi) { |
| 805 | if (hi > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 806 | p = hex_byte_pack(p, hi); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 807 | else |
| 808 | *p++ = hex_asc_lo(hi); |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 809 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 810 | } |
André Goddard Rosa | b5ff992 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 811 | else if (lo > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 812 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 813 | else |
| 814 | *p++ = hex_asc_lo(lo); |
| 815 | needcolon = true; |
| 816 | } |
| 817 | |
| 818 | if (useIPv4) { |
| 819 | if (needcolon) |
| 820 | *p++ = ':'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 821 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 822 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 823 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 824 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 825 | return p; |
| 826 | } |
| 827 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 828 | static noinline_for_stack |
| 829 | char *ip6_string(char *p, const char *addr, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 830 | { |
| 831 | int i; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 832 | |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 833 | for (i = 0; i < 8; i++) { |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 834 | p = hex_byte_pack(p, *addr++); |
| 835 | p = hex_byte_pack(p, *addr++); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 836 | if (fmt[0] == 'I' && i != 7) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 837 | *p++ = ':'; |
| 838 | } |
| 839 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 840 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 841 | return p; |
| 842 | } |
| 843 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 844 | static noinline_for_stack |
| 845 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, |
| 846 | struct printf_spec spec, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 847 | { |
| 848 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
| 849 | |
| 850 | if (fmt[0] == 'I' && fmt[2] == 'c') |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 851 | ip6_compressed_string(ip6_addr, addr); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 852 | else |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 853 | ip6_string(ip6_addr, addr, fmt); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 854 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 855 | return string(buf, end, ip6_addr, spec); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 858 | static noinline_for_stack |
| 859 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
| 860 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 861 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 862 | char ip4_addr[sizeof("255.255.255.255")]; |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 863 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 864 | ip4_string(ip4_addr, addr, fmt); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 865 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 866 | return string(buf, end, ip4_addr, spec); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 869 | static noinline_for_stack |
| 870 | char *uuid_string(char *buf, char *end, const u8 *addr, |
| 871 | struct printf_spec spec, const char *fmt) |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 872 | { |
| 873 | char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; |
| 874 | char *p = uuid; |
| 875 | int i; |
| 876 | static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
| 877 | static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; |
| 878 | const u8 *index = be; |
| 879 | bool uc = false; |
| 880 | |
| 881 | switch (*(++fmt)) { |
| 882 | case 'L': |
| 883 | uc = true; /* fall-through */ |
| 884 | case 'l': |
| 885 | index = le; |
| 886 | break; |
| 887 | case 'B': |
| 888 | uc = true; |
| 889 | break; |
| 890 | } |
| 891 | |
| 892 | for (i = 0; i < 16; i++) { |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 893 | p = hex_byte_pack(p, addr[index[i]]); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 894 | switch (i) { |
| 895 | case 3: |
| 896 | case 5: |
| 897 | case 7: |
| 898 | case 9: |
| 899 | *p++ = '-'; |
| 900 | break; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | *p = 0; |
| 905 | |
| 906 | if (uc) { |
| 907 | p = uuid; |
| 908 | do { |
| 909 | *p = toupper(*p); |
| 910 | } while (*(++p)); |
| 911 | } |
| 912 | |
| 913 | return string(buf, end, uuid, spec); |
| 914 | } |
| 915 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 916 | static |
| 917 | char *netdev_feature_string(char *buf, char *end, const u8 *addr, |
| 918 | struct printf_spec spec) |
| 919 | { |
| 920 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 921 | if (spec.field_width == -1) |
| 922 | spec.field_width = 2 + 2 * sizeof(netdev_features_t); |
| 923 | spec.base = 16; |
| 924 | |
| 925 | return number(buf, end, *(const netdev_features_t *)addr, spec); |
| 926 | } |
| 927 | |
Ingo Molnar | 411f05f | 2011-05-12 23:00:28 +0200 | [diff] [blame] | 928 | int kptr_restrict __read_mostly; |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 929 | |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 930 | /* |
| 931 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 932 | * by an extra set of alphanumeric characters that are extended format |
| 933 | * specifiers. |
| 934 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 935 | * Right now we handle: |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 936 | * |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 937 | * - 'F' For symbolic function descriptor pointers with offset |
| 938 | * - 'f' For simple symbolic function names without offset |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 939 | * - 'S' For symbolic direct pointers with offset |
| 940 | * - 's' For symbolic direct pointers without offset |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 941 | * - 'B' For backtraced symbolic direct pointers with offset |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 942 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
| 943 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 944 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 945 | * usual colon-separated hex notation |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 946 | * - '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] | 947 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
Joe Perches | c8e0006 | 2010-01-11 00:44:14 -0800 | [diff] [blame] | 948 | * with a dash-separated hex notation |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 949 | * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 950 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
| 951 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
| 952 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
| 953 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
| 954 | * IPv6 omits the colons (01020304...0f) |
| 955 | * 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] | 956 | * - '[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] | 957 | * - 'I6c' for IPv6 addresses printed as specified by |
Joe Perches | 29cf519 | 2011-06-09 11:23:37 -0700 | [diff] [blame] | 958 | * http://tools.ietf.org/html/rfc5952 |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 959 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
| 960 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 961 | * Options for %pU are: |
| 962 | * b big endian lower case hex (default) |
| 963 | * B big endian UPPER case hex |
| 964 | * l little endian lower case hex |
| 965 | * L little endian UPPER case hex |
| 966 | * big endian output byte order is: |
| 967 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
| 968 | * little endian output byte order is: |
| 969 | * [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] | 970 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
| 971 | * call vsnprintf(->format, *->va_list). |
| 972 | * Implements a "recursive vsnprintf". |
| 973 | * Do not use this feature without some mechanism to verify the |
| 974 | * correctness of the format string and va_list arguments. |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 975 | * - '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] | 976 | * - 'NF' For a netdev_features_t |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 977 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 978 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 979 | * function pointers are really function descriptors, which contain a |
| 980 | * pointer to the real address. |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 981 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 982 | static noinline_for_stack |
| 983 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 984 | struct printf_spec spec) |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 985 | { |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 986 | int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); |
| 987 | |
Kees Cook | 9f36e2c | 2011-03-22 16:34:22 -0700 | [diff] [blame] | 988 | if (!ptr && *fmt != 'K') { |
Joe Perches | 5e05798 | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 989 | /* |
| 990 | * Print (null) with the same width as a pointer so it makes |
| 991 | * tabular output look nice. |
| 992 | */ |
| 993 | if (spec.field_width == -1) |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 994 | spec.field_width = default_width; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 995 | return string(buf, end, "(null)", spec); |
Joe Perches | 5e05798 | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 996 | } |
Linus Torvalds | d97106a | 2009-01-03 11:46:17 -0800 | [diff] [blame] | 997 | |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 998 | switch (*fmt) { |
| 999 | case 'F': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1000 | case 'f': |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1001 | ptr = dereference_function_descriptor(ptr); |
| 1002 | /* Fallthrough */ |
| 1003 | case 'S': |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1004 | case 's': |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1005 | case 'B': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1006 | return symbol_string(buf, end, ptr, spec, *fmt); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1007 | case 'R': |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1008 | case 'r': |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1009 | return resource_string(buf, end, ptr, spec, fmt); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1010 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
| 1011 | case 'm': /* Contiguous: 000102030405 */ |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1012 | /* [mM]F (FDDI) */ |
| 1013 | /* [mM]R (Reverse order; Bluetooth) */ |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1014 | return mac_address_string(buf, end, ptr, spec, fmt); |
| 1015 | case 'I': /* Formatted IP supported |
| 1016 | * 4: 1.2.3.4 |
| 1017 | * 6: 0001:0203:...:0708 |
| 1018 | * 6c: 1::708 or 1::1.2.3.4 |
| 1019 | */ |
| 1020 | case 'i': /* Contiguous: |
| 1021 | * 4: 001.002.003.004 |
| 1022 | * 6: 000102...0f |
| 1023 | */ |
| 1024 | switch (fmt[1]) { |
| 1025 | case '6': |
| 1026 | return ip6_addr_string(buf, end, ptr, spec, fmt); |
| 1027 | case '4': |
| 1028 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
| 1029 | } |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1030 | break; |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1031 | case 'U': |
| 1032 | return uuid_string(buf, end, ptr, spec, fmt); |
Joe Perches | 7db6f5f | 2010-06-27 01:02:33 +0000 | [diff] [blame] | 1033 | case 'V': |
Jan Beulich | 5756b76 | 2012-03-05 16:49:24 +0000 | [diff] [blame] | 1034 | { |
| 1035 | va_list va; |
| 1036 | |
| 1037 | va_copy(va, *((struct va_format *)ptr)->va); |
| 1038 | buf += vsnprintf(buf, end > buf ? end - buf : 0, |
| 1039 | ((struct va_format *)ptr)->fmt, va); |
| 1040 | va_end(va); |
| 1041 | return buf; |
| 1042 | } |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1043 | case 'K': |
| 1044 | /* |
| 1045 | * %pK cannot be used in IRQ context because its test |
| 1046 | * for CAP_SYSLOG would be meaningless. |
| 1047 | */ |
| 1048 | if (in_irq() || in_serving_softirq() || in_nmi()) { |
| 1049 | if (spec.field_width == -1) |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1050 | spec.field_width = default_width; |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1051 | return string(buf, end, "pK-error", spec); |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1052 | } |
Joe Perches | 2629760 | 2011-03-22 16:34:19 -0700 | [diff] [blame] | 1053 | if (!((kptr_restrict == 0) || |
| 1054 | (kptr_restrict == 1 && |
| 1055 | has_capability_noaudit(current, CAP_SYSLOG)))) |
| 1056 | ptr = NULL; |
| 1057 | break; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1058 | case 'N': |
| 1059 | switch (fmt[1]) { |
| 1060 | case 'F': |
| 1061 | return netdev_feature_string(buf, end, ptr, spec); |
| 1062 | } |
| 1063 | break; |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1064 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1065 | spec.flags |= SMALL; |
| 1066 | if (spec.field_width == -1) { |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1067 | spec.field_width = default_width; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1068 | spec.flags |= ZEROPAD; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1069 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1070 | spec.base = 16; |
| 1071 | |
| 1072 | return number(buf, end, (unsigned long) ptr, spec); |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Helper function to decode printf style format. |
| 1077 | * Each call decode a token from the format and return the |
| 1078 | * number of characters read (or likely the delta where it wants |
| 1079 | * to go on the next call). |
| 1080 | * The decoded token is returned through the parameters |
| 1081 | * |
| 1082 | * 'h', 'l', or 'L' for integer fields |
| 1083 | * 'z' support added 23/7/1999 S.H. |
| 1084 | * 'z' changed to 'Z' --davidm 1/25/99 |
| 1085 | * 't' added for ptrdiff_t |
| 1086 | * |
| 1087 | * @fmt: the format string |
| 1088 | * @type of the token returned |
| 1089 | * @flags: various flags such as +, -, # tokens.. |
| 1090 | * @field_width: overwritten width |
| 1091 | * @base: base of the number (octal, hex, ...) |
| 1092 | * @precision: precision of a number |
| 1093 | * @qualifier: qualifier of a number (long, size_t, ...) |
| 1094 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1095 | static noinline_for_stack |
| 1096 | int format_decode(const char *fmt, struct printf_spec *spec) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1097 | { |
| 1098 | const char *start = fmt; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1099 | |
| 1100 | /* we finished early by reading the field width */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1101 | if (spec->type == FORMAT_TYPE_WIDTH) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1102 | if (spec->field_width < 0) { |
| 1103 | spec->field_width = -spec->field_width; |
| 1104 | spec->flags |= LEFT; |
| 1105 | } |
| 1106 | spec->type = FORMAT_TYPE_NONE; |
| 1107 | goto precision; |
| 1108 | } |
| 1109 | |
| 1110 | /* we finished early by reading the precision */ |
| 1111 | if (spec->type == FORMAT_TYPE_PRECISION) { |
| 1112 | if (spec->precision < 0) |
| 1113 | spec->precision = 0; |
| 1114 | |
| 1115 | spec->type = FORMAT_TYPE_NONE; |
| 1116 | goto qualifier; |
| 1117 | } |
| 1118 | |
| 1119 | /* By default */ |
| 1120 | spec->type = FORMAT_TYPE_NONE; |
| 1121 | |
| 1122 | for (; *fmt ; ++fmt) { |
| 1123 | if (*fmt == '%') |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | /* Return the current non-format string */ |
| 1128 | if (fmt != start || !*fmt) |
| 1129 | return fmt - start; |
| 1130 | |
| 1131 | /* Process flags */ |
| 1132 | spec->flags = 0; |
| 1133 | |
| 1134 | while (1) { /* this also skips first '%' */ |
| 1135 | bool found = true; |
| 1136 | |
| 1137 | ++fmt; |
| 1138 | |
| 1139 | switch (*fmt) { |
| 1140 | case '-': spec->flags |= LEFT; break; |
| 1141 | case '+': spec->flags |= PLUS; break; |
| 1142 | case ' ': spec->flags |= SPACE; break; |
| 1143 | case '#': spec->flags |= SPECIAL; break; |
| 1144 | case '0': spec->flags |= ZEROPAD; break; |
| 1145 | default: found = false; |
| 1146 | } |
| 1147 | |
| 1148 | if (!found) |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | /* get field width */ |
| 1153 | spec->field_width = -1; |
| 1154 | |
| 1155 | if (isdigit(*fmt)) |
| 1156 | spec->field_width = skip_atoi(&fmt); |
| 1157 | else if (*fmt == '*') { |
| 1158 | /* it's the next argument */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1159 | spec->type = FORMAT_TYPE_WIDTH; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1160 | return ++fmt - start; |
| 1161 | } |
| 1162 | |
| 1163 | precision: |
| 1164 | /* get the precision */ |
| 1165 | spec->precision = -1; |
| 1166 | if (*fmt == '.') { |
| 1167 | ++fmt; |
| 1168 | if (isdigit(*fmt)) { |
| 1169 | spec->precision = skip_atoi(&fmt); |
| 1170 | if (spec->precision < 0) |
| 1171 | spec->precision = 0; |
| 1172 | } else if (*fmt == '*') { |
| 1173 | /* it's the next argument */ |
Vegard Nossum | adf26f8 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1174 | spec->type = FORMAT_TYPE_PRECISION; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1175 | return ++fmt - start; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | qualifier: |
| 1180 | /* get the conversion qualifier */ |
| 1181 | spec->qualifier = -1; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 1182 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
| 1183 | _tolower(*fmt) == 'z' || *fmt == 't') { |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1184 | spec->qualifier = *fmt++; |
| 1185 | if (unlikely(spec->qualifier == *fmt)) { |
| 1186 | if (spec->qualifier == 'l') { |
| 1187 | spec->qualifier = 'L'; |
| 1188 | ++fmt; |
| 1189 | } else if (spec->qualifier == 'h') { |
| 1190 | spec->qualifier = 'H'; |
| 1191 | ++fmt; |
| 1192 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | /* default base */ |
| 1197 | spec->base = 10; |
| 1198 | switch (*fmt) { |
| 1199 | case 'c': |
| 1200 | spec->type = FORMAT_TYPE_CHAR; |
| 1201 | return ++fmt - start; |
| 1202 | |
| 1203 | case 's': |
| 1204 | spec->type = FORMAT_TYPE_STR; |
| 1205 | return ++fmt - start; |
| 1206 | |
| 1207 | case 'p': |
| 1208 | spec->type = FORMAT_TYPE_PTR; |
| 1209 | return fmt - start; |
| 1210 | /* skip alnum */ |
| 1211 | |
| 1212 | case 'n': |
| 1213 | spec->type = FORMAT_TYPE_NRCHARS; |
| 1214 | return ++fmt - start; |
| 1215 | |
| 1216 | case '%': |
| 1217 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
| 1218 | return ++fmt - start; |
| 1219 | |
| 1220 | /* integer number formats - set up the flags and "break" */ |
| 1221 | case 'o': |
| 1222 | spec->base = 8; |
| 1223 | break; |
| 1224 | |
| 1225 | case 'x': |
| 1226 | spec->flags |= SMALL; |
| 1227 | |
| 1228 | case 'X': |
| 1229 | spec->base = 16; |
| 1230 | break; |
| 1231 | |
| 1232 | case 'd': |
| 1233 | case 'i': |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1234 | spec->flags |= SIGN; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1235 | case 'u': |
| 1236 | break; |
| 1237 | |
| 1238 | default: |
| 1239 | spec->type = FORMAT_TYPE_INVALID; |
| 1240 | return fmt - start; |
| 1241 | } |
| 1242 | |
| 1243 | if (spec->qualifier == 'L') |
| 1244 | spec->type = FORMAT_TYPE_LONG_LONG; |
| 1245 | else if (spec->qualifier == 'l') { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1246 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1247 | spec->type = FORMAT_TYPE_LONG; |
| 1248 | else |
| 1249 | spec->type = FORMAT_TYPE_ULONG; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 1250 | } else if (_tolower(spec->qualifier) == 'z') { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1251 | spec->type = FORMAT_TYPE_SIZE_T; |
| 1252 | } else if (spec->qualifier == 't') { |
| 1253 | spec->type = FORMAT_TYPE_PTRDIFF; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1254 | } else if (spec->qualifier == 'H') { |
| 1255 | if (spec->flags & SIGN) |
| 1256 | spec->type = FORMAT_TYPE_BYTE; |
| 1257 | else |
| 1258 | spec->type = FORMAT_TYPE_UBYTE; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1259 | } else if (spec->qualifier == 'h') { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1260 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1261 | spec->type = FORMAT_TYPE_SHORT; |
| 1262 | else |
| 1263 | spec->type = FORMAT_TYPE_USHORT; |
| 1264 | } else { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1265 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1266 | spec->type = FORMAT_TYPE_INT; |
| 1267 | else |
| 1268 | spec->type = FORMAT_TYPE_UINT; |
| 1269 | } |
| 1270 | |
| 1271 | return ++fmt - start; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | /** |
| 1275 | * vsnprintf - Format a string and place it in a buffer |
| 1276 | * @buf: The buffer to place the result into |
| 1277 | * @size: The size of the buffer, including the trailing null space |
| 1278 | * @fmt: The format string to use |
| 1279 | * @args: Arguments for the format string |
| 1280 | * |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1281 | * This function follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 91adcd2 | 2009-09-16 20:03:06 -0400 | [diff] [blame] | 1282 | * %pS output the name of a text symbol with offset |
| 1283 | * %ps output the name of a text symbol without offset |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1284 | * %pF output the name of a function pointer with its offset |
| 1285 | * %pf output the name of a function pointer without its offset |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1286 | * %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] | 1287 | * %pR output the address range in a struct resource with decoded flags |
| 1288 | * %pr output the address range in a struct resource with raw flags |
| 1289 | * %pM output a 6-byte MAC address with colons |
| 1290 | * %pm output a 6-byte MAC address without colons |
| 1291 | * %pI4 print an IPv4 address without leading zeros |
| 1292 | * %pi4 print an IPv4 address with leading zeros |
| 1293 | * %pI6 print an IPv6 address with colons |
| 1294 | * %pi6 print an IPv6 address without colons |
Jan Engelhardt | f996f20 | 2011-07-14 18:48:56 +0200 | [diff] [blame] | 1295 | * %pI6c print an IPv6 address as specified by RFC 5952 |
Uwe Kleine-König | 8a79503 | 2009-12-17 15:27:12 -0800 | [diff] [blame] | 1296 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
| 1297 | * case. |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 1298 | * %n is ignored |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1299 | * |
Andrew Morton | 80f548e | 2012-07-30 14:40:25 -0700 | [diff] [blame^] | 1300 | * ** Please update Documentation/printk-formats.txt when making changes ** |
| 1301 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | * The return value is the number of characters which would |
| 1303 | * be generated for the given input, excluding the trailing |
| 1304 | * '\0', as per ISO C99. If you want to have the exact |
| 1305 | * number of characters written into @buf as return value |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1306 | * (not including the trailing '\0'), use vscnprintf(). If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | * return is greater than or equal to @size, the resulting |
| 1308 | * string is truncated. |
| 1309 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 1310 | * 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] | 1311 | */ |
| 1312 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1313 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | unsigned long long num; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1315 | char *str, *end; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1316 | struct printf_spec spec = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1318 | /* Reject out-of-range values early. Large positive sizes are |
| 1319 | used for unknown buffer sizes. */ |
Marcin Slusarz | 2f30b1f9 | 2009-09-21 17:04:29 -0700 | [diff] [blame] | 1320 | if (WARN_ON_ONCE((int) size < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| 1323 | str = buf; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1324 | end = buf + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1326 | /* Make sure end is always >= buf */ |
| 1327 | if (end < buf) { |
| 1328 | end = ((void *)-1); |
| 1329 | size = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1332 | while (*fmt) { |
| 1333 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1334 | int read = format_decode(fmt, &spec); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1335 | |
| 1336 | fmt += read; |
| 1337 | |
| 1338 | switch (spec.type) { |
| 1339 | case FORMAT_TYPE_NONE: { |
| 1340 | int copy = read; |
| 1341 | if (str < end) { |
| 1342 | if (copy > end - str) |
| 1343 | copy = end - str; |
| 1344 | memcpy(str, old_fmt, copy); |
| 1345 | } |
| 1346 | str += read; |
| 1347 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1350 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1351 | spec.field_width = va_arg(args, int); |
| 1352 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1354 | case FORMAT_TYPE_PRECISION: |
| 1355 | spec.precision = va_arg(args, int); |
| 1356 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1358 | case FORMAT_TYPE_CHAR: { |
| 1359 | char c; |
| 1360 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1361 | if (!(spec.flags & LEFT)) { |
| 1362 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1363 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | *str = ' '; |
| 1365 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1368 | } |
| 1369 | c = (unsigned char) va_arg(args, int); |
| 1370 | if (str < end) |
| 1371 | *str = c; |
| 1372 | ++str; |
| 1373 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1374 | if (str < end) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1375 | *str = ' '; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1377 | } |
| 1378 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1381 | case FORMAT_TYPE_STR: |
| 1382 | str = string(str, end, va_arg(args, char *), spec); |
| 1383 | break; |
| 1384 | |
| 1385 | case FORMAT_TYPE_PTR: |
| 1386 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
| 1387 | spec); |
| 1388 | while (isalnum(*fmt)) |
| 1389 | fmt++; |
| 1390 | break; |
| 1391 | |
| 1392 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1393 | if (str < end) |
| 1394 | *str = '%'; |
| 1395 | ++str; |
| 1396 | break; |
| 1397 | |
| 1398 | case FORMAT_TYPE_INVALID: |
| 1399 | if (str < end) |
| 1400 | *str = '%'; |
| 1401 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1402 | break; |
| 1403 | |
| 1404 | case FORMAT_TYPE_NRCHARS: { |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 1405 | u8 qualifier = spec.qualifier; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1406 | |
| 1407 | if (qualifier == 'l') { |
| 1408 | long *ip = va_arg(args, long *); |
| 1409 | *ip = (str - buf); |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 1410 | } else if (_tolower(qualifier) == 'z') { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1411 | size_t *ip = va_arg(args, size_t *); |
| 1412 | *ip = (str - buf); |
| 1413 | } else { |
| 1414 | int *ip = va_arg(args, int *); |
| 1415 | *ip = (str - buf); |
| 1416 | } |
| 1417 | break; |
| 1418 | } |
| 1419 | |
| 1420 | default: |
| 1421 | switch (spec.type) { |
| 1422 | case FORMAT_TYPE_LONG_LONG: |
| 1423 | num = va_arg(args, long long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1425 | case FORMAT_TYPE_ULONG: |
| 1426 | num = va_arg(args, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1428 | case FORMAT_TYPE_LONG: |
| 1429 | num = va_arg(args, long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1431 | case FORMAT_TYPE_SIZE_T: |
| 1432 | num = va_arg(args, size_t); |
| 1433 | break; |
| 1434 | case FORMAT_TYPE_PTRDIFF: |
| 1435 | num = va_arg(args, ptrdiff_t); |
| 1436 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1437 | case FORMAT_TYPE_UBYTE: |
| 1438 | num = (unsigned char) va_arg(args, int); |
| 1439 | break; |
| 1440 | case FORMAT_TYPE_BYTE: |
| 1441 | num = (signed char) va_arg(args, int); |
| 1442 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1443 | case FORMAT_TYPE_USHORT: |
| 1444 | num = (unsigned short) va_arg(args, int); |
| 1445 | break; |
| 1446 | case FORMAT_TYPE_SHORT: |
| 1447 | num = (short) va_arg(args, int); |
| 1448 | break; |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1449 | case FORMAT_TYPE_INT: |
| 1450 | num = (int) va_arg(args, int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1451 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1453 | num = va_arg(args, unsigned int); |
| 1454 | } |
| 1455 | |
| 1456 | str = number(str, end, num, spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1459 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1460 | if (size > 0) { |
| 1461 | if (str < end) |
| 1462 | *str = '\0'; |
| 1463 | else |
Linus Torvalds | 0a6047e | 2006-06-28 17:09:34 -0700 | [diff] [blame] | 1464 | end[-1] = '\0'; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1465 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1466 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1467 | /* the trailing null byte doesn't count towards the total */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | return str-buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | EXPORT_SYMBOL(vsnprintf); |
| 1472 | |
| 1473 | /** |
| 1474 | * vscnprintf - Format a string and place it in a buffer |
| 1475 | * @buf: The buffer to place the result into |
| 1476 | * @size: The size of the buffer, including the trailing null space |
| 1477 | * @fmt: The format string to use |
| 1478 | * @args: Arguments for the format string |
| 1479 | * |
| 1480 | * 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] | 1481 | * 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] | 1482 | * returns 0. |
| 1483 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 1484 | * 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] | 1485 | * |
| 1486 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | */ |
| 1488 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1489 | { |
| 1490 | int i; |
| 1491 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1492 | i = vsnprintf(buf, size, fmt, args); |
| 1493 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 1494 | if (likely(i < size)) |
| 1495 | return i; |
| 1496 | if (size != 0) |
| 1497 | return size - 1; |
| 1498 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | EXPORT_SYMBOL(vscnprintf); |
| 1501 | |
| 1502 | /** |
| 1503 | * snprintf - Format a string and place it in a buffer |
| 1504 | * @buf: The buffer to place the result into |
| 1505 | * @size: The size of the buffer, including the trailing null space |
| 1506 | * @fmt: The format string to use |
| 1507 | * @...: Arguments for the format string |
| 1508 | * |
| 1509 | * The return value is the number of characters which would be |
| 1510 | * generated for the given input, excluding the trailing null, |
| 1511 | * as per ISO C99. If the return is greater than or equal to |
| 1512 | * @size, the resulting string is truncated. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1513 | * |
| 1514 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1516 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | { |
| 1518 | va_list args; |
| 1519 | int i; |
| 1520 | |
| 1521 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1522 | i = vsnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | return i; |
| 1526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | EXPORT_SYMBOL(snprintf); |
| 1528 | |
| 1529 | /** |
| 1530 | * scnprintf - 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 | * @...: Arguments for the format string |
| 1535 | * |
| 1536 | * 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] | 1537 | * the trailing '\0'. If @size is == 0 the function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | */ |
| 1539 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1540 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | { |
| 1542 | va_list args; |
| 1543 | int i; |
| 1544 | |
| 1545 | va_start(args, fmt); |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 1546 | i = vscnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1548 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 1549 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | } |
| 1551 | EXPORT_SYMBOL(scnprintf); |
| 1552 | |
| 1553 | /** |
| 1554 | * vsprintf - Format a string and place it in a buffer |
| 1555 | * @buf: The buffer to place the result into |
| 1556 | * @fmt: The format string to use |
| 1557 | * @args: Arguments for the format string |
| 1558 | * |
| 1559 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1560 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | * buffer overflows. |
| 1562 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 1563 | * 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] | 1564 | * |
| 1565 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | */ |
| 1567 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 1568 | { |
| 1569 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 1570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | EXPORT_SYMBOL(vsprintf); |
| 1572 | |
| 1573 | /** |
| 1574 | * sprintf - Format a string and place it in a buffer |
| 1575 | * @buf: The buffer to place the result into |
| 1576 | * @fmt: The format string to use |
| 1577 | * @...: Arguments for the format string |
| 1578 | * |
| 1579 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1580 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | * buffer overflows. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1582 | * |
| 1583 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1585 | int sprintf(char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | { |
| 1587 | va_list args; |
| 1588 | int i; |
| 1589 | |
| 1590 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1591 | i = vsnprintf(buf, INT_MAX, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | return i; |
| 1595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | EXPORT_SYMBOL(sprintf); |
| 1597 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1598 | #ifdef CONFIG_BINARY_PRINTF |
| 1599 | /* |
| 1600 | * bprintf service: |
| 1601 | * vbin_printf() - VA arguments to binary data |
| 1602 | * bstr_printf() - Binary data to text string |
| 1603 | */ |
| 1604 | |
| 1605 | /** |
| 1606 | * vbin_printf - Parse a format string and place args' binary value in a buffer |
| 1607 | * @bin_buf: The buffer to place args' binary value |
| 1608 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1609 | * @fmt: The format string to use |
| 1610 | * @args: Arguments for the format string |
| 1611 | * |
| 1612 | * The format follows C99 vsnprintf, except %n is ignored, and its argument |
| 1613 | * is skiped. |
| 1614 | * |
| 1615 | * The return value is the number of words(32bits) which would be generated for |
| 1616 | * the given input. |
| 1617 | * |
| 1618 | * NOTE: |
| 1619 | * If the return value is greater than @size, the resulting bin_buf is NOT |
| 1620 | * valid for bstr_printf(). |
| 1621 | */ |
| 1622 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) |
| 1623 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1624 | struct printf_spec spec = {0}; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1625 | char *str, *end; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1626 | |
| 1627 | str = (char *)bin_buf; |
| 1628 | end = (char *)(bin_buf + size); |
| 1629 | |
| 1630 | #define save_arg(type) \ |
| 1631 | do { \ |
| 1632 | if (sizeof(type) == 8) { \ |
| 1633 | unsigned long long value; \ |
| 1634 | str = PTR_ALIGN(str, sizeof(u32)); \ |
| 1635 | value = va_arg(args, unsigned long long); \ |
| 1636 | if (str + sizeof(type) <= end) { \ |
| 1637 | *(u32 *)str = *(u32 *)&value; \ |
| 1638 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ |
| 1639 | } \ |
| 1640 | } else { \ |
| 1641 | unsigned long value; \ |
| 1642 | str = PTR_ALIGN(str, sizeof(type)); \ |
| 1643 | value = va_arg(args, int); \ |
| 1644 | if (str + sizeof(type) <= end) \ |
| 1645 | *(typeof(type) *)str = (type)value; \ |
| 1646 | } \ |
| 1647 | str += sizeof(type); \ |
| 1648 | } while (0) |
| 1649 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1650 | while (*fmt) { |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1651 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1652 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1653 | fmt += read; |
| 1654 | |
| 1655 | switch (spec.type) { |
| 1656 | case FORMAT_TYPE_NONE: |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1657 | case FORMAT_TYPE_INVALID: |
| 1658 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1659 | break; |
| 1660 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1661 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1662 | case FORMAT_TYPE_PRECISION: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1663 | save_arg(int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1664 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1665 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1666 | case FORMAT_TYPE_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1667 | save_arg(char); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1668 | break; |
| 1669 | |
| 1670 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1671 | const char *save_str = va_arg(args, char *); |
| 1672 | size_t len; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 1673 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1674 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
| 1675 | || (unsigned long)save_str < PAGE_SIZE) |
André Goddard Rosa | 0f4f81d | 2009-12-14 18:00:55 -0800 | [diff] [blame] | 1676 | save_str = "(null)"; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 1677 | len = strlen(save_str) + 1; |
| 1678 | if (str + len < end) |
| 1679 | memcpy(str, save_str, len); |
| 1680 | str += len; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1681 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1682 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1683 | |
| 1684 | case FORMAT_TYPE_PTR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1685 | save_arg(void *); |
| 1686 | /* skip all alphanumeric pointer suffixes */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1687 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1688 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1689 | break; |
| 1690 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1691 | case FORMAT_TYPE_NRCHARS: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1692 | /* skip %n 's argument */ |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 1693 | u8 qualifier = spec.qualifier; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1694 | void *skip_arg; |
| 1695 | if (qualifier == 'l') |
| 1696 | skip_arg = va_arg(args, long *); |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 1697 | else if (_tolower(qualifier) == 'z') |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1698 | skip_arg = va_arg(args, size_t *); |
| 1699 | else |
| 1700 | skip_arg = va_arg(args, int *); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1701 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1702 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1703 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1704 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1705 | switch (spec.type) { |
| 1706 | |
| 1707 | case FORMAT_TYPE_LONG_LONG: |
| 1708 | save_arg(long long); |
| 1709 | break; |
| 1710 | case FORMAT_TYPE_ULONG: |
| 1711 | case FORMAT_TYPE_LONG: |
| 1712 | save_arg(unsigned long); |
| 1713 | break; |
| 1714 | case FORMAT_TYPE_SIZE_T: |
| 1715 | save_arg(size_t); |
| 1716 | break; |
| 1717 | case FORMAT_TYPE_PTRDIFF: |
| 1718 | save_arg(ptrdiff_t); |
| 1719 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1720 | case FORMAT_TYPE_UBYTE: |
| 1721 | case FORMAT_TYPE_BYTE: |
| 1722 | save_arg(char); |
| 1723 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1724 | case FORMAT_TYPE_USHORT: |
| 1725 | case FORMAT_TYPE_SHORT: |
| 1726 | save_arg(short); |
| 1727 | break; |
| 1728 | default: |
| 1729 | save_arg(int); |
| 1730 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1731 | } |
| 1732 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1733 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1734 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1735 | #undef save_arg |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1736 | } |
| 1737 | EXPORT_SYMBOL_GPL(vbin_printf); |
| 1738 | |
| 1739 | /** |
| 1740 | * bstr_printf - Format a string from binary arguments and place it in a buffer |
| 1741 | * @buf: The buffer to place the result into |
| 1742 | * @size: The size of the buffer, including the trailing null space |
| 1743 | * @fmt: The format string to use |
| 1744 | * @bin_buf: Binary arguments for the format string |
| 1745 | * |
| 1746 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets |
| 1747 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is |
| 1748 | * a binary buffer that generated by vbin_printf. |
| 1749 | * |
| 1750 | * The format follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 1751 | * see vsnprintf comment for details. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1752 | * |
| 1753 | * The return value is the number of characters which would |
| 1754 | * be generated for the given input, excluding the trailing |
| 1755 | * '\0', as per ISO C99. If you want to have the exact |
| 1756 | * number of characters written into @buf as return value |
| 1757 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 1758 | * return is greater than or equal to @size, the resulting |
| 1759 | * string is truncated. |
| 1760 | */ |
| 1761 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
| 1762 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1763 | struct printf_spec spec = {0}; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1764 | char *str, *end; |
| 1765 | const char *args = (const char *)bin_buf; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1766 | |
Marcin Slusarz | 2f30b1f9 | 2009-09-21 17:04:29 -0700 | [diff] [blame] | 1767 | if (WARN_ON_ONCE((int) size < 0)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1768 | return 0; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1769 | |
| 1770 | str = buf; |
| 1771 | end = buf + size; |
| 1772 | |
| 1773 | #define get_arg(type) \ |
| 1774 | ({ \ |
| 1775 | typeof(type) value; \ |
| 1776 | if (sizeof(type) == 8) { \ |
| 1777 | args = PTR_ALIGN(args, sizeof(u32)); \ |
| 1778 | *(u32 *)&value = *(u32 *)args; \ |
| 1779 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ |
| 1780 | } else { \ |
| 1781 | args = PTR_ALIGN(args, sizeof(type)); \ |
| 1782 | value = *(typeof(type) *)args; \ |
| 1783 | } \ |
| 1784 | args += sizeof(type); \ |
| 1785 | value; \ |
| 1786 | }) |
| 1787 | |
| 1788 | /* Make sure end is always >= buf */ |
| 1789 | if (end < buf) { |
| 1790 | end = ((void *)-1); |
| 1791 | size = end - buf; |
| 1792 | } |
| 1793 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1794 | while (*fmt) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1795 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1796 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1797 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1798 | fmt += read; |
| 1799 | |
| 1800 | switch (spec.type) { |
| 1801 | case FORMAT_TYPE_NONE: { |
| 1802 | int copy = read; |
| 1803 | if (str < end) { |
| 1804 | if (copy > end - str) |
| 1805 | copy = end - str; |
| 1806 | memcpy(str, old_fmt, copy); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1807 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1808 | str += read; |
| 1809 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1810 | } |
| 1811 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1812 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1813 | spec.field_width = get_arg(int); |
| 1814 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1815 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1816 | case FORMAT_TYPE_PRECISION: |
| 1817 | spec.precision = get_arg(int); |
| 1818 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1819 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1820 | case FORMAT_TYPE_CHAR: { |
| 1821 | char c; |
| 1822 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1823 | if (!(spec.flags & LEFT)) { |
| 1824 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1825 | if (str < end) |
| 1826 | *str = ' '; |
| 1827 | ++str; |
| 1828 | } |
| 1829 | } |
| 1830 | c = (unsigned char) get_arg(char); |
| 1831 | if (str < end) |
| 1832 | *str = c; |
| 1833 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1834 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1835 | if (str < end) |
| 1836 | *str = ' '; |
| 1837 | ++str; |
| 1838 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1839 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1840 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1841 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1842 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1843 | const char *str_arg = args; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1844 | args += strlen(str_arg) + 1; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1845 | str = string(str, end, (char *)str_arg, spec); |
| 1846 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1849 | case FORMAT_TYPE_PTR: |
| 1850 | str = pointer(fmt+1, str, end, get_arg(void *), spec); |
| 1851 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1852 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1853 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1854 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1855 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1856 | case FORMAT_TYPE_INVALID: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1857 | if (str < end) |
| 1858 | *str = '%'; |
| 1859 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1860 | break; |
| 1861 | |
| 1862 | case FORMAT_TYPE_NRCHARS: |
| 1863 | /* skip */ |
| 1864 | break; |
| 1865 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1866 | default: { |
| 1867 | unsigned long long num; |
| 1868 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1869 | switch (spec.type) { |
| 1870 | |
| 1871 | case FORMAT_TYPE_LONG_LONG: |
| 1872 | num = get_arg(long long); |
| 1873 | break; |
| 1874 | case FORMAT_TYPE_ULONG: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1875 | case FORMAT_TYPE_LONG: |
| 1876 | num = get_arg(unsigned long); |
| 1877 | break; |
| 1878 | case FORMAT_TYPE_SIZE_T: |
| 1879 | num = get_arg(size_t); |
| 1880 | break; |
| 1881 | case FORMAT_TYPE_PTRDIFF: |
| 1882 | num = get_arg(ptrdiff_t); |
| 1883 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1884 | case FORMAT_TYPE_UBYTE: |
| 1885 | num = get_arg(unsigned char); |
| 1886 | break; |
| 1887 | case FORMAT_TYPE_BYTE: |
| 1888 | num = get_arg(signed char); |
| 1889 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1890 | case FORMAT_TYPE_USHORT: |
| 1891 | num = get_arg(unsigned short); |
| 1892 | break; |
| 1893 | case FORMAT_TYPE_SHORT: |
| 1894 | num = get_arg(short); |
| 1895 | break; |
| 1896 | case FORMAT_TYPE_UINT: |
| 1897 | num = get_arg(unsigned int); |
| 1898 | break; |
| 1899 | default: |
| 1900 | num = get_arg(int); |
| 1901 | } |
| 1902 | |
| 1903 | str = number(str, end, num, spec); |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1904 | } /* default: */ |
| 1905 | } /* switch(spec.type) */ |
| 1906 | } /* while(*fmt) */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1907 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1908 | if (size > 0) { |
| 1909 | if (str < end) |
| 1910 | *str = '\0'; |
| 1911 | else |
| 1912 | end[-1] = '\0'; |
| 1913 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1914 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1915 | #undef get_arg |
| 1916 | |
| 1917 | /* the trailing null byte doesn't count towards the total */ |
| 1918 | return str - buf; |
| 1919 | } |
| 1920 | EXPORT_SYMBOL_GPL(bstr_printf); |
| 1921 | |
| 1922 | /** |
| 1923 | * bprintf - Parse a format string and place args' binary value in a buffer |
| 1924 | * @bin_buf: The buffer to place args' binary value |
| 1925 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1926 | * @fmt: The format string to use |
| 1927 | * @...: Arguments for the format string |
| 1928 | * |
| 1929 | * The function returns the number of words(u32) written |
| 1930 | * into @bin_buf. |
| 1931 | */ |
| 1932 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) |
| 1933 | { |
| 1934 | va_list args; |
| 1935 | int ret; |
| 1936 | |
| 1937 | va_start(args, fmt); |
| 1938 | ret = vbin_printf(bin_buf, size, fmt, args); |
| 1939 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1940 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1941 | return ret; |
| 1942 | } |
| 1943 | EXPORT_SYMBOL_GPL(bprintf); |
| 1944 | |
| 1945 | #endif /* CONFIG_BINARY_PRINTF */ |
| 1946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | /** |
| 1948 | * vsscanf - Unformat a buffer into a list of arguments |
| 1949 | * @buf: input buffer |
| 1950 | * @fmt: format of buffer |
| 1951 | * @args: arguments |
| 1952 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1953 | int vsscanf(const char *buf, const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | { |
| 1955 | const char *str = buf; |
| 1956 | char *next; |
| 1957 | char digit; |
| 1958 | int num = 0; |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 1959 | u8 qualifier; |
| 1960 | u8 base; |
| 1961 | s16 field_width; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1962 | bool is_sign; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1964 | while (*fmt && *str) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | /* skip any white space in format */ |
| 1966 | /* white space in format matchs any amount of |
| 1967 | * white space, including none, in the input. |
| 1968 | */ |
| 1969 | if (isspace(*fmt)) { |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 1970 | fmt = skip_spaces(++fmt); |
| 1971 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /* anything that is not a conversion must match exactly */ |
| 1975 | if (*fmt != '%' && *fmt) { |
| 1976 | if (*fmt++ != *str++) |
| 1977 | break; |
| 1978 | continue; |
| 1979 | } |
| 1980 | |
| 1981 | if (!*fmt) |
| 1982 | break; |
| 1983 | ++fmt; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | /* skip this conversion. |
| 1986 | * advance both strings to next white space |
| 1987 | */ |
| 1988 | if (*fmt == '*') { |
Andy Spencer | 8fccae2 | 2009-10-01 15:44:27 -0700 | [diff] [blame] | 1989 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | fmt++; |
| 1991 | while (!isspace(*str) && *str) |
| 1992 | str++; |
| 1993 | continue; |
| 1994 | } |
| 1995 | |
| 1996 | /* get field width */ |
| 1997 | field_width = -1; |
| 1998 | if (isdigit(*fmt)) |
| 1999 | field_width = skip_atoi(&fmt); |
| 2000 | |
| 2001 | /* get conversion qualifier */ |
| 2002 | qualifier = -1; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 2003 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
| 2004 | _tolower(*fmt) == 'z') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | qualifier = *fmt++; |
| 2006 | if (unlikely(qualifier == *fmt)) { |
| 2007 | if (qualifier == 'h') { |
| 2008 | qualifier = 'H'; |
| 2009 | fmt++; |
| 2010 | } else if (qualifier == 'l') { |
| 2011 | qualifier = 'L'; |
| 2012 | fmt++; |
| 2013 | } |
| 2014 | } |
| 2015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | |
| 2017 | if (!*fmt || !*str) |
| 2018 | break; |
| 2019 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2020 | base = 10; |
| 2021 | is_sign = 0; |
| 2022 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2023 | switch (*fmt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | case 'c': |
| 2025 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2026 | char *s = (char *)va_arg(args, char*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | if (field_width == -1) |
| 2028 | field_width = 1; |
| 2029 | do { |
| 2030 | *s++ = *str++; |
| 2031 | } while (--field_width > 0 && *str); |
| 2032 | num++; |
| 2033 | } |
| 2034 | continue; |
| 2035 | case 's': |
| 2036 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2037 | char *s = (char *)va_arg(args, char *); |
| 2038 | if (field_width == -1) |
Alexey Dobriyan | 4be929b | 2010-05-24 14:33:03 -0700 | [diff] [blame] | 2039 | field_width = SHRT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | /* first, skip leading white space in buffer */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2041 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | |
| 2043 | /* now copy until next white space */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2044 | while (*str && !isspace(*str) && field_width--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | *s++ = *str++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | *s = '\0'; |
| 2047 | num++; |
| 2048 | } |
| 2049 | continue; |
| 2050 | case 'n': |
| 2051 | /* return number of characters read so far */ |
| 2052 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2053 | int *i = (int *)va_arg(args, int*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | *i = str - buf; |
| 2055 | } |
| 2056 | continue; |
| 2057 | case 'o': |
| 2058 | base = 8; |
| 2059 | break; |
| 2060 | case 'x': |
| 2061 | case 'X': |
| 2062 | base = 16; |
| 2063 | break; |
| 2064 | case 'i': |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2065 | base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | case 'd': |
| 2067 | is_sign = 1; |
| 2068 | case 'u': |
| 2069 | break; |
| 2070 | case '%': |
| 2071 | /* looking for '%' in str */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2072 | if (*str++ != '%') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | return num; |
| 2074 | continue; |
| 2075 | default: |
| 2076 | /* invalid format; stop here */ |
| 2077 | return num; |
| 2078 | } |
| 2079 | |
| 2080 | /* have some sort of integer conversion. |
| 2081 | * first, skip white space in buffer. |
| 2082 | */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2083 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | |
| 2085 | digit = *str; |
| 2086 | if (is_sign && digit == '-') |
| 2087 | digit = *(str + 1); |
| 2088 | |
| 2089 | if (!digit |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2090 | || (base == 16 && !isxdigit(digit)) |
| 2091 | || (base == 10 && !isdigit(digit)) |
| 2092 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 2093 | || (base == 0 && !isdigit(digit))) |
| 2094 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2096 | switch (qualifier) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | case 'H': /* that's 'hh' in format */ |
| 2098 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2099 | signed char *s = (signed char *)va_arg(args, signed char *); |
| 2100 | *s = (signed char)simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2102 | unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); |
| 2103 | *s = (unsigned char)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
| 2105 | break; |
| 2106 | case 'h': |
| 2107 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2108 | short *s = (short *)va_arg(args, short *); |
| 2109 | *s = (short)simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2111 | unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); |
| 2112 | *s = (unsigned short)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
| 2114 | break; |
| 2115 | case 'l': |
| 2116 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2117 | long *l = (long *)va_arg(args, long *); |
| 2118 | *l = simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2120 | unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); |
| 2121 | *l = simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } |
| 2123 | break; |
| 2124 | case 'L': |
| 2125 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2126 | long long *l = (long long *)va_arg(args, long long *); |
| 2127 | *l = simple_strtoll(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2129 | unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); |
| 2130 | *l = simple_strtoull(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | } |
| 2132 | break; |
| 2133 | case 'Z': |
| 2134 | case 'z': |
| 2135 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2136 | size_t *s = (size_t *)va_arg(args, size_t *); |
| 2137 | *s = (size_t)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | } |
| 2139 | break; |
| 2140 | default: |
| 2141 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2142 | int *i = (int *)va_arg(args, int *); |
| 2143 | *i = (int)simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2145 | unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); |
| 2146 | *i = (unsigned int)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
| 2148 | break; |
| 2149 | } |
| 2150 | num++; |
| 2151 | |
| 2152 | if (!next) |
| 2153 | break; |
| 2154 | str = next; |
| 2155 | } |
Johannes Berg | c6b40d1 | 2007-05-08 00:27:20 -0700 | [diff] [blame] | 2156 | |
| 2157 | /* |
| 2158 | * Now we've come all the way through so either the input string or the |
| 2159 | * format ended. In the former case, there can be a %n at the current |
| 2160 | * position in the format that needs to be filled. |
| 2161 | */ |
| 2162 | if (*fmt == '%' && *(fmt + 1) == 'n') { |
| 2163 | int *p = (int *)va_arg(args, int *); |
| 2164 | *p = str - buf; |
| 2165 | } |
| 2166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | return num; |
| 2168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | EXPORT_SYMBOL(vsscanf); |
| 2170 | |
| 2171 | /** |
| 2172 | * sscanf - Unformat a buffer into a list of arguments |
| 2173 | * @buf: input buffer |
| 2174 | * @fmt: formatting of buffer |
| 2175 | * @...: resulting arguments |
| 2176 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2177 | int sscanf(const char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | { |
| 2179 | va_list args; |
| 2180 | int i; |
| 2181 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2182 | va_start(args, fmt); |
| 2183 | i = vsscanf(buf, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | return i; |
| 2187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | EXPORT_SYMBOL(sscanf); |