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