blob: 98d632277ca8c932add9d0daf666dd1795c7f785 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
12/*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070025#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110027#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Tim Schmielau4e57b682005-10-30 15:03:48 -080029#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/div64.h>
James Bottomleydeac93d2008-09-03 20:43:36 -050031#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010033/* Works only for digits and letters, but small and fast */
34#define TOLOWER(x) ((x) | 0x20)
35
Harvey Harrisonaa46a632008-10-16 13:40:34 -070036static unsigned int simple_guess_base(const char *cp)
37{
38 if (cp[0] == '0') {
39 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
40 return 16;
41 else
42 return 8;
43 } else {
44 return 10;
45 }
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
49 * simple_strtoul - convert a string to an unsigned long
50 * @cp: The start of the string
51 * @endp: A pointer to the end of the parsed string will be placed here
52 * @base: The number base to use
53 */
Harvey Harrison22d27052008-10-16 13:40:35 -070054unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Harvey Harrisonaa46a632008-10-16 13:40:34 -070056 unsigned long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Harvey Harrisonaa46a632008-10-16 13:40:34 -070058 if (!base)
59 base = simple_guess_base(cp);
60
61 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
62 cp += 2;
63
64 while (isxdigit(*cp)) {
65 unsigned int value;
66
67 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
68 if (value >= base)
69 break;
70 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 cp++;
72 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (endp)
75 *endp = (char *)cp;
76 return result;
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078EXPORT_SYMBOL(simple_strtoul);
79
80/**
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
85 */
Harvey Harrison22d27052008-10-16 13:40:35 -070086long simple_strtol(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Harvey Harrison22d27052008-10-16 13:40:35 -070088 if(*cp == '-')
89 return -simple_strtoul(cp + 1, endp, base);
90 return simple_strtoul(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092EXPORT_SYMBOL(simple_strtol);
93
94/**
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
99 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700100unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700102 unsigned long long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700104 if (!base)
105 base = simple_guess_base(cp);
106
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
109
110 while (isxdigit(*cp)) {
111 unsigned int value;
112
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
114 if (value >= base)
115 break;
116 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 cp++;
118 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (endp)
121 *endp = (char *)cp;
122 return result;
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124EXPORT_SYMBOL(simple_strtoull);
125
126/**
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
131 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700132long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 if(*cp=='-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700135 return -simple_strtoull(cp + 1, endp, base);
136 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Yi Yang06b2a762008-02-08 04:21:57 -0800139/**
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
144 *
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
150 *
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
152 *
153 * echo will append a newline to the tail.
154 *
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
157 *
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
160 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700161int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
162{
163 char *tail;
164 unsigned long val;
165 size_t len;
166
167 *res = 0;
168 len = strlen(cp);
169 if (len == 0)
170 return -EINVAL;
171
172 val = simple_strtoul(cp, &tail, base);
173 if ((*tail == '\0') ||
174 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
175 *res = val;
176 return 0;
177 }
178
179 return -EINVAL;
180}
181EXPORT_SYMBOL(strict_strtoul);
Yi Yang06b2a762008-02-08 04:21:57 -0800182
183/**
184 * strict_strtol - convert a string to a long strictly
185 * @cp: The string to be converted
186 * @base: The number base to use
187 * @res: The converted result value
188 *
189 * strict_strtol is similiar to strict_strtoul, but it allows the first
190 * character of a string is '-'.
191 *
192 * It returns 0 if conversion is successful and *res is set to the converted
193 * value, otherwise it returns -EINVAL and *res is set to 0.
194 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700195int strict_strtol(const char *cp, unsigned int base, long *res)
196{
197 int ret;
198 if (*cp == '-') {
199 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
200 if (!ret)
201 *res = -(*res);
202 } else {
203 ret = strict_strtoul(cp, base, (unsigned long *)res);
204 }
205
206 return ret;
207}
208EXPORT_SYMBOL(strict_strtol);
Yi Yang06b2a762008-02-08 04:21:57 -0800209
210/**
211 * strict_strtoull - convert a string to an unsigned long long strictly
212 * @cp: The string to be converted
213 * @base: The number base to use
214 * @res: The converted result value
215 *
216 * strict_strtoull converts a string to an unsigned long long only if the
217 * string is really an unsigned long long string, any string containing
218 * any invalid char at the tail will be rejected and -EINVAL is returned,
219 * only a newline char at the tail is acceptible because people generally
220 * change a module parameter in the following way:
221 *
222 * echo 1024 > /sys/module/e1000/parameters/copybreak
223 *
224 * echo will append a newline to the tail of the string.
225 *
226 * It returns 0 if conversion is successful and *res is set to the converted
227 * value, otherwise it returns -EINVAL and *res is set to 0.
228 *
229 * simple_strtoull just ignores the successive invalid characters and
230 * return the converted value of prefix part of the string.
231 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700232int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
233{
234 char *tail;
235 unsigned long long val;
236 size_t len;
237
238 *res = 0;
239 len = strlen(cp);
240 if (len == 0)
241 return -EINVAL;
242
243 val = simple_strtoull(cp, &tail, base);
244 if ((*tail == '\0') ||
245 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
246 *res = val;
247 return 0;
248 }
249
250 return -EINVAL;
251}
252EXPORT_SYMBOL(strict_strtoull);
Yi Yang06b2a762008-02-08 04:21:57 -0800253
254/**
255 * strict_strtoll - convert a string to a long long strictly
256 * @cp: The string to be converted
257 * @base: The number base to use
258 * @res: The converted result value
259 *
260 * strict_strtoll is similiar to strict_strtoull, but it allows the first
261 * character of a string is '-'.
262 *
263 * It returns 0 if conversion is successful and *res is set to the converted
264 * value, otherwise it returns -EINVAL and *res is set to 0.
265 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700266int strict_strtoll(const char *cp, unsigned int base, long long *res)
267{
268 int ret;
269 if (*cp == '-') {
270 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
271 if (!ret)
272 *res = -(*res);
273 } else {
274 ret = strict_strtoull(cp, base, (unsigned long long *)res);
275 }
Yi Yang06b2a762008-02-08 04:21:57 -0800276
Harvey Harrison9d85db22008-10-16 13:40:35 -0700277 return ret;
278}
Yi Yang06b2a762008-02-08 04:21:57 -0800279EXPORT_SYMBOL(strict_strtoll);
Yi Yang06b2a762008-02-08 04:21:57 -0800280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static int skip_atoi(const char **s)
282{
283 int i=0;
284
285 while (isdigit(**s))
286 i = i*10 + *((*s)++) - '0';
287 return i;
288}
289
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700290/* Decimal conversion is by far the most typical, and is used
291 * for /proc and /sys data. This directly impacts e.g. top performance
292 * with many processes running. We optimize it for speed
293 * using code from
294 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
295 * (with permission from the author, Douglas W. Jones). */
296
297/* Formats correctly any integer in [0,99999].
298 * Outputs from one to five digits depending on input.
299 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
300static char* put_dec_trunc(char *buf, unsigned q)
301{
302 unsigned d3, d2, d1, d0;
303 d1 = (q>>4) & 0xf;
304 d2 = (q>>8) & 0xf;
305 d3 = (q>>12);
306
307 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
308 q = (d0 * 0xcd) >> 11;
309 d0 = d0 - 10*q;
310 *buf++ = d0 + '0'; /* least significant digit */
311 d1 = q + 9*d3 + 5*d2 + d1;
312 if (d1 != 0) {
313 q = (d1 * 0xcd) >> 11;
314 d1 = d1 - 10*q;
315 *buf++ = d1 + '0'; /* next digit */
316
317 d2 = q + 2*d2;
318 if ((d2 != 0) || (d3 != 0)) {
319 q = (d2 * 0xd) >> 7;
320 d2 = d2 - 10*q;
321 *buf++ = d2 + '0'; /* next digit */
322
323 d3 = q + 4*d3;
324 if (d3 != 0) {
325 q = (d3 * 0xcd) >> 11;
326 d3 = d3 - 10*q;
327 *buf++ = d3 + '0'; /* next digit */
328 if (q != 0)
329 *buf++ = q + '0'; /* most sign. digit */
330 }
331 }
332 }
333 return buf;
334}
335/* Same with if's removed. Always emits five digits */
336static char* put_dec_full(char *buf, unsigned q)
337{
338 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
339 /* but anyway, gcc produces better code with full-sized ints */
340 unsigned d3, d2, d1, d0;
341 d1 = (q>>4) & 0xf;
342 d2 = (q>>8) & 0xf;
343 d3 = (q>>12);
344
345 /* Possible ways to approx. divide by 10 */
346 /* gcc -O2 replaces multiply with shifts and adds */
347 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
348 // (x * 0x67) >> 10: 1100111
349 // (x * 0x34) >> 9: 110100 - same
350 // (x * 0x1a) >> 8: 11010 - same
351 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
352
353 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
354 q = (d0 * 0xcd) >> 11;
355 d0 = d0 - 10*q;
356 *buf++ = d0 + '0';
357 d1 = q + 9*d3 + 5*d2 + d1;
358 q = (d1 * 0xcd) >> 11;
359 d1 = d1 - 10*q;
360 *buf++ = d1 + '0';
361
362 d2 = q + 2*d2;
363 q = (d2 * 0xd) >> 7;
364 d2 = d2 - 10*q;
365 *buf++ = d2 + '0';
366
367 d3 = q + 4*d3;
368 q = (d3 * 0xcd) >> 11; /* - shorter code */
369 /* q = (d3 * 0x67) >> 10; - would also work */
370 d3 = d3 - 10*q;
371 *buf++ = d3 + '0';
372 *buf++ = q + '0';
373 return buf;
374}
375/* No inlining helps gcc to use registers better */
376static noinline char* put_dec(char *buf, unsigned long long num)
377{
378 while (1) {
379 unsigned rem;
380 if (num < 100000)
381 return put_dec_trunc(buf, num);
382 rem = do_div(num, 100000);
383 buf = put_dec_full(buf, rem);
384 }
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#define ZEROPAD 1 /* pad with zero */
388#define SIGN 2 /* unsigned/signed long */
389#define PLUS 4 /* show plus */
390#define SPACE 8 /* space if plus */
391#define LEFT 16 /* left justified */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100392#define SMALL 32 /* Must be 32 == 0x20 */
393#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700395static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100397 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
398 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
399
400 char tmp[66];
401 char sign;
402 char locase;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700403 int need_pfx = ((type & SPECIAL) && base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int i;
405
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100406 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
407 * produces same digits or (maybe lowercased) letters */
408 locase = (type & SMALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (type & LEFT)
410 type &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sign = 0;
412 if (type & SIGN) {
413 if ((signed long long) num < 0) {
414 sign = '-';
415 num = - (signed long long) num;
416 size--;
417 } else if (type & PLUS) {
418 sign = '+';
419 size--;
420 } else if (type & SPACE) {
421 sign = ' ';
422 size--;
423 }
424 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700425 if (need_pfx) {
426 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (base == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 size--;
429 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700430
431 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 i = 0;
433 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700434 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700435 /* Generic code, for any base:
436 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100437 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700438 } while (num != 0);
439 */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700440 else if (base != 10) { /* 8 or 16 */
441 int mask = base - 1;
442 int shift = 3;
443 if (base == 16) shift = 4;
444 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100445 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700446 num >>= shift;
447 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700448 } else { /* base 10 */
449 i = put_dec(tmp, num) - tmp;
450 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700451
452 /* printing 100 using %2d gives "100", not "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (i > precision)
454 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700455 /* leading space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 size -= precision;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700457 if (!(type & (ZEROPAD+LEFT))) {
458 while(--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700459 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *buf = ' ';
461 ++buf;
462 }
463 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700464 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700466 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *buf = sign;
468 ++buf;
469 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700470 /* "0x" / "0" prefix */
471 if (need_pfx) {
472 if (buf < end)
473 *buf = '0';
474 ++buf;
475 if (base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700476 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100477 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ++buf;
479 }
480 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700481 /* zero or space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!(type & LEFT)) {
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700483 char c = (type & ZEROPAD) ? '0' : ' ';
484 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700485 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *buf = c;
487 ++buf;
488 }
489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* hmm even more zero padding? */
491 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700492 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *buf = '0';
494 ++buf;
495 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700496 /* actual digits of result */
497 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700498 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *buf = tmp[i];
500 ++buf;
501 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700502 /* trailing space padding */
503 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700504 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *buf = ' ';
506 ++buf;
507 }
508 return buf;
509}
510
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700511static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
512{
513 int len, i;
514
515 if ((unsigned long)s < PAGE_SIZE)
516 s = "<NULL>";
517
518 len = strnlen(s, precision);
519
520 if (!(flags & LEFT)) {
521 while (len < field_width--) {
522 if (buf < end)
523 *buf = ' ';
524 ++buf;
525 }
526 }
527 for (i = 0; i < len; ++i) {
528 if (buf < end)
529 *buf = *s;
530 ++buf; ++s;
531 }
532 while (len < field_width--) {
533 if (buf < end)
534 *buf = ' ';
535 ++buf;
536 }
537 return buf;
538}
539
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700540static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
541{
542 unsigned long value = (unsigned long) ptr;
543#ifdef CONFIG_KALLSYMS
544 char sym[KSYM_SYMBOL_LEN];
545 sprint_symbol(sym, value);
546 return string(buf, end, sym, field_width, precision, flags);
547#else
548 field_width = 2*sizeof(void *);
549 flags |= SPECIAL | SMALL | ZEROPAD;
550 return number(buf, end, value, 16, field_width, precision, flags);
551#endif
552}
553
Linus Torvalds332d2e72008-10-20 15:07:34 +1100554static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
555{
556#ifndef IO_RSRC_PRINTK_SIZE
557#define IO_RSRC_PRINTK_SIZE 4
558#endif
559
560#ifndef MEM_RSRC_PRINTK_SIZE
561#define MEM_RSRC_PRINTK_SIZE 8
562#endif
563
564 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
565 char sym[4*sizeof(resource_size_t) + 8];
566 char *p = sym, *pend = sym + sizeof(sym);
567 int size = -1;
568
569 if (res->flags & IORESOURCE_IO)
570 size = IO_RSRC_PRINTK_SIZE;
571 else if (res->flags & IORESOURCE_MEM)
572 size = MEM_RSRC_PRINTK_SIZE;
573
574 *p++ = '[';
575 p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
576 *p++ = '-';
577 p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
578 *p++ = ']';
579 *p = 0;
580
581 return string(buf, end, sym, field_width, precision, flags);
582}
583
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700584static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
585 int precision, int flags)
586{
587 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
588 char *p = mac_addr;
589 int i;
590
591 for (i = 0; i < 6; i++) {
592 p = pack_hex_byte(p, addr[i]);
593 if (!(flags & SPECIAL) && i != 5)
594 *p++ = ':';
595 }
596 *p = '\0';
597
598 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
599}
600
Harvey Harrison689afa72008-10-28 16:04:44 -0700601static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
602 int precision, int flags)
603{
604 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
605 char *p = ip6_addr;
606 int i;
607
608 for (i = 0; i < 8; i++) {
609 p = pack_hex_byte(p, addr[2 * i]);
610 p = pack_hex_byte(p, addr[2 * i + 1]);
611 if (!(flags & SPECIAL) && i != 7)
612 *p++ = ':';
613 }
614 *p = '\0';
615
616 return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
617}
618
Harvey Harrison4aa99602008-10-29 12:49:58 -0700619static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
620 int precision, int flags)
621{
622 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800623 char temp[3]; /* hold each IP quad in reverse order */
Harvey Harrison4aa99602008-10-29 12:49:58 -0700624 char *p = ip4_addr;
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800625 int i, digits;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700626
627 for (i = 0; i < 4; i++) {
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800628 digits = put_dec_trunc(temp, addr[i]) - temp;
629 /* reverse the digits in the quad */
630 while (digits--)
631 *p++ = temp[digits];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700632 if (i != 3)
633 *p++ = '.';
634 }
635 *p = '\0';
636
637 return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
638}
639
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700640/*
641 * Show a '%p' thing. A kernel extension is that the '%p' is followed
642 * by an extra set of alphanumeric characters that are extended format
643 * specifiers.
644 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100645 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700646 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100647 * - 'F' For symbolic function descriptor pointers
648 * - 'S' For symbolic direct pointers
649 * - 'R' For a struct resource pointer, it prints the range of
650 * addresses (not the name nor the flags)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700651 * - 'M' For a 6-byte MAC address, it prints the address in the
652 * usual colon-separated hex notation
Harvey Harrison4aa99602008-10-29 12:49:58 -0700653 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
654 * decimal for v4 and colon separated network-order 16 bit hex for v6)
655 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
Harvey Harrison6b9a1062008-10-29 12:53:10 -0700656 * currently the same
Linus Torvalds332d2e72008-10-20 15:07:34 +1100657 *
658 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
659 * function pointers are really function descriptors, which contain a
660 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700661 */
662static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700663{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800664 if (!ptr)
665 return string(buf, end, "(null)", field_width, precision, flags);
666
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700667 switch (*fmt) {
668 case 'F':
669 ptr = dereference_function_descriptor(ptr);
670 /* Fallthrough */
671 case 'S':
672 return symbol_string(buf, end, ptr, field_width, precision, flags);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100673 case 'R':
674 return resource_string(buf, end, ptr, field_width, precision, flags);
Harvey Harrison411c41e2008-11-25 00:40:37 -0800675 case 'm':
676 flags |= SPECIAL;
677 /* Fallthrough */
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700678 case 'M':
679 return mac_address_string(buf, end, ptr, field_width, precision, flags);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700680 case 'i':
681 flags |= SPECIAL;
682 /* Fallthrough */
683 case 'I':
684 if (fmt[1] == '6')
685 return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
686 if (fmt[1] == '4')
687 return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
688 flags &= ~SPECIAL;
689 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700690 }
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700691 flags |= SMALL;
692 if (field_width == -1) {
693 field_width = 2*sizeof(void *);
694 flags |= ZEROPAD;
695 }
696 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/**
700 * vsnprintf - Format a string and place it in a buffer
701 * @buf: The buffer to place the result into
702 * @size: The size of the buffer, including the trailing null space
703 * @fmt: The format string to use
704 * @args: Arguments for the format string
705 *
Andi Kleen20036fd2008-10-15 22:02:02 -0700706 * This function follows C99 vsnprintf, but has some extensions:
707 * %pS output the name of a text symbol
708 * %pF output the name of a function pointer
Linus Torvalds332d2e72008-10-20 15:07:34 +1100709 * %pR output the address range in a struct resource
Andi Kleen20036fd2008-10-15 22:02:02 -0700710 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * The return value is the number of characters which would
712 * be generated for the given input, excluding the trailing
713 * '\0', as per ISO C99. If you want to have the exact
714 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800715 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * return is greater than or equal to @size, the resulting
717 * string is truncated.
718 *
719 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800720 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
722int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned long long num;
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700725 int base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 char *str, *end, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 int flags; /* flags to number() */
729
730 int field_width; /* width of output field */
731 int precision; /* min. # of digits for integers; max
732 number of chars for from string */
733 int qualifier; /* 'h', 'l', or 'L' for integer fields */
734 /* 'z' support added 23/7/1999 S.H. */
735 /* 'z' changed to 'Z' --davidm 1/25/99 */
Al Viro80322302005-08-23 22:48:17 +0100736 /* 't' added for ptrdiff_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700738 /* Reject out-of-range values early. Large positive sizes are
739 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (unlikely((int) size < 0)) {
741 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700742 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 WARN_ON(warn);
744 warn = 0;
745 return 0;
746 }
747
748 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700749 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700751 /* Make sure end is always >= buf */
752 if (end < buf) {
753 end = ((void *)-1);
754 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
757 for (; *fmt ; ++fmt) {
758 if (*fmt != '%') {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700759 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *str = *fmt;
761 ++str;
762 continue;
763 }
764
765 /* process flags */
766 flags = 0;
767 repeat:
768 ++fmt; /* this also skips first '%' */
769 switch (*fmt) {
770 case '-': flags |= LEFT; goto repeat;
771 case '+': flags |= PLUS; goto repeat;
772 case ' ': flags |= SPACE; goto repeat;
773 case '#': flags |= SPECIAL; goto repeat;
774 case '0': flags |= ZEROPAD; goto repeat;
775 }
776
777 /* get field width */
778 field_width = -1;
779 if (isdigit(*fmt))
780 field_width = skip_atoi(&fmt);
781 else if (*fmt == '*') {
782 ++fmt;
783 /* it's the next argument */
784 field_width = va_arg(args, int);
785 if (field_width < 0) {
786 field_width = -field_width;
787 flags |= LEFT;
788 }
789 }
790
791 /* get the precision */
792 precision = -1;
793 if (*fmt == '.') {
794 ++fmt;
795 if (isdigit(*fmt))
796 precision = skip_atoi(&fmt);
797 else if (*fmt == '*') {
798 ++fmt;
799 /* it's the next argument */
800 precision = va_arg(args, int);
801 }
802 if (precision < 0)
803 precision = 0;
804 }
805
806 /* get the conversion qualifier */
807 qualifier = -1;
808 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Al Viro80322302005-08-23 22:48:17 +0100809 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 qualifier = *fmt;
811 ++fmt;
812 if (qualifier == 'l' && *fmt == 'l') {
813 qualifier = 'L';
814 ++fmt;
815 }
816 }
817
818 /* default base */
819 base = 10;
820
821 switch (*fmt) {
822 case 'c':
823 if (!(flags & LEFT)) {
824 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700825 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *str = ' ';
827 ++str;
828 }
829 }
830 c = (unsigned char) va_arg(args, int);
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700831 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 *str = c;
833 ++str;
834 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700835 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *str = ' ';
837 ++str;
838 }
839 continue;
840
841 case 's':
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700842 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 continue;
844
845 case 'p':
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700846 str = pointer(fmt+1, str, end,
847 va_arg(args, void *),
848 field_width, precision, flags);
849 /* Skip all alphanumeric pointer suffixes */
850 while (isalnum(fmt[1]))
851 fmt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 continue;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 case 'n':
855 /* FIXME:
856 * What does C99 say about the overflow case here? */
857 if (qualifier == 'l') {
858 long * ip = va_arg(args, long *);
859 *ip = (str - buf);
860 } else if (qualifier == 'Z' || qualifier == 'z') {
861 size_t * ip = va_arg(args, size_t *);
862 *ip = (str - buf);
863 } else {
864 int * ip = va_arg(args, int *);
865 *ip = (str - buf);
866 }
867 continue;
868
869 case '%':
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700870 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 *str = '%';
872 ++str;
873 continue;
874
875 /* integer number formats - set up the flags and "break" */
876 case 'o':
877 base = 8;
878 break;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 case 'x':
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100881 flags |= SMALL;
882 case 'X':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 base = 16;
884 break;
885
886 case 'd':
887 case 'i':
888 flags |= SIGN;
889 case 'u':
890 break;
891
892 default:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700893 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *str = '%';
895 ++str;
896 if (*fmt) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700897 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *str = *fmt;
899 ++str;
900 } else {
901 --fmt;
902 }
903 continue;
904 }
905 if (qualifier == 'L')
906 num = va_arg(args, long long);
907 else if (qualifier == 'l') {
908 num = va_arg(args, unsigned long);
909 if (flags & SIGN)
910 num = (signed long) num;
911 } else if (qualifier == 'Z' || qualifier == 'z') {
912 num = va_arg(args, size_t);
Al Viro80322302005-08-23 22:48:17 +0100913 } else if (qualifier == 't') {
914 num = va_arg(args, ptrdiff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 } else if (qualifier == 'h') {
916 num = (unsigned short) va_arg(args, int);
917 if (flags & SIGN)
918 num = (signed short) num;
919 } else {
920 num = va_arg(args, unsigned int);
921 if (flags & SIGN)
922 num = (signed int) num;
923 }
924 str = number(str, end, num, base,
925 field_width, precision, flags);
926 }
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700927 if (size > 0) {
928 if (str < end)
929 *str = '\0';
930 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -0700931 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700932 }
933 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return str-buf;
935}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936EXPORT_SYMBOL(vsnprintf);
937
938/**
939 * vscnprintf - Format a string and place it in a buffer
940 * @buf: The buffer to place the result into
941 * @size: The size of the buffer, including the trailing null space
942 * @fmt: The format string to use
943 * @args: Arguments for the format string
944 *
945 * The return value is the number of characters which have been written into
946 * the @buf not including the trailing '\0'. If @size is <= 0 the function
947 * returns 0.
948 *
949 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800950 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -0700951 *
952 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
954int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
955{
956 int i;
957
958 i=vsnprintf(buf,size,fmt,args);
959 return (i >= size) ? (size - 1) : i;
960}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961EXPORT_SYMBOL(vscnprintf);
962
963/**
964 * snprintf - Format a string and place it in a buffer
965 * @buf: The buffer to place the result into
966 * @size: The size of the buffer, including the trailing null space
967 * @fmt: The format string to use
968 * @...: Arguments for the format string
969 *
970 * The return value is the number of characters which would be
971 * generated for the given input, excluding the trailing null,
972 * as per ISO C99. If the return is greater than or equal to
973 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -0700974 *
975 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
977int snprintf(char * buf, size_t size, const char *fmt, ...)
978{
979 va_list args;
980 int i;
981
982 va_start(args, fmt);
983 i=vsnprintf(buf,size,fmt,args);
984 va_end(args);
985 return i;
986}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987EXPORT_SYMBOL(snprintf);
988
989/**
990 * scnprintf - Format a string and place it in a buffer
991 * @buf: The buffer to place the result into
992 * @size: The size of the buffer, including the trailing null space
993 * @fmt: The format string to use
994 * @...: Arguments for the format string
995 *
996 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -0800997 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999
1000int scnprintf(char * buf, size_t size, const char *fmt, ...)
1001{
1002 va_list args;
1003 int i;
1004
1005 va_start(args, fmt);
1006 i = vsnprintf(buf, size, fmt, args);
1007 va_end(args);
1008 return (i >= size) ? (size - 1) : i;
1009}
1010EXPORT_SYMBOL(scnprintf);
1011
1012/**
1013 * vsprintf - Format a string and place it in a buffer
1014 * @buf: The buffer to place the result into
1015 * @fmt: The format string to use
1016 * @args: Arguments for the format string
1017 *
1018 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001019 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * buffer overflows.
1021 *
1022 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001023 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001024 *
1025 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
1027int vsprintf(char *buf, const char *fmt, va_list args)
1028{
1029 return vsnprintf(buf, INT_MAX, fmt, args);
1030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031EXPORT_SYMBOL(vsprintf);
1032
1033/**
1034 * sprintf - Format a string and place it in a buffer
1035 * @buf: The buffer to place the result into
1036 * @fmt: The format string to use
1037 * @...: Arguments for the format string
1038 *
1039 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001040 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001042 *
1043 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
1045int sprintf(char * buf, const char *fmt, ...)
1046{
1047 va_list args;
1048 int i;
1049
1050 va_start(args, fmt);
1051 i=vsnprintf(buf, INT_MAX, fmt, args);
1052 va_end(args);
1053 return i;
1054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055EXPORT_SYMBOL(sprintf);
1056
1057/**
1058 * vsscanf - Unformat a buffer into a list of arguments
1059 * @buf: input buffer
1060 * @fmt: format of buffer
1061 * @args: arguments
1062 */
1063int vsscanf(const char * buf, const char * fmt, va_list args)
1064{
1065 const char *str = buf;
1066 char *next;
1067 char digit;
1068 int num = 0;
1069 int qualifier;
1070 int base;
1071 int field_width;
1072 int is_sign = 0;
1073
1074 while(*fmt && *str) {
1075 /* skip any white space in format */
1076 /* white space in format matchs any amount of
1077 * white space, including none, in the input.
1078 */
1079 if (isspace(*fmt)) {
1080 while (isspace(*fmt))
1081 ++fmt;
1082 while (isspace(*str))
1083 ++str;
1084 }
1085
1086 /* anything that is not a conversion must match exactly */
1087 if (*fmt != '%' && *fmt) {
1088 if (*fmt++ != *str++)
1089 break;
1090 continue;
1091 }
1092
1093 if (!*fmt)
1094 break;
1095 ++fmt;
1096
1097 /* skip this conversion.
1098 * advance both strings to next white space
1099 */
1100 if (*fmt == '*') {
1101 while (!isspace(*fmt) && *fmt)
1102 fmt++;
1103 while (!isspace(*str) && *str)
1104 str++;
1105 continue;
1106 }
1107
1108 /* get field width */
1109 field_width = -1;
1110 if (isdigit(*fmt))
1111 field_width = skip_atoi(&fmt);
1112
1113 /* get conversion qualifier */
1114 qualifier = -1;
1115 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1116 *fmt == 'Z' || *fmt == 'z') {
1117 qualifier = *fmt++;
1118 if (unlikely(qualifier == *fmt)) {
1119 if (qualifier == 'h') {
1120 qualifier = 'H';
1121 fmt++;
1122 } else if (qualifier == 'l') {
1123 qualifier = 'L';
1124 fmt++;
1125 }
1126 }
1127 }
1128 base = 10;
1129 is_sign = 0;
1130
1131 if (!*fmt || !*str)
1132 break;
1133
1134 switch(*fmt++) {
1135 case 'c':
1136 {
1137 char *s = (char *) va_arg(args,char*);
1138 if (field_width == -1)
1139 field_width = 1;
1140 do {
1141 *s++ = *str++;
1142 } while (--field_width > 0 && *str);
1143 num++;
1144 }
1145 continue;
1146 case 's':
1147 {
1148 char *s = (char *) va_arg(args, char *);
1149 if(field_width == -1)
1150 field_width = INT_MAX;
1151 /* first, skip leading white space in buffer */
1152 while (isspace(*str))
1153 str++;
1154
1155 /* now copy until next white space */
1156 while (*str && !isspace(*str) && field_width--) {
1157 *s++ = *str++;
1158 }
1159 *s = '\0';
1160 num++;
1161 }
1162 continue;
1163 case 'n':
1164 /* return number of characters read so far */
1165 {
1166 int *i = (int *)va_arg(args,int*);
1167 *i = str - buf;
1168 }
1169 continue;
1170 case 'o':
1171 base = 8;
1172 break;
1173 case 'x':
1174 case 'X':
1175 base = 16;
1176 break;
1177 case 'i':
1178 base = 0;
1179 case 'd':
1180 is_sign = 1;
1181 case 'u':
1182 break;
1183 case '%':
1184 /* looking for '%' in str */
1185 if (*str++ != '%')
1186 return num;
1187 continue;
1188 default:
1189 /* invalid format; stop here */
1190 return num;
1191 }
1192
1193 /* have some sort of integer conversion.
1194 * first, skip white space in buffer.
1195 */
1196 while (isspace(*str))
1197 str++;
1198
1199 digit = *str;
1200 if (is_sign && digit == '-')
1201 digit = *(str + 1);
1202
1203 if (!digit
1204 || (base == 16 && !isxdigit(digit))
1205 || (base == 10 && !isdigit(digit))
1206 || (base == 8 && (!isdigit(digit) || digit > '7'))
1207 || (base == 0 && !isdigit(digit)))
1208 break;
1209
1210 switch(qualifier) {
1211 case 'H': /* that's 'hh' in format */
1212 if (is_sign) {
1213 signed char *s = (signed char *) va_arg(args,signed char *);
1214 *s = (signed char) simple_strtol(str,&next,base);
1215 } else {
1216 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1217 *s = (unsigned char) simple_strtoul(str, &next, base);
1218 }
1219 break;
1220 case 'h':
1221 if (is_sign) {
1222 short *s = (short *) va_arg(args,short *);
1223 *s = (short) simple_strtol(str,&next,base);
1224 } else {
1225 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1226 *s = (unsigned short) simple_strtoul(str, &next, base);
1227 }
1228 break;
1229 case 'l':
1230 if (is_sign) {
1231 long *l = (long *) va_arg(args,long *);
1232 *l = simple_strtol(str,&next,base);
1233 } else {
1234 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1235 *l = simple_strtoul(str,&next,base);
1236 }
1237 break;
1238 case 'L':
1239 if (is_sign) {
1240 long long *l = (long long*) va_arg(args,long long *);
1241 *l = simple_strtoll(str,&next,base);
1242 } else {
1243 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1244 *l = simple_strtoull(str,&next,base);
1245 }
1246 break;
1247 case 'Z':
1248 case 'z':
1249 {
1250 size_t *s = (size_t*) va_arg(args,size_t*);
1251 *s = (size_t) simple_strtoul(str,&next,base);
1252 }
1253 break;
1254 default:
1255 if (is_sign) {
1256 int *i = (int *) va_arg(args, int*);
1257 *i = (int) simple_strtol(str,&next,base);
1258 } else {
1259 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1260 *i = (unsigned int) simple_strtoul(str,&next,base);
1261 }
1262 break;
1263 }
1264 num++;
1265
1266 if (!next)
1267 break;
1268 str = next;
1269 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001270
1271 /*
1272 * Now we've come all the way through so either the input string or the
1273 * format ended. In the former case, there can be a %n at the current
1274 * position in the format that needs to be filled.
1275 */
1276 if (*fmt == '%' && *(fmt + 1) == 'n') {
1277 int *p = (int *)va_arg(args, int *);
1278 *p = str - buf;
1279 }
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return num;
1282}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283EXPORT_SYMBOL(vsscanf);
1284
1285/**
1286 * sscanf - Unformat a buffer into a list of arguments
1287 * @buf: input buffer
1288 * @fmt: formatting of buffer
1289 * @...: resulting arguments
1290 */
1291int sscanf(const char * buf, const char * fmt, ...)
1292{
1293 va_list args;
1294 int i;
1295
1296 va_start(args,fmt);
1297 i = vsscanf(buf,fmt,args);
1298 va_end(args);
1299 return i;
1300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301EXPORT_SYMBOL(sscanf);