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