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