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