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