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