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