blob: 0fbd0121d91d8eff2e5299650579cd22c6ac9d3e [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);
Pavel Macheke899aa82009-01-06 14:40:53 -0800173 if (tail == cp)
174 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700175 if ((*tail == '\0') ||
176 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
177 *res = val;
178 return 0;
179 }
180
181 return -EINVAL;
182}
183EXPORT_SYMBOL(strict_strtoul);
Yi Yang06b2a762008-02-08 04:21:57 -0800184
185/**
186 * strict_strtol - convert a string to a long strictly
187 * @cp: The string to be converted
188 * @base: The number base to use
189 * @res: The converted result value
190 *
191 * strict_strtol is similiar to strict_strtoul, but it allows the first
192 * character of a string is '-'.
193 *
194 * It returns 0 if conversion is successful and *res is set to the converted
195 * value, otherwise it returns -EINVAL and *res is set to 0.
196 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700197int strict_strtol(const char *cp, unsigned int base, long *res)
198{
199 int ret;
200 if (*cp == '-') {
201 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
202 if (!ret)
203 *res = -(*res);
204 } else {
205 ret = strict_strtoul(cp, base, (unsigned long *)res);
206 }
207
208 return ret;
209}
210EXPORT_SYMBOL(strict_strtol);
Yi Yang06b2a762008-02-08 04:21:57 -0800211
212/**
213 * strict_strtoull - convert a string to an unsigned long long strictly
214 * @cp: The string to be converted
215 * @base: The number base to use
216 * @res: The converted result value
217 *
218 * strict_strtoull converts a string to an unsigned long long only if the
219 * string is really an unsigned long long string, any string containing
220 * any invalid char at the tail will be rejected and -EINVAL is returned,
221 * only a newline char at the tail is acceptible because people generally
222 * change a module parameter in the following way:
223 *
224 * echo 1024 > /sys/module/e1000/parameters/copybreak
225 *
226 * echo will append a newline to the tail of the string.
227 *
228 * It returns 0 if conversion is successful and *res is set to the converted
229 * value, otherwise it returns -EINVAL and *res is set to 0.
230 *
231 * simple_strtoull just ignores the successive invalid characters and
232 * return the converted value of prefix part of the string.
233 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700234int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
235{
236 char *tail;
237 unsigned long long val;
238 size_t len;
239
240 *res = 0;
241 len = strlen(cp);
242 if (len == 0)
243 return -EINVAL;
244
245 val = simple_strtoull(cp, &tail, base);
Pavel Macheke899aa82009-01-06 14:40:53 -0800246 if (tail == cp)
247 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700248 if ((*tail == '\0') ||
249 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
250 *res = val;
251 return 0;
252 }
253
254 return -EINVAL;
255}
256EXPORT_SYMBOL(strict_strtoull);
Yi Yang06b2a762008-02-08 04:21:57 -0800257
258/**
259 * strict_strtoll - convert a string to a long long strictly
260 * @cp: The string to be converted
261 * @base: The number base to use
262 * @res: The converted result value
263 *
264 * strict_strtoll is similiar to strict_strtoull, but it allows the first
265 * character of a string is '-'.
266 *
267 * It returns 0 if conversion is successful and *res is set to the converted
268 * value, otherwise it returns -EINVAL and *res is set to 0.
269 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700270int strict_strtoll(const char *cp, unsigned int base, long long *res)
271{
272 int ret;
273 if (*cp == '-') {
274 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
275 if (!ret)
276 *res = -(*res);
277 } else {
278 ret = strict_strtoull(cp, base, (unsigned long long *)res);
279 }
Yi Yang06b2a762008-02-08 04:21:57 -0800280
Harvey Harrison9d85db22008-10-16 13:40:35 -0700281 return ret;
282}
Yi Yang06b2a762008-02-08 04:21:57 -0800283EXPORT_SYMBOL(strict_strtoll);
Yi Yang06b2a762008-02-08 04:21:57 -0800284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int skip_atoi(const char **s)
286{
287 int i=0;
288
289 while (isdigit(**s))
290 i = i*10 + *((*s)++) - '0';
291 return i;
292}
293
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700294/* Decimal conversion is by far the most typical, and is used
295 * for /proc and /sys data. This directly impacts e.g. top performance
296 * with many processes running. We optimize it for speed
297 * using code from
298 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
299 * (with permission from the author, Douglas W. Jones). */
300
301/* Formats correctly any integer in [0,99999].
302 * Outputs from one to five digits depending on input.
303 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
304static char* put_dec_trunc(char *buf, unsigned q)
305{
306 unsigned d3, d2, d1, d0;
307 d1 = (q>>4) & 0xf;
308 d2 = (q>>8) & 0xf;
309 d3 = (q>>12);
310
311 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
312 q = (d0 * 0xcd) >> 11;
313 d0 = d0 - 10*q;
314 *buf++ = d0 + '0'; /* least significant digit */
315 d1 = q + 9*d3 + 5*d2 + d1;
316 if (d1 != 0) {
317 q = (d1 * 0xcd) >> 11;
318 d1 = d1 - 10*q;
319 *buf++ = d1 + '0'; /* next digit */
320
321 d2 = q + 2*d2;
322 if ((d2 != 0) || (d3 != 0)) {
323 q = (d2 * 0xd) >> 7;
324 d2 = d2 - 10*q;
325 *buf++ = d2 + '0'; /* next digit */
326
327 d3 = q + 4*d3;
328 if (d3 != 0) {
329 q = (d3 * 0xcd) >> 11;
330 d3 = d3 - 10*q;
331 *buf++ = d3 + '0'; /* next digit */
332 if (q != 0)
333 *buf++ = q + '0'; /* most sign. digit */
334 }
335 }
336 }
337 return buf;
338}
339/* Same with if's removed. Always emits five digits */
340static char* put_dec_full(char *buf, unsigned q)
341{
342 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
343 /* but anyway, gcc produces better code with full-sized ints */
344 unsigned d3, d2, d1, d0;
345 d1 = (q>>4) & 0xf;
346 d2 = (q>>8) & 0xf;
347 d3 = (q>>12);
348
349 /* Possible ways to approx. divide by 10 */
350 /* gcc -O2 replaces multiply with shifts and adds */
351 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
352 // (x * 0x67) >> 10: 1100111
353 // (x * 0x34) >> 9: 110100 - same
354 // (x * 0x1a) >> 8: 11010 - same
355 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
356
357 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
358 q = (d0 * 0xcd) >> 11;
359 d0 = d0 - 10*q;
360 *buf++ = d0 + '0';
361 d1 = q + 9*d3 + 5*d2 + d1;
362 q = (d1 * 0xcd) >> 11;
363 d1 = d1 - 10*q;
364 *buf++ = d1 + '0';
365
366 d2 = q + 2*d2;
367 q = (d2 * 0xd) >> 7;
368 d2 = d2 - 10*q;
369 *buf++ = d2 + '0';
370
371 d3 = q + 4*d3;
372 q = (d3 * 0xcd) >> 11; /* - shorter code */
373 /* q = (d3 * 0x67) >> 10; - would also work */
374 d3 = d3 - 10*q;
375 *buf++ = d3 + '0';
376 *buf++ = q + '0';
377 return buf;
378}
379/* No inlining helps gcc to use registers better */
380static noinline char* put_dec(char *buf, unsigned long long num)
381{
382 while (1) {
383 unsigned rem;
384 if (num < 100000)
385 return put_dec_trunc(buf, num);
386 rem = do_div(num, 100000);
387 buf = put_dec_full(buf, rem);
388 }
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#define ZEROPAD 1 /* pad with zero */
392#define SIGN 2 /* unsigned/signed long */
393#define PLUS 4 /* show plus */
394#define SPACE 8 /* space if plus */
395#define LEFT 16 /* left justified */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100396#define SMALL 32 /* Must be 32 == 0x20 */
397#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700399static 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 -0700400{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100401 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
402 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
403
404 char tmp[66];
405 char sign;
406 char locase;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700407 int need_pfx = ((type & SPECIAL) && base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int i;
409
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100410 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
411 * produces same digits or (maybe lowercased) letters */
412 locase = (type & SMALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (type & LEFT)
414 type &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sign = 0;
416 if (type & SIGN) {
417 if ((signed long long) num < 0) {
418 sign = '-';
419 num = - (signed long long) num;
420 size--;
421 } else if (type & PLUS) {
422 sign = '+';
423 size--;
424 } else if (type & SPACE) {
425 sign = ' ';
426 size--;
427 }
428 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700429 if (need_pfx) {
430 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (base == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 size--;
433 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700434
435 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 i = 0;
437 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700438 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700439 /* Generic code, for any base:
440 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100441 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700442 } while (num != 0);
443 */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700444 else if (base != 10) { /* 8 or 16 */
445 int mask = base - 1;
446 int shift = 3;
447 if (base == 16) shift = 4;
448 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100449 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450 num >>= shift;
451 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700452 } else { /* base 10 */
453 i = put_dec(tmp, num) - tmp;
454 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700455
456 /* printing 100 using %2d gives "100", not "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (i > precision)
458 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700459 /* leading space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 size -= precision;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700461 if (!(type & (ZEROPAD+LEFT))) {
462 while(--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700463 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *buf = ' ';
465 ++buf;
466 }
467 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700468 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700470 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *buf = sign;
472 ++buf;
473 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700474 /* "0x" / "0" prefix */
475 if (need_pfx) {
476 if (buf < end)
477 *buf = '0';
478 ++buf;
479 if (base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700480 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100481 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ++buf;
483 }
484 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700485 /* zero or space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!(type & LEFT)) {
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700487 char c = (type & ZEROPAD) ? '0' : ' ';
488 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700489 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *buf = c;
491 ++buf;
492 }
493 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700494 /* hmm even more zero padding? */
495 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700496 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *buf = '0';
498 ++buf;
499 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700500 /* actual digits of result */
501 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700502 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *buf = tmp[i];
504 ++buf;
505 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700506 /* trailing space padding */
507 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700508 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buf = ' ';
510 ++buf;
511 }
512 return buf;
513}
514
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700515static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
516{
517 int len, i;
518
519 if ((unsigned long)s < PAGE_SIZE)
520 s = "<NULL>";
521
522 len = strnlen(s, precision);
523
524 if (!(flags & LEFT)) {
525 while (len < field_width--) {
526 if (buf < end)
527 *buf = ' ';
528 ++buf;
529 }
530 }
531 for (i = 0; i < len; ++i) {
532 if (buf < end)
533 *buf = *s;
534 ++buf; ++s;
535 }
536 while (len < field_width--) {
537 if (buf < end)
538 *buf = ' ';
539 ++buf;
540 }
541 return buf;
542}
543
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700544static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
545{
546 unsigned long value = (unsigned long) ptr;
547#ifdef CONFIG_KALLSYMS
548 char sym[KSYM_SYMBOL_LEN];
549 sprint_symbol(sym, value);
550 return string(buf, end, sym, field_width, precision, flags);
551#else
552 field_width = 2*sizeof(void *);
553 flags |= SPECIAL | SMALL | ZEROPAD;
554 return number(buf, end, value, 16, field_width, precision, flags);
555#endif
556}
557
Linus Torvalds332d2e72008-10-20 15:07:34 +1100558static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
559{
560#ifndef IO_RSRC_PRINTK_SIZE
561#define IO_RSRC_PRINTK_SIZE 4
562#endif
563
564#ifndef MEM_RSRC_PRINTK_SIZE
565#define MEM_RSRC_PRINTK_SIZE 8
566#endif
567
568 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
569 char sym[4*sizeof(resource_size_t) + 8];
570 char *p = sym, *pend = sym + sizeof(sym);
571 int size = -1;
572
573 if (res->flags & IORESOURCE_IO)
574 size = IO_RSRC_PRINTK_SIZE;
575 else if (res->flags & IORESOURCE_MEM)
576 size = MEM_RSRC_PRINTK_SIZE;
577
578 *p++ = '[';
579 p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
580 *p++ = '-';
581 p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
582 *p++ = ']';
583 *p = 0;
584
585 return string(buf, end, sym, field_width, precision, flags);
586}
587
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700588static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
589 int precision, int flags)
590{
591 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
592 char *p = mac_addr;
593 int i;
594
595 for (i = 0; i < 6; i++) {
596 p = pack_hex_byte(p, addr[i]);
597 if (!(flags & SPECIAL) && i != 5)
598 *p++ = ':';
599 }
600 *p = '\0';
601
602 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
603}
604
Harvey Harrison689afa72008-10-28 16:04:44 -0700605static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
606 int precision, int flags)
607{
608 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
609 char *p = ip6_addr;
610 int i;
611
612 for (i = 0; i < 8; i++) {
613 p = pack_hex_byte(p, addr[2 * i]);
614 p = pack_hex_byte(p, addr[2 * i + 1]);
615 if (!(flags & SPECIAL) && i != 7)
616 *p++ = ':';
617 }
618 *p = '\0';
619
620 return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
621}
622
Harvey Harrison4aa99602008-10-29 12:49:58 -0700623static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
624 int precision, int flags)
625{
626 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800627 char temp[3]; /* hold each IP quad in reverse order */
Harvey Harrison4aa99602008-10-29 12:49:58 -0700628 char *p = ip4_addr;
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800629 int i, digits;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700630
631 for (i = 0; i < 4; i++) {
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800632 digits = put_dec_trunc(temp, addr[i]) - temp;
633 /* reverse the digits in the quad */
634 while (digits--)
635 *p++ = temp[digits];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700636 if (i != 3)
637 *p++ = '.';
638 }
639 *p = '\0';
640
641 return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
642}
643
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700644/*
645 * Show a '%p' thing. A kernel extension is that the '%p' is followed
646 * by an extra set of alphanumeric characters that are extended format
647 * specifiers.
648 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100649 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700650 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100651 * - 'F' For symbolic function descriptor pointers
652 * - 'S' For symbolic direct pointers
653 * - 'R' For a struct resource pointer, it prints the range of
654 * addresses (not the name nor the flags)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700655 * - 'M' For a 6-byte MAC address, it prints the address in the
656 * usual colon-separated hex notation
Harvey Harrison4aa99602008-10-29 12:49:58 -0700657 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
658 * decimal for v4 and colon separated network-order 16 bit hex for v6)
659 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
Harvey Harrison6b9a1062008-10-29 12:53:10 -0700660 * currently the same
Linus Torvalds332d2e72008-10-20 15:07:34 +1100661 *
662 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
663 * function pointers are really function descriptors, which contain a
664 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700665 */
666static 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 -0700667{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800668 if (!ptr)
669 return string(buf, end, "(null)", field_width, precision, flags);
670
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700671 switch (*fmt) {
672 case 'F':
673 ptr = dereference_function_descriptor(ptr);
674 /* Fallthrough */
675 case 'S':
676 return symbol_string(buf, end, ptr, field_width, precision, flags);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100677 case 'R':
678 return resource_string(buf, end, ptr, field_width, precision, flags);
Harvey Harrison411c41e2008-11-25 00:40:37 -0800679 case 'm':
680 flags |= SPECIAL;
681 /* Fallthrough */
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700682 case 'M':
683 return mac_address_string(buf, end, ptr, field_width, precision, flags);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700684 case 'i':
685 flags |= SPECIAL;
686 /* Fallthrough */
687 case 'I':
688 if (fmt[1] == '6')
689 return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
690 if (fmt[1] == '4')
691 return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
692 flags &= ~SPECIAL;
693 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700694 }
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700695 flags |= SMALL;
696 if (field_width == -1) {
697 field_width = 2*sizeof(void *);
698 flags |= ZEROPAD;
699 }
700 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/**
704 * vsnprintf - Format a string and place it in a buffer
705 * @buf: The buffer to place the result into
706 * @size: The size of the buffer, including the trailing null space
707 * @fmt: The format string to use
708 * @args: Arguments for the format string
709 *
Andi Kleen20036fd2008-10-15 22:02:02 -0700710 * This function follows C99 vsnprintf, but has some extensions:
711 * %pS output the name of a text symbol
712 * %pF output the name of a function pointer
Linus Torvalds332d2e72008-10-20 15:07:34 +1100713 * %pR output the address range in a struct resource
Andi Kleen20036fd2008-10-15 22:02:02 -0700714 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * The return value is the number of characters which would
716 * be generated for the given input, excluding the trailing
717 * '\0', as per ISO C99. If you want to have the exact
718 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800719 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * return is greater than or equal to @size, the resulting
721 * string is truncated.
722 *
723 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800724 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
726int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long long num;
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700729 int base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 char *str, *end, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 int flags; /* flags to number() */
733
734 int field_width; /* width of output field */
735 int precision; /* min. # of digits for integers; max
736 number of chars for from string */
737 int qualifier; /* 'h', 'l', or 'L' for integer fields */
738 /* 'z' support added 23/7/1999 S.H. */
739 /* 'z' changed to 'Z' --davidm 1/25/99 */
Al Viro80322302005-08-23 22:48:17 +0100740 /* 't' added for ptrdiff_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700742 /* Reject out-of-range values early. Large positive sizes are
743 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (unlikely((int) size < 0)) {
745 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700746 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 WARN_ON(warn);
748 warn = 0;
749 return 0;
750 }
751
752 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700753 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700755 /* Make sure end is always >= buf */
756 if (end < buf) {
757 end = ((void *)-1);
758 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 for (; *fmt ; ++fmt) {
762 if (*fmt != '%') {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700763 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *str = *fmt;
765 ++str;
766 continue;
767 }
768
769 /* process flags */
770 flags = 0;
771 repeat:
772 ++fmt; /* this also skips first '%' */
773 switch (*fmt) {
774 case '-': flags |= LEFT; goto repeat;
775 case '+': flags |= PLUS; goto repeat;
776 case ' ': flags |= SPACE; goto repeat;
777 case '#': flags |= SPECIAL; goto repeat;
778 case '0': flags |= ZEROPAD; goto repeat;
779 }
780
781 /* get field width */
782 field_width = -1;
783 if (isdigit(*fmt))
784 field_width = skip_atoi(&fmt);
785 else if (*fmt == '*') {
786 ++fmt;
787 /* it's the next argument */
788 field_width = va_arg(args, int);
789 if (field_width < 0) {
790 field_width = -field_width;
791 flags |= LEFT;
792 }
793 }
794
795 /* get the precision */
796 precision = -1;
797 if (*fmt == '.') {
798 ++fmt;
799 if (isdigit(*fmt))
800 precision = skip_atoi(&fmt);
801 else if (*fmt == '*') {
802 ++fmt;
803 /* it's the next argument */
804 precision = va_arg(args, int);
805 }
806 if (precision < 0)
807 precision = 0;
808 }
809
810 /* get the conversion qualifier */
811 qualifier = -1;
812 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Al Viro80322302005-08-23 22:48:17 +0100813 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 qualifier = *fmt;
815 ++fmt;
816 if (qualifier == 'l' && *fmt == 'l') {
817 qualifier = 'L';
818 ++fmt;
819 }
820 }
821
822 /* default base */
823 base = 10;
824
825 switch (*fmt) {
826 case 'c':
827 if (!(flags & LEFT)) {
828 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700829 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *str = ' ';
831 ++str;
832 }
833 }
834 c = (unsigned char) va_arg(args, int);
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700835 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *str = c;
837 ++str;
838 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700839 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 *str = ' ';
841 ++str;
842 }
843 continue;
844
845 case 's':
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700846 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 continue;
848
849 case 'p':
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700850 str = pointer(fmt+1, str, end,
851 va_arg(args, void *),
852 field_width, precision, flags);
853 /* Skip all alphanumeric pointer suffixes */
854 while (isalnum(fmt[1]))
855 fmt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 continue;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case 'n':
859 /* FIXME:
860 * What does C99 say about the overflow case here? */
861 if (qualifier == 'l') {
862 long * ip = va_arg(args, long *);
863 *ip = (str - buf);
864 } else if (qualifier == 'Z' || qualifier == 'z') {
865 size_t * ip = va_arg(args, size_t *);
866 *ip = (str - buf);
867 } else {
868 int * ip = va_arg(args, int *);
869 *ip = (str - buf);
870 }
871 continue;
872
873 case '%':
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700874 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *str = '%';
876 ++str;
877 continue;
878
879 /* integer number formats - set up the flags and "break" */
880 case 'o':
881 base = 8;
882 break;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 case 'x':
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100885 flags |= SMALL;
886 case 'X':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 base = 16;
888 break;
889
890 case 'd':
891 case 'i':
892 flags |= SIGN;
893 case 'u':
894 break;
895
896 default:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700897 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *str = '%';
899 ++str;
900 if (*fmt) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700901 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 *str = *fmt;
903 ++str;
904 } else {
905 --fmt;
906 }
907 continue;
908 }
909 if (qualifier == 'L')
910 num = va_arg(args, long long);
911 else if (qualifier == 'l') {
912 num = va_arg(args, unsigned long);
913 if (flags & SIGN)
914 num = (signed long) num;
915 } else if (qualifier == 'Z' || qualifier == 'z') {
916 num = va_arg(args, size_t);
Al Viro80322302005-08-23 22:48:17 +0100917 } else if (qualifier == 't') {
918 num = va_arg(args, ptrdiff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 } else if (qualifier == 'h') {
920 num = (unsigned short) va_arg(args, int);
921 if (flags & SIGN)
922 num = (signed short) num;
923 } else {
924 num = va_arg(args, unsigned int);
925 if (flags & SIGN)
926 num = (signed int) num;
927 }
928 str = number(str, end, num, base,
929 field_width, precision, flags);
930 }
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700931 if (size > 0) {
932 if (str < end)
933 *str = '\0';
934 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -0700935 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700936 }
937 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return str-buf;
939}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940EXPORT_SYMBOL(vsnprintf);
941
942/**
943 * vscnprintf - Format a string and place it in a buffer
944 * @buf: The buffer to place the result into
945 * @size: The size of the buffer, including the trailing null space
946 * @fmt: The format string to use
947 * @args: Arguments for the format string
948 *
949 * The return value is the number of characters which have been written into
950 * the @buf not including the trailing '\0'. If @size is <= 0 the function
951 * returns 0.
952 *
953 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800954 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -0700955 *
956 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
959{
960 int i;
961
962 i=vsnprintf(buf,size,fmt,args);
963 return (i >= size) ? (size - 1) : i;
964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965EXPORT_SYMBOL(vscnprintf);
966
967/**
968 * snprintf - Format a string and place it in a buffer
969 * @buf: The buffer to place the result into
970 * @size: The size of the buffer, including the trailing null space
971 * @fmt: The format string to use
972 * @...: Arguments for the format string
973 *
974 * The return value is the number of characters which would be
975 * generated for the given input, excluding the trailing null,
976 * as per ISO C99. If the return is greater than or equal to
977 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -0700978 *
979 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
981int snprintf(char * buf, size_t size, const char *fmt, ...)
982{
983 va_list args;
984 int i;
985
986 va_start(args, fmt);
987 i=vsnprintf(buf,size,fmt,args);
988 va_end(args);
989 return i;
990}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991EXPORT_SYMBOL(snprintf);
992
993/**
994 * scnprintf - Format a string and place it in a buffer
995 * @buf: The buffer to place the result into
996 * @size: The size of the buffer, including the trailing null space
997 * @fmt: The format string to use
998 * @...: Arguments for the format string
999 *
1000 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001001 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
1003
1004int scnprintf(char * buf, size_t size, const char *fmt, ...)
1005{
1006 va_list args;
1007 int i;
1008
1009 va_start(args, fmt);
1010 i = vsnprintf(buf, size, fmt, args);
1011 va_end(args);
1012 return (i >= size) ? (size - 1) : i;
1013}
1014EXPORT_SYMBOL(scnprintf);
1015
1016/**
1017 * vsprintf - Format a string and place it in a buffer
1018 * @buf: The buffer to place the result into
1019 * @fmt: The format string to use
1020 * @args: Arguments for the format string
1021 *
1022 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001023 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * buffer overflows.
1025 *
1026 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001027 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001028 *
1029 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031int vsprintf(char *buf, const char *fmt, va_list args)
1032{
1033 return vsnprintf(buf, INT_MAX, fmt, args);
1034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035EXPORT_SYMBOL(vsprintf);
1036
1037/**
1038 * sprintf - Format a string and place it in a buffer
1039 * @buf: The buffer to place the result into
1040 * @fmt: The format string to use
1041 * @...: Arguments for the format string
1042 *
1043 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001044 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001046 *
1047 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
1049int sprintf(char * buf, const char *fmt, ...)
1050{
1051 va_list args;
1052 int i;
1053
1054 va_start(args, fmt);
1055 i=vsnprintf(buf, INT_MAX, fmt, args);
1056 va_end(args);
1057 return i;
1058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059EXPORT_SYMBOL(sprintf);
1060
1061/**
1062 * vsscanf - Unformat a buffer into a list of arguments
1063 * @buf: input buffer
1064 * @fmt: format of buffer
1065 * @args: arguments
1066 */
1067int vsscanf(const char * buf, const char * fmt, va_list args)
1068{
1069 const char *str = buf;
1070 char *next;
1071 char digit;
1072 int num = 0;
1073 int qualifier;
1074 int base;
1075 int field_width;
1076 int is_sign = 0;
1077
1078 while(*fmt && *str) {
1079 /* skip any white space in format */
1080 /* white space in format matchs any amount of
1081 * white space, including none, in the input.
1082 */
1083 if (isspace(*fmt)) {
1084 while (isspace(*fmt))
1085 ++fmt;
1086 while (isspace(*str))
1087 ++str;
1088 }
1089
1090 /* anything that is not a conversion must match exactly */
1091 if (*fmt != '%' && *fmt) {
1092 if (*fmt++ != *str++)
1093 break;
1094 continue;
1095 }
1096
1097 if (!*fmt)
1098 break;
1099 ++fmt;
1100
1101 /* skip this conversion.
1102 * advance both strings to next white space
1103 */
1104 if (*fmt == '*') {
1105 while (!isspace(*fmt) && *fmt)
1106 fmt++;
1107 while (!isspace(*str) && *str)
1108 str++;
1109 continue;
1110 }
1111
1112 /* get field width */
1113 field_width = -1;
1114 if (isdigit(*fmt))
1115 field_width = skip_atoi(&fmt);
1116
1117 /* get conversion qualifier */
1118 qualifier = -1;
1119 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1120 *fmt == 'Z' || *fmt == 'z') {
1121 qualifier = *fmt++;
1122 if (unlikely(qualifier == *fmt)) {
1123 if (qualifier == 'h') {
1124 qualifier = 'H';
1125 fmt++;
1126 } else if (qualifier == 'l') {
1127 qualifier = 'L';
1128 fmt++;
1129 }
1130 }
1131 }
1132 base = 10;
1133 is_sign = 0;
1134
1135 if (!*fmt || !*str)
1136 break;
1137
1138 switch(*fmt++) {
1139 case 'c':
1140 {
1141 char *s = (char *) va_arg(args,char*);
1142 if (field_width == -1)
1143 field_width = 1;
1144 do {
1145 *s++ = *str++;
1146 } while (--field_width > 0 && *str);
1147 num++;
1148 }
1149 continue;
1150 case 's':
1151 {
1152 char *s = (char *) va_arg(args, char *);
1153 if(field_width == -1)
1154 field_width = INT_MAX;
1155 /* first, skip leading white space in buffer */
1156 while (isspace(*str))
1157 str++;
1158
1159 /* now copy until next white space */
1160 while (*str && !isspace(*str) && field_width--) {
1161 *s++ = *str++;
1162 }
1163 *s = '\0';
1164 num++;
1165 }
1166 continue;
1167 case 'n':
1168 /* return number of characters read so far */
1169 {
1170 int *i = (int *)va_arg(args,int*);
1171 *i = str - buf;
1172 }
1173 continue;
1174 case 'o':
1175 base = 8;
1176 break;
1177 case 'x':
1178 case 'X':
1179 base = 16;
1180 break;
1181 case 'i':
1182 base = 0;
1183 case 'd':
1184 is_sign = 1;
1185 case 'u':
1186 break;
1187 case '%':
1188 /* looking for '%' in str */
1189 if (*str++ != '%')
1190 return num;
1191 continue;
1192 default:
1193 /* invalid format; stop here */
1194 return num;
1195 }
1196
1197 /* have some sort of integer conversion.
1198 * first, skip white space in buffer.
1199 */
1200 while (isspace(*str))
1201 str++;
1202
1203 digit = *str;
1204 if (is_sign && digit == '-')
1205 digit = *(str + 1);
1206
1207 if (!digit
1208 || (base == 16 && !isxdigit(digit))
1209 || (base == 10 && !isdigit(digit))
1210 || (base == 8 && (!isdigit(digit) || digit > '7'))
1211 || (base == 0 && !isdigit(digit)))
1212 break;
1213
1214 switch(qualifier) {
1215 case 'H': /* that's 'hh' in format */
1216 if (is_sign) {
1217 signed char *s = (signed char *) va_arg(args,signed char *);
1218 *s = (signed char) simple_strtol(str,&next,base);
1219 } else {
1220 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1221 *s = (unsigned char) simple_strtoul(str, &next, base);
1222 }
1223 break;
1224 case 'h':
1225 if (is_sign) {
1226 short *s = (short *) va_arg(args,short *);
1227 *s = (short) simple_strtol(str,&next,base);
1228 } else {
1229 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1230 *s = (unsigned short) simple_strtoul(str, &next, base);
1231 }
1232 break;
1233 case 'l':
1234 if (is_sign) {
1235 long *l = (long *) va_arg(args,long *);
1236 *l = simple_strtol(str,&next,base);
1237 } else {
1238 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1239 *l = simple_strtoul(str,&next,base);
1240 }
1241 break;
1242 case 'L':
1243 if (is_sign) {
1244 long long *l = (long long*) va_arg(args,long long *);
1245 *l = simple_strtoll(str,&next,base);
1246 } else {
1247 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1248 *l = simple_strtoull(str,&next,base);
1249 }
1250 break;
1251 case 'Z':
1252 case 'z':
1253 {
1254 size_t *s = (size_t*) va_arg(args,size_t*);
1255 *s = (size_t) simple_strtoul(str,&next,base);
1256 }
1257 break;
1258 default:
1259 if (is_sign) {
1260 int *i = (int *) va_arg(args, int*);
1261 *i = (int) simple_strtol(str,&next,base);
1262 } else {
1263 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1264 *i = (unsigned int) simple_strtoul(str,&next,base);
1265 }
1266 break;
1267 }
1268 num++;
1269
1270 if (!next)
1271 break;
1272 str = next;
1273 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001274
1275 /*
1276 * Now we've come all the way through so either the input string or the
1277 * format ended. In the former case, there can be a %n at the current
1278 * position in the format that needs to be filled.
1279 */
1280 if (*fmt == '%' && *(fmt + 1) == 'n') {
1281 int *p = (int *)va_arg(args, int *);
1282 *p = str - buf;
1283 }
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return num;
1286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287EXPORT_SYMBOL(vsscanf);
1288
1289/**
1290 * sscanf - Unformat a buffer into a list of arguments
1291 * @buf: input buffer
1292 * @fmt: formatting of buffer
1293 * @...: resulting arguments
1294 */
1295int sscanf(const char * buf, const char * fmt, ...)
1296{
1297 va_list args;
1298 int i;
1299
1300 va_start(args,fmt);
1301 i = vsscanf(buf,fmt,args);
1302 va_end(args);
1303 return i;
1304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305EXPORT_SYMBOL(sscanf);