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