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 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 12 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
| 14 | * - changed to provide snprintf and vsnprintf functions |
| 15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> |
| 16 | * - scnprintf and vscnprintf |
| 17 | */ |
| 18 | |
| 19 | #include <stdarg.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/ctype.h> |
| 24 | #include <linux/kernel.h> |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 25 | #include <linux/kallsyms.h> |
| 26 | #include <linux/uaccess.h> |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 27 | #include <linux/ioport.h> |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 28 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 30 | #include <asm/page.h> /* for PAGE_SIZE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/div64.h> |
James Bottomley | deac93d | 2008-09-03 20:43:36 -0500 | [diff] [blame] | 32 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 34 | /* Works only for digits and letters, but small and fast */ |
| 35 | #define TOLOWER(x) ((x) | 0x20) |
| 36 | |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 37 | static unsigned int simple_guess_base(const char *cp) |
| 38 | { |
| 39 | if (cp[0] == '0') { |
| 40 | if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) |
| 41 | return 16; |
| 42 | else |
| 43 | return 8; |
| 44 | } else { |
| 45 | return 10; |
| 46 | } |
| 47 | } |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /** |
| 50 | * simple_strtoul - convert a string to an unsigned long |
| 51 | * @cp: The start of the string |
| 52 | * @endp: A pointer to the end of the parsed string will be placed here |
| 53 | * @base: The number base to use |
| 54 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 55 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
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 | unsigned long result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 59 | if (!base) |
| 60 | base = simple_guess_base(cp); |
| 61 | |
| 62 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') |
| 63 | cp += 2; |
| 64 | |
| 65 | while (isxdigit(*cp)) { |
| 66 | unsigned int value; |
| 67 | |
| 68 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; |
| 69 | if (value >= base) |
| 70 | break; |
| 71 | result = result * base + value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | cp++; |
| 73 | } |
| 74 | if (endp) |
| 75 | *endp = (char *)cp; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return result; |
| 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | EXPORT_SYMBOL(simple_strtoul); |
| 80 | |
| 81 | /** |
| 82 | * simple_strtol - convert a string to a signed long |
| 83 | * @cp: The start of the string |
| 84 | * @endp: A pointer to the end of the parsed string will be placed here |
| 85 | * @base: The number base to use |
| 86 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 87 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 89 | if (*cp == '-') |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 90 | return -simple_strtoul(cp + 1, endp, base); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 91 | |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 92 | return simple_strtoul(cp, endp, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | EXPORT_SYMBOL(simple_strtol); |
| 95 | |
| 96 | /** |
| 97 | * simple_strtoull - convert a string to an unsigned long long |
| 98 | * @cp: The start of the string |
| 99 | * @endp: A pointer to the end of the parsed string will be placed here |
| 100 | * @base: The number base to use |
| 101 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 102 | 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] | 103 | { |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 104 | unsigned long long result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 106 | if (!base) |
| 107 | base = simple_guess_base(cp); |
| 108 | |
| 109 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') |
| 110 | cp += 2; |
| 111 | |
| 112 | while (isxdigit(*cp)) { |
| 113 | unsigned int value; |
| 114 | |
| 115 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; |
| 116 | if (value >= base) |
| 117 | break; |
| 118 | result = result * base + value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | cp++; |
| 120 | } |
| 121 | if (endp) |
| 122 | *endp = (char *)cp; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return result; |
| 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | EXPORT_SYMBOL(simple_strtoull); |
| 127 | |
| 128 | /** |
| 129 | * simple_strtoll - convert a string to a signed long long |
| 130 | * @cp: The start of the string |
| 131 | * @endp: A pointer to the end of the parsed string will be placed here |
| 132 | * @base: The number base to use |
| 133 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 134 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 136 | if (*cp == '-') |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 137 | return -simple_strtoull(cp + 1, endp, base); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 138 | |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 139 | return simple_strtoull(cp, endp, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 142 | /** |
| 143 | * strict_strtoul - convert a string to an unsigned long strictly |
| 144 | * @cp: The string to be converted |
| 145 | * @base: The number base to use |
| 146 | * @res: The converted result value |
| 147 | * |
| 148 | * strict_strtoul converts a string to an unsigned long only if the |
| 149 | * string is really an unsigned long string, any string containing |
| 150 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
| 151 | * only a newline char at the tail is acceptible because people generally |
| 152 | * change a module parameter in the following way: |
| 153 | * |
| 154 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
| 155 | * |
| 156 | * echo will append a newline to the tail. |
| 157 | * |
| 158 | * It returns 0 if conversion is successful and *res is set to the converted |
| 159 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 160 | * |
| 161 | * simple_strtoul just ignores the successive invalid characters and |
| 162 | * return the converted value of prefix part of the string. |
| 163 | */ |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 164 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) |
| 165 | { |
| 166 | char *tail; |
| 167 | unsigned long val; |
| 168 | size_t len; |
| 169 | |
| 170 | *res = 0; |
| 171 | len = strlen(cp); |
| 172 | if (len == 0) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | val = simple_strtoul(cp, &tail, base); |
Pavel Machek | e899aa8 | 2009-01-06 14:40:53 -0800 | [diff] [blame] | 176 | if (tail == cp) |
| 177 | return -EINVAL; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 178 | |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 179 | if ((*tail == '\0') || |
| 180 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { |
| 181 | *res = val; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | EXPORT_SYMBOL(strict_strtoul); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * strict_strtol - convert a string to a long strictly |
| 191 | * @cp: The string to be converted |
| 192 | * @base: The number base to use |
| 193 | * @res: The converted result value |
| 194 | * |
| 195 | * strict_strtol is similiar to strict_strtoul, but it allows the first |
| 196 | * character of a string is '-'. |
| 197 | * |
| 198 | * It returns 0 if conversion is successful and *res is set to the converted |
| 199 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 200 | */ |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 201 | int strict_strtol(const char *cp, unsigned int base, long *res) |
| 202 | { |
| 203 | int ret; |
| 204 | if (*cp == '-') { |
| 205 | ret = strict_strtoul(cp + 1, base, (unsigned long *)res); |
| 206 | if (!ret) |
| 207 | *res = -(*res); |
| 208 | } else { |
| 209 | ret = strict_strtoul(cp, base, (unsigned long *)res); |
| 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | EXPORT_SYMBOL(strict_strtol); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * strict_strtoull - convert a string to an unsigned long long strictly |
| 218 | * @cp: The string to be converted |
| 219 | * @base: The number base to use |
| 220 | * @res: The converted result value |
| 221 | * |
| 222 | * strict_strtoull converts a string to an unsigned long long only if the |
| 223 | * string is really an unsigned long long string, any string containing |
| 224 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
| 225 | * only a newline char at the tail is acceptible because people generally |
| 226 | * change a module parameter in the following way: |
| 227 | * |
| 228 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
| 229 | * |
| 230 | * echo will append a newline to the tail of the string. |
| 231 | * |
| 232 | * It returns 0 if conversion is successful and *res is set to the converted |
| 233 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 234 | * |
| 235 | * simple_strtoull just ignores the successive invalid characters and |
| 236 | * return the converted value of prefix part of the string. |
| 237 | */ |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 238 | int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) |
| 239 | { |
| 240 | char *tail; |
| 241 | unsigned long long val; |
| 242 | size_t len; |
| 243 | |
| 244 | *res = 0; |
| 245 | len = strlen(cp); |
| 246 | if (len == 0) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | val = simple_strtoull(cp, &tail, base); |
Pavel Machek | e899aa8 | 2009-01-06 14:40:53 -0800 | [diff] [blame] | 250 | if (tail == cp) |
| 251 | return -EINVAL; |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 252 | if ((*tail == '\0') || |
| 253 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { |
| 254 | *res = val; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | EXPORT_SYMBOL(strict_strtoull); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * strict_strtoll - convert a string to a long long strictly |
| 264 | * @cp: The string to be converted |
| 265 | * @base: The number base to use |
| 266 | * @res: The converted result value |
| 267 | * |
| 268 | * strict_strtoll is similiar to strict_strtoull, but it allows the first |
| 269 | * character of a string is '-'. |
| 270 | * |
| 271 | * It returns 0 if conversion is successful and *res is set to the converted |
| 272 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 273 | */ |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 274 | int strict_strtoll(const char *cp, unsigned int base, long long *res) |
| 275 | { |
| 276 | int ret; |
| 277 | if (*cp == '-') { |
| 278 | ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); |
| 279 | if (!ret) |
| 280 | *res = -(*res); |
| 281 | } else { |
| 282 | ret = strict_strtoull(cp, base, (unsigned long long *)res); |
| 283 | } |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 284 | |
Harvey Harrison | 9d85db2 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 285 | return ret; |
| 286 | } |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 287 | EXPORT_SYMBOL(strict_strtoll); |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | static int skip_atoi(const char **s) |
| 290 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 291 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | while (isdigit(**s)) |
| 294 | i = i*10 + *((*s)++) - '0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return i; |
| 297 | } |
| 298 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 299 | /* Decimal conversion is by far the most typical, and is used |
| 300 | * for /proc and /sys data. This directly impacts e.g. top performance |
| 301 | * with many processes running. We optimize it for speed |
| 302 | * using code from |
| 303 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
| 304 | * (with permission from the author, Douglas W. Jones). */ |
| 305 | |
| 306 | /* Formats correctly any integer in [0,99999]. |
| 307 | * Outputs from one to five digits depending on input. |
| 308 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 309 | static char *put_dec_trunc(char *buf, unsigned q) |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 310 | { |
| 311 | unsigned d3, d2, d1, d0; |
| 312 | d1 = (q>>4) & 0xf; |
| 313 | d2 = (q>>8) & 0xf; |
| 314 | d3 = (q>>12); |
| 315 | |
| 316 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 317 | q = (d0 * 0xcd) >> 11; |
| 318 | d0 = d0 - 10*q; |
| 319 | *buf++ = d0 + '0'; /* least significant digit */ |
| 320 | d1 = q + 9*d3 + 5*d2 + d1; |
| 321 | if (d1 != 0) { |
| 322 | q = (d1 * 0xcd) >> 11; |
| 323 | d1 = d1 - 10*q; |
| 324 | *buf++ = d1 + '0'; /* next digit */ |
| 325 | |
| 326 | d2 = q + 2*d2; |
| 327 | if ((d2 != 0) || (d3 != 0)) { |
| 328 | q = (d2 * 0xd) >> 7; |
| 329 | d2 = d2 - 10*q; |
| 330 | *buf++ = d2 + '0'; /* next digit */ |
| 331 | |
| 332 | d3 = q + 4*d3; |
| 333 | if (d3 != 0) { |
| 334 | q = (d3 * 0xcd) >> 11; |
| 335 | d3 = d3 - 10*q; |
| 336 | *buf++ = d3 + '0'; /* next digit */ |
| 337 | if (q != 0) |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 338 | *buf++ = q + '0'; /* most sign. digit */ |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 342 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 343 | return buf; |
| 344 | } |
| 345 | /* Same with if's removed. Always emits five digits */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 346 | static char *put_dec_full(char *buf, unsigned q) |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 347 | { |
| 348 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
| 349 | /* but anyway, gcc produces better code with full-sized ints */ |
| 350 | unsigned d3, d2, d1, d0; |
| 351 | d1 = (q>>4) & 0xf; |
| 352 | d2 = (q>>8) & 0xf; |
| 353 | d3 = (q>>12); |
| 354 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 355 | /* |
| 356 | * Possible ways to approx. divide by 10 |
| 357 | * gcc -O2 replaces multiply with shifts and adds |
| 358 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
| 359 | * (x * 0x67) >> 10: 1100111 |
| 360 | * (x * 0x34) >> 9: 110100 - same |
| 361 | * (x * 0x1a) >> 8: 11010 - same |
| 362 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) |
| 363 | */ |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 364 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 365 | q = (d0 * 0xcd) >> 11; |
| 366 | d0 = d0 - 10*q; |
| 367 | *buf++ = d0 + '0'; |
| 368 | d1 = q + 9*d3 + 5*d2 + d1; |
| 369 | q = (d1 * 0xcd) >> 11; |
| 370 | d1 = d1 - 10*q; |
| 371 | *buf++ = d1 + '0'; |
| 372 | |
| 373 | d2 = q + 2*d2; |
| 374 | q = (d2 * 0xd) >> 7; |
| 375 | d2 = d2 - 10*q; |
| 376 | *buf++ = d2 + '0'; |
| 377 | |
| 378 | d3 = q + 4*d3; |
| 379 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
| 380 | /* q = (d3 * 0x67) >> 10; - would also work */ |
| 381 | d3 = d3 - 10*q; |
| 382 | *buf++ = d3 + '0'; |
| 383 | *buf++ = q + '0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 384 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 385 | return buf; |
| 386 | } |
| 387 | /* No inlining helps gcc to use registers better */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 388 | static noinline char *put_dec(char *buf, unsigned long long num) |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 389 | { |
| 390 | while (1) { |
| 391 | unsigned rem; |
| 392 | if (num < 100000) |
| 393 | return put_dec_trunc(buf, num); |
| 394 | rem = do_div(num, 100000); |
| 395 | buf = put_dec_full(buf, rem); |
| 396 | } |
| 397 | } |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | #define ZEROPAD 1 /* pad with zero */ |
| 400 | #define SIGN 2 /* unsigned/signed long */ |
| 401 | #define PLUS 4 /* show plus */ |
| 402 | #define SPACE 8 /* space if plus */ |
| 403 | #define LEFT 16 /* left justified */ |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 404 | #define SMALL 32 /* Must be 32 == 0x20 */ |
| 405 | #define SPECIAL 64 /* 0x */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 407 | enum format_type { |
| 408 | FORMAT_TYPE_NONE, /* Just a string part */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 409 | FORMAT_TYPE_WIDTH, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 410 | FORMAT_TYPE_PRECISION, |
| 411 | FORMAT_TYPE_CHAR, |
| 412 | FORMAT_TYPE_STR, |
| 413 | FORMAT_TYPE_PTR, |
| 414 | FORMAT_TYPE_PERCENT_CHAR, |
| 415 | FORMAT_TYPE_INVALID, |
| 416 | FORMAT_TYPE_LONG_LONG, |
| 417 | FORMAT_TYPE_ULONG, |
| 418 | FORMAT_TYPE_LONG, |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 419 | FORMAT_TYPE_UBYTE, |
| 420 | FORMAT_TYPE_BYTE, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 421 | FORMAT_TYPE_USHORT, |
| 422 | FORMAT_TYPE_SHORT, |
| 423 | FORMAT_TYPE_UINT, |
| 424 | FORMAT_TYPE_INT, |
| 425 | FORMAT_TYPE_NRCHARS, |
| 426 | FORMAT_TYPE_SIZE_T, |
| 427 | FORMAT_TYPE_PTRDIFF |
| 428 | }; |
| 429 | |
| 430 | struct printf_spec { |
| 431 | enum format_type type; |
| 432 | int flags; /* flags to number() */ |
| 433 | int field_width; /* width of output field */ |
| 434 | int base; |
| 435 | int precision; /* # of digits/chars */ |
| 436 | int qualifier; |
| 437 | }; |
| 438 | |
| 439 | static char *number(char *buf, char *end, unsigned long long num, |
| 440 | struct printf_spec spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 442 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
| 443 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
| 444 | |
| 445 | char tmp[66]; |
| 446 | char sign; |
| 447 | char locase; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 448 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | int i; |
| 450 | |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 451 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
| 452 | * produces same digits or (maybe lowercased) letters */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 453 | locase = (spec.flags & SMALL); |
| 454 | if (spec.flags & LEFT) |
| 455 | spec.flags &= ~ZEROPAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | sign = 0; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 457 | if (spec.flags & SIGN) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 458 | if ((signed long long)num < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | sign = '-'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 460 | num = -(signed long long)num; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 461 | spec.field_width--; |
| 462 | } else if (spec.flags & PLUS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | sign = '+'; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 464 | spec.field_width--; |
| 465 | } else if (spec.flags & SPACE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | sign = ' '; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 467 | spec.field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 470 | if (need_pfx) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 471 | spec.field_width--; |
| 472 | if (spec.base == 16) |
| 473 | spec.field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 475 | |
| 476 | /* generate full string in tmp[], in reverse order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | i = 0; |
| 478 | if (num == 0) |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 479 | tmp[i++] = '0'; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 480 | /* Generic code, for any base: |
| 481 | else do { |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 482 | tmp[i++] = (digits[do_div(num,base)] | locase); |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 483 | } while (num != 0); |
| 484 | */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 485 | else if (spec.base != 10) { /* 8 or 16 */ |
| 486 | int mask = spec.base - 1; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 487 | int shift = 3; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 488 | |
| 489 | if (spec.base == 16) |
| 490 | shift = 4; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 491 | do { |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 492 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 493 | num >>= shift; |
| 494 | } while (num); |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 495 | } else { /* base 10 */ |
| 496 | i = put_dec(tmp, num) - tmp; |
| 497 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 498 | |
| 499 | /* printing 100 using %2d gives "100", not "00" */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 500 | if (i > spec.precision) |
| 501 | spec.precision = i; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 502 | /* leading space padding */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 503 | spec.field_width -= spec.precision; |
| 504 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 505 | while (--spec.field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 506 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | *buf = ' '; |
| 508 | ++buf; |
| 509 | } |
| 510 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 511 | /* sign */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (sign) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 513 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | *buf = sign; |
| 515 | ++buf; |
| 516 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 517 | /* "0x" / "0" prefix */ |
| 518 | if (need_pfx) { |
| 519 | if (buf < end) |
| 520 | *buf = '0'; |
| 521 | ++buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 522 | if (spec.base == 16) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 523 | if (buf < end) |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 524 | *buf = ('X' | locase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | ++buf; |
| 526 | } |
| 527 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 528 | /* zero or space padding */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 529 | if (!(spec.flags & LEFT)) { |
| 530 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; |
| 531 | while (--spec.field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 532 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | *buf = c; |
| 534 | ++buf; |
| 535 | } |
| 536 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 537 | /* hmm even more zero padding? */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 538 | while (i <= --spec.precision) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 539 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | *buf = '0'; |
| 541 | ++buf; |
| 542 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 543 | /* actual digits of result */ |
| 544 | while (--i >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 545 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | *buf = tmp[i]; |
| 547 | ++buf; |
| 548 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 549 | /* trailing space padding */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 550 | while (--spec.field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 551 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | *buf = ' '; |
| 553 | ++buf; |
| 554 | } |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | return buf; |
| 557 | } |
| 558 | |
André Goddard Rosa | 0f4f81d | 2009-12-14 18:00:55 -0800 | [diff] [blame] | 559 | static char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 560 | { |
| 561 | int len, i; |
| 562 | |
| 563 | if ((unsigned long)s < PAGE_SIZE) |
André Goddard Rosa | 0f4f81d | 2009-12-14 18:00:55 -0800 | [diff] [blame] | 564 | s = "(null)"; |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 565 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 566 | len = strnlen(s, spec.precision); |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 567 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 568 | if (!(spec.flags & LEFT)) { |
| 569 | while (len < spec.field_width--) { |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 570 | if (buf < end) |
| 571 | *buf = ' '; |
| 572 | ++buf; |
| 573 | } |
| 574 | } |
| 575 | for (i = 0; i < len; ++i) { |
| 576 | if (buf < end) |
| 577 | *buf = *s; |
| 578 | ++buf; ++s; |
| 579 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 580 | while (len < spec.field_width--) { |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 581 | if (buf < end) |
| 582 | *buf = ' '; |
| 583 | ++buf; |
| 584 | } |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 585 | |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 586 | return buf; |
| 587 | } |
| 588 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 589 | static char *symbol_string(char *buf, char *end, void *ptr, |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 590 | struct printf_spec spec, char ext) |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 591 | { |
| 592 | unsigned long value = (unsigned long) ptr; |
| 593 | #ifdef CONFIG_KALLSYMS |
| 594 | char sym[KSYM_SYMBOL_LEN]; |
Steven Rostedt | 91adcd2 | 2009-09-16 20:03:06 -0400 | [diff] [blame] | 595 | if (ext != 'f' && ext != 's') |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 596 | sprint_symbol(sym, value); |
| 597 | else |
| 598 | kallsyms_lookup(value, NULL, NULL, NULL, sym); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 599 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 600 | return string(buf, end, sym, spec); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 601 | #else |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 602 | spec.field_width = 2 * sizeof(void *); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 603 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 604 | spec.base = 16; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 605 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 606 | return number(buf, end, value, spec); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 607 | #endif |
| 608 | } |
| 609 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 610 | static char *resource_string(char *buf, char *end, struct resource *res, |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 611 | struct printf_spec spec, const char *fmt) |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 612 | { |
| 613 | #ifndef IO_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 614 | #define IO_RSRC_PRINTK_SIZE 6 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 615 | #endif |
| 616 | |
| 617 | #ifndef MEM_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 618 | #define MEM_RSRC_PRINTK_SIZE 10 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 619 | #endif |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 620 | struct printf_spec hex_spec = { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 621 | .base = 16, |
| 622 | .precision = -1, |
| 623 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 624 | }; |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 625 | struct printf_spec dec_spec = { |
| 626 | .base = 10, |
| 627 | .precision = -1, |
| 628 | .flags = 0, |
| 629 | }; |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 630 | struct printf_spec str_spec = { |
| 631 | .field_width = -1, |
| 632 | .precision = 10, |
| 633 | .flags = LEFT, |
| 634 | }; |
| 635 | struct printf_spec flag_spec = { |
| 636 | .base = 16, |
| 637 | .precision = -1, |
| 638 | .flags = SPECIAL | SMALL, |
| 639 | }; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 640 | |
| 641 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
| 642 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
| 643 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
| 644 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
| 645 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref disabled]") |
| 646 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
| 647 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
| 648 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
| 649 | |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 650 | char *p = sym, *pend = sym + sizeof(sym); |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 651 | int size = -1, addr = 0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 652 | int decode = (fmt[0] == 'R') ? 1 : 0; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 653 | |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 654 | if (res->flags & IORESOURCE_IO) { |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 655 | size = IO_RSRC_PRINTK_SIZE; |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 656 | addr = 1; |
| 657 | } else if (res->flags & IORESOURCE_MEM) { |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 658 | size = MEM_RSRC_PRINTK_SIZE; |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 659 | addr = 1; |
| 660 | } |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 661 | |
| 662 | *p++ = '['; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 663 | if (res->flags & IORESOURCE_IO) |
| 664 | p = string(p, pend, "io ", str_spec); |
| 665 | else if (res->flags & IORESOURCE_MEM) |
| 666 | p = string(p, pend, "mem ", str_spec); |
| 667 | else if (res->flags & IORESOURCE_IRQ) |
| 668 | p = string(p, pend, "irq ", str_spec); |
| 669 | else if (res->flags & IORESOURCE_DMA) |
| 670 | p = string(p, pend, "dma ", str_spec); |
| 671 | else { |
| 672 | p = string(p, pend, "??? ", str_spec); |
| 673 | decode = 0; |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 674 | } |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 675 | hex_spec.field_width = size; |
| 676 | p = number(p, pend, res->start, addr ? hex_spec : dec_spec); |
| 677 | if (res->start != res->end) { |
| 678 | *p++ = '-'; |
| 679 | p = number(p, pend, res->end, addr ? hex_spec : dec_spec); |
| 680 | } |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 681 | if (decode) { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 682 | if (res->flags & IORESOURCE_MEM_64) |
| 683 | p = string(p, pend, " 64bit", str_spec); |
| 684 | if (res->flags & IORESOURCE_PREFETCH) |
| 685 | p = string(p, pend, " pref", str_spec); |
| 686 | if (res->flags & IORESOURCE_DISABLED) |
| 687 | p = string(p, pend, " disabled", str_spec); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 688 | } else { |
| 689 | p = string(p, pend, " flags ", str_spec); |
| 690 | p = number(p, pend, res->flags, flag_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 691 | } |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 692 | *p++ = ']'; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 693 | *p = '\0'; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 694 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 695 | return string(buf, end, sym, spec); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 696 | } |
| 697 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 698 | static char *mac_address_string(char *buf, char *end, u8 *addr, |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 699 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 700 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 701 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 702 | char *p = mac_addr; |
| 703 | int i; |
| 704 | |
| 705 | for (i = 0; i < 6; i++) { |
| 706 | p = pack_hex_byte(p, addr[i]); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 707 | if (fmt[0] == 'M' && i != 5) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 708 | *p++ = ':'; |
| 709 | } |
| 710 | *p = '\0'; |
| 711 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 712 | return string(buf, end, mac_addr, spec); |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 715 | static char *ip4_string(char *p, const u8 *addr, bool leading_zeros) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 716 | { |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 717 | int i; |
| 718 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 719 | for (i = 0; i < 4; i++) { |
| 720 | char temp[3]; /* hold each IP quad in reverse order */ |
| 721 | int digits = put_dec_trunc(temp, addr[i]) - temp; |
| 722 | if (leading_zeros) { |
| 723 | if (digits < 3) |
| 724 | *p++ = '0'; |
| 725 | if (digits < 2) |
| 726 | *p++ = '0'; |
| 727 | } |
| 728 | /* reverse the digits in the quad */ |
| 729 | while (digits--) |
| 730 | *p++ = temp[digits]; |
| 731 | if (i < 3) |
| 732 | *p++ = '.'; |
| 733 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 734 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 735 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 736 | return p; |
| 737 | } |
| 738 | |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 739 | static char *ip6_compressed_string(char *p, const char *addr) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 740 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 741 | int i, j, range; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 742 | unsigned char zerolength[8]; |
| 743 | int longest = 1; |
| 744 | int colonpos = -1; |
| 745 | u16 word; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 746 | u8 hi, lo; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 747 | bool needcolon = false; |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 748 | bool useIPv4; |
| 749 | struct in6_addr in6; |
| 750 | |
| 751 | memcpy(&in6, addr, sizeof(struct in6_addr)); |
| 752 | |
| 753 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 754 | |
| 755 | memset(zerolength, 0, sizeof(zerolength)); |
| 756 | |
| 757 | if (useIPv4) |
| 758 | range = 6; |
| 759 | else |
| 760 | range = 8; |
| 761 | |
| 762 | /* find position of longest 0 run */ |
| 763 | for (i = 0; i < range; i++) { |
| 764 | for (j = i; j < range; j++) { |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 765 | if (in6.s6_addr16[j] != 0) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 766 | break; |
| 767 | zerolength[i]++; |
| 768 | } |
| 769 | } |
| 770 | for (i = 0; i < range; i++) { |
| 771 | if (zerolength[i] > longest) { |
| 772 | longest = zerolength[i]; |
| 773 | colonpos = i; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | /* emit address */ |
| 778 | for (i = 0; i < range; i++) { |
| 779 | if (i == colonpos) { |
| 780 | if (needcolon || i == 0) |
| 781 | *p++ = ':'; |
| 782 | *p++ = ':'; |
| 783 | needcolon = false; |
| 784 | i += longest - 1; |
| 785 | continue; |
| 786 | } |
| 787 | if (needcolon) { |
| 788 | *p++ = ':'; |
| 789 | needcolon = false; |
| 790 | } |
| 791 | /* hex u16 without leading 0s */ |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 792 | word = ntohs(in6.s6_addr16[i]); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 793 | hi = word >> 8; |
| 794 | lo = word & 0xff; |
| 795 | if (hi) { |
| 796 | if (hi > 0x0f) |
| 797 | p = pack_hex_byte(p, hi); |
| 798 | else |
| 799 | *p++ = hex_asc_lo(hi); |
André Goddard Rosa | b5ff992 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 800 | p = pack_hex_byte(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 801 | } |
André Goddard Rosa | b5ff992 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 802 | else if (lo > 0x0f) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 803 | p = pack_hex_byte(p, lo); |
| 804 | else |
| 805 | *p++ = hex_asc_lo(lo); |
| 806 | needcolon = true; |
| 807 | } |
| 808 | |
| 809 | if (useIPv4) { |
| 810 | if (needcolon) |
| 811 | *p++ = ':'; |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 812 | p = ip4_string(p, &in6.s6_addr[12], false); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 813 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 814 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 815 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 816 | return p; |
| 817 | } |
| 818 | |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 819 | static char *ip6_string(char *p, const char *addr, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 820 | { |
| 821 | int i; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 822 | |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 823 | for (i = 0; i < 8; i++) { |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 824 | p = pack_hex_byte(p, *addr++); |
| 825 | p = pack_hex_byte(p, *addr++); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 826 | if (fmt[0] == 'I' && i != 7) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 827 | *p++ = ':'; |
| 828 | } |
| 829 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 830 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 831 | return p; |
| 832 | } |
| 833 | |
| 834 | static char *ip6_addr_string(char *buf, char *end, const u8 *addr, |
| 835 | struct printf_spec spec, const char *fmt) |
| 836 | { |
| 837 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
| 838 | |
| 839 | if (fmt[0] == 'I' && fmt[2] == 'c') |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 840 | ip6_compressed_string(ip6_addr, addr); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 841 | else |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 842 | ip6_string(ip6_addr, addr, fmt); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 843 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 844 | return string(buf, end, ip6_addr, spec); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 847 | static char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
| 848 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 849 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 850 | char ip4_addr[sizeof("255.255.255.255")]; |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 851 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 852 | ip4_string(ip4_addr, addr, fmt[0] == 'i'); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 853 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 854 | return string(buf, end, ip4_addr, spec); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 857 | /* |
| 858 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 859 | * by an extra set of alphanumeric characters that are extended format |
| 860 | * specifiers. |
| 861 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 862 | * Right now we handle: |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 863 | * |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 864 | * - 'F' For symbolic function descriptor pointers with offset |
| 865 | * - 'f' For simple symbolic function names without offset |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 866 | * - 'S' For symbolic direct pointers with offset |
| 867 | * - 's' For symbolic direct pointers without offset |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 868 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
| 869 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 870 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 871 | * usual colon-separated hex notation |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 872 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
| 873 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
| 874 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
| 875 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
| 876 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
| 877 | * IPv6 omits the colons (01020304...0f) |
| 878 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
| 879 | * - 'I6c' for IPv6 addresses printed as specified by |
| 880 | * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 881 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 882 | * function pointers are really function descriptors, which contain a |
| 883 | * pointer to the real address. |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 884 | */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 885 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 886 | struct printf_spec spec) |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 887 | { |
Linus Torvalds | d97106a | 2009-01-03 11:46:17 -0800 | [diff] [blame] | 888 | if (!ptr) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 889 | return string(buf, end, "(null)", spec); |
Linus Torvalds | d97106a | 2009-01-03 11:46:17 -0800 | [diff] [blame] | 890 | |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 891 | switch (*fmt) { |
| 892 | case 'F': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 893 | case 'f': |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 894 | ptr = dereference_function_descriptor(ptr); |
| 895 | /* Fallthrough */ |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 896 | case 's': |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 897 | case 'S': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 898 | return symbol_string(buf, end, ptr, spec, *fmt); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 899 | case 'R': |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 900 | case 'r': |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 901 | return resource_string(buf, end, ptr, spec, fmt); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 902 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
| 903 | case 'm': /* Contiguous: 000102030405 */ |
| 904 | return mac_address_string(buf, end, ptr, spec, fmt); |
| 905 | case 'I': /* Formatted IP supported |
| 906 | * 4: 1.2.3.4 |
| 907 | * 6: 0001:0203:...:0708 |
| 908 | * 6c: 1::708 or 1::1.2.3.4 |
| 909 | */ |
| 910 | case 'i': /* Contiguous: |
| 911 | * 4: 001.002.003.004 |
| 912 | * 6: 000102...0f |
| 913 | */ |
| 914 | switch (fmt[1]) { |
| 915 | case '6': |
| 916 | return ip6_addr_string(buf, end, ptr, spec, fmt); |
| 917 | case '4': |
| 918 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
| 919 | } |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 920 | break; |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 921 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 922 | spec.flags |= SMALL; |
| 923 | if (spec.field_width == -1) { |
| 924 | spec.field_width = 2*sizeof(void *); |
| 925 | spec.flags |= ZEROPAD; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 926 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 927 | spec.base = 16; |
| 928 | |
| 929 | return number(buf, end, (unsigned long) ptr, spec); |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Helper function to decode printf style format. |
| 934 | * Each call decode a token from the format and return the |
| 935 | * number of characters read (or likely the delta where it wants |
| 936 | * to go on the next call). |
| 937 | * The decoded token is returned through the parameters |
| 938 | * |
| 939 | * 'h', 'l', or 'L' for integer fields |
| 940 | * 'z' support added 23/7/1999 S.H. |
| 941 | * 'z' changed to 'Z' --davidm 1/25/99 |
| 942 | * 't' added for ptrdiff_t |
| 943 | * |
| 944 | * @fmt: the format string |
| 945 | * @type of the token returned |
| 946 | * @flags: various flags such as +, -, # tokens.. |
| 947 | * @field_width: overwritten width |
| 948 | * @base: base of the number (octal, hex, ...) |
| 949 | * @precision: precision of a number |
| 950 | * @qualifier: qualifier of a number (long, size_t, ...) |
| 951 | */ |
| 952 | static int format_decode(const char *fmt, struct printf_spec *spec) |
| 953 | { |
| 954 | const char *start = fmt; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 955 | |
| 956 | /* we finished early by reading the field width */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 957 | if (spec->type == FORMAT_TYPE_WIDTH) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 958 | if (spec->field_width < 0) { |
| 959 | spec->field_width = -spec->field_width; |
| 960 | spec->flags |= LEFT; |
| 961 | } |
| 962 | spec->type = FORMAT_TYPE_NONE; |
| 963 | goto precision; |
| 964 | } |
| 965 | |
| 966 | /* we finished early by reading the precision */ |
| 967 | if (spec->type == FORMAT_TYPE_PRECISION) { |
| 968 | if (spec->precision < 0) |
| 969 | spec->precision = 0; |
| 970 | |
| 971 | spec->type = FORMAT_TYPE_NONE; |
| 972 | goto qualifier; |
| 973 | } |
| 974 | |
| 975 | /* By default */ |
| 976 | spec->type = FORMAT_TYPE_NONE; |
| 977 | |
| 978 | for (; *fmt ; ++fmt) { |
| 979 | if (*fmt == '%') |
| 980 | break; |
| 981 | } |
| 982 | |
| 983 | /* Return the current non-format string */ |
| 984 | if (fmt != start || !*fmt) |
| 985 | return fmt - start; |
| 986 | |
| 987 | /* Process flags */ |
| 988 | spec->flags = 0; |
| 989 | |
| 990 | while (1) { /* this also skips first '%' */ |
| 991 | bool found = true; |
| 992 | |
| 993 | ++fmt; |
| 994 | |
| 995 | switch (*fmt) { |
| 996 | case '-': spec->flags |= LEFT; break; |
| 997 | case '+': spec->flags |= PLUS; break; |
| 998 | case ' ': spec->flags |= SPACE; break; |
| 999 | case '#': spec->flags |= SPECIAL; break; |
| 1000 | case '0': spec->flags |= ZEROPAD; break; |
| 1001 | default: found = false; |
| 1002 | } |
| 1003 | |
| 1004 | if (!found) |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | /* get field width */ |
| 1009 | spec->field_width = -1; |
| 1010 | |
| 1011 | if (isdigit(*fmt)) |
| 1012 | spec->field_width = skip_atoi(&fmt); |
| 1013 | else if (*fmt == '*') { |
| 1014 | /* it's the next argument */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1015 | spec->type = FORMAT_TYPE_WIDTH; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1016 | return ++fmt - start; |
| 1017 | } |
| 1018 | |
| 1019 | precision: |
| 1020 | /* get the precision */ |
| 1021 | spec->precision = -1; |
| 1022 | if (*fmt == '.') { |
| 1023 | ++fmt; |
| 1024 | if (isdigit(*fmt)) { |
| 1025 | spec->precision = skip_atoi(&fmt); |
| 1026 | if (spec->precision < 0) |
| 1027 | spec->precision = 0; |
| 1028 | } else if (*fmt == '*') { |
| 1029 | /* it's the next argument */ |
Vegard Nossum | adf26f8 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1030 | spec->type = FORMAT_TYPE_PRECISION; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1031 | return ++fmt - start; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | qualifier: |
| 1036 | /* get the conversion qualifier */ |
| 1037 | spec->qualifier = -1; |
André Goddard Rosa | 08562cb | 2009-12-14 18:00:58 -0800 | [diff] [blame] | 1038 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
| 1039 | TOLOWER(*fmt) == 'z' || *fmt == 't') { |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1040 | spec->qualifier = *fmt++; |
| 1041 | if (unlikely(spec->qualifier == *fmt)) { |
| 1042 | if (spec->qualifier == 'l') { |
| 1043 | spec->qualifier = 'L'; |
| 1044 | ++fmt; |
| 1045 | } else if (spec->qualifier == 'h') { |
| 1046 | spec->qualifier = 'H'; |
| 1047 | ++fmt; |
| 1048 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /* default base */ |
| 1053 | spec->base = 10; |
| 1054 | switch (*fmt) { |
| 1055 | case 'c': |
| 1056 | spec->type = FORMAT_TYPE_CHAR; |
| 1057 | return ++fmt - start; |
| 1058 | |
| 1059 | case 's': |
| 1060 | spec->type = FORMAT_TYPE_STR; |
| 1061 | return ++fmt - start; |
| 1062 | |
| 1063 | case 'p': |
| 1064 | spec->type = FORMAT_TYPE_PTR; |
| 1065 | return fmt - start; |
| 1066 | /* skip alnum */ |
| 1067 | |
| 1068 | case 'n': |
| 1069 | spec->type = FORMAT_TYPE_NRCHARS; |
| 1070 | return ++fmt - start; |
| 1071 | |
| 1072 | case '%': |
| 1073 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
| 1074 | return ++fmt - start; |
| 1075 | |
| 1076 | /* integer number formats - set up the flags and "break" */ |
| 1077 | case 'o': |
| 1078 | spec->base = 8; |
| 1079 | break; |
| 1080 | |
| 1081 | case 'x': |
| 1082 | spec->flags |= SMALL; |
| 1083 | |
| 1084 | case 'X': |
| 1085 | spec->base = 16; |
| 1086 | break; |
| 1087 | |
| 1088 | case 'd': |
| 1089 | case 'i': |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1090 | spec->flags |= SIGN; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1091 | case 'u': |
| 1092 | break; |
| 1093 | |
| 1094 | default: |
| 1095 | spec->type = FORMAT_TYPE_INVALID; |
| 1096 | return fmt - start; |
| 1097 | } |
| 1098 | |
| 1099 | if (spec->qualifier == 'L') |
| 1100 | spec->type = FORMAT_TYPE_LONG_LONG; |
| 1101 | else if (spec->qualifier == 'l') { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1102 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1103 | spec->type = FORMAT_TYPE_LONG; |
| 1104 | else |
| 1105 | spec->type = FORMAT_TYPE_ULONG; |
André Goddard Rosa | 08562cb | 2009-12-14 18:00:58 -0800 | [diff] [blame] | 1106 | } else if (TOLOWER(spec->qualifier) == 'z') { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1107 | spec->type = FORMAT_TYPE_SIZE_T; |
| 1108 | } else if (spec->qualifier == 't') { |
| 1109 | spec->type = FORMAT_TYPE_PTRDIFF; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1110 | } else if (spec->qualifier == 'H') { |
| 1111 | if (spec->flags & SIGN) |
| 1112 | spec->type = FORMAT_TYPE_BYTE; |
| 1113 | else |
| 1114 | spec->type = FORMAT_TYPE_UBYTE; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1115 | } else if (spec->qualifier == 'h') { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1116 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1117 | spec->type = FORMAT_TYPE_SHORT; |
| 1118 | else |
| 1119 | spec->type = FORMAT_TYPE_USHORT; |
| 1120 | } else { |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1121 | if (spec->flags & SIGN) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1122 | spec->type = FORMAT_TYPE_INT; |
| 1123 | else |
| 1124 | spec->type = FORMAT_TYPE_UINT; |
| 1125 | } |
| 1126 | |
| 1127 | return ++fmt - start; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | /** |
| 1131 | * vsnprintf - Format a string and place it in a buffer |
| 1132 | * @buf: The buffer to place the result into |
| 1133 | * @size: The size of the buffer, including the trailing null space |
| 1134 | * @fmt: The format string to use |
| 1135 | * @args: Arguments for the format string |
| 1136 | * |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1137 | * This function follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 91adcd2 | 2009-09-16 20:03:06 -0400 | [diff] [blame] | 1138 | * %pS output the name of a text symbol with offset |
| 1139 | * %ps output the name of a text symbol without offset |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1140 | * %pF output the name of a function pointer with its offset |
| 1141 | * %pf output the name of a function pointer without its offset |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1142 | * %pR output the address range in a struct resource |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 1143 | * %n is ignored |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1144 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | * The return value is the number of characters which would |
| 1146 | * be generated for the given input, excluding the trailing |
| 1147 | * '\0', as per ISO C99. If you want to have the exact |
| 1148 | * number of characters written into @buf as return value |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1149 | * (not including the trailing '\0'), use vscnprintf(). If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | * return is greater than or equal to @size, the resulting |
| 1151 | * string is truncated. |
| 1152 | * |
| 1153 | * 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] | 1154 | * You probably want snprintf() instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | */ |
| 1156 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | unsigned long long num; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1159 | char *str, *end; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1160 | struct printf_spec spec = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1162 | /* Reject out-of-range values early. Large positive sizes are |
| 1163 | used for unknown buffer sizes. */ |
Marcin Slusarz | 2f30b1f9 | 2009-09-21 17:04:29 -0700 | [diff] [blame] | 1164 | if (WARN_ON_ONCE((int) size < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | str = buf; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1168 | end = buf + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1170 | /* Make sure end is always >= buf */ |
| 1171 | if (end < buf) { |
| 1172 | end = ((void *)-1); |
| 1173 | size = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1176 | while (*fmt) { |
| 1177 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1178 | int read = format_decode(fmt, &spec); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1179 | |
| 1180 | fmt += read; |
| 1181 | |
| 1182 | switch (spec.type) { |
| 1183 | case FORMAT_TYPE_NONE: { |
| 1184 | int copy = read; |
| 1185 | if (str < end) { |
| 1186 | if (copy > end - str) |
| 1187 | copy = end - str; |
| 1188 | memcpy(str, old_fmt, copy); |
| 1189 | } |
| 1190 | str += read; |
| 1191 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1194 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1195 | spec.field_width = va_arg(args, int); |
| 1196 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1198 | case FORMAT_TYPE_PRECISION: |
| 1199 | spec.precision = va_arg(args, int); |
| 1200 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1202 | case FORMAT_TYPE_CHAR: { |
| 1203 | char c; |
| 1204 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1205 | if (!(spec.flags & LEFT)) { |
| 1206 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1207 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | *str = ' '; |
| 1209 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1212 | } |
| 1213 | c = (unsigned char) va_arg(args, int); |
| 1214 | if (str < end) |
| 1215 | *str = c; |
| 1216 | ++str; |
| 1217 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1218 | if (str < end) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1219 | *str = ' '; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1221 | } |
| 1222 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1225 | case FORMAT_TYPE_STR: |
| 1226 | str = string(str, end, va_arg(args, char *), spec); |
| 1227 | break; |
| 1228 | |
| 1229 | case FORMAT_TYPE_PTR: |
| 1230 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
| 1231 | spec); |
| 1232 | while (isalnum(*fmt)) |
| 1233 | fmt++; |
| 1234 | break; |
| 1235 | |
| 1236 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1237 | if (str < end) |
| 1238 | *str = '%'; |
| 1239 | ++str; |
| 1240 | break; |
| 1241 | |
| 1242 | case FORMAT_TYPE_INVALID: |
| 1243 | if (str < end) |
| 1244 | *str = '%'; |
| 1245 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1246 | break; |
| 1247 | |
| 1248 | case FORMAT_TYPE_NRCHARS: { |
| 1249 | int qualifier = spec.qualifier; |
| 1250 | |
| 1251 | if (qualifier == 'l') { |
| 1252 | long *ip = va_arg(args, long *); |
| 1253 | *ip = (str - buf); |
André Goddard Rosa | 08562cb | 2009-12-14 18:00:58 -0800 | [diff] [blame] | 1254 | } else if (TOLOWER(qualifier) == 'z') { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1255 | size_t *ip = va_arg(args, size_t *); |
| 1256 | *ip = (str - buf); |
| 1257 | } else { |
| 1258 | int *ip = va_arg(args, int *); |
| 1259 | *ip = (str - buf); |
| 1260 | } |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | default: |
| 1265 | switch (spec.type) { |
| 1266 | case FORMAT_TYPE_LONG_LONG: |
| 1267 | num = va_arg(args, long long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1269 | case FORMAT_TYPE_ULONG: |
| 1270 | num = va_arg(args, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1272 | case FORMAT_TYPE_LONG: |
| 1273 | num = va_arg(args, long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1275 | case FORMAT_TYPE_SIZE_T: |
| 1276 | num = va_arg(args, size_t); |
| 1277 | break; |
| 1278 | case FORMAT_TYPE_PTRDIFF: |
| 1279 | num = va_arg(args, ptrdiff_t); |
| 1280 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1281 | case FORMAT_TYPE_UBYTE: |
| 1282 | num = (unsigned char) va_arg(args, int); |
| 1283 | break; |
| 1284 | case FORMAT_TYPE_BYTE: |
| 1285 | num = (signed char) va_arg(args, int); |
| 1286 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1287 | case FORMAT_TYPE_USHORT: |
| 1288 | num = (unsigned short) va_arg(args, int); |
| 1289 | break; |
| 1290 | case FORMAT_TYPE_SHORT: |
| 1291 | num = (short) va_arg(args, int); |
| 1292 | break; |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1293 | case FORMAT_TYPE_INT: |
| 1294 | num = (int) va_arg(args, int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1295 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1297 | num = va_arg(args, unsigned int); |
| 1298 | } |
| 1299 | |
| 1300 | str = number(str, end, num, spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1303 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1304 | if (size > 0) { |
| 1305 | if (str < end) |
| 1306 | *str = '\0'; |
| 1307 | else |
Linus Torvalds | 0a6047e | 2006-06-28 17:09:34 -0700 | [diff] [blame] | 1308 | end[-1] = '\0'; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1309 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1310 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1311 | /* the trailing null byte doesn't count towards the total */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | return str-buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | EXPORT_SYMBOL(vsnprintf); |
| 1316 | |
| 1317 | /** |
| 1318 | * vscnprintf - Format a string and place it in a buffer |
| 1319 | * @buf: The buffer to place the result into |
| 1320 | * @size: The size of the buffer, including the trailing null space |
| 1321 | * @fmt: The format string to use |
| 1322 | * @args: Arguments for the format string |
| 1323 | * |
| 1324 | * The return value is the number of characters which have been written into |
| 1325 | * the @buf not including the trailing '\0'. If @size is <= 0 the function |
| 1326 | * returns 0. |
| 1327 | * |
| 1328 | * 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] | 1329 | * You probably want scnprintf() instead. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1330 | * |
| 1331 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | */ |
| 1333 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1334 | { |
| 1335 | int i; |
| 1336 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1337 | i = vsnprintf(buf, size, fmt, args); |
| 1338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | return (i >= size) ? (size - 1) : i; |
| 1340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | EXPORT_SYMBOL(vscnprintf); |
| 1342 | |
| 1343 | /** |
| 1344 | * snprintf - Format a string and place it in a buffer |
| 1345 | * @buf: The buffer to place the result into |
| 1346 | * @size: The size of the buffer, including the trailing null space |
| 1347 | * @fmt: The format string to use |
| 1348 | * @...: Arguments for the format string |
| 1349 | * |
| 1350 | * The return value is the number of characters which would be |
| 1351 | * generated for the given input, excluding the trailing null, |
| 1352 | * as per ISO C99. If the return is greater than or equal to |
| 1353 | * @size, the resulting string is truncated. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1354 | * |
| 1355 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1357 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | { |
| 1359 | va_list args; |
| 1360 | int i; |
| 1361 | |
| 1362 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1363 | i = vsnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | return i; |
| 1367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | EXPORT_SYMBOL(snprintf); |
| 1369 | |
| 1370 | /** |
| 1371 | * scnprintf - Format a string and place it in a buffer |
| 1372 | * @buf: The buffer to place the result into |
| 1373 | * @size: The size of the buffer, including the trailing null space |
| 1374 | * @fmt: The format string to use |
| 1375 | * @...: Arguments for the format string |
| 1376 | * |
| 1377 | * 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] | 1378 | * the trailing '\0'. If @size is <= 0 the function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | */ |
| 1380 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1381 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | { |
| 1383 | va_list args; |
| 1384 | int i; |
| 1385 | |
| 1386 | va_start(args, fmt); |
| 1387 | i = vsnprintf(buf, size, fmt, args); |
| 1388 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | return (i >= size) ? (size - 1) : i; |
| 1391 | } |
| 1392 | EXPORT_SYMBOL(scnprintf); |
| 1393 | |
| 1394 | /** |
| 1395 | * vsprintf - Format a string and place it in a buffer |
| 1396 | * @buf: The buffer to place the result into |
| 1397 | * @fmt: The format string to use |
| 1398 | * @args: Arguments for the format string |
| 1399 | * |
| 1400 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1401 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | * buffer overflows. |
| 1403 | * |
| 1404 | * 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] | 1405 | * You probably want sprintf() instead. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1406 | * |
| 1407 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | */ |
| 1409 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 1410 | { |
| 1411 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 1412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | EXPORT_SYMBOL(vsprintf); |
| 1414 | |
| 1415 | /** |
| 1416 | * sprintf - Format a string and place it in a buffer |
| 1417 | * @buf: The buffer to place the result into |
| 1418 | * @fmt: The format string to use |
| 1419 | * @...: Arguments for the format string |
| 1420 | * |
| 1421 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1422 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | * buffer overflows. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1424 | * |
| 1425 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1427 | int sprintf(char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | { |
| 1429 | va_list args; |
| 1430 | int i; |
| 1431 | |
| 1432 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1433 | i = vsnprintf(buf, INT_MAX, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | return i; |
| 1437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | EXPORT_SYMBOL(sprintf); |
| 1439 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1440 | #ifdef CONFIG_BINARY_PRINTF |
| 1441 | /* |
| 1442 | * bprintf service: |
| 1443 | * vbin_printf() - VA arguments to binary data |
| 1444 | * bstr_printf() - Binary data to text string |
| 1445 | */ |
| 1446 | |
| 1447 | /** |
| 1448 | * vbin_printf - Parse a format string and place args' binary value in a buffer |
| 1449 | * @bin_buf: The buffer to place args' binary value |
| 1450 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1451 | * @fmt: The format string to use |
| 1452 | * @args: Arguments for the format string |
| 1453 | * |
| 1454 | * The format follows C99 vsnprintf, except %n is ignored, and its argument |
| 1455 | * is skiped. |
| 1456 | * |
| 1457 | * The return value is the number of words(32bits) which would be generated for |
| 1458 | * the given input. |
| 1459 | * |
| 1460 | * NOTE: |
| 1461 | * If the return value is greater than @size, the resulting bin_buf is NOT |
| 1462 | * valid for bstr_printf(). |
| 1463 | */ |
| 1464 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) |
| 1465 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1466 | struct printf_spec spec = {0}; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1467 | char *str, *end; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1468 | |
| 1469 | str = (char *)bin_buf; |
| 1470 | end = (char *)(bin_buf + size); |
| 1471 | |
| 1472 | #define save_arg(type) \ |
| 1473 | do { \ |
| 1474 | if (sizeof(type) == 8) { \ |
| 1475 | unsigned long long value; \ |
| 1476 | str = PTR_ALIGN(str, sizeof(u32)); \ |
| 1477 | value = va_arg(args, unsigned long long); \ |
| 1478 | if (str + sizeof(type) <= end) { \ |
| 1479 | *(u32 *)str = *(u32 *)&value; \ |
| 1480 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ |
| 1481 | } \ |
| 1482 | } else { \ |
| 1483 | unsigned long value; \ |
| 1484 | str = PTR_ALIGN(str, sizeof(type)); \ |
| 1485 | value = va_arg(args, int); \ |
| 1486 | if (str + sizeof(type) <= end) \ |
| 1487 | *(typeof(type) *)str = (type)value; \ |
| 1488 | } \ |
| 1489 | str += sizeof(type); \ |
| 1490 | } while (0) |
| 1491 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1492 | while (*fmt) { |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1493 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1494 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1495 | fmt += read; |
| 1496 | |
| 1497 | switch (spec.type) { |
| 1498 | case FORMAT_TYPE_NONE: |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1499 | case FORMAT_TYPE_INVALID: |
| 1500 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1501 | break; |
| 1502 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1503 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1504 | case FORMAT_TYPE_PRECISION: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1505 | save_arg(int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1506 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1507 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1508 | case FORMAT_TYPE_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1509 | save_arg(char); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1510 | break; |
| 1511 | |
| 1512 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1513 | const char *save_str = va_arg(args, char *); |
| 1514 | size_t len; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 1515 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1516 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
| 1517 | || (unsigned long)save_str < PAGE_SIZE) |
André Goddard Rosa | 0f4f81d | 2009-12-14 18:00:55 -0800 | [diff] [blame] | 1518 | save_str = "(null)"; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 1519 | len = strlen(save_str) + 1; |
| 1520 | if (str + len < end) |
| 1521 | memcpy(str, save_str, len); |
| 1522 | str += len; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1523 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1524 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1525 | |
| 1526 | case FORMAT_TYPE_PTR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1527 | save_arg(void *); |
| 1528 | /* skip all alphanumeric pointer suffixes */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1529 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1530 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1531 | break; |
| 1532 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1533 | case FORMAT_TYPE_NRCHARS: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1534 | /* skip %n 's argument */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1535 | int qualifier = spec.qualifier; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1536 | void *skip_arg; |
| 1537 | if (qualifier == 'l') |
| 1538 | skip_arg = va_arg(args, long *); |
André Goddard Rosa | 08562cb | 2009-12-14 18:00:58 -0800 | [diff] [blame] | 1539 | else if (TOLOWER(qualifier) == 'z') |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1540 | skip_arg = va_arg(args, size_t *); |
| 1541 | else |
| 1542 | skip_arg = va_arg(args, int *); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1543 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1544 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1545 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1546 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1547 | switch (spec.type) { |
| 1548 | |
| 1549 | case FORMAT_TYPE_LONG_LONG: |
| 1550 | save_arg(long long); |
| 1551 | break; |
| 1552 | case FORMAT_TYPE_ULONG: |
| 1553 | case FORMAT_TYPE_LONG: |
| 1554 | save_arg(unsigned long); |
| 1555 | break; |
| 1556 | case FORMAT_TYPE_SIZE_T: |
| 1557 | save_arg(size_t); |
| 1558 | break; |
| 1559 | case FORMAT_TYPE_PTRDIFF: |
| 1560 | save_arg(ptrdiff_t); |
| 1561 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1562 | case FORMAT_TYPE_UBYTE: |
| 1563 | case FORMAT_TYPE_BYTE: |
| 1564 | save_arg(char); |
| 1565 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1566 | case FORMAT_TYPE_USHORT: |
| 1567 | case FORMAT_TYPE_SHORT: |
| 1568 | save_arg(short); |
| 1569 | break; |
| 1570 | default: |
| 1571 | save_arg(int); |
| 1572 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1573 | } |
| 1574 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1575 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1576 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1577 | #undef save_arg |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1578 | } |
| 1579 | EXPORT_SYMBOL_GPL(vbin_printf); |
| 1580 | |
| 1581 | /** |
| 1582 | * bstr_printf - Format a string from binary arguments and place it in a buffer |
| 1583 | * @buf: The buffer to place the result into |
| 1584 | * @size: The size of the buffer, including the trailing null space |
| 1585 | * @fmt: The format string to use |
| 1586 | * @bin_buf: Binary arguments for the format string |
| 1587 | * |
| 1588 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets |
| 1589 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is |
| 1590 | * a binary buffer that generated by vbin_printf. |
| 1591 | * |
| 1592 | * The format follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 1593 | * see vsnprintf comment for details. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1594 | * |
| 1595 | * The return value is the number of characters which would |
| 1596 | * be generated for the given input, excluding the trailing |
| 1597 | * '\0', as per ISO C99. If you want to have the exact |
| 1598 | * number of characters written into @buf as return value |
| 1599 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 1600 | * return is greater than or equal to @size, the resulting |
| 1601 | * string is truncated. |
| 1602 | */ |
| 1603 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
| 1604 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1605 | struct printf_spec spec = {0}; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1606 | char *str, *end; |
| 1607 | const char *args = (const char *)bin_buf; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1608 | |
Marcin Slusarz | 2f30b1f9 | 2009-09-21 17:04:29 -0700 | [diff] [blame] | 1609 | if (WARN_ON_ONCE((int) size < 0)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1610 | return 0; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1611 | |
| 1612 | str = buf; |
| 1613 | end = buf + size; |
| 1614 | |
| 1615 | #define get_arg(type) \ |
| 1616 | ({ \ |
| 1617 | typeof(type) value; \ |
| 1618 | if (sizeof(type) == 8) { \ |
| 1619 | args = PTR_ALIGN(args, sizeof(u32)); \ |
| 1620 | *(u32 *)&value = *(u32 *)args; \ |
| 1621 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ |
| 1622 | } else { \ |
| 1623 | args = PTR_ALIGN(args, sizeof(type)); \ |
| 1624 | value = *(typeof(type) *)args; \ |
| 1625 | } \ |
| 1626 | args += sizeof(type); \ |
| 1627 | value; \ |
| 1628 | }) |
| 1629 | |
| 1630 | /* Make sure end is always >= buf */ |
| 1631 | if (end < buf) { |
| 1632 | end = ((void *)-1); |
| 1633 | size = end - buf; |
| 1634 | } |
| 1635 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1636 | while (*fmt) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1637 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1638 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1639 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1640 | fmt += read; |
| 1641 | |
| 1642 | switch (spec.type) { |
| 1643 | case FORMAT_TYPE_NONE: { |
| 1644 | int copy = read; |
| 1645 | if (str < end) { |
| 1646 | if (copy > end - str) |
| 1647 | copy = end - str; |
| 1648 | memcpy(str, old_fmt, copy); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1649 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1650 | str += read; |
| 1651 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1654 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1655 | spec.field_width = get_arg(int); |
| 1656 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1657 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1658 | case FORMAT_TYPE_PRECISION: |
| 1659 | spec.precision = get_arg(int); |
| 1660 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1661 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1662 | case FORMAT_TYPE_CHAR: { |
| 1663 | char c; |
| 1664 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1665 | if (!(spec.flags & LEFT)) { |
| 1666 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1667 | if (str < end) |
| 1668 | *str = ' '; |
| 1669 | ++str; |
| 1670 | } |
| 1671 | } |
| 1672 | c = (unsigned char) get_arg(char); |
| 1673 | if (str < end) |
| 1674 | *str = c; |
| 1675 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1676 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1677 | if (str < end) |
| 1678 | *str = ' '; |
| 1679 | ++str; |
| 1680 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1681 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1682 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1683 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1684 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1685 | const char *str_arg = args; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1686 | args += strlen(str_arg) + 1; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1687 | str = string(str, end, (char *)str_arg, spec); |
| 1688 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1689 | } |
| 1690 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1691 | case FORMAT_TYPE_PTR: |
| 1692 | str = pointer(fmt+1, str, end, get_arg(void *), spec); |
| 1693 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1694 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1695 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1696 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1697 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1698 | case FORMAT_TYPE_INVALID: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1699 | if (str < end) |
| 1700 | *str = '%'; |
| 1701 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1702 | break; |
| 1703 | |
| 1704 | case FORMAT_TYPE_NRCHARS: |
| 1705 | /* skip */ |
| 1706 | break; |
| 1707 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1708 | default: { |
| 1709 | unsigned long long num; |
| 1710 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1711 | switch (spec.type) { |
| 1712 | |
| 1713 | case FORMAT_TYPE_LONG_LONG: |
| 1714 | num = get_arg(long long); |
| 1715 | break; |
| 1716 | case FORMAT_TYPE_ULONG: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1717 | case FORMAT_TYPE_LONG: |
| 1718 | num = get_arg(unsigned long); |
| 1719 | break; |
| 1720 | case FORMAT_TYPE_SIZE_T: |
| 1721 | num = get_arg(size_t); |
| 1722 | break; |
| 1723 | case FORMAT_TYPE_PTRDIFF: |
| 1724 | num = get_arg(ptrdiff_t); |
| 1725 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1726 | case FORMAT_TYPE_UBYTE: |
| 1727 | num = get_arg(unsigned char); |
| 1728 | break; |
| 1729 | case FORMAT_TYPE_BYTE: |
| 1730 | num = get_arg(signed char); |
| 1731 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1732 | case FORMAT_TYPE_USHORT: |
| 1733 | num = get_arg(unsigned short); |
| 1734 | break; |
| 1735 | case FORMAT_TYPE_SHORT: |
| 1736 | num = get_arg(short); |
| 1737 | break; |
| 1738 | case FORMAT_TYPE_UINT: |
| 1739 | num = get_arg(unsigned int); |
| 1740 | break; |
| 1741 | default: |
| 1742 | num = get_arg(int); |
| 1743 | } |
| 1744 | |
| 1745 | str = number(str, end, num, spec); |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1746 | } /* default: */ |
| 1747 | } /* switch(spec.type) */ |
| 1748 | } /* while(*fmt) */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1749 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1750 | if (size > 0) { |
| 1751 | if (str < end) |
| 1752 | *str = '\0'; |
| 1753 | else |
| 1754 | end[-1] = '\0'; |
| 1755 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1756 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1757 | #undef get_arg |
| 1758 | |
| 1759 | /* the trailing null byte doesn't count towards the total */ |
| 1760 | return str - buf; |
| 1761 | } |
| 1762 | EXPORT_SYMBOL_GPL(bstr_printf); |
| 1763 | |
| 1764 | /** |
| 1765 | * bprintf - Parse a format string and place args' binary value in a buffer |
| 1766 | * @bin_buf: The buffer to place args' binary value |
| 1767 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1768 | * @fmt: The format string to use |
| 1769 | * @...: Arguments for the format string |
| 1770 | * |
| 1771 | * The function returns the number of words(u32) written |
| 1772 | * into @bin_buf. |
| 1773 | */ |
| 1774 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) |
| 1775 | { |
| 1776 | va_list args; |
| 1777 | int ret; |
| 1778 | |
| 1779 | va_start(args, fmt); |
| 1780 | ret = vbin_printf(bin_buf, size, fmt, args); |
| 1781 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1782 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 1783 | return ret; |
| 1784 | } |
| 1785 | EXPORT_SYMBOL_GPL(bprintf); |
| 1786 | |
| 1787 | #endif /* CONFIG_BINARY_PRINTF */ |
| 1788 | |
André Goddard Rosa | c5484d7 | 2009-12-14 18:01:00 -0800 | [diff] [blame^] | 1789 | static noinline char *skip_space(const char *str) |
| 1790 | { |
| 1791 | while (isspace(*str)) |
| 1792 | ++str; |
| 1793 | return (char *)str; |
| 1794 | } |
| 1795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | /** |
| 1797 | * vsscanf - Unformat a buffer into a list of arguments |
| 1798 | * @buf: input buffer |
| 1799 | * @fmt: format of buffer |
| 1800 | * @args: arguments |
| 1801 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1802 | int vsscanf(const char *buf, const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | { |
| 1804 | const char *str = buf; |
| 1805 | char *next; |
| 1806 | char digit; |
| 1807 | int num = 0; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1808 | int qualifier, base, field_width; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1809 | bool is_sign; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1811 | while (*fmt && *str) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | /* skip any white space in format */ |
| 1813 | /* white space in format matchs any amount of |
| 1814 | * white space, including none, in the input. |
| 1815 | */ |
| 1816 | if (isspace(*fmt)) { |
André Goddard Rosa | c5484d7 | 2009-12-14 18:01:00 -0800 | [diff] [blame^] | 1817 | fmt = skip_space(fmt); |
| 1818 | str = skip_space(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | /* anything that is not a conversion must match exactly */ |
| 1822 | if (*fmt != '%' && *fmt) { |
| 1823 | if (*fmt++ != *str++) |
| 1824 | break; |
| 1825 | continue; |
| 1826 | } |
| 1827 | |
| 1828 | if (!*fmt) |
| 1829 | break; |
| 1830 | ++fmt; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | /* skip this conversion. |
| 1833 | * advance both strings to next white space |
| 1834 | */ |
| 1835 | if (*fmt == '*') { |
Andy Spencer | 8fccae2 | 2009-10-01 15:44:27 -0700 | [diff] [blame] | 1836 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | fmt++; |
| 1838 | while (!isspace(*str) && *str) |
| 1839 | str++; |
| 1840 | continue; |
| 1841 | } |
| 1842 | |
| 1843 | /* get field width */ |
| 1844 | field_width = -1; |
| 1845 | if (isdigit(*fmt)) |
| 1846 | field_width = skip_atoi(&fmt); |
| 1847 | |
| 1848 | /* get conversion qualifier */ |
| 1849 | qualifier = -1; |
André Goddard Rosa | 08562cb | 2009-12-14 18:00:58 -0800 | [diff] [blame] | 1850 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
| 1851 | TOLOWER(*fmt) == 'z') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | qualifier = *fmt++; |
| 1853 | if (unlikely(qualifier == *fmt)) { |
| 1854 | if (qualifier == 'h') { |
| 1855 | qualifier = 'H'; |
| 1856 | fmt++; |
| 1857 | } else if (qualifier == 'l') { |
| 1858 | qualifier = 'L'; |
| 1859 | fmt++; |
| 1860 | } |
| 1861 | } |
| 1862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
| 1864 | if (!*fmt || !*str) |
| 1865 | break; |
| 1866 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1867 | base = 10; |
| 1868 | is_sign = 0; |
| 1869 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1870 | switch (*fmt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | case 'c': |
| 1872 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1873 | char *s = (char *)va_arg(args, char*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | if (field_width == -1) |
| 1875 | field_width = 1; |
| 1876 | do { |
| 1877 | *s++ = *str++; |
| 1878 | } while (--field_width > 0 && *str); |
| 1879 | num++; |
| 1880 | } |
| 1881 | continue; |
| 1882 | case 's': |
| 1883 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1884 | char *s = (char *)va_arg(args, char *); |
| 1885 | if (field_width == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | field_width = INT_MAX; |
| 1887 | /* first, skip leading white space in buffer */ |
André Goddard Rosa | c5484d7 | 2009-12-14 18:01:00 -0800 | [diff] [blame^] | 1888 | str = skip_space(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
| 1890 | /* now copy until next white space */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1891 | while (*str && !isspace(*str) && field_width--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | *s++ = *str++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | *s = '\0'; |
| 1894 | num++; |
| 1895 | } |
| 1896 | continue; |
| 1897 | case 'n': |
| 1898 | /* return number of characters read so far */ |
| 1899 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1900 | int *i = (int *)va_arg(args, int*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | *i = str - buf; |
| 1902 | } |
| 1903 | continue; |
| 1904 | case 'o': |
| 1905 | base = 8; |
| 1906 | break; |
| 1907 | case 'x': |
| 1908 | case 'X': |
| 1909 | base = 16; |
| 1910 | break; |
| 1911 | case 'i': |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1912 | base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | case 'd': |
| 1914 | is_sign = 1; |
| 1915 | case 'u': |
| 1916 | break; |
| 1917 | case '%': |
| 1918 | /* looking for '%' in str */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1919 | if (*str++ != '%') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | return num; |
| 1921 | continue; |
| 1922 | default: |
| 1923 | /* invalid format; stop here */ |
| 1924 | return num; |
| 1925 | } |
| 1926 | |
| 1927 | /* have some sort of integer conversion. |
| 1928 | * first, skip white space in buffer. |
| 1929 | */ |
André Goddard Rosa | c5484d7 | 2009-12-14 18:01:00 -0800 | [diff] [blame^] | 1930 | str = skip_space(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | |
| 1932 | digit = *str; |
| 1933 | if (is_sign && digit == '-') |
| 1934 | digit = *(str + 1); |
| 1935 | |
| 1936 | if (!digit |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1937 | || (base == 16 && !isxdigit(digit)) |
| 1938 | || (base == 10 && !isdigit(digit)) |
| 1939 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 1940 | || (base == 0 && !isdigit(digit))) |
| 1941 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1943 | switch (qualifier) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | case 'H': /* that's 'hh' in format */ |
| 1945 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1946 | signed char *s = (signed char *)va_arg(args, signed char *); |
| 1947 | *s = (signed char)simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1949 | unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); |
| 1950 | *s = (unsigned char)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | } |
| 1952 | break; |
| 1953 | case 'h': |
| 1954 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1955 | short *s = (short *)va_arg(args, short *); |
| 1956 | *s = (short)simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1958 | unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); |
| 1959 | *s = (unsigned short)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | } |
| 1961 | break; |
| 1962 | case 'l': |
| 1963 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1964 | long *l = (long *)va_arg(args, long *); |
| 1965 | *l = simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1967 | unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); |
| 1968 | *l = simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | } |
| 1970 | break; |
| 1971 | case 'L': |
| 1972 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1973 | long long *l = (long long *)va_arg(args, long long *); |
| 1974 | *l = simple_strtoll(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1976 | unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); |
| 1977 | *l = simple_strtoull(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | } |
| 1979 | break; |
| 1980 | case 'Z': |
| 1981 | case 'z': |
| 1982 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1983 | size_t *s = (size_t *)va_arg(args, size_t *); |
| 1984 | *s = (size_t)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | } |
| 1986 | break; |
| 1987 | default: |
| 1988 | if (is_sign) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1989 | int *i = (int *)va_arg(args, int *); |
| 1990 | *i = (int)simple_strtol(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | } else { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1992 | unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); |
| 1993 | *i = (unsigned int)simple_strtoul(str, &next, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | } |
| 1995 | break; |
| 1996 | } |
| 1997 | num++; |
| 1998 | |
| 1999 | if (!next) |
| 2000 | break; |
| 2001 | str = next; |
| 2002 | } |
Johannes Berg | c6b40d1 | 2007-05-08 00:27:20 -0700 | [diff] [blame] | 2003 | |
| 2004 | /* |
| 2005 | * Now we've come all the way through so either the input string or the |
| 2006 | * format ended. In the former case, there can be a %n at the current |
| 2007 | * position in the format that needs to be filled. |
| 2008 | */ |
| 2009 | if (*fmt == '%' && *(fmt + 1) == 'n') { |
| 2010 | int *p = (int *)va_arg(args, int *); |
| 2011 | *p = str - buf; |
| 2012 | } |
| 2013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | return num; |
| 2015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | EXPORT_SYMBOL(vsscanf); |
| 2017 | |
| 2018 | /** |
| 2019 | * sscanf - Unformat a buffer into a list of arguments |
| 2020 | * @buf: input buffer |
| 2021 | * @fmt: formatting of buffer |
| 2022 | * @...: resulting arguments |
| 2023 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2024 | int sscanf(const char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | { |
| 2026 | va_list args; |
| 2027 | int i; |
| 2028 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2029 | va_start(args, fmt); |
| 2030 | i = vsscanf(buf, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | return i; |
| 2034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | EXPORT_SYMBOL(sscanf); |