blob: 25f01578c8567ecbd78af49c396967d78a409bc1 [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
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100399enum format_type {
400 FORMAT_TYPE_NONE, /* Just a string part */
401 FORMAT_TYPE_WITDH,
402 FORMAT_TYPE_PRECISION,
403 FORMAT_TYPE_CHAR,
404 FORMAT_TYPE_STR,
405 FORMAT_TYPE_PTR,
406 FORMAT_TYPE_PERCENT_CHAR,
407 FORMAT_TYPE_INVALID,
408 FORMAT_TYPE_LONG_LONG,
409 FORMAT_TYPE_ULONG,
410 FORMAT_TYPE_LONG,
411 FORMAT_TYPE_USHORT,
412 FORMAT_TYPE_SHORT,
413 FORMAT_TYPE_UINT,
414 FORMAT_TYPE_INT,
415 FORMAT_TYPE_NRCHARS,
416 FORMAT_TYPE_SIZE_T,
417 FORMAT_TYPE_PTRDIFF
418};
419
420struct printf_spec {
421 enum format_type type;
422 int flags; /* flags to number() */
423 int field_width; /* width of output field */
424 int base;
425 int precision; /* # of digits/chars */
426 int qualifier;
427};
428
429static char *number(char *buf, char *end, unsigned long long num,
430 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100432 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
433 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
434
435 char tmp[66];
436 char sign;
437 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100438 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int i;
440
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100441 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
442 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100443 locase = (spec.flags & SMALL);
444 if (spec.flags & LEFT)
445 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100447 if (spec.flags & SIGN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if ((signed long long) num < 0) {
449 sign = '-';
450 num = - (signed long long) num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100451 spec.field_width--;
452 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100454 spec.field_width--;
455 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100457 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100461 spec.field_width--;
462 if (spec.base == 16)
463 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700465
466 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 i = 0;
468 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700469 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700470 /* Generic code, for any base:
471 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100472 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700473 } while (num != 0);
474 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100475 else if (spec.base != 10) { /* 8 or 16 */
476 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700477 int shift = 3;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100478 if (spec.base == 16) shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100480 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700481 num >>= shift;
482 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700483 } else { /* base 10 */
484 i = put_dec(tmp, num) - tmp;
485 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700486
487 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100488 if (i > spec.precision)
489 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100491 spec.field_width -= spec.precision;
492 if (!(spec.flags & (ZEROPAD+LEFT))) {
493 while(--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700494 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 *buf = ' ';
496 ++buf;
497 }
498 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700499 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700501 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *buf = sign;
503 ++buf;
504 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700505 /* "0x" / "0" prefix */
506 if (need_pfx) {
507 if (buf < end)
508 *buf = '0';
509 ++buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100510 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700511 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100512 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ++buf;
514 }
515 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700516 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100517 if (!(spec.flags & LEFT)) {
518 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
519 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700520 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *buf = c;
522 ++buf;
523 }
524 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700525 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100526 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700527 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *buf = '0';
529 ++buf;
530 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700531 /* actual digits of result */
532 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700533 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *buf = tmp[i];
535 ++buf;
536 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700537 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100538 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700539 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *buf = ' ';
541 ++buf;
542 }
543 return buf;
544}
545
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100546static char *string(char *buf, char *end, char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700547{
548 int len, i;
549
550 if ((unsigned long)s < PAGE_SIZE)
551 s = "<NULL>";
552
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100553 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700554
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100555 if (!(spec.flags & LEFT)) {
556 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700557 if (buf < end)
558 *buf = ' ';
559 ++buf;
560 }
561 }
562 for (i = 0; i < len; ++i) {
563 if (buf < end)
564 *buf = *s;
565 ++buf; ++s;
566 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100567 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700568 if (buf < end)
569 *buf = ' ';
570 ++buf;
571 }
572 return buf;
573}
574
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100575static char *symbol_string(char *buf, char *end, void *ptr,
576 struct printf_spec spec)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700577{
578 unsigned long value = (unsigned long) ptr;
579#ifdef CONFIG_KALLSYMS
580 char sym[KSYM_SYMBOL_LEN];
581 sprint_symbol(sym, value);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100582 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700583#else
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100584 spec.field_width = 2*sizeof(void *);
585 spec.flags |= SPECIAL | SMALL | ZEROPAD;
586 spec.base = 16;
587 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700588#endif
589}
590
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100591static char *resource_string(char *buf, char *end, struct resource *res,
592 struct printf_spec spec)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100593{
594#ifndef IO_RSRC_PRINTK_SIZE
595#define IO_RSRC_PRINTK_SIZE 4
596#endif
597
598#ifndef MEM_RSRC_PRINTK_SIZE
599#define MEM_RSRC_PRINTK_SIZE 8
600#endif
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100601 struct printf_spec num_spec = {
602 .base = 16,
603 .precision = -1,
604 .flags = SPECIAL | SMALL | ZEROPAD,
605 };
Linus Torvalds332d2e72008-10-20 15:07:34 +1100606 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
607 char sym[4*sizeof(resource_size_t) + 8];
608 char *p = sym, *pend = sym + sizeof(sym);
609 int size = -1;
610
611 if (res->flags & IORESOURCE_IO)
612 size = IO_RSRC_PRINTK_SIZE;
613 else if (res->flags & IORESOURCE_MEM)
614 size = MEM_RSRC_PRINTK_SIZE;
615
616 *p++ = '[';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100617 num_spec.field_width = size;
618 p = number(p, pend, res->start, num_spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100619 *p++ = '-';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100620 p = number(p, pend, res->end, num_spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100621 *p++ = ']';
622 *p = 0;
623
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100624 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100625}
626
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100627static char *mac_address_string(char *buf, char *end, u8 *addr,
628 struct printf_spec spec)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700629{
630 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
631 char *p = mac_addr;
632 int i;
633
634 for (i = 0; i < 6; i++) {
635 p = pack_hex_byte(p, addr[i]);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100636 if (!(spec.flags & SPECIAL) && i != 5)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700637 *p++ = ':';
638 }
639 *p = '\0';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100640 spec.flags &= ~SPECIAL;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700641
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100642 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700643}
644
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100645static char *ip6_addr_string(char *buf, char *end, u8 *addr,
646 struct printf_spec spec)
Harvey Harrison689afa72008-10-28 16:04:44 -0700647{
648 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
649 char *p = ip6_addr;
650 int i;
651
652 for (i = 0; i < 8; i++) {
653 p = pack_hex_byte(p, addr[2 * i]);
654 p = pack_hex_byte(p, addr[2 * i + 1]);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100655 if (!(spec.flags & SPECIAL) && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700656 *p++ = ':';
657 }
658 *p = '\0';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100659 spec.flags &= ~SPECIAL;
Harvey Harrison689afa72008-10-28 16:04:44 -0700660
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100661 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700662}
663
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100664static char *ip4_addr_string(char *buf, char *end, u8 *addr,
665 struct printf_spec spec)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700666{
667 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800668 char temp[3]; /* hold each IP quad in reverse order */
Harvey Harrison4aa99602008-10-29 12:49:58 -0700669 char *p = ip4_addr;
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800670 int i, digits;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700671
672 for (i = 0; i < 4; i++) {
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800673 digits = put_dec_trunc(temp, addr[i]) - temp;
674 /* reverse the digits in the quad */
675 while (digits--)
676 *p++ = temp[digits];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700677 if (i != 3)
678 *p++ = '.';
679 }
680 *p = '\0';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100681 spec.flags &= ~SPECIAL;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700682
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100683 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700684}
685
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700686/*
687 * Show a '%p' thing. A kernel extension is that the '%p' is followed
688 * by an extra set of alphanumeric characters that are extended format
689 * specifiers.
690 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100691 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700692 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100693 * - 'F' For symbolic function descriptor pointers
694 * - 'S' For symbolic direct pointers
695 * - 'R' For a struct resource pointer, it prints the range of
696 * addresses (not the name nor the flags)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700697 * - 'M' For a 6-byte MAC address, it prints the address in the
698 * usual colon-separated hex notation
Harvey Harrison4aa99602008-10-29 12:49:58 -0700699 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
700 * decimal for v4 and colon separated network-order 16 bit hex for v6)
701 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
Harvey Harrison6b9a1062008-10-29 12:53:10 -0700702 * currently the same
Linus Torvalds332d2e72008-10-20 15:07:34 +1100703 *
704 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
705 * function pointers are really function descriptors, which contain a
706 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700707 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100708static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
709 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700710{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800711 if (!ptr)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100712 return string(buf, end, "(null)", spec);
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800713
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700714 switch (*fmt) {
715 case 'F':
716 ptr = dereference_function_descriptor(ptr);
717 /* Fallthrough */
718 case 'S':
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100719 return symbol_string(buf, end, ptr, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100720 case 'R':
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100721 return resource_string(buf, end, ptr, spec);
Harvey Harrison411c41e2008-11-25 00:40:37 -0800722 case 'm':
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100723 spec.flags |= SPECIAL;
Harvey Harrison411c41e2008-11-25 00:40:37 -0800724 /* Fallthrough */
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700725 case 'M':
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100726 return mac_address_string(buf, end, ptr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700727 case 'i':
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100728 spec.flags |= SPECIAL;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700729 /* Fallthrough */
730 case 'I':
731 if (fmt[1] == '6')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100732 return ip6_addr_string(buf, end, ptr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700733 if (fmt[1] == '4')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100734 return ip4_addr_string(buf, end, ptr, spec);
735 spec.flags &= ~SPECIAL;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700736 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700737 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100738 spec.flags |= SMALL;
739 if (spec.field_width == -1) {
740 spec.field_width = 2*sizeof(void *);
741 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700742 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100743 spec.base = 16;
744
745 return number(buf, end, (unsigned long) ptr, spec);
746}
747
748/*
749 * Helper function to decode printf style format.
750 * Each call decode a token from the format and return the
751 * number of characters read (or likely the delta where it wants
752 * to go on the next call).
753 * The decoded token is returned through the parameters
754 *
755 * 'h', 'l', or 'L' for integer fields
756 * 'z' support added 23/7/1999 S.H.
757 * 'z' changed to 'Z' --davidm 1/25/99
758 * 't' added for ptrdiff_t
759 *
760 * @fmt: the format string
761 * @type of the token returned
762 * @flags: various flags such as +, -, # tokens..
763 * @field_width: overwritten width
764 * @base: base of the number (octal, hex, ...)
765 * @precision: precision of a number
766 * @qualifier: qualifier of a number (long, size_t, ...)
767 */
768static int format_decode(const char *fmt, struct printf_spec *spec)
769{
770 const char *start = fmt;
771 bool sign = false;
772
773 /* we finished early by reading the field width */
774 if (spec->type == FORMAT_TYPE_WITDH) {
775 if (spec->field_width < 0) {
776 spec->field_width = -spec->field_width;
777 spec->flags |= LEFT;
778 }
779 spec->type = FORMAT_TYPE_NONE;
780 goto precision;
781 }
782
783 /* we finished early by reading the precision */
784 if (spec->type == FORMAT_TYPE_PRECISION) {
785 if (spec->precision < 0)
786 spec->precision = 0;
787
788 spec->type = FORMAT_TYPE_NONE;
789 goto qualifier;
790 }
791
792 /* By default */
793 spec->type = FORMAT_TYPE_NONE;
794
795 for (; *fmt ; ++fmt) {
796 if (*fmt == '%')
797 break;
798 }
799
800 /* Return the current non-format string */
801 if (fmt != start || !*fmt)
802 return fmt - start;
803
804 /* Process flags */
805 spec->flags = 0;
806
807 while (1) { /* this also skips first '%' */
808 bool found = true;
809
810 ++fmt;
811
812 switch (*fmt) {
813 case '-': spec->flags |= LEFT; break;
814 case '+': spec->flags |= PLUS; break;
815 case ' ': spec->flags |= SPACE; break;
816 case '#': spec->flags |= SPECIAL; break;
817 case '0': spec->flags |= ZEROPAD; break;
818 default: found = false;
819 }
820
821 if (!found)
822 break;
823 }
824
825 /* get field width */
826 spec->field_width = -1;
827
828 if (isdigit(*fmt))
829 spec->field_width = skip_atoi(&fmt);
830 else if (*fmt == '*') {
831 /* it's the next argument */
832 spec->type = FORMAT_TYPE_WITDH;
833 return ++fmt - start;
834 }
835
836precision:
837 /* get the precision */
838 spec->precision = -1;
839 if (*fmt == '.') {
840 ++fmt;
841 if (isdigit(*fmt)) {
842 spec->precision = skip_atoi(&fmt);
843 if (spec->precision < 0)
844 spec->precision = 0;
845 } else if (*fmt == '*') {
846 /* it's the next argument */
847 spec->type = FORMAT_TYPE_WITDH;
848 return ++fmt - start;
849 }
850 }
851
852qualifier:
853 /* get the conversion qualifier */
854 spec->qualifier = -1;
855 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
856 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
857 spec->qualifier = *fmt;
858 ++fmt;
859 if (spec->qualifier == 'l' && *fmt == 'l') {
860 spec->qualifier = 'L';
861 ++fmt;
862 }
863 }
864
865 /* default base */
866 spec->base = 10;
867 switch (*fmt) {
868 case 'c':
869 spec->type = FORMAT_TYPE_CHAR;
870 return ++fmt - start;
871
872 case 's':
873 spec->type = FORMAT_TYPE_STR;
874 return ++fmt - start;
875
876 case 'p':
877 spec->type = FORMAT_TYPE_PTR;
878 return fmt - start;
879 /* skip alnum */
880
881 case 'n':
882 spec->type = FORMAT_TYPE_NRCHARS;
883 return ++fmt - start;
884
885 case '%':
886 spec->type = FORMAT_TYPE_PERCENT_CHAR;
887 return ++fmt - start;
888
889 /* integer number formats - set up the flags and "break" */
890 case 'o':
891 spec->base = 8;
892 break;
893
894 case 'x':
895 spec->flags |= SMALL;
896
897 case 'X':
898 spec->base = 16;
899 break;
900
901 case 'd':
902 case 'i':
903 sign = true;
904 case 'u':
905 break;
906
907 default:
908 spec->type = FORMAT_TYPE_INVALID;
909 return fmt - start;
910 }
911
912 if (spec->qualifier == 'L')
913 spec->type = FORMAT_TYPE_LONG_LONG;
914 else if (spec->qualifier == 'l') {
915 if (sign)
916 spec->type = FORMAT_TYPE_LONG;
917 else
918 spec->type = FORMAT_TYPE_ULONG;
919 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
920 spec->type = FORMAT_TYPE_SIZE_T;
921 } else if (spec->qualifier == 't') {
922 spec->type = FORMAT_TYPE_PTRDIFF;
923 } else if (spec->qualifier == 'h') {
924 if (sign)
925 spec->type = FORMAT_TYPE_SHORT;
926 else
927 spec->type = FORMAT_TYPE_USHORT;
928 } else {
929 if (sign)
930 spec->type = FORMAT_TYPE_INT;
931 else
932 spec->type = FORMAT_TYPE_UINT;
933 }
934
935 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/**
939 * vsnprintf - 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 *
Andi Kleen20036fd2008-10-15 22:02:02 -0700945 * This function follows C99 vsnprintf, but has some extensions:
946 * %pS output the name of a text symbol
947 * %pF output the name of a function pointer
Linus Torvalds332d2e72008-10-20 15:07:34 +1100948 * %pR output the address range in a struct resource
Andi Kleen20036fd2008-10-15 22:02:02 -0700949 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * The return value is the number of characters which would
951 * be generated for the given input, excluding the trailing
952 * '\0', as per ISO C99. If you want to have the exact
953 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800954 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * return is greater than or equal to @size, the resulting
956 * string is truncated.
957 *
958 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800959 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
961int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
962{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned long long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 char *str, *end, c;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100965 int read;
966 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700968 /* Reject out-of-range values early. Large positive sizes are
969 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (unlikely((int) size < 0)) {
971 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700972 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 WARN_ON(warn);
974 warn = 0;
975 return 0;
976 }
977
978 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700979 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700981 /* Make sure end is always >= buf */
982 if (end < buf) {
983 end = ((void *)-1);
984 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100987 while (*fmt) {
988 const char *old_fmt = fmt;
989
990 read = format_decode(fmt, &spec);
991
992 fmt += read;
993
994 switch (spec.type) {
995 case FORMAT_TYPE_NONE: {
996 int copy = read;
997 if (str < end) {
998 if (copy > end - str)
999 copy = end - str;
1000 memcpy(str, old_fmt, copy);
1001 }
1002 str += read;
1003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001006 case FORMAT_TYPE_WITDH:
1007 spec.field_width = va_arg(args, int);
1008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001010 case FORMAT_TYPE_PRECISION:
1011 spec.precision = va_arg(args, int);
1012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001014 case FORMAT_TYPE_CHAR:
1015 if (!(spec.flags & LEFT)) {
1016 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001017 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *str = ' ';
1019 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001022 }
1023 c = (unsigned char) va_arg(args, int);
1024 if (str < end)
1025 *str = c;
1026 ++str;
1027 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001028 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001029 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001031 }
1032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001034 case FORMAT_TYPE_STR:
1035 str = string(str, end, va_arg(args, char *), spec);
1036 break;
1037
1038 case FORMAT_TYPE_PTR:
1039 str = pointer(fmt+1, str, end, va_arg(args, void *),
1040 spec);
1041 while (isalnum(*fmt))
1042 fmt++;
1043 break;
1044
1045 case FORMAT_TYPE_PERCENT_CHAR:
1046 if (str < end)
1047 *str = '%';
1048 ++str;
1049 break;
1050
1051 case FORMAT_TYPE_INVALID:
1052 if (str < end)
1053 *str = '%';
1054 ++str;
1055 if (*fmt) {
1056 if (str < end)
1057 *str = *fmt;
1058 ++str;
1059 } else {
1060 --fmt;
1061 }
1062 break;
1063
1064 case FORMAT_TYPE_NRCHARS: {
1065 int qualifier = spec.qualifier;
1066
1067 if (qualifier == 'l') {
1068 long *ip = va_arg(args, long *);
1069 *ip = (str - buf);
1070 } else if (qualifier == 'Z' ||
1071 qualifier == 'z') {
1072 size_t *ip = va_arg(args, size_t *);
1073 *ip = (str - buf);
1074 } else {
1075 int *ip = va_arg(args, int *);
1076 *ip = (str - buf);
1077 }
1078 break;
1079 }
1080
1081 default:
1082 switch (spec.type) {
1083 case FORMAT_TYPE_LONG_LONG:
1084 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001086 case FORMAT_TYPE_ULONG:
1087 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001089 case FORMAT_TYPE_LONG:
1090 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001092 case FORMAT_TYPE_SIZE_T:
1093 num = va_arg(args, size_t);
1094 break;
1095 case FORMAT_TYPE_PTRDIFF:
1096 num = va_arg(args, ptrdiff_t);
1097 break;
1098 case FORMAT_TYPE_USHORT:
1099 num = (unsigned short) va_arg(args, int);
1100 break;
1101 case FORMAT_TYPE_SHORT:
1102 num = (short) va_arg(args, int);
1103 break;
1104 case FORMAT_TYPE_UINT:
1105 num = va_arg(args, unsigned int);
1106 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001108 num = va_arg(args, unsigned int);
1109 }
1110
1111 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001114
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001115 if (size > 0) {
1116 if (str < end)
1117 *str = '\0';
1118 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001119 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001120 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001121
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001122 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126EXPORT_SYMBOL(vsnprintf);
1127
1128/**
1129 * vscnprintf - Format a string and place it in a buffer
1130 * @buf: The buffer to place the result into
1131 * @size: The size of the buffer, including the trailing null space
1132 * @fmt: The format string to use
1133 * @args: Arguments for the format string
1134 *
1135 * The return value is the number of characters which have been written into
1136 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1137 * returns 0.
1138 *
1139 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001140 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001141 *
1142 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
1144int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1145{
1146 int i;
1147
1148 i=vsnprintf(buf,size,fmt,args);
1149 return (i >= size) ? (size - 1) : i;
1150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151EXPORT_SYMBOL(vscnprintf);
1152
1153/**
1154 * snprintf - Format a string and place it in a buffer
1155 * @buf: The buffer to place the result into
1156 * @size: The size of the buffer, including the trailing null space
1157 * @fmt: The format string to use
1158 * @...: Arguments for the format string
1159 *
1160 * The return value is the number of characters which would be
1161 * generated for the given input, excluding the trailing null,
1162 * as per ISO C99. If the return is greater than or equal to
1163 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001164 *
1165 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
1167int snprintf(char * buf, size_t size, const char *fmt, ...)
1168{
1169 va_list args;
1170 int i;
1171
1172 va_start(args, fmt);
1173 i=vsnprintf(buf,size,fmt,args);
1174 va_end(args);
1175 return i;
1176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177EXPORT_SYMBOL(snprintf);
1178
1179/**
1180 * scnprintf - Format a string and place it in a buffer
1181 * @buf: The buffer to place the result into
1182 * @size: The size of the buffer, including the trailing null space
1183 * @fmt: The format string to use
1184 * @...: Arguments for the format string
1185 *
1186 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001187 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
1189
1190int scnprintf(char * buf, size_t size, const char *fmt, ...)
1191{
1192 va_list args;
1193 int i;
1194
1195 va_start(args, fmt);
1196 i = vsnprintf(buf, size, fmt, args);
1197 va_end(args);
1198 return (i >= size) ? (size - 1) : i;
1199}
1200EXPORT_SYMBOL(scnprintf);
1201
1202/**
1203 * vsprintf - Format a string and place it in a buffer
1204 * @buf: The buffer to place the result into
1205 * @fmt: The format string to use
1206 * @args: Arguments for the format string
1207 *
1208 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001209 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * buffer overflows.
1211 *
1212 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001213 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001214 *
1215 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
1217int vsprintf(char *buf, const char *fmt, va_list args)
1218{
1219 return vsnprintf(buf, INT_MAX, fmt, args);
1220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221EXPORT_SYMBOL(vsprintf);
1222
1223/**
1224 * sprintf - Format a string and place it in a buffer
1225 * @buf: The buffer to place the result into
1226 * @fmt: The format string to use
1227 * @...: Arguments for the format string
1228 *
1229 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001230 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001232 *
1233 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
1235int sprintf(char * buf, const char *fmt, ...)
1236{
1237 va_list args;
1238 int i;
1239
1240 va_start(args, fmt);
1241 i=vsnprintf(buf, INT_MAX, fmt, args);
1242 va_end(args);
1243 return i;
1244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245EXPORT_SYMBOL(sprintf);
1246
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001247#ifdef CONFIG_BINARY_PRINTF
1248/*
1249 * bprintf service:
1250 * vbin_printf() - VA arguments to binary data
1251 * bstr_printf() - Binary data to text string
1252 */
1253
1254/**
1255 * vbin_printf - Parse a format string and place args' binary value in a buffer
1256 * @bin_buf: The buffer to place args' binary value
1257 * @size: The size of the buffer(by words(32bits), not characters)
1258 * @fmt: The format string to use
1259 * @args: Arguments for the format string
1260 *
1261 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1262 * is skiped.
1263 *
1264 * The return value is the number of words(32bits) which would be generated for
1265 * the given input.
1266 *
1267 * NOTE:
1268 * If the return value is greater than @size, the resulting bin_buf is NOT
1269 * valid for bstr_printf().
1270 */
1271int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1272{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001273 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001274 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001275 int read;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001276
1277 str = (char *)bin_buf;
1278 end = (char *)(bin_buf + size);
1279
1280#define save_arg(type) \
1281do { \
1282 if (sizeof(type) == 8) { \
1283 unsigned long long value; \
1284 str = PTR_ALIGN(str, sizeof(u32)); \
1285 value = va_arg(args, unsigned long long); \
1286 if (str + sizeof(type) <= end) { \
1287 *(u32 *)str = *(u32 *)&value; \
1288 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1289 } \
1290 } else { \
1291 unsigned long value; \
1292 str = PTR_ALIGN(str, sizeof(type)); \
1293 value = va_arg(args, int); \
1294 if (str + sizeof(type) <= end) \
1295 *(typeof(type) *)str = (type)value; \
1296 } \
1297 str += sizeof(type); \
1298} while (0)
1299
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001300
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001301 while (*fmt) {
1302 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001303
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001304 fmt += read;
1305
1306 switch (spec.type) {
1307 case FORMAT_TYPE_NONE:
1308 break;
1309
1310 case FORMAT_TYPE_WITDH:
1311 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001312 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001313 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001314
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001315 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001316 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001317 break;
1318
1319 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001320 const char *save_str = va_arg(args, char *);
1321 size_t len;
1322 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1323 || (unsigned long)save_str < PAGE_SIZE)
1324 save_str = "<NULL>";
1325 len = strlen(save_str);
1326 if (str + len + 1 < end)
1327 memcpy(str, save_str, len + 1);
1328 str += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001329 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001330 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001331
1332 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001333 save_arg(void *);
1334 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001335 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001336 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001337 break;
1338
1339 case FORMAT_TYPE_PERCENT_CHAR:
1340 break;
1341
1342 case FORMAT_TYPE_INVALID:
1343 if (!*fmt)
1344 --fmt;
1345 break;
1346
1347 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001348 /* skip %n 's argument */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001349 int qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001350 void *skip_arg;
1351 if (qualifier == 'l')
1352 skip_arg = va_arg(args, long *);
1353 else if (qualifier == 'Z' || qualifier == 'z')
1354 skip_arg = va_arg(args, size_t *);
1355 else
1356 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001357 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001358 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001359
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001360 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001361 switch (spec.type) {
1362
1363 case FORMAT_TYPE_LONG_LONG:
1364 save_arg(long long);
1365 break;
1366 case FORMAT_TYPE_ULONG:
1367 case FORMAT_TYPE_LONG:
1368 save_arg(unsigned long);
1369 break;
1370 case FORMAT_TYPE_SIZE_T:
1371 save_arg(size_t);
1372 break;
1373 case FORMAT_TYPE_PTRDIFF:
1374 save_arg(ptrdiff_t);
1375 break;
1376 case FORMAT_TYPE_USHORT:
1377 case FORMAT_TYPE_SHORT:
1378 save_arg(short);
1379 break;
1380 default:
1381 save_arg(int);
1382 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001383 }
1384 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001385 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001386
1387#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001388}
1389EXPORT_SYMBOL_GPL(vbin_printf);
1390
1391/**
1392 * bstr_printf - Format a string from binary arguments and place it in a buffer
1393 * @buf: The buffer to place the result into
1394 * @size: The size of the buffer, including the trailing null space
1395 * @fmt: The format string to use
1396 * @bin_buf: Binary arguments for the format string
1397 *
1398 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1399 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1400 * a binary buffer that generated by vbin_printf.
1401 *
1402 * The format follows C99 vsnprintf, but has some extensions:
1403 * %pS output the name of a text symbol
1404 * %pF output the name of a function pointer
1405 * %pR output the address range in a struct resource
1406 * %n is ignored
1407 *
1408 * The return value is the number of characters which would
1409 * be generated for the given input, excluding the trailing
1410 * '\0', as per ISO C99. If you want to have the exact
1411 * number of characters written into @buf as return value
1412 * (not including the trailing '\0'), use vscnprintf(). If the
1413 * return is greater than or equal to @size, the resulting
1414 * string is truncated.
1415 */
1416int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1417{
1418 unsigned long long num;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001419 char *str, *end, c;
1420 const char *args = (const char *)bin_buf;
1421
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001422 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001423
1424 if (unlikely((int) size < 0)) {
1425 /* There can be only one.. */
1426 static char warn = 1;
1427 WARN_ON(warn);
1428 warn = 0;
1429 return 0;
1430 }
1431
1432 str = buf;
1433 end = buf + size;
1434
1435#define get_arg(type) \
1436({ \
1437 typeof(type) value; \
1438 if (sizeof(type) == 8) { \
1439 args = PTR_ALIGN(args, sizeof(u32)); \
1440 *(u32 *)&value = *(u32 *)args; \
1441 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1442 } else { \
1443 args = PTR_ALIGN(args, sizeof(type)); \
1444 value = *(typeof(type) *)args; \
1445 } \
1446 args += sizeof(type); \
1447 value; \
1448})
1449
1450 /* Make sure end is always >= buf */
1451 if (end < buf) {
1452 end = ((void *)-1);
1453 size = end - buf;
1454 }
1455
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001456 while (*fmt) {
1457 int read;
1458 const char *old_fmt = fmt;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001459
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001460 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001461
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001462 fmt += read;
1463
1464 switch (spec.type) {
1465 case FORMAT_TYPE_NONE: {
1466 int copy = read;
1467 if (str < end) {
1468 if (copy > end - str)
1469 copy = end - str;
1470 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001471 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001472 str += read;
1473 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001474 }
1475
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001476 case FORMAT_TYPE_WITDH:
1477 spec.field_width = get_arg(int);
1478 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001479
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001480 case FORMAT_TYPE_PRECISION:
1481 spec.precision = get_arg(int);
1482 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001483
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001484 case FORMAT_TYPE_CHAR:
1485 if (!(spec.flags & LEFT)) {
1486 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001487 if (str < end)
1488 *str = ' ';
1489 ++str;
1490 }
1491 }
1492 c = (unsigned char) get_arg(char);
1493 if (str < end)
1494 *str = c;
1495 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001496 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001497 if (str < end)
1498 *str = ' ';
1499 ++str;
1500 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001501 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001502
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001503 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001504 const char *str_arg = args;
1505 size_t len = strlen(str_arg);
1506 args += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001507 str = string(str, end, (char *)str_arg, spec);
1508 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001509 }
1510
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001511 case FORMAT_TYPE_PTR:
1512 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1513 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001514 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001515 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001516
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001517 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001518 if (str < end)
1519 *str = '%';
1520 ++str;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001521 break;
1522
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001523 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001524 if (str < end)
1525 *str = '%';
1526 ++str;
1527 if (*fmt) {
1528 if (str < end)
1529 *str = *fmt;
1530 ++str;
1531 } else {
1532 --fmt;
1533 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001534 break;
1535
1536 case FORMAT_TYPE_NRCHARS:
1537 /* skip */
1538 break;
1539
1540 default:
1541 switch (spec.type) {
1542
1543 case FORMAT_TYPE_LONG_LONG:
1544 num = get_arg(long long);
1545 break;
1546 case FORMAT_TYPE_ULONG:
1547 num = get_arg(unsigned long);
1548 break;
1549 case FORMAT_TYPE_LONG:
1550 num = get_arg(unsigned long);
1551 break;
1552 case FORMAT_TYPE_SIZE_T:
1553 num = get_arg(size_t);
1554 break;
1555 case FORMAT_TYPE_PTRDIFF:
1556 num = get_arg(ptrdiff_t);
1557 break;
1558 case FORMAT_TYPE_USHORT:
1559 num = get_arg(unsigned short);
1560 break;
1561 case FORMAT_TYPE_SHORT:
1562 num = get_arg(short);
1563 break;
1564 case FORMAT_TYPE_UINT:
1565 num = get_arg(unsigned int);
1566 break;
1567 default:
1568 num = get_arg(int);
1569 }
1570
1571 str = number(str, end, num, spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001572 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001573 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001574
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001575 if (size > 0) {
1576 if (str < end)
1577 *str = '\0';
1578 else
1579 end[-1] = '\0';
1580 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001581
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001582#undef get_arg
1583
1584 /* the trailing null byte doesn't count towards the total */
1585 return str - buf;
1586}
1587EXPORT_SYMBOL_GPL(bstr_printf);
1588
1589/**
1590 * bprintf - Parse a format string and place args' binary value in a buffer
1591 * @bin_buf: The buffer to place args' binary value
1592 * @size: The size of the buffer(by words(32bits), not characters)
1593 * @fmt: The format string to use
1594 * @...: Arguments for the format string
1595 *
1596 * The function returns the number of words(u32) written
1597 * into @bin_buf.
1598 */
1599int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1600{
1601 va_list args;
1602 int ret;
1603
1604 va_start(args, fmt);
1605 ret = vbin_printf(bin_buf, size, fmt, args);
1606 va_end(args);
1607 return ret;
1608}
1609EXPORT_SYMBOL_GPL(bprintf);
1610
1611#endif /* CONFIG_BINARY_PRINTF */
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/**
1614 * vsscanf - Unformat a buffer into a list of arguments
1615 * @buf: input buffer
1616 * @fmt: format of buffer
1617 * @args: arguments
1618 */
1619int vsscanf(const char * buf, const char * fmt, va_list args)
1620{
1621 const char *str = buf;
1622 char *next;
1623 char digit;
1624 int num = 0;
1625 int qualifier;
1626 int base;
1627 int field_width;
1628 int is_sign = 0;
1629
1630 while(*fmt && *str) {
1631 /* skip any white space in format */
1632 /* white space in format matchs any amount of
1633 * white space, including none, in the input.
1634 */
1635 if (isspace(*fmt)) {
1636 while (isspace(*fmt))
1637 ++fmt;
1638 while (isspace(*str))
1639 ++str;
1640 }
1641
1642 /* anything that is not a conversion must match exactly */
1643 if (*fmt != '%' && *fmt) {
1644 if (*fmt++ != *str++)
1645 break;
1646 continue;
1647 }
1648
1649 if (!*fmt)
1650 break;
1651 ++fmt;
1652
1653 /* skip this conversion.
1654 * advance both strings to next white space
1655 */
1656 if (*fmt == '*') {
1657 while (!isspace(*fmt) && *fmt)
1658 fmt++;
1659 while (!isspace(*str) && *str)
1660 str++;
1661 continue;
1662 }
1663
1664 /* get field width */
1665 field_width = -1;
1666 if (isdigit(*fmt))
1667 field_width = skip_atoi(&fmt);
1668
1669 /* get conversion qualifier */
1670 qualifier = -1;
1671 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1672 *fmt == 'Z' || *fmt == 'z') {
1673 qualifier = *fmt++;
1674 if (unlikely(qualifier == *fmt)) {
1675 if (qualifier == 'h') {
1676 qualifier = 'H';
1677 fmt++;
1678 } else if (qualifier == 'l') {
1679 qualifier = 'L';
1680 fmt++;
1681 }
1682 }
1683 }
1684 base = 10;
1685 is_sign = 0;
1686
1687 if (!*fmt || !*str)
1688 break;
1689
1690 switch(*fmt++) {
1691 case 'c':
1692 {
1693 char *s = (char *) va_arg(args,char*);
1694 if (field_width == -1)
1695 field_width = 1;
1696 do {
1697 *s++ = *str++;
1698 } while (--field_width > 0 && *str);
1699 num++;
1700 }
1701 continue;
1702 case 's':
1703 {
1704 char *s = (char *) va_arg(args, char *);
1705 if(field_width == -1)
1706 field_width = INT_MAX;
1707 /* first, skip leading white space in buffer */
1708 while (isspace(*str))
1709 str++;
1710
1711 /* now copy until next white space */
1712 while (*str && !isspace(*str) && field_width--) {
1713 *s++ = *str++;
1714 }
1715 *s = '\0';
1716 num++;
1717 }
1718 continue;
1719 case 'n':
1720 /* return number of characters read so far */
1721 {
1722 int *i = (int *)va_arg(args,int*);
1723 *i = str - buf;
1724 }
1725 continue;
1726 case 'o':
1727 base = 8;
1728 break;
1729 case 'x':
1730 case 'X':
1731 base = 16;
1732 break;
1733 case 'i':
1734 base = 0;
1735 case 'd':
1736 is_sign = 1;
1737 case 'u':
1738 break;
1739 case '%':
1740 /* looking for '%' in str */
1741 if (*str++ != '%')
1742 return num;
1743 continue;
1744 default:
1745 /* invalid format; stop here */
1746 return num;
1747 }
1748
1749 /* have some sort of integer conversion.
1750 * first, skip white space in buffer.
1751 */
1752 while (isspace(*str))
1753 str++;
1754
1755 digit = *str;
1756 if (is_sign && digit == '-')
1757 digit = *(str + 1);
1758
1759 if (!digit
1760 || (base == 16 && !isxdigit(digit))
1761 || (base == 10 && !isdigit(digit))
1762 || (base == 8 && (!isdigit(digit) || digit > '7'))
1763 || (base == 0 && !isdigit(digit)))
1764 break;
1765
1766 switch(qualifier) {
1767 case 'H': /* that's 'hh' in format */
1768 if (is_sign) {
1769 signed char *s = (signed char *) va_arg(args,signed char *);
1770 *s = (signed char) simple_strtol(str,&next,base);
1771 } else {
1772 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1773 *s = (unsigned char) simple_strtoul(str, &next, base);
1774 }
1775 break;
1776 case 'h':
1777 if (is_sign) {
1778 short *s = (short *) va_arg(args,short *);
1779 *s = (short) simple_strtol(str,&next,base);
1780 } else {
1781 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1782 *s = (unsigned short) simple_strtoul(str, &next, base);
1783 }
1784 break;
1785 case 'l':
1786 if (is_sign) {
1787 long *l = (long *) va_arg(args,long *);
1788 *l = simple_strtol(str,&next,base);
1789 } else {
1790 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1791 *l = simple_strtoul(str,&next,base);
1792 }
1793 break;
1794 case 'L':
1795 if (is_sign) {
1796 long long *l = (long long*) va_arg(args,long long *);
1797 *l = simple_strtoll(str,&next,base);
1798 } else {
1799 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1800 *l = simple_strtoull(str,&next,base);
1801 }
1802 break;
1803 case 'Z':
1804 case 'z':
1805 {
1806 size_t *s = (size_t*) va_arg(args,size_t*);
1807 *s = (size_t) simple_strtoul(str,&next,base);
1808 }
1809 break;
1810 default:
1811 if (is_sign) {
1812 int *i = (int *) va_arg(args, int*);
1813 *i = (int) simple_strtol(str,&next,base);
1814 } else {
1815 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1816 *i = (unsigned int) simple_strtoul(str,&next,base);
1817 }
1818 break;
1819 }
1820 num++;
1821
1822 if (!next)
1823 break;
1824 str = next;
1825 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001826
1827 /*
1828 * Now we've come all the way through so either the input string or the
1829 * format ended. In the former case, there can be a %n at the current
1830 * position in the format that needs to be filled.
1831 */
1832 if (*fmt == '%' && *(fmt + 1) == 'n') {
1833 int *p = (int *)va_arg(args, int *);
1834 *p = str - buf;
1835 }
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return num;
1838}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839EXPORT_SYMBOL(vsscanf);
1840
1841/**
1842 * sscanf - Unformat a buffer into a list of arguments
1843 * @buf: input buffer
1844 * @fmt: formatting of buffer
1845 * @...: resulting arguments
1846 */
1847int sscanf(const char * buf, const char * fmt, ...)
1848{
1849 va_list args;
1850 int i;
1851
1852 va_start(args,fmt);
1853 i = vsscanf(buf,fmt,args);
1854 va_end(args);
1855 return i;
1856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857EXPORT_SYMBOL(sscanf);