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