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