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]; |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 584 | if (ext != 'f') |
| 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 |
| 601 | #define IO_RSRC_PRINTK_SIZE 4 |
| 602 | #endif |
| 603 | |
| 604 | #ifndef MEM_RSRC_PRINTK_SIZE |
| 605 | #define MEM_RSRC_PRINTK_SIZE 8 |
| 606 | #endif |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 607 | struct printf_spec num_spec = { |
| 608 | .base = 16, |
| 609 | .precision = -1, |
| 610 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 611 | }; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 612 | /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ |
| 613 | char sym[4*sizeof(resource_size_t) + 8]; |
| 614 | char *p = sym, *pend = sym + sizeof(sym); |
| 615 | int size = -1; |
| 616 | |
| 617 | if (res->flags & IORESOURCE_IO) |
| 618 | size = IO_RSRC_PRINTK_SIZE; |
| 619 | else if (res->flags & IORESOURCE_MEM) |
| 620 | size = MEM_RSRC_PRINTK_SIZE; |
| 621 | |
| 622 | *p++ = '['; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 623 | num_spec.field_width = size; |
| 624 | p = number(p, pend, res->start, num_spec); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 625 | *p++ = '-'; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 626 | p = number(p, pend, res->end, num_spec); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 627 | *p++ = ']'; |
| 628 | *p = 0; |
| 629 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 630 | return string(buf, end, sym, spec); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 631 | } |
| 632 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 633 | static char *mac_address_string(char *buf, char *end, u8 *addr, |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 634 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 635 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 636 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 637 | char *p = mac_addr; |
| 638 | int i; |
| 639 | |
| 640 | for (i = 0; i < 6; i++) { |
| 641 | p = pack_hex_byte(p, addr[i]); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 642 | if (fmt[0] == 'M' && i != 5) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 643 | *p++ = ':'; |
| 644 | } |
| 645 | *p = '\0'; |
| 646 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 647 | return string(buf, end, mac_addr, spec); |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 650 | static char *ip4_string(char *p, const u8 *addr, bool leading_zeros) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 651 | { |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 652 | int i; |
| 653 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 654 | for (i = 0; i < 4; i++) { |
| 655 | char temp[3]; /* hold each IP quad in reverse order */ |
| 656 | int digits = put_dec_trunc(temp, addr[i]) - temp; |
| 657 | if (leading_zeros) { |
| 658 | if (digits < 3) |
| 659 | *p++ = '0'; |
| 660 | if (digits < 2) |
| 661 | *p++ = '0'; |
| 662 | } |
| 663 | /* reverse the digits in the quad */ |
| 664 | while (digits--) |
| 665 | *p++ = temp[digits]; |
| 666 | if (i < 3) |
| 667 | *p++ = '.'; |
| 668 | } |
| 669 | |
| 670 | *p = '\0'; |
| 671 | return p; |
| 672 | } |
| 673 | |
| 674 | static char *ip6_compressed_string(char *p, const struct in6_addr *addr) |
| 675 | { |
| 676 | int i; |
| 677 | int j; |
| 678 | int range; |
| 679 | unsigned char zerolength[8]; |
| 680 | int longest = 1; |
| 681 | int colonpos = -1; |
| 682 | u16 word; |
| 683 | u8 hi; |
| 684 | u8 lo; |
| 685 | bool needcolon = false; |
| 686 | bool useIPv4 = ipv6_addr_v4mapped(addr) || ipv6_addr_is_isatap(addr); |
| 687 | |
| 688 | memset(zerolength, 0, sizeof(zerolength)); |
| 689 | |
| 690 | if (useIPv4) |
| 691 | range = 6; |
| 692 | else |
| 693 | range = 8; |
| 694 | |
| 695 | /* find position of longest 0 run */ |
| 696 | for (i = 0; i < range; i++) { |
| 697 | for (j = i; j < range; j++) { |
| 698 | if (addr->s6_addr16[j] != 0) |
| 699 | break; |
| 700 | zerolength[i]++; |
| 701 | } |
| 702 | } |
| 703 | for (i = 0; i < range; i++) { |
| 704 | if (zerolength[i] > longest) { |
| 705 | longest = zerolength[i]; |
| 706 | colonpos = i; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /* emit address */ |
| 711 | for (i = 0; i < range; i++) { |
| 712 | if (i == colonpos) { |
| 713 | if (needcolon || i == 0) |
| 714 | *p++ = ':'; |
| 715 | *p++ = ':'; |
| 716 | needcolon = false; |
| 717 | i += longest - 1; |
| 718 | continue; |
| 719 | } |
| 720 | if (needcolon) { |
| 721 | *p++ = ':'; |
| 722 | needcolon = false; |
| 723 | } |
| 724 | /* hex u16 without leading 0s */ |
| 725 | word = ntohs(addr->s6_addr16[i]); |
| 726 | hi = word >> 8; |
| 727 | lo = word & 0xff; |
| 728 | if (hi) { |
| 729 | if (hi > 0x0f) |
| 730 | p = pack_hex_byte(p, hi); |
| 731 | else |
| 732 | *p++ = hex_asc_lo(hi); |
| 733 | } |
| 734 | if (hi || lo > 0x0f) |
| 735 | p = pack_hex_byte(p, lo); |
| 736 | else |
| 737 | *p++ = hex_asc_lo(lo); |
| 738 | needcolon = true; |
| 739 | } |
| 740 | |
| 741 | if (useIPv4) { |
| 742 | if (needcolon) |
| 743 | *p++ = ':'; |
| 744 | p = ip4_string(p, &addr->s6_addr[12], false); |
| 745 | } |
| 746 | |
| 747 | *p = '\0'; |
| 748 | return p; |
| 749 | } |
| 750 | |
| 751 | static char *ip6_string(char *p, const struct in6_addr *addr, const char *fmt) |
| 752 | { |
| 753 | int i; |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 754 | for (i = 0; i < 8; i++) { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 755 | p = pack_hex_byte(p, addr->s6_addr[2 * i]); |
| 756 | p = pack_hex_byte(p, addr->s6_addr[2 * i + 1]); |
| 757 | if (fmt[0] == 'I' && i != 7) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 758 | *p++ = ':'; |
| 759 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 760 | |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 761 | *p = '\0'; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 762 | return p; |
| 763 | } |
| 764 | |
| 765 | static char *ip6_addr_string(char *buf, char *end, const u8 *addr, |
| 766 | struct printf_spec spec, const char *fmt) |
| 767 | { |
| 768 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
| 769 | |
| 770 | if (fmt[0] == 'I' && fmt[2] == 'c') |
| 771 | ip6_compressed_string(ip6_addr, (const struct in6_addr *)addr); |
| 772 | else |
| 773 | ip6_string(ip6_addr, (const struct in6_addr *)addr, fmt); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 774 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 775 | return string(buf, end, ip6_addr, spec); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 778 | static char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
| 779 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 780 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 781 | char ip4_addr[sizeof("255.255.255.255")]; |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 782 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 783 | ip4_string(ip4_addr, addr, fmt[0] == 'i'); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 784 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 785 | return string(buf, end, ip4_addr, spec); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 788 | /* |
| 789 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 790 | * by an extra set of alphanumeric characters that are extended format |
| 791 | * specifiers. |
| 792 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 793 | * Right now we handle: |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 794 | * |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 795 | * - 'F' For symbolic function descriptor pointers with offset |
| 796 | * - 'f' For simple symbolic function names without offset |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 797 | * - 'S' For symbolic direct pointers |
| 798 | * - 'R' For a struct resource pointer, it prints the range of |
| 799 | * addresses (not the name nor the flags) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 800 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 801 | * usual colon-separated hex notation |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 802 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
| 803 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
| 804 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
| 805 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
| 806 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
| 807 | * IPv6 omits the colons (01020304...0f) |
| 808 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
| 809 | * - 'I6c' for IPv6 addresses printed as specified by |
| 810 | * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 811 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 812 | * function pointers are really function descriptors, which contain a |
| 813 | * pointer to the real address. |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 814 | */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 815 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 816 | struct printf_spec spec) |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 817 | { |
Linus Torvalds | d97106a | 2009-01-03 11:46:17 -0800 | [diff] [blame] | 818 | if (!ptr) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 819 | return string(buf, end, "(null)", spec); |
Linus Torvalds | d97106a | 2009-01-03 11:46:17 -0800 | [diff] [blame] | 820 | |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 821 | switch (*fmt) { |
| 822 | case 'F': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 823 | case 'f': |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 824 | ptr = dereference_function_descriptor(ptr); |
| 825 | /* Fallthrough */ |
| 826 | case 'S': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 827 | return symbol_string(buf, end, ptr, spec, *fmt); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 828 | case 'R': |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 829 | return resource_string(buf, end, ptr, spec); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 830 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
| 831 | case 'm': /* Contiguous: 000102030405 */ |
| 832 | return mac_address_string(buf, end, ptr, spec, fmt); |
| 833 | case 'I': /* Formatted IP supported |
| 834 | * 4: 1.2.3.4 |
| 835 | * 6: 0001:0203:...:0708 |
| 836 | * 6c: 1::708 or 1::1.2.3.4 |
| 837 | */ |
| 838 | case 'i': /* Contiguous: |
| 839 | * 4: 001.002.003.004 |
| 840 | * 6: 000102...0f |
| 841 | */ |
| 842 | switch (fmt[1]) { |
| 843 | case '6': |
| 844 | return ip6_addr_string(buf, end, ptr, spec, fmt); |
| 845 | case '4': |
| 846 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
| 847 | } |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 848 | break; |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 849 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 850 | spec.flags |= SMALL; |
| 851 | if (spec.field_width == -1) { |
| 852 | spec.field_width = 2*sizeof(void *); |
| 853 | spec.flags |= ZEROPAD; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 854 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 855 | spec.base = 16; |
| 856 | |
| 857 | return number(buf, end, (unsigned long) ptr, spec); |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Helper function to decode printf style format. |
| 862 | * Each call decode a token from the format and return the |
| 863 | * number of characters read (or likely the delta where it wants |
| 864 | * to go on the next call). |
| 865 | * The decoded token is returned through the parameters |
| 866 | * |
| 867 | * 'h', 'l', or 'L' for integer fields |
| 868 | * 'z' support added 23/7/1999 S.H. |
| 869 | * 'z' changed to 'Z' --davidm 1/25/99 |
| 870 | * 't' added for ptrdiff_t |
| 871 | * |
| 872 | * @fmt: the format string |
| 873 | * @type of the token returned |
| 874 | * @flags: various flags such as +, -, # tokens.. |
| 875 | * @field_width: overwritten width |
| 876 | * @base: base of the number (octal, hex, ...) |
| 877 | * @precision: precision of a number |
| 878 | * @qualifier: qualifier of a number (long, size_t, ...) |
| 879 | */ |
| 880 | static int format_decode(const char *fmt, struct printf_spec *spec) |
| 881 | { |
| 882 | const char *start = fmt; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 883 | |
| 884 | /* we finished early by reading the field width */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 885 | if (spec->type == FORMAT_TYPE_WIDTH) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 886 | if (spec->field_width < 0) { |
| 887 | spec->field_width = -spec->field_width; |
| 888 | spec->flags |= LEFT; |
| 889 | } |
| 890 | spec->type = FORMAT_TYPE_NONE; |
| 891 | goto precision; |
| 892 | } |
| 893 | |
| 894 | /* we finished early by reading the precision */ |
| 895 | if (spec->type == FORMAT_TYPE_PRECISION) { |
| 896 | if (spec->precision < 0) |
| 897 | spec->precision = 0; |
| 898 | |
| 899 | spec->type = FORMAT_TYPE_NONE; |
| 900 | goto qualifier; |
| 901 | } |
| 902 | |
| 903 | /* By default */ |
| 904 | spec->type = FORMAT_TYPE_NONE; |
| 905 | |
| 906 | for (; *fmt ; ++fmt) { |
| 907 | if (*fmt == '%') |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | /* Return the current non-format string */ |
| 912 | if (fmt != start || !*fmt) |
| 913 | return fmt - start; |
| 914 | |
| 915 | /* Process flags */ |
| 916 | spec->flags = 0; |
| 917 | |
| 918 | while (1) { /* this also skips first '%' */ |
| 919 | bool found = true; |
| 920 | |
| 921 | ++fmt; |
| 922 | |
| 923 | switch (*fmt) { |
| 924 | case '-': spec->flags |= LEFT; break; |
| 925 | case '+': spec->flags |= PLUS; break; |
| 926 | case ' ': spec->flags |= SPACE; break; |
| 927 | case '#': spec->flags |= SPECIAL; break; |
| 928 | case '0': spec->flags |= ZEROPAD; break; |
| 929 | default: found = false; |
| 930 | } |
| 931 | |
| 932 | if (!found) |
| 933 | break; |
| 934 | } |
| 935 | |
| 936 | /* get field width */ |
| 937 | spec->field_width = -1; |
| 938 | |
| 939 | if (isdigit(*fmt)) |
| 940 | spec->field_width = skip_atoi(&fmt); |
| 941 | else if (*fmt == '*') { |
| 942 | /* it's the next argument */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 943 | spec->type = FORMAT_TYPE_WIDTH; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 944 | return ++fmt - start; |
| 945 | } |
| 946 | |
| 947 | precision: |
| 948 | /* get the precision */ |
| 949 | spec->precision = -1; |
| 950 | if (*fmt == '.') { |
| 951 | ++fmt; |
| 952 | if (isdigit(*fmt)) { |
| 953 | spec->precision = skip_atoi(&fmt); |
| 954 | if (spec->precision < 0) |
| 955 | spec->precision = 0; |
| 956 | } else if (*fmt == '*') { |
| 957 | /* it's the next argument */ |
Vegard Nossum | adf26f8 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 958 | spec->type = FORMAT_TYPE_PRECISION; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 959 | return ++fmt - start; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | qualifier: |
| 964 | /* get the conversion qualifier */ |
| 965 | spec->qualifier = -1; |
| 966 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
| 967 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 968 | spec->qualifier = *fmt++; |
| 969 | if (unlikely(spec->qualifier == *fmt)) { |
| 970 | if (spec->qualifier == 'l') { |
| 971 | spec->qualifier = 'L'; |
| 972 | ++fmt; |
| 973 | } else if (spec->qualifier == 'h') { |
| 974 | spec->qualifier = 'H'; |
| 975 | ++fmt; |
| 976 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | /* default base */ |
| 981 | spec->base = 10; |
| 982 | switch (*fmt) { |
| 983 | case 'c': |
| 984 | spec->type = FORMAT_TYPE_CHAR; |
| 985 | return ++fmt - start; |
| 986 | |
| 987 | case 's': |
| 988 | spec->type = FORMAT_TYPE_STR; |
| 989 | return ++fmt - start; |
| 990 | |
| 991 | case 'p': |
| 992 | spec->type = FORMAT_TYPE_PTR; |
| 993 | return fmt - start; |
| 994 | /* skip alnum */ |
| 995 | |
| 996 | case 'n': |
| 997 | spec->type = FORMAT_TYPE_NRCHARS; |
| 998 | return ++fmt - start; |
| 999 | |
| 1000 | case '%': |
| 1001 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
| 1002 | return ++fmt - start; |
| 1003 | |
| 1004 | /* integer number formats - set up the flags and "break" */ |
| 1005 | case 'o': |
| 1006 | spec->base = 8; |
| 1007 | break; |
| 1008 | |
| 1009 | case 'x': |
| 1010 | spec->flags |= SMALL; |
| 1011 | |
| 1012 | case 'X': |
| 1013 | spec->base = 16; |
| 1014 | break; |
| 1015 | |
| 1016 | case 'd': |
| 1017 | case 'i': |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1018 | spec->flags |= SIGN; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1019 | case 'u': |
| 1020 | break; |
| 1021 | |
| 1022 | default: |
| 1023 | spec->type = FORMAT_TYPE_INVALID; |
| 1024 | return fmt - start; |
| 1025 | } |
| 1026 | |
| 1027 | if (spec->qualifier == 'L') |
| 1028 | spec->type = FORMAT_TYPE_LONG_LONG; |
| 1029 | else if (spec->qualifier == 'l') { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1030 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1031 | spec->type = FORMAT_TYPE_LONG; |
| 1032 | else |
| 1033 | spec->type = FORMAT_TYPE_ULONG; |
| 1034 | } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') { |
| 1035 | spec->type = FORMAT_TYPE_SIZE_T; |
| 1036 | } else if (spec->qualifier == 't') { |
| 1037 | spec->type = FORMAT_TYPE_PTRDIFF; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1038 | } else if (spec->qualifier == 'H') { |
| 1039 | if (spec->flags & SIGN) |
| 1040 | spec->type = FORMAT_TYPE_BYTE; |
| 1041 | else |
| 1042 | spec->type = FORMAT_TYPE_UBYTE; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1043 | } else if (spec->qualifier == 'h') { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1044 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1045 | spec->type = FORMAT_TYPE_SHORT; |
| 1046 | else |
| 1047 | spec->type = FORMAT_TYPE_USHORT; |
| 1048 | } else { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1049 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1050 | spec->type = FORMAT_TYPE_INT; |
| 1051 | else |
| 1052 | spec->type = FORMAT_TYPE_UINT; |
| 1053 | } |
| 1054 | |
| 1055 | return ++fmt - start; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | /** |
| 1059 | * vsnprintf - Format a string and place it in a buffer |
| 1060 | * @buf: The buffer to place the result into |
| 1061 | * @size: The size of the buffer, including the trailing null space |
| 1062 | * @fmt: The format string to use |
| 1063 | * @args: Arguments for the format string |
| 1064 | * |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1065 | * This function follows C99 vsnprintf, but has some extensions: |
| 1066 | * %pS output the name of a text symbol |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1067 | * %pF output the name of a function pointer with its offset |
| 1068 | * %pf output the name of a function pointer without its offset |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1069 | * %pR output the address range in a struct resource |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1070 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | * The return value is the number of characters which would |
| 1072 | * be generated for the given input, excluding the trailing |
| 1073 | * '\0', as per ISO C99. If you want to have the exact |
| 1074 | * number of characters written into @buf as return value |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1075 | * (not including the trailing '\0'), use vscnprintf(). If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | * return is greater than or equal to @size, the resulting |
| 1077 | * string is truncated. |
| 1078 | * |
| 1079 | * 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] | 1080 | * You probably want snprintf() instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | */ |
| 1082 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1083 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | unsigned long long num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | char *str, *end, c; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1086 | int read; |
| 1087 | struct printf_spec spec = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1089 | /* Reject out-of-range values early. Large positive sizes are |
| 1090 | used for unknown buffer sizes. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if (unlikely((int) size < 0)) { |
| 1092 | /* There can be only one.. */ |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 1093 | static char warn = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | WARN_ON(warn); |
| 1095 | warn = 0; |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | str = buf; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1100 | end = buf + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1102 | /* Make sure end is always >= buf */ |
| 1103 | if (end < buf) { |
| 1104 | end = ((void *)-1); |
| 1105 | size = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1108 | while (*fmt) { |
| 1109 | const char *old_fmt = fmt; |
| 1110 | |
| 1111 | read = format_decode(fmt, &spec); |
| 1112 | |
| 1113 | fmt += read; |
| 1114 | |
| 1115 | switch (spec.type) { |
| 1116 | case FORMAT_TYPE_NONE: { |
| 1117 | int copy = read; |
| 1118 | if (str < end) { |
| 1119 | if (copy > end - str) |
| 1120 | copy = end - str; |
| 1121 | memcpy(str, old_fmt, copy); |
| 1122 | } |
| 1123 | str += read; |
| 1124 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1127 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1128 | spec.field_width = va_arg(args, int); |
| 1129 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1131 | case FORMAT_TYPE_PRECISION: |
| 1132 | spec.precision = va_arg(args, int); |
| 1133 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1135 | case FORMAT_TYPE_CHAR: |
| 1136 | if (!(spec.flags & LEFT)) { |
| 1137 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1138 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | *str = ' '; |
| 1140 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1143 | } |
| 1144 | c = (unsigned char) va_arg(args, int); |
| 1145 | if (str < end) |
| 1146 | *str = c; |
| 1147 | ++str; |
| 1148 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1149 | if (str < end) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1150 | *str = ' '; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1152 | } |
| 1153 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1155 | case FORMAT_TYPE_STR: |
| 1156 | str = string(str, end, va_arg(args, char *), spec); |
| 1157 | break; |
| 1158 | |
| 1159 | case FORMAT_TYPE_PTR: |
| 1160 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
| 1161 | spec); |
| 1162 | while (isalnum(*fmt)) |
| 1163 | fmt++; |
| 1164 | break; |
| 1165 | |
| 1166 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1167 | if (str < end) |
| 1168 | *str = '%'; |
| 1169 | ++str; |
| 1170 | break; |
| 1171 | |
| 1172 | case FORMAT_TYPE_INVALID: |
| 1173 | if (str < end) |
| 1174 | *str = '%'; |
| 1175 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1176 | break; |
| 1177 | |
| 1178 | case FORMAT_TYPE_NRCHARS: { |
| 1179 | int qualifier = spec.qualifier; |
| 1180 | |
| 1181 | if (qualifier == 'l') { |
| 1182 | long *ip = va_arg(args, long *); |
| 1183 | *ip = (str - buf); |
| 1184 | } else if (qualifier == 'Z' || |
| 1185 | qualifier == 'z') { |
| 1186 | size_t *ip = va_arg(args, size_t *); |
| 1187 | *ip = (str - buf); |
| 1188 | } else { |
| 1189 | int *ip = va_arg(args, int *); |
| 1190 | *ip = (str - buf); |
| 1191 | } |
| 1192 | break; |
| 1193 | } |
| 1194 | |
| 1195 | default: |
| 1196 | switch (spec.type) { |
| 1197 | case FORMAT_TYPE_LONG_LONG: |
| 1198 | num = va_arg(args, long long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1200 | case FORMAT_TYPE_ULONG: |
| 1201 | num = va_arg(args, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1203 | case FORMAT_TYPE_LONG: |
| 1204 | num = va_arg(args, long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1206 | case FORMAT_TYPE_SIZE_T: |
| 1207 | num = va_arg(args, size_t); |
| 1208 | break; |
| 1209 | case FORMAT_TYPE_PTRDIFF: |
| 1210 | num = va_arg(args, ptrdiff_t); |
| 1211 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1212 | case FORMAT_TYPE_UBYTE: |
| 1213 | num = (unsigned char) va_arg(args, int); |
| 1214 | break; |
| 1215 | case FORMAT_TYPE_BYTE: |
| 1216 | num = (signed char) va_arg(args, int); |
| 1217 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1218 | case FORMAT_TYPE_USHORT: |
| 1219 | num = (unsigned short) va_arg(args, int); |
| 1220 | break; |
| 1221 | case FORMAT_TYPE_SHORT: |
| 1222 | num = (short) va_arg(args, int); |
| 1223 | break; |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1224 | case FORMAT_TYPE_INT: |
| 1225 | num = (int) va_arg(args, int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1226 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1228 | num = va_arg(args, unsigned int); |
| 1229 | } |
| 1230 | |
| 1231 | str = number(str, end, num, spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1234 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1235 | if (size > 0) { |
| 1236 | if (str < end) |
| 1237 | *str = '\0'; |
| 1238 | else |
Linus Torvalds | 0a6047e | 2006-06-28 17:09:34 -0700 | [diff] [blame] | 1239 | end[-1] = '\0'; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1240 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1241 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1242 | /* the trailing null byte doesn't count towards the total */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | return str-buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | EXPORT_SYMBOL(vsnprintf); |
| 1247 | |
| 1248 | /** |
| 1249 | * vscnprintf - Format a string and place it in a buffer |
| 1250 | * @buf: The buffer to place the result into |
| 1251 | * @size: The size of the buffer, including the trailing null space |
| 1252 | * @fmt: The format string to use |
| 1253 | * @args: Arguments for the format string |
| 1254 | * |
| 1255 | * The return value is the number of characters which have been written into |
| 1256 | * the @buf not including the trailing '\0'. If @size is <= 0 the function |
| 1257 | * returns 0. |
| 1258 | * |
| 1259 | * 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] | 1260 | * You probably want scnprintf() instead. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1261 | * |
| 1262 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | */ |
| 1264 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1265 | { |
| 1266 | int i; |
| 1267 | |
| 1268 | i=vsnprintf(buf,size,fmt,args); |
| 1269 | return (i >= size) ? (size - 1) : i; |
| 1270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | EXPORT_SYMBOL(vscnprintf); |
| 1272 | |
| 1273 | /** |
| 1274 | * snprintf - Format a string and place it in a buffer |
| 1275 | * @buf: The buffer to place the result into |
| 1276 | * @size: The size of the buffer, including the trailing null space |
| 1277 | * @fmt: The format string to use |
| 1278 | * @...: Arguments for the format string |
| 1279 | * |
| 1280 | * The return value is the number of characters which would be |
| 1281 | * generated for the given input, excluding the trailing null, |
| 1282 | * as per ISO C99. If the return is greater than or equal to |
| 1283 | * @size, the resulting string is truncated. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1284 | * |
| 1285 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | */ |
| 1287 | int snprintf(char * buf, size_t size, const char *fmt, ...) |
| 1288 | { |
| 1289 | va_list args; |
| 1290 | int i; |
| 1291 | |
| 1292 | va_start(args, fmt); |
| 1293 | i=vsnprintf(buf,size,fmt,args); |
| 1294 | va_end(args); |
| 1295 | return i; |
| 1296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | EXPORT_SYMBOL(snprintf); |
| 1298 | |
| 1299 | /** |
| 1300 | * scnprintf - Format a string and place it in a buffer |
| 1301 | * @buf: The buffer to place the result into |
| 1302 | * @size: The size of the buffer, including the trailing null space |
| 1303 | * @fmt: The format string to use |
| 1304 | * @...: Arguments for the format string |
| 1305 | * |
| 1306 | * 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] | 1307 | * the trailing '\0'. If @size is <= 0 the function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | */ |
| 1309 | |
| 1310 | int scnprintf(char * buf, size_t size, const char *fmt, ...) |
| 1311 | { |
| 1312 | va_list args; |
| 1313 | int i; |
| 1314 | |
| 1315 | va_start(args, fmt); |
| 1316 | i = vsnprintf(buf, size, fmt, args); |
| 1317 | va_end(args); |
| 1318 | return (i >= size) ? (size - 1) : i; |
| 1319 | } |
| 1320 | EXPORT_SYMBOL(scnprintf); |
| 1321 | |
| 1322 | /** |
| 1323 | * vsprintf - Format a string and place it in a buffer |
| 1324 | * @buf: The buffer to place the result into |
| 1325 | * @fmt: The format string to use |
| 1326 | * @args: Arguments for the format string |
| 1327 | * |
| 1328 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1329 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | * buffer overflows. |
| 1331 | * |
| 1332 | * 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] | 1333 | * You probably want sprintf() instead. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1334 | * |
| 1335 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | */ |
| 1337 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 1338 | { |
| 1339 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 1340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | EXPORT_SYMBOL(vsprintf); |
| 1342 | |
| 1343 | /** |
| 1344 | * sprintf - Format a string and place it in a buffer |
| 1345 | * @buf: The buffer to place the result into |
| 1346 | * @fmt: The format string to use |
| 1347 | * @...: Arguments for the format string |
| 1348 | * |
| 1349 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1350 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | * buffer overflows. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1352 | * |
| 1353 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | */ |
| 1355 | int sprintf(char * buf, const char *fmt, ...) |
| 1356 | { |
| 1357 | va_list args; |
| 1358 | int i; |
| 1359 | |
| 1360 | va_start(args, fmt); |
| 1361 | i=vsnprintf(buf, INT_MAX, fmt, args); |
| 1362 | va_end(args); |
| 1363 | return i; |
| 1364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | EXPORT_SYMBOL(sprintf); |
| 1366 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1367 | #ifdef CONFIG_BINARY_PRINTF |
| 1368 | /* |
| 1369 | * bprintf service: |
| 1370 | * vbin_printf() - VA arguments to binary data |
| 1371 | * bstr_printf() - Binary data to text string |
| 1372 | */ |
| 1373 | |
| 1374 | /** |
| 1375 | * vbin_printf - Parse a format string and place args' binary value in a buffer |
| 1376 | * @bin_buf: The buffer to place args' binary value |
| 1377 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1378 | * @fmt: The format string to use |
| 1379 | * @args: Arguments for the format string |
| 1380 | * |
| 1381 | * The format follows C99 vsnprintf, except %n is ignored, and its argument |
| 1382 | * is skiped. |
| 1383 | * |
| 1384 | * The return value is the number of words(32bits) which would be generated for |
| 1385 | * the given input. |
| 1386 | * |
| 1387 | * NOTE: |
| 1388 | * If the return value is greater than @size, the resulting bin_buf is NOT |
| 1389 | * valid for bstr_printf(). |
| 1390 | */ |
| 1391 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) |
| 1392 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1393 | struct printf_spec spec = {0}; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1394 | char *str, *end; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1395 | int read; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1396 | |
| 1397 | str = (char *)bin_buf; |
| 1398 | end = (char *)(bin_buf + size); |
| 1399 | |
| 1400 | #define save_arg(type) \ |
| 1401 | do { \ |
| 1402 | if (sizeof(type) == 8) { \ |
| 1403 | unsigned long long value; \ |
| 1404 | str = PTR_ALIGN(str, sizeof(u32)); \ |
| 1405 | value = va_arg(args, unsigned long long); \ |
| 1406 | if (str + sizeof(type) <= end) { \ |
| 1407 | *(u32 *)str = *(u32 *)&value; \ |
| 1408 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ |
| 1409 | } \ |
| 1410 | } else { \ |
| 1411 | unsigned long value; \ |
| 1412 | str = PTR_ALIGN(str, sizeof(type)); \ |
| 1413 | value = va_arg(args, int); \ |
| 1414 | if (str + sizeof(type) <= end) \ |
| 1415 | *(typeof(type) *)str = (type)value; \ |
| 1416 | } \ |
| 1417 | str += sizeof(type); \ |
| 1418 | } while (0) |
| 1419 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1420 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1421 | while (*fmt) { |
| 1422 | read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1423 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1424 | fmt += read; |
| 1425 | |
| 1426 | switch (spec.type) { |
| 1427 | case FORMAT_TYPE_NONE: |
| 1428 | break; |
| 1429 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1430 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1431 | case FORMAT_TYPE_PRECISION: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1432 | save_arg(int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1433 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1434 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1435 | case FORMAT_TYPE_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1436 | save_arg(char); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1437 | break; |
| 1438 | |
| 1439 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1440 | const char *save_str = va_arg(args, char *); |
| 1441 | size_t len; |
| 1442 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
| 1443 | || (unsigned long)save_str < PAGE_SIZE) |
| 1444 | save_str = "<NULL>"; |
| 1445 | len = strlen(save_str); |
| 1446 | if (str + len + 1 < end) |
| 1447 | memcpy(str, save_str, len + 1); |
| 1448 | str += len + 1; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1449 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1450 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1451 | |
| 1452 | case FORMAT_TYPE_PTR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1453 | save_arg(void *); |
| 1454 | /* skip all alphanumeric pointer suffixes */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1455 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1456 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1457 | break; |
| 1458 | |
| 1459 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1460 | break; |
| 1461 | |
| 1462 | case FORMAT_TYPE_INVALID: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1463 | break; |
| 1464 | |
| 1465 | case FORMAT_TYPE_NRCHARS: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1466 | /* skip %n 's argument */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1467 | int qualifier = spec.qualifier; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1468 | void *skip_arg; |
| 1469 | if (qualifier == 'l') |
| 1470 | skip_arg = va_arg(args, long *); |
| 1471 | else if (qualifier == 'Z' || qualifier == 'z') |
| 1472 | skip_arg = va_arg(args, size_t *); |
| 1473 | else |
| 1474 | skip_arg = va_arg(args, int *); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1475 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1476 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1477 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1478 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1479 | switch (spec.type) { |
| 1480 | |
| 1481 | case FORMAT_TYPE_LONG_LONG: |
| 1482 | save_arg(long long); |
| 1483 | break; |
| 1484 | case FORMAT_TYPE_ULONG: |
| 1485 | case FORMAT_TYPE_LONG: |
| 1486 | save_arg(unsigned long); |
| 1487 | break; |
| 1488 | case FORMAT_TYPE_SIZE_T: |
| 1489 | save_arg(size_t); |
| 1490 | break; |
| 1491 | case FORMAT_TYPE_PTRDIFF: |
| 1492 | save_arg(ptrdiff_t); |
| 1493 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1494 | case FORMAT_TYPE_UBYTE: |
| 1495 | case FORMAT_TYPE_BYTE: |
| 1496 | save_arg(char); |
| 1497 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1498 | case FORMAT_TYPE_USHORT: |
| 1499 | case FORMAT_TYPE_SHORT: |
| 1500 | save_arg(short); |
| 1501 | break; |
| 1502 | default: |
| 1503 | save_arg(int); |
| 1504 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1505 | } |
| 1506 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1507 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1508 | |
| 1509 | #undef save_arg |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1510 | } |
| 1511 | EXPORT_SYMBOL_GPL(vbin_printf); |
| 1512 | |
| 1513 | /** |
| 1514 | * bstr_printf - Format a string from binary arguments and place it in a buffer |
| 1515 | * @buf: The buffer to place the result into |
| 1516 | * @size: The size of the buffer, including the trailing null space |
| 1517 | * @fmt: The format string to use |
| 1518 | * @bin_buf: Binary arguments for the format string |
| 1519 | * |
| 1520 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets |
| 1521 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is |
| 1522 | * a binary buffer that generated by vbin_printf. |
| 1523 | * |
| 1524 | * The format follows C99 vsnprintf, but has some extensions: |
| 1525 | * %pS output the name of a text symbol |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1526 | * %pF output the name of a function pointer with its offset |
| 1527 | * %pf output the name of a function pointer without its offset |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1528 | * %pR output the address range in a struct resource |
| 1529 | * %n is ignored |
| 1530 | * |
| 1531 | * The return value is the number of characters which would |
| 1532 | * be generated for the given input, excluding the trailing |
| 1533 | * '\0', as per ISO C99. If you want to have the exact |
| 1534 | * number of characters written into @buf as return value |
| 1535 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 1536 | * return is greater than or equal to @size, the resulting |
| 1537 | * string is truncated. |
| 1538 | */ |
| 1539 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
| 1540 | { |
| 1541 | unsigned long long num; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1542 | char *str, *end, c; |
| 1543 | const char *args = (const char *)bin_buf; |
| 1544 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1545 | struct printf_spec spec = {0}; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1546 | |
| 1547 | if (unlikely((int) size < 0)) { |
| 1548 | /* There can be only one.. */ |
| 1549 | static char warn = 1; |
| 1550 | WARN_ON(warn); |
| 1551 | warn = 0; |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | str = buf; |
| 1556 | end = buf + size; |
| 1557 | |
| 1558 | #define get_arg(type) \ |
| 1559 | ({ \ |
| 1560 | typeof(type) value; \ |
| 1561 | if (sizeof(type) == 8) { \ |
| 1562 | args = PTR_ALIGN(args, sizeof(u32)); \ |
| 1563 | *(u32 *)&value = *(u32 *)args; \ |
| 1564 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ |
| 1565 | } else { \ |
| 1566 | args = PTR_ALIGN(args, sizeof(type)); \ |
| 1567 | value = *(typeof(type) *)args; \ |
| 1568 | } \ |
| 1569 | args += sizeof(type); \ |
| 1570 | value; \ |
| 1571 | }) |
| 1572 | |
| 1573 | /* Make sure end is always >= buf */ |
| 1574 | if (end < buf) { |
| 1575 | end = ((void *)-1); |
| 1576 | size = end - buf; |
| 1577 | } |
| 1578 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1579 | while (*fmt) { |
| 1580 | int read; |
| 1581 | const char *old_fmt = fmt; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1582 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1583 | read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1584 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1585 | fmt += read; |
| 1586 | |
| 1587 | switch (spec.type) { |
| 1588 | case FORMAT_TYPE_NONE: { |
| 1589 | int copy = read; |
| 1590 | if (str < end) { |
| 1591 | if (copy > end - str) |
| 1592 | copy = end - str; |
| 1593 | memcpy(str, old_fmt, copy); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1594 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1595 | str += read; |
| 1596 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1599 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1600 | spec.field_width = get_arg(int); |
| 1601 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1602 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1603 | case FORMAT_TYPE_PRECISION: |
| 1604 | spec.precision = get_arg(int); |
| 1605 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1606 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1607 | case FORMAT_TYPE_CHAR: |
| 1608 | if (!(spec.flags & LEFT)) { |
| 1609 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1610 | if (str < end) |
| 1611 | *str = ' '; |
| 1612 | ++str; |
| 1613 | } |
| 1614 | } |
| 1615 | c = (unsigned char) get_arg(char); |
| 1616 | if (str < end) |
| 1617 | *str = c; |
| 1618 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1619 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1620 | if (str < end) |
| 1621 | *str = ' '; |
| 1622 | ++str; |
| 1623 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1624 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1625 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1626 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1627 | const char *str_arg = args; |
| 1628 | size_t len = strlen(str_arg); |
| 1629 | args += len + 1; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1630 | str = string(str, end, (char *)str_arg, spec); |
| 1631 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1632 | } |
| 1633 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1634 | case FORMAT_TYPE_PTR: |
| 1635 | str = pointer(fmt+1, str, end, get_arg(void *), spec); |
| 1636 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1637 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1638 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1639 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1640 | case FORMAT_TYPE_PERCENT_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1641 | if (str < end) |
| 1642 | *str = '%'; |
| 1643 | ++str; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1644 | break; |
| 1645 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1646 | case FORMAT_TYPE_INVALID: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1647 | if (str < end) |
| 1648 | *str = '%'; |
| 1649 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1650 | break; |
| 1651 | |
| 1652 | case FORMAT_TYPE_NRCHARS: |
| 1653 | /* skip */ |
| 1654 | break; |
| 1655 | |
| 1656 | default: |
| 1657 | switch (spec.type) { |
| 1658 | |
| 1659 | case FORMAT_TYPE_LONG_LONG: |
| 1660 | num = get_arg(long long); |
| 1661 | break; |
| 1662 | case FORMAT_TYPE_ULONG: |
| 1663 | num = get_arg(unsigned long); |
| 1664 | break; |
| 1665 | case FORMAT_TYPE_LONG: |
| 1666 | num = get_arg(unsigned long); |
| 1667 | break; |
| 1668 | case FORMAT_TYPE_SIZE_T: |
| 1669 | num = get_arg(size_t); |
| 1670 | break; |
| 1671 | case FORMAT_TYPE_PTRDIFF: |
| 1672 | num = get_arg(ptrdiff_t); |
| 1673 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1674 | case FORMAT_TYPE_UBYTE: |
| 1675 | num = get_arg(unsigned char); |
| 1676 | break; |
| 1677 | case FORMAT_TYPE_BYTE: |
| 1678 | num = get_arg(signed char); |
| 1679 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1680 | case FORMAT_TYPE_USHORT: |
| 1681 | num = get_arg(unsigned short); |
| 1682 | break; |
| 1683 | case FORMAT_TYPE_SHORT: |
| 1684 | num = get_arg(short); |
| 1685 | break; |
| 1686 | case FORMAT_TYPE_UINT: |
| 1687 | num = get_arg(unsigned int); |
| 1688 | break; |
| 1689 | default: |
| 1690 | num = get_arg(int); |
| 1691 | } |
| 1692 | |
| 1693 | str = number(str, end, num, spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1694 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1695 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1696 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1697 | if (size > 0) { |
| 1698 | if (str < end) |
| 1699 | *str = '\0'; |
| 1700 | else |
| 1701 | end[-1] = '\0'; |
| 1702 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1703 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1704 | #undef get_arg |
| 1705 | |
| 1706 | /* the trailing null byte doesn't count towards the total */ |
| 1707 | return str - buf; |
| 1708 | } |
| 1709 | EXPORT_SYMBOL_GPL(bstr_printf); |
| 1710 | |
| 1711 | /** |
| 1712 | * bprintf - Parse a format string and place args' binary value in a buffer |
| 1713 | * @bin_buf: The buffer to place args' binary value |
| 1714 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1715 | * @fmt: The format string to use |
| 1716 | * @...: Arguments for the format string |
| 1717 | * |
| 1718 | * The function returns the number of words(u32) written |
| 1719 | * into @bin_buf. |
| 1720 | */ |
| 1721 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) |
| 1722 | { |
| 1723 | va_list args; |
| 1724 | int ret; |
| 1725 | |
| 1726 | va_start(args, fmt); |
| 1727 | ret = vbin_printf(bin_buf, size, fmt, args); |
| 1728 | va_end(args); |
| 1729 | return ret; |
| 1730 | } |
| 1731 | EXPORT_SYMBOL_GPL(bprintf); |
| 1732 | |
| 1733 | #endif /* CONFIG_BINARY_PRINTF */ |
| 1734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | /** |
| 1736 | * vsscanf - Unformat a buffer into a list of arguments |
| 1737 | * @buf: input buffer |
| 1738 | * @fmt: format of buffer |
| 1739 | * @args: arguments |
| 1740 | */ |
| 1741 | int vsscanf(const char * buf, const char * fmt, va_list args) |
| 1742 | { |
| 1743 | const char *str = buf; |
| 1744 | char *next; |
| 1745 | char digit; |
| 1746 | int num = 0; |
| 1747 | int qualifier; |
| 1748 | int base; |
| 1749 | int field_width; |
| 1750 | int is_sign = 0; |
| 1751 | |
| 1752 | while(*fmt && *str) { |
| 1753 | /* skip any white space in format */ |
| 1754 | /* white space in format matchs any amount of |
| 1755 | * white space, including none, in the input. |
| 1756 | */ |
| 1757 | if (isspace(*fmt)) { |
| 1758 | while (isspace(*fmt)) |
| 1759 | ++fmt; |
| 1760 | while (isspace(*str)) |
| 1761 | ++str; |
| 1762 | } |
| 1763 | |
| 1764 | /* anything that is not a conversion must match exactly */ |
| 1765 | if (*fmt != '%' && *fmt) { |
| 1766 | if (*fmt++ != *str++) |
| 1767 | break; |
| 1768 | continue; |
| 1769 | } |
| 1770 | |
| 1771 | if (!*fmt) |
| 1772 | break; |
| 1773 | ++fmt; |
| 1774 | |
| 1775 | /* skip this conversion. |
| 1776 | * advance both strings to next white space |
| 1777 | */ |
| 1778 | if (*fmt == '*') { |
| 1779 | while (!isspace(*fmt) && *fmt) |
| 1780 | fmt++; |
| 1781 | while (!isspace(*str) && *str) |
| 1782 | str++; |
| 1783 | continue; |
| 1784 | } |
| 1785 | |
| 1786 | /* get field width */ |
| 1787 | field_width = -1; |
| 1788 | if (isdigit(*fmt)) |
| 1789 | field_width = skip_atoi(&fmt); |
| 1790 | |
| 1791 | /* get conversion qualifier */ |
| 1792 | qualifier = -1; |
| 1793 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
| 1794 | *fmt == 'Z' || *fmt == 'z') { |
| 1795 | qualifier = *fmt++; |
| 1796 | if (unlikely(qualifier == *fmt)) { |
| 1797 | if (qualifier == 'h') { |
| 1798 | qualifier = 'H'; |
| 1799 | fmt++; |
| 1800 | } else if (qualifier == 'l') { |
| 1801 | qualifier = 'L'; |
| 1802 | fmt++; |
| 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | base = 10; |
| 1807 | is_sign = 0; |
| 1808 | |
| 1809 | if (!*fmt || !*str) |
| 1810 | break; |
| 1811 | |
| 1812 | switch(*fmt++) { |
| 1813 | case 'c': |
| 1814 | { |
| 1815 | char *s = (char *) va_arg(args,char*); |
| 1816 | if (field_width == -1) |
| 1817 | field_width = 1; |
| 1818 | do { |
| 1819 | *s++ = *str++; |
| 1820 | } while (--field_width > 0 && *str); |
| 1821 | num++; |
| 1822 | } |
| 1823 | continue; |
| 1824 | case 's': |
| 1825 | { |
| 1826 | char *s = (char *) va_arg(args, char *); |
| 1827 | if(field_width == -1) |
| 1828 | field_width = INT_MAX; |
| 1829 | /* first, skip leading white space in buffer */ |
| 1830 | while (isspace(*str)) |
| 1831 | str++; |
| 1832 | |
| 1833 | /* now copy until next white space */ |
| 1834 | while (*str && !isspace(*str) && field_width--) { |
| 1835 | *s++ = *str++; |
| 1836 | } |
| 1837 | *s = '\0'; |
| 1838 | num++; |
| 1839 | } |
| 1840 | continue; |
| 1841 | case 'n': |
| 1842 | /* return number of characters read so far */ |
| 1843 | { |
| 1844 | int *i = (int *)va_arg(args,int*); |
| 1845 | *i = str - buf; |
| 1846 | } |
| 1847 | continue; |
| 1848 | case 'o': |
| 1849 | base = 8; |
| 1850 | break; |
| 1851 | case 'x': |
| 1852 | case 'X': |
| 1853 | base = 16; |
| 1854 | break; |
| 1855 | case 'i': |
| 1856 | base = 0; |
| 1857 | case 'd': |
| 1858 | is_sign = 1; |
| 1859 | case 'u': |
| 1860 | break; |
| 1861 | case '%': |
| 1862 | /* looking for '%' in str */ |
| 1863 | if (*str++ != '%') |
| 1864 | return num; |
| 1865 | continue; |
| 1866 | default: |
| 1867 | /* invalid format; stop here */ |
| 1868 | return num; |
| 1869 | } |
| 1870 | |
| 1871 | /* have some sort of integer conversion. |
| 1872 | * first, skip white space in buffer. |
| 1873 | */ |
| 1874 | while (isspace(*str)) |
| 1875 | str++; |
| 1876 | |
| 1877 | digit = *str; |
| 1878 | if (is_sign && digit == '-') |
| 1879 | digit = *(str + 1); |
| 1880 | |
| 1881 | if (!digit |
| 1882 | || (base == 16 && !isxdigit(digit)) |
| 1883 | || (base == 10 && !isdigit(digit)) |
| 1884 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 1885 | || (base == 0 && !isdigit(digit))) |
| 1886 | break; |
| 1887 | |
| 1888 | switch(qualifier) { |
| 1889 | case 'H': /* that's 'hh' in format */ |
| 1890 | if (is_sign) { |
| 1891 | signed char *s = (signed char *) va_arg(args,signed char *); |
| 1892 | *s = (signed char) simple_strtol(str,&next,base); |
| 1893 | } else { |
| 1894 | unsigned char *s = (unsigned char *) va_arg(args, unsigned char *); |
| 1895 | *s = (unsigned char) simple_strtoul(str, &next, base); |
| 1896 | } |
| 1897 | break; |
| 1898 | case 'h': |
| 1899 | if (is_sign) { |
| 1900 | short *s = (short *) va_arg(args,short *); |
| 1901 | *s = (short) simple_strtol(str,&next,base); |
| 1902 | } else { |
| 1903 | unsigned short *s = (unsigned short *) va_arg(args, unsigned short *); |
| 1904 | *s = (unsigned short) simple_strtoul(str, &next, base); |
| 1905 | } |
| 1906 | break; |
| 1907 | case 'l': |
| 1908 | if (is_sign) { |
| 1909 | long *l = (long *) va_arg(args,long *); |
| 1910 | *l = simple_strtol(str,&next,base); |
| 1911 | } else { |
| 1912 | unsigned long *l = (unsigned long*) va_arg(args,unsigned long*); |
| 1913 | *l = simple_strtoul(str,&next,base); |
| 1914 | } |
| 1915 | break; |
| 1916 | case 'L': |
| 1917 | if (is_sign) { |
| 1918 | long long *l = (long long*) va_arg(args,long long *); |
| 1919 | *l = simple_strtoll(str,&next,base); |
| 1920 | } else { |
| 1921 | unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*); |
| 1922 | *l = simple_strtoull(str,&next,base); |
| 1923 | } |
| 1924 | break; |
| 1925 | case 'Z': |
| 1926 | case 'z': |
| 1927 | { |
| 1928 | size_t *s = (size_t*) va_arg(args,size_t*); |
| 1929 | *s = (size_t) simple_strtoul(str,&next,base); |
| 1930 | } |
| 1931 | break; |
| 1932 | default: |
| 1933 | if (is_sign) { |
| 1934 | int *i = (int *) va_arg(args, int*); |
| 1935 | *i = (int) simple_strtol(str,&next,base); |
| 1936 | } else { |
| 1937 | unsigned int *i = (unsigned int*) va_arg(args, unsigned int*); |
| 1938 | *i = (unsigned int) simple_strtoul(str,&next,base); |
| 1939 | } |
| 1940 | break; |
| 1941 | } |
| 1942 | num++; |
| 1943 | |
| 1944 | if (!next) |
| 1945 | break; |
| 1946 | str = next; |
| 1947 | } |
Johannes Berg | c6b40d1 | 2007-05-08 00:27:20 -0700 | [diff] [blame] | 1948 | |
| 1949 | /* |
| 1950 | * Now we've come all the way through so either the input string or the |
| 1951 | * format ended. In the former case, there can be a %n at the current |
| 1952 | * position in the format that needs to be filled. |
| 1953 | */ |
| 1954 | if (*fmt == '%' && *(fmt + 1) == 'n') { |
| 1955 | int *p = (int *)va_arg(args, int *); |
| 1956 | *p = str - buf; |
| 1957 | } |
| 1958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | return num; |
| 1960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | EXPORT_SYMBOL(vsscanf); |
| 1962 | |
| 1963 | /** |
| 1964 | * sscanf - Unformat a buffer into a list of arguments |
| 1965 | * @buf: input buffer |
| 1966 | * @fmt: formatting of buffer |
| 1967 | * @...: resulting arguments |
| 1968 | */ |
| 1969 | int sscanf(const char * buf, const char * fmt, ...) |
| 1970 | { |
| 1971 | va_list args; |
| 1972 | int i; |
| 1973 | |
| 1974 | va_start(args,fmt); |
| 1975 | i = vsscanf(buf,fmt,args); |
| 1976 | va_end(args); |
| 1977 | return i; |
| 1978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | EXPORT_SYMBOL(sscanf); |