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