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); |
| 173 | if ((*tail == '\0') || |
| 174 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { |
| 175 | *res = val; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | EXPORT_SYMBOL(strict_strtoul); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * strict_strtol - convert a string to a long strictly |
| 185 | * @cp: The string to be converted |
| 186 | * @base: The number base to use |
| 187 | * @res: The converted result value |
| 188 | * |
| 189 | * strict_strtol is similiar to strict_strtoul, but it allows the first |
| 190 | * character of a string is '-'. |
| 191 | * |
| 192 | * It returns 0 if conversion is successful and *res is set to the converted |
| 193 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 194 | */ |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 195 | int strict_strtol(const char *cp, unsigned int base, long *res) |
| 196 | { |
| 197 | int ret; |
| 198 | if (*cp == '-') { |
| 199 | ret = strict_strtoul(cp + 1, base, (unsigned long *)res); |
| 200 | if (!ret) |
| 201 | *res = -(*res); |
| 202 | } else { |
| 203 | ret = strict_strtoul(cp, base, (unsigned long *)res); |
| 204 | } |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | EXPORT_SYMBOL(strict_strtol); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * strict_strtoull - convert a string to an unsigned long long strictly |
| 212 | * @cp: The string to be converted |
| 213 | * @base: The number base to use |
| 214 | * @res: The converted result value |
| 215 | * |
| 216 | * strict_strtoull converts a string to an unsigned long long only if the |
| 217 | * string is really an unsigned long long string, any string containing |
| 218 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
| 219 | * only a newline char at the tail is acceptible because people generally |
| 220 | * change a module parameter in the following way: |
| 221 | * |
| 222 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
| 223 | * |
| 224 | * echo will append a newline to the tail of the string. |
| 225 | * |
| 226 | * It returns 0 if conversion is successful and *res is set to the converted |
| 227 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 228 | * |
| 229 | * simple_strtoull just ignores the successive invalid characters and |
| 230 | * return the converted value of prefix part of the string. |
| 231 | */ |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 232 | int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) |
| 233 | { |
| 234 | char *tail; |
| 235 | unsigned long long val; |
| 236 | size_t len; |
| 237 | |
| 238 | *res = 0; |
| 239 | len = strlen(cp); |
| 240 | if (len == 0) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | val = simple_strtoull(cp, &tail, base); |
| 244 | if ((*tail == '\0') || |
| 245 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { |
| 246 | *res = val; |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | EXPORT_SYMBOL(strict_strtoull); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * strict_strtoll - convert a string to a long long strictly |
| 256 | * @cp: The string to be converted |
| 257 | * @base: The number base to use |
| 258 | * @res: The converted result value |
| 259 | * |
| 260 | * strict_strtoll is similiar to strict_strtoull, but it allows the first |
| 261 | * character of a string is '-'. |
| 262 | * |
| 263 | * It returns 0 if conversion is successful and *res is set to the converted |
| 264 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 265 | */ |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 266 | int strict_strtoll(const char *cp, unsigned int base, long long *res) |
| 267 | { |
| 268 | int ret; |
| 269 | if (*cp == '-') { |
| 270 | ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); |
| 271 | if (!ret) |
| 272 | *res = -(*res); |
| 273 | } else { |
| 274 | ret = strict_strtoull(cp, base, (unsigned long long *)res); |
| 275 | } |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 276 | |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 277 | return ret; |
| 278 | } |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 279 | EXPORT_SYMBOL(strict_strtoll); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | static int skip_atoi(const char **s) |
| 282 | { |
| 283 | int i=0; |
| 284 | |
| 285 | while (isdigit(**s)) |
| 286 | i = i*10 + *((*s)++) - '0'; |
| 287 | return i; |
| 288 | } |
| 289 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 290 | /* Decimal conversion is by far the most typical, and is used |
| 291 | * for /proc and /sys data. This directly impacts e.g. top performance |
| 292 | * with many processes running. We optimize it for speed |
| 293 | * using code from |
| 294 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
| 295 | * (with permission from the author, Douglas W. Jones). */ |
| 296 | |
| 297 | /* Formats correctly any integer in [0,99999]. |
| 298 | * Outputs from one to five digits depending on input. |
| 299 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
| 300 | static char* put_dec_trunc(char *buf, unsigned q) |
| 301 | { |
| 302 | unsigned d3, d2, d1, d0; |
| 303 | d1 = (q>>4) & 0xf; |
| 304 | d2 = (q>>8) & 0xf; |
| 305 | d3 = (q>>12); |
| 306 | |
| 307 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 308 | q = (d0 * 0xcd) >> 11; |
| 309 | d0 = d0 - 10*q; |
| 310 | *buf++ = d0 + '0'; /* least significant digit */ |
| 311 | d1 = q + 9*d3 + 5*d2 + d1; |
| 312 | if (d1 != 0) { |
| 313 | q = (d1 * 0xcd) >> 11; |
| 314 | d1 = d1 - 10*q; |
| 315 | *buf++ = d1 + '0'; /* next digit */ |
| 316 | |
| 317 | d2 = q + 2*d2; |
| 318 | if ((d2 != 0) || (d3 != 0)) { |
| 319 | q = (d2 * 0xd) >> 7; |
| 320 | d2 = d2 - 10*q; |
| 321 | *buf++ = d2 + '0'; /* next digit */ |
| 322 | |
| 323 | d3 = q + 4*d3; |
| 324 | if (d3 != 0) { |
| 325 | q = (d3 * 0xcd) >> 11; |
| 326 | d3 = d3 - 10*q; |
| 327 | *buf++ = d3 + '0'; /* next digit */ |
| 328 | if (q != 0) |
| 329 | *buf++ = q + '0'; /* most sign. digit */ |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | return buf; |
| 334 | } |
| 335 | /* Same with if's removed. Always emits five digits */ |
| 336 | static char* put_dec_full(char *buf, unsigned q) |
| 337 | { |
| 338 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
| 339 | /* but anyway, gcc produces better code with full-sized ints */ |
| 340 | unsigned d3, d2, d1, d0; |
| 341 | d1 = (q>>4) & 0xf; |
| 342 | d2 = (q>>8) & 0xf; |
| 343 | d3 = (q>>12); |
| 344 | |
| 345 | /* Possible ways to approx. divide by 10 */ |
| 346 | /* gcc -O2 replaces multiply with shifts and adds */ |
| 347 | // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
| 348 | // (x * 0x67) >> 10: 1100111 |
| 349 | // (x * 0x34) >> 9: 110100 - same |
| 350 | // (x * 0x1a) >> 8: 11010 - same |
| 351 | // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) |
| 352 | |
| 353 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 354 | q = (d0 * 0xcd) >> 11; |
| 355 | d0 = d0 - 10*q; |
| 356 | *buf++ = d0 + '0'; |
| 357 | d1 = q + 9*d3 + 5*d2 + d1; |
| 358 | q = (d1 * 0xcd) >> 11; |
| 359 | d1 = d1 - 10*q; |
| 360 | *buf++ = d1 + '0'; |
| 361 | |
| 362 | d2 = q + 2*d2; |
| 363 | q = (d2 * 0xd) >> 7; |
| 364 | d2 = d2 - 10*q; |
| 365 | *buf++ = d2 + '0'; |
| 366 | |
| 367 | d3 = q + 4*d3; |
| 368 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
| 369 | /* q = (d3 * 0x67) >> 10; - would also work */ |
| 370 | d3 = d3 - 10*q; |
| 371 | *buf++ = d3 + '0'; |
| 372 | *buf++ = q + '0'; |
| 373 | return buf; |
| 374 | } |
| 375 | /* No inlining helps gcc to use registers better */ |
| 376 | static noinline char* put_dec(char *buf, unsigned long long num) |
| 377 | { |
| 378 | while (1) { |
| 379 | unsigned rem; |
| 380 | if (num < 100000) |
| 381 | return put_dec_trunc(buf, num); |
| 382 | rem = do_div(num, 100000); |
| 383 | buf = put_dec_full(buf, rem); |
| 384 | } |
| 385 | } |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | #define ZEROPAD 1 /* pad with zero */ |
| 388 | #define SIGN 2 /* unsigned/signed long */ |
| 389 | #define PLUS 4 /* show plus */ |
| 390 | #define SPACE 8 /* space if plus */ |
| 391 | #define LEFT 16 /* left justified */ |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 392 | #define SMALL 32 /* Must be 32 == 0x20 */ |
| 393 | #define SPECIAL 64 /* 0x */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 395 | static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 397 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
| 398 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
| 399 | |
| 400 | char tmp[66]; |
| 401 | char sign; |
| 402 | char locase; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 403 | int need_pfx = ((type & SPECIAL) && base != 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | int i; |
| 405 | |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 406 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
| 407 | * produces same digits or (maybe lowercased) letters */ |
| 408 | locase = (type & SMALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (type & LEFT) |
| 410 | type &= ~ZEROPAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | sign = 0; |
| 412 | if (type & SIGN) { |
| 413 | if ((signed long long) num < 0) { |
| 414 | sign = '-'; |
| 415 | num = - (signed long long) num; |
| 416 | size--; |
| 417 | } else if (type & PLUS) { |
| 418 | sign = '+'; |
| 419 | size--; |
| 420 | } else if (type & SPACE) { |
| 421 | sign = ' '; |
| 422 | size--; |
| 423 | } |
| 424 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 425 | if (need_pfx) { |
| 426 | size--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (base == 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | size--; |
| 429 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 430 | |
| 431 | /* generate full string in tmp[], in reverse order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | i = 0; |
| 433 | if (num == 0) |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 434 | tmp[i++] = '0'; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 435 | /* Generic code, for any base: |
| 436 | else do { |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 437 | tmp[i++] = (digits[do_div(num,base)] | locase); |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 438 | } while (num != 0); |
| 439 | */ |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 440 | else if (base != 10) { /* 8 or 16 */ |
| 441 | int mask = base - 1; |
| 442 | int shift = 3; |
| 443 | if (base == 16) shift = 4; |
| 444 | do { |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 445 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 446 | num >>= shift; |
| 447 | } while (num); |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 448 | } else { /* base 10 */ |
| 449 | i = put_dec(tmp, num) - tmp; |
| 450 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 451 | |
| 452 | /* printing 100 using %2d gives "100", not "00" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | if (i > precision) |
| 454 | precision = i; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 455 | /* leading space padding */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | size -= precision; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 457 | if (!(type & (ZEROPAD+LEFT))) { |
| 458 | while(--size >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 459 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | *buf = ' '; |
| 461 | ++buf; |
| 462 | } |
| 463 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 464 | /* sign */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (sign) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 466 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | *buf = sign; |
| 468 | ++buf; |
| 469 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 470 | /* "0x" / "0" prefix */ |
| 471 | if (need_pfx) { |
| 472 | if (buf < end) |
| 473 | *buf = '0'; |
| 474 | ++buf; |
| 475 | if (base == 16) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 476 | if (buf < end) |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 477 | *buf = ('X' | locase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | ++buf; |
| 479 | } |
| 480 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 481 | /* zero or space padding */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (!(type & LEFT)) { |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 483 | char c = (type & ZEROPAD) ? '0' : ' '; |
| 484 | while (--size >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 485 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | *buf = c; |
| 487 | ++buf; |
| 488 | } |
| 489 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 490 | /* hmm even more zero padding? */ |
| 491 | while (i <= --precision) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 492 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | *buf = '0'; |
| 494 | ++buf; |
| 495 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 496 | /* actual digits of result */ |
| 497 | while (--i >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 498 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | *buf = tmp[i]; |
| 500 | ++buf; |
| 501 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 502 | /* trailing space padding */ |
| 503 | while (--size >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 504 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | *buf = ' '; |
| 506 | ++buf; |
| 507 | } |
| 508 | return buf; |
| 509 | } |
| 510 | |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 511 | static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags) |
| 512 | { |
| 513 | int len, i; |
| 514 | |
| 515 | if ((unsigned long)s < PAGE_SIZE) |
| 516 | s = "<NULL>"; |
| 517 | |
| 518 | len = strnlen(s, precision); |
| 519 | |
| 520 | if (!(flags & LEFT)) { |
| 521 | while (len < field_width--) { |
| 522 | if (buf < end) |
| 523 | *buf = ' '; |
| 524 | ++buf; |
| 525 | } |
| 526 | } |
| 527 | for (i = 0; i < len; ++i) { |
| 528 | if (buf < end) |
| 529 | *buf = *s; |
| 530 | ++buf; ++s; |
| 531 | } |
| 532 | while (len < field_width--) { |
| 533 | if (buf < end) |
| 534 | *buf = ' '; |
| 535 | ++buf; |
| 536 | } |
| 537 | return buf; |
| 538 | } |
| 539 | |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 540 | static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags) |
| 541 | { |
| 542 | unsigned long value = (unsigned long) ptr; |
| 543 | #ifdef CONFIG_KALLSYMS |
| 544 | char sym[KSYM_SYMBOL_LEN]; |
| 545 | sprint_symbol(sym, value); |
| 546 | return string(buf, end, sym, field_width, precision, flags); |
| 547 | #else |
| 548 | field_width = 2*sizeof(void *); |
| 549 | flags |= SPECIAL | SMALL | ZEROPAD; |
| 550 | return number(buf, end, value, 16, field_width, precision, flags); |
| 551 | #endif |
| 552 | } |
| 553 | |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame^] | 554 | static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags) |
| 555 | { |
| 556 | #ifndef IO_RSRC_PRINTK_SIZE |
| 557 | #define IO_RSRC_PRINTK_SIZE 4 |
| 558 | #endif |
| 559 | |
| 560 | #ifndef MEM_RSRC_PRINTK_SIZE |
| 561 | #define MEM_RSRC_PRINTK_SIZE 8 |
| 562 | #endif |
| 563 | |
| 564 | /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ |
| 565 | char sym[4*sizeof(resource_size_t) + 8]; |
| 566 | char *p = sym, *pend = sym + sizeof(sym); |
| 567 | int size = -1; |
| 568 | |
| 569 | if (res->flags & IORESOURCE_IO) |
| 570 | size = IO_RSRC_PRINTK_SIZE; |
| 571 | else if (res->flags & IORESOURCE_MEM) |
| 572 | size = MEM_RSRC_PRINTK_SIZE; |
| 573 | |
| 574 | *p++ = '['; |
| 575 | p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD); |
| 576 | *p++ = '-'; |
| 577 | p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD); |
| 578 | *p++ = ']'; |
| 579 | *p = 0; |
| 580 | |
| 581 | return string(buf, end, sym, field_width, precision, flags); |
| 582 | } |
| 583 | |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 584 | /* |
| 585 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 586 | * by an extra set of alphanumeric characters that are extended format |
| 587 | * specifiers. |
| 588 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame^] | 589 | * Right now we handle: |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 590 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame^] | 591 | * - 'F' For symbolic function descriptor pointers |
| 592 | * - 'S' For symbolic direct pointers |
| 593 | * - 'R' For a struct resource pointer, it prints the range of |
| 594 | * addresses (not the name nor the flags) |
| 595 | * |
| 596 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 597 | * function pointers are really function descriptors, which contain a |
| 598 | * pointer to the real address. |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 599 | */ |
| 600 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags) |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 601 | { |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 602 | switch (*fmt) { |
| 603 | case 'F': |
| 604 | ptr = dereference_function_descriptor(ptr); |
| 605 | /* Fallthrough */ |
| 606 | case 'S': |
| 607 | return symbol_string(buf, end, ptr, field_width, precision, flags); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame^] | 608 | case 'R': |
| 609 | return resource_string(buf, end, ptr, field_width, precision, flags); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 610 | } |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 611 | flags |= SMALL; |
| 612 | if (field_width == -1) { |
| 613 | field_width = 2*sizeof(void *); |
| 614 | flags |= ZEROPAD; |
| 615 | } |
| 616 | return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags); |
| 617 | } |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | /** |
| 620 | * vsnprintf - Format a string and place it in a buffer |
| 621 | * @buf: The buffer to place the result into |
| 622 | * @size: The size of the buffer, including the trailing null space |
| 623 | * @fmt: The format string to use |
| 624 | * @args: Arguments for the format string |
| 625 | * |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 626 | * This function follows C99 vsnprintf, but has some extensions: |
| 627 | * %pS output the name of a text symbol |
| 628 | * %pF output the name of a function pointer |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame^] | 629 | * %pR output the address range in a struct resource |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 630 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | * The return value is the number of characters which would |
| 632 | * be generated for the given input, excluding the trailing |
| 633 | * '\0', as per ISO C99. If you want to have the exact |
| 634 | * number of characters written into @buf as return value |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 635 | * (not including the trailing '\0'), use vscnprintf(). If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | * return is greater than or equal to @size, the resulting |
| 637 | * string is truncated. |
| 638 | * |
| 639 | * 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] | 640 | * You probably want snprintf() instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | */ |
| 642 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 643 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | unsigned long long num; |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 645 | int base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | char *str, *end, c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | int flags; /* flags to number() */ |
| 649 | |
| 650 | int field_width; /* width of output field */ |
| 651 | int precision; /* min. # of digits for integers; max |
| 652 | number of chars for from string */ |
| 653 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ |
| 654 | /* 'z' support added 23/7/1999 S.H. */ |
| 655 | /* 'z' changed to 'Z' --davidm 1/25/99 */ |
Al Viro | 8032230 | 2005-08-23 22:48:17 +0100 | [diff] [blame] | 656 | /* 't' added for ptrdiff_t */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 658 | /* Reject out-of-range values early. Large positive sizes are |
| 659 | used for unknown buffer sizes. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | if (unlikely((int) size < 0)) { |
| 661 | /* There can be only one.. */ |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 662 | static char warn = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | WARN_ON(warn); |
| 664 | warn = 0; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | str = buf; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 669 | end = buf + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 671 | /* Make sure end is always >= buf */ |
| 672 | if (end < buf) { |
| 673 | end = ((void *)-1); |
| 674 | size = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | for (; *fmt ; ++fmt) { |
| 678 | if (*fmt != '%') { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 679 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | *str = *fmt; |
| 681 | ++str; |
| 682 | continue; |
| 683 | } |
| 684 | |
| 685 | /* process flags */ |
| 686 | flags = 0; |
| 687 | repeat: |
| 688 | ++fmt; /* this also skips first '%' */ |
| 689 | switch (*fmt) { |
| 690 | case '-': flags |= LEFT; goto repeat; |
| 691 | case '+': flags |= PLUS; goto repeat; |
| 692 | case ' ': flags |= SPACE; goto repeat; |
| 693 | case '#': flags |= SPECIAL; goto repeat; |
| 694 | case '0': flags |= ZEROPAD; goto repeat; |
| 695 | } |
| 696 | |
| 697 | /* get field width */ |
| 698 | field_width = -1; |
| 699 | if (isdigit(*fmt)) |
| 700 | field_width = skip_atoi(&fmt); |
| 701 | else if (*fmt == '*') { |
| 702 | ++fmt; |
| 703 | /* it's the next argument */ |
| 704 | field_width = va_arg(args, int); |
| 705 | if (field_width < 0) { |
| 706 | field_width = -field_width; |
| 707 | flags |= LEFT; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | /* get the precision */ |
| 712 | precision = -1; |
| 713 | if (*fmt == '.') { |
| 714 | ++fmt; |
| 715 | if (isdigit(*fmt)) |
| 716 | precision = skip_atoi(&fmt); |
| 717 | else if (*fmt == '*') { |
| 718 | ++fmt; |
| 719 | /* it's the next argument */ |
| 720 | precision = va_arg(args, int); |
| 721 | } |
| 722 | if (precision < 0) |
| 723 | precision = 0; |
| 724 | } |
| 725 | |
| 726 | /* get the conversion qualifier */ |
| 727 | qualifier = -1; |
| 728 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
Al Viro | 8032230 | 2005-08-23 22:48:17 +0100 | [diff] [blame] | 729 | *fmt =='Z' || *fmt == 'z' || *fmt == 't') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | qualifier = *fmt; |
| 731 | ++fmt; |
| 732 | if (qualifier == 'l' && *fmt == 'l') { |
| 733 | qualifier = 'L'; |
| 734 | ++fmt; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* default base */ |
| 739 | base = 10; |
| 740 | |
| 741 | switch (*fmt) { |
| 742 | case 'c': |
| 743 | if (!(flags & LEFT)) { |
| 744 | while (--field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 745 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | *str = ' '; |
| 747 | ++str; |
| 748 | } |
| 749 | } |
| 750 | c = (unsigned char) va_arg(args, int); |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 751 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | *str = c; |
| 753 | ++str; |
| 754 | while (--field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 755 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | *str = ' '; |
| 757 | ++str; |
| 758 | } |
| 759 | continue; |
| 760 | |
| 761 | case 's': |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 762 | str = string(str, end, va_arg(args, char *), field_width, precision, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | continue; |
| 764 | |
| 765 | case 'p': |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 766 | str = pointer(fmt+1, str, end, |
| 767 | va_arg(args, void *), |
| 768 | field_width, precision, flags); |
| 769 | /* Skip all alphanumeric pointer suffixes */ |
| 770 | while (isalnum(fmt[1])) |
| 771 | fmt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | continue; |
| 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | case 'n': |
| 775 | /* FIXME: |
| 776 | * What does C99 say about the overflow case here? */ |
| 777 | if (qualifier == 'l') { |
| 778 | long * ip = va_arg(args, long *); |
| 779 | *ip = (str - buf); |
| 780 | } else if (qualifier == 'Z' || qualifier == 'z') { |
| 781 | size_t * ip = va_arg(args, size_t *); |
| 782 | *ip = (str - buf); |
| 783 | } else { |
| 784 | int * ip = va_arg(args, int *); |
| 785 | *ip = (str - buf); |
| 786 | } |
| 787 | continue; |
| 788 | |
| 789 | case '%': |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 790 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | *str = '%'; |
| 792 | ++str; |
| 793 | continue; |
| 794 | |
| 795 | /* integer number formats - set up the flags and "break" */ |
| 796 | case 'o': |
| 797 | base = 8; |
| 798 | break; |
| 799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | case 'x': |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 801 | flags |= SMALL; |
| 802 | case 'X': |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | base = 16; |
| 804 | break; |
| 805 | |
| 806 | case 'd': |
| 807 | case 'i': |
| 808 | flags |= SIGN; |
| 809 | case 'u': |
| 810 | break; |
| 811 | |
| 812 | default: |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 813 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | *str = '%'; |
| 815 | ++str; |
| 816 | if (*fmt) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 817 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | *str = *fmt; |
| 819 | ++str; |
| 820 | } else { |
| 821 | --fmt; |
| 822 | } |
| 823 | continue; |
| 824 | } |
| 825 | if (qualifier == 'L') |
| 826 | num = va_arg(args, long long); |
| 827 | else if (qualifier == 'l') { |
| 828 | num = va_arg(args, unsigned long); |
| 829 | if (flags & SIGN) |
| 830 | num = (signed long) num; |
| 831 | } else if (qualifier == 'Z' || qualifier == 'z') { |
| 832 | num = va_arg(args, size_t); |
Al Viro | 8032230 | 2005-08-23 22:48:17 +0100 | [diff] [blame] | 833 | } else if (qualifier == 't') { |
| 834 | num = va_arg(args, ptrdiff_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } else if (qualifier == 'h') { |
| 836 | num = (unsigned short) va_arg(args, int); |
| 837 | if (flags & SIGN) |
| 838 | num = (signed short) num; |
| 839 | } else { |
| 840 | num = va_arg(args, unsigned int); |
| 841 | if (flags & SIGN) |
| 842 | num = (signed int) num; |
| 843 | } |
| 844 | str = number(str, end, num, base, |
| 845 | field_width, precision, flags); |
| 846 | } |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 847 | if (size > 0) { |
| 848 | if (str < end) |
| 849 | *str = '\0'; |
| 850 | else |
Linus Torvalds | 0a6047e | 2006-06-28 17:09:34 -0700 | [diff] [blame] | 851 | end[-1] = '\0'; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 852 | } |
| 853 | /* the trailing null byte doesn't count towards the total */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return str-buf; |
| 855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | EXPORT_SYMBOL(vsnprintf); |
| 857 | |
| 858 | /** |
| 859 | * vscnprintf - Format a string and place it in a buffer |
| 860 | * @buf: The buffer to place the result into |
| 861 | * @size: The size of the buffer, including the trailing null space |
| 862 | * @fmt: The format string to use |
| 863 | * @args: Arguments for the format string |
| 864 | * |
| 865 | * The return value is the number of characters which have been written into |
| 866 | * the @buf not including the trailing '\0'. If @size is <= 0 the function |
| 867 | * returns 0. |
| 868 | * |
| 869 | * 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] | 870 | * You probably want scnprintf() instead. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 871 | * |
| 872 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | */ |
| 874 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 875 | { |
| 876 | int i; |
| 877 | |
| 878 | i=vsnprintf(buf,size,fmt,args); |
| 879 | return (i >= size) ? (size - 1) : i; |
| 880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | EXPORT_SYMBOL(vscnprintf); |
| 882 | |
| 883 | /** |
| 884 | * snprintf - Format a string and place it in a buffer |
| 885 | * @buf: The buffer to place the result into |
| 886 | * @size: The size of the buffer, including the trailing null space |
| 887 | * @fmt: The format string to use |
| 888 | * @...: Arguments for the format string |
| 889 | * |
| 890 | * The return value is the number of characters which would be |
| 891 | * generated for the given input, excluding the trailing null, |
| 892 | * as per ISO C99. If the return is greater than or equal to |
| 893 | * @size, the resulting string is truncated. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 894 | * |
| 895 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | */ |
| 897 | int snprintf(char * buf, size_t size, const char *fmt, ...) |
| 898 | { |
| 899 | va_list args; |
| 900 | int i; |
| 901 | |
| 902 | va_start(args, fmt); |
| 903 | i=vsnprintf(buf,size,fmt,args); |
| 904 | va_end(args); |
| 905 | return i; |
| 906 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | EXPORT_SYMBOL(snprintf); |
| 908 | |
| 909 | /** |
| 910 | * scnprintf - Format a string and place it in a buffer |
| 911 | * @buf: The buffer to place the result into |
| 912 | * @size: The size of the buffer, including the trailing null space |
| 913 | * @fmt: The format string to use |
| 914 | * @...: Arguments for the format string |
| 915 | * |
| 916 | * 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] | 917 | * the trailing '\0'. If @size is <= 0 the function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | */ |
| 919 | |
| 920 | int scnprintf(char * buf, size_t size, const char *fmt, ...) |
| 921 | { |
| 922 | va_list args; |
| 923 | int i; |
| 924 | |
| 925 | va_start(args, fmt); |
| 926 | i = vsnprintf(buf, size, fmt, args); |
| 927 | va_end(args); |
| 928 | return (i >= size) ? (size - 1) : i; |
| 929 | } |
| 930 | EXPORT_SYMBOL(scnprintf); |
| 931 | |
| 932 | /** |
| 933 | * vsprintf - Format a string and place it in a buffer |
| 934 | * @buf: The buffer to place the result into |
| 935 | * @fmt: The format string to use |
| 936 | * @args: Arguments for the format string |
| 937 | * |
| 938 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 939 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * buffer overflows. |
| 941 | * |
| 942 | * 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] | 943 | * You probably want sprintf() instead. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 944 | * |
| 945 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | */ |
| 947 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 948 | { |
| 949 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | EXPORT_SYMBOL(vsprintf); |
| 952 | |
| 953 | /** |
| 954 | * sprintf - Format a string and place it in a buffer |
| 955 | * @buf: The buffer to place the result into |
| 956 | * @fmt: The format string to use |
| 957 | * @...: Arguments for the format string |
| 958 | * |
| 959 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 960 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | * buffer overflows. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 962 | * |
| 963 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | */ |
| 965 | int sprintf(char * buf, const char *fmt, ...) |
| 966 | { |
| 967 | va_list args; |
| 968 | int i; |
| 969 | |
| 970 | va_start(args, fmt); |
| 971 | i=vsnprintf(buf, INT_MAX, fmt, args); |
| 972 | va_end(args); |
| 973 | return i; |
| 974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | EXPORT_SYMBOL(sprintf); |
| 976 | |
| 977 | /** |
| 978 | * vsscanf - Unformat a buffer into a list of arguments |
| 979 | * @buf: input buffer |
| 980 | * @fmt: format of buffer |
| 981 | * @args: arguments |
| 982 | */ |
| 983 | int vsscanf(const char * buf, const char * fmt, va_list args) |
| 984 | { |
| 985 | const char *str = buf; |
| 986 | char *next; |
| 987 | char digit; |
| 988 | int num = 0; |
| 989 | int qualifier; |
| 990 | int base; |
| 991 | int field_width; |
| 992 | int is_sign = 0; |
| 993 | |
| 994 | while(*fmt && *str) { |
| 995 | /* skip any white space in format */ |
| 996 | /* white space in format matchs any amount of |
| 997 | * white space, including none, in the input. |
| 998 | */ |
| 999 | if (isspace(*fmt)) { |
| 1000 | while (isspace(*fmt)) |
| 1001 | ++fmt; |
| 1002 | while (isspace(*str)) |
| 1003 | ++str; |
| 1004 | } |
| 1005 | |
| 1006 | /* anything that is not a conversion must match exactly */ |
| 1007 | if (*fmt != '%' && *fmt) { |
| 1008 | if (*fmt++ != *str++) |
| 1009 | break; |
| 1010 | continue; |
| 1011 | } |
| 1012 | |
| 1013 | if (!*fmt) |
| 1014 | break; |
| 1015 | ++fmt; |
| 1016 | |
| 1017 | /* skip this conversion. |
| 1018 | * advance both strings to next white space |
| 1019 | */ |
| 1020 | if (*fmt == '*') { |
| 1021 | while (!isspace(*fmt) && *fmt) |
| 1022 | fmt++; |
| 1023 | while (!isspace(*str) && *str) |
| 1024 | str++; |
| 1025 | continue; |
| 1026 | } |
| 1027 | |
| 1028 | /* get field width */ |
| 1029 | field_width = -1; |
| 1030 | if (isdigit(*fmt)) |
| 1031 | field_width = skip_atoi(&fmt); |
| 1032 | |
| 1033 | /* get conversion qualifier */ |
| 1034 | qualifier = -1; |
| 1035 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
| 1036 | *fmt == 'Z' || *fmt == 'z') { |
| 1037 | qualifier = *fmt++; |
| 1038 | if (unlikely(qualifier == *fmt)) { |
| 1039 | if (qualifier == 'h') { |
| 1040 | qualifier = 'H'; |
| 1041 | fmt++; |
| 1042 | } else if (qualifier == 'l') { |
| 1043 | qualifier = 'L'; |
| 1044 | fmt++; |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | base = 10; |
| 1049 | is_sign = 0; |
| 1050 | |
| 1051 | if (!*fmt || !*str) |
| 1052 | break; |
| 1053 | |
| 1054 | switch(*fmt++) { |
| 1055 | case 'c': |
| 1056 | { |
| 1057 | char *s = (char *) va_arg(args,char*); |
| 1058 | if (field_width == -1) |
| 1059 | field_width = 1; |
| 1060 | do { |
| 1061 | *s++ = *str++; |
| 1062 | } while (--field_width > 0 && *str); |
| 1063 | num++; |
| 1064 | } |
| 1065 | continue; |
| 1066 | case 's': |
| 1067 | { |
| 1068 | char *s = (char *) va_arg(args, char *); |
| 1069 | if(field_width == -1) |
| 1070 | field_width = INT_MAX; |
| 1071 | /* first, skip leading white space in buffer */ |
| 1072 | while (isspace(*str)) |
| 1073 | str++; |
| 1074 | |
| 1075 | /* now copy until next white space */ |
| 1076 | while (*str && !isspace(*str) && field_width--) { |
| 1077 | *s++ = *str++; |
| 1078 | } |
| 1079 | *s = '\0'; |
| 1080 | num++; |
| 1081 | } |
| 1082 | continue; |
| 1083 | case 'n': |
| 1084 | /* return number of characters read so far */ |
| 1085 | { |
| 1086 | int *i = (int *)va_arg(args,int*); |
| 1087 | *i = str - buf; |
| 1088 | } |
| 1089 | continue; |
| 1090 | case 'o': |
| 1091 | base = 8; |
| 1092 | break; |
| 1093 | case 'x': |
| 1094 | case 'X': |
| 1095 | base = 16; |
| 1096 | break; |
| 1097 | case 'i': |
| 1098 | base = 0; |
| 1099 | case 'd': |
| 1100 | is_sign = 1; |
| 1101 | case 'u': |
| 1102 | break; |
| 1103 | case '%': |
| 1104 | /* looking for '%' in str */ |
| 1105 | if (*str++ != '%') |
| 1106 | return num; |
| 1107 | continue; |
| 1108 | default: |
| 1109 | /* invalid format; stop here */ |
| 1110 | return num; |
| 1111 | } |
| 1112 | |
| 1113 | /* have some sort of integer conversion. |
| 1114 | * first, skip white space in buffer. |
| 1115 | */ |
| 1116 | while (isspace(*str)) |
| 1117 | str++; |
| 1118 | |
| 1119 | digit = *str; |
| 1120 | if (is_sign && digit == '-') |
| 1121 | digit = *(str + 1); |
| 1122 | |
| 1123 | if (!digit |
| 1124 | || (base == 16 && !isxdigit(digit)) |
| 1125 | || (base == 10 && !isdigit(digit)) |
| 1126 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 1127 | || (base == 0 && !isdigit(digit))) |
| 1128 | break; |
| 1129 | |
| 1130 | switch(qualifier) { |
| 1131 | case 'H': /* that's 'hh' in format */ |
| 1132 | if (is_sign) { |
| 1133 | signed char *s = (signed char *) va_arg(args,signed char *); |
| 1134 | *s = (signed char) simple_strtol(str,&next,base); |
| 1135 | } else { |
| 1136 | unsigned char *s = (unsigned char *) va_arg(args, unsigned char *); |
| 1137 | *s = (unsigned char) simple_strtoul(str, &next, base); |
| 1138 | } |
| 1139 | break; |
| 1140 | case 'h': |
| 1141 | if (is_sign) { |
| 1142 | short *s = (short *) va_arg(args,short *); |
| 1143 | *s = (short) simple_strtol(str,&next,base); |
| 1144 | } else { |
| 1145 | unsigned short *s = (unsigned short *) va_arg(args, unsigned short *); |
| 1146 | *s = (unsigned short) simple_strtoul(str, &next, base); |
| 1147 | } |
| 1148 | break; |
| 1149 | case 'l': |
| 1150 | if (is_sign) { |
| 1151 | long *l = (long *) va_arg(args,long *); |
| 1152 | *l = simple_strtol(str,&next,base); |
| 1153 | } else { |
| 1154 | unsigned long *l = (unsigned long*) va_arg(args,unsigned long*); |
| 1155 | *l = simple_strtoul(str,&next,base); |
| 1156 | } |
| 1157 | break; |
| 1158 | case 'L': |
| 1159 | if (is_sign) { |
| 1160 | long long *l = (long long*) va_arg(args,long long *); |
| 1161 | *l = simple_strtoll(str,&next,base); |
| 1162 | } else { |
| 1163 | unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*); |
| 1164 | *l = simple_strtoull(str,&next,base); |
| 1165 | } |
| 1166 | break; |
| 1167 | case 'Z': |
| 1168 | case 'z': |
| 1169 | { |
| 1170 | size_t *s = (size_t*) va_arg(args,size_t*); |
| 1171 | *s = (size_t) simple_strtoul(str,&next,base); |
| 1172 | } |
| 1173 | break; |
| 1174 | default: |
| 1175 | if (is_sign) { |
| 1176 | int *i = (int *) va_arg(args, int*); |
| 1177 | *i = (int) simple_strtol(str,&next,base); |
| 1178 | } else { |
| 1179 | unsigned int *i = (unsigned int*) va_arg(args, unsigned int*); |
| 1180 | *i = (unsigned int) simple_strtoul(str,&next,base); |
| 1181 | } |
| 1182 | break; |
| 1183 | } |
| 1184 | num++; |
| 1185 | |
| 1186 | if (!next) |
| 1187 | break; |
| 1188 | str = next; |
| 1189 | } |
Johannes Berg | c6b40d1 | 2007-05-08 00:27:20 -0700 | [diff] [blame] | 1190 | |
| 1191 | /* |
| 1192 | * Now we've come all the way through so either the input string or the |
| 1193 | * format ended. In the former case, there can be a %n at the current |
| 1194 | * position in the format that needs to be filled. |
| 1195 | */ |
| 1196 | if (*fmt == '%' && *(fmt + 1) == 'n') { |
| 1197 | int *p = (int *)va_arg(args, int *); |
| 1198 | *p = str - buf; |
| 1199 | } |
| 1200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | return num; |
| 1202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | EXPORT_SYMBOL(vsscanf); |
| 1204 | |
| 1205 | /** |
| 1206 | * sscanf - Unformat a buffer into a list of arguments |
| 1207 | * @buf: input buffer |
| 1208 | * @fmt: formatting of buffer |
| 1209 | * @...: resulting arguments |
| 1210 | */ |
| 1211 | int sscanf(const char * buf, const char * fmt, ...) |
| 1212 | { |
| 1213 | va_list args; |
| 1214 | int i; |
| 1215 | |
| 1216 | va_start(args,fmt); |
| 1217 | i = vsscanf(buf,fmt,args); |
| 1218 | va_end(args); |
| 1219 | return i; |
| 1220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | EXPORT_SYMBOL(sscanf); |