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