blob: 756ccafa9cec04ada5c940f7031e6dd56aeb7837 [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 Weisbeckerfef20d9c2009-03-06 17:21:50 +0100399enum format_type {
400 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100401 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100402 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,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800411 FORMAT_TYPE_UBYTE,
412 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100413 FORMAT_TYPE_USHORT,
414 FORMAT_TYPE_SHORT,
415 FORMAT_TYPE_UINT,
416 FORMAT_TYPE_INT,
417 FORMAT_TYPE_NRCHARS,
418 FORMAT_TYPE_SIZE_T,
419 FORMAT_TYPE_PTRDIFF
420};
421
422struct printf_spec {
423 enum format_type type;
424 int flags; /* flags to number() */
425 int field_width; /* width of output field */
426 int base;
427 int precision; /* # of digits/chars */
428 int qualifier;
429};
430
431static char *number(char *buf, char *end, unsigned long long num,
432 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100434 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
435 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
436
437 char tmp[66];
438 char sign;
439 char locase;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100440 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int i;
442
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100443 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
444 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100445 locase = (spec.flags & SMALL);
446 if (spec.flags & LEFT)
447 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 sign = 0;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100449 if (spec.flags & SIGN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if ((signed long long) num < 0) {
451 sign = '-';
452 num = - (signed long long) num;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100453 spec.field_width--;
454 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sign = '+';
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100456 spec.field_width--;
457 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sign = ' ';
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100459 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700462 if (need_pfx) {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100463 spec.field_width--;
464 if (spec.base == 16)
465 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700467
468 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 i = 0;
470 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700471 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700472 /* Generic code, for any base:
473 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100474 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700475 } while (num != 0);
476 */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100477 else if (spec.base != 10) { /* 8 or 16 */
478 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 int shift = 3;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100480 if (spec.base == 16) shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700481 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100482 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700483 num >>= shift;
484 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700485 } else { /* base 10 */
486 i = put_dec(tmp, num) - tmp;
487 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700488
489 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100490 if (i > spec.precision)
491 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700492 /* leading space padding */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100493 spec.field_width -= spec.precision;
494 if (!(spec.flags & (ZEROPAD+LEFT))) {
495 while(--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700496 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *buf = ' ';
498 ++buf;
499 }
500 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700501 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700503 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *buf = sign;
505 ++buf;
506 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700507 /* "0x" / "0" prefix */
508 if (need_pfx) {
509 if (buf < end)
510 *buf = '0';
511 ++buf;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100512 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700513 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100514 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ++buf;
516 }
517 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700518 /* zero or space padding */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100519 if (!(spec.flags & LEFT)) {
520 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
521 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700522 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *buf = c;
524 ++buf;
525 }
526 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700527 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100528 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700529 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *buf = '0';
531 ++buf;
532 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700533 /* actual digits of result */
534 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700535 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 *buf = tmp[i];
537 ++buf;
538 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700539 /* trailing space padding */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100540 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700541 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *buf = ' ';
543 ++buf;
544 }
545 return buf;
546}
547
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100548static char *string(char *buf, char *end, char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700549{
550 int len, i;
551
552 if ((unsigned long)s < PAGE_SIZE)
553 s = "<NULL>";
554
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100555 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700556
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100557 if (!(spec.flags & LEFT)) {
558 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700559 if (buf < end)
560 *buf = ' ';
561 ++buf;
562 }
563 }
564 for (i = 0; i < len; ++i) {
565 if (buf < end)
566 *buf = *s;
567 ++buf; ++s;
568 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100569 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700570 if (buf < end)
571 *buf = ' ';
572 ++buf;
573 }
574 return buf;
575}
576
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100577static char *symbol_string(char *buf, char *end, void *ptr,
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200578 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700579{
580 unsigned long value = (unsigned long) ptr;
581#ifdef CONFIG_KALLSYMS
582 char sym[KSYM_SYMBOL_LEN];
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200583 if (ext != 'f')
584 sprint_symbol(sym, value);
585 else
586 kallsyms_lookup(value, NULL, NULL, NULL, sym);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100587 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700588#else
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100589 spec.field_width = 2*sizeof(void *);
590 spec.flags |= SPECIAL | SMALL | ZEROPAD;
591 spec.base = 16;
592 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700593#endif
594}
595
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100596static char *resource_string(char *buf, char *end, struct resource *res,
597 struct printf_spec spec)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100598{
599#ifndef IO_RSRC_PRINTK_SIZE
600#define IO_RSRC_PRINTK_SIZE 4
601#endif
602
603#ifndef MEM_RSRC_PRINTK_SIZE
604#define MEM_RSRC_PRINTK_SIZE 8
605#endif
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100606 struct printf_spec num_spec = {
607 .base = 16,
608 .precision = -1,
609 .flags = SPECIAL | SMALL | ZEROPAD,
610 };
Linus Torvalds332d2e72008-10-20 15:07:34 +1100611 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
612 char sym[4*sizeof(resource_size_t) + 8];
613 char *p = sym, *pend = sym + sizeof(sym);
614 int size = -1;
615
616 if (res->flags & IORESOURCE_IO)
617 size = IO_RSRC_PRINTK_SIZE;
618 else if (res->flags & IORESOURCE_MEM)
619 size = MEM_RSRC_PRINTK_SIZE;
620
621 *p++ = '[';
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100622 num_spec.field_width = size;
623 p = number(p, pend, res->start, num_spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100624 *p++ = '-';
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100625 p = number(p, pend, res->end, num_spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100626 *p++ = ']';
627 *p = 0;
628
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100629 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100630}
631
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100632static char *mac_address_string(char *buf, char *end, u8 *addr,
633 struct printf_spec spec)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700634{
635 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
636 char *p = mac_addr;
637 int i;
638
639 for (i = 0; i < 6; i++) {
640 p = pack_hex_byte(p, addr[i]);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100641 if (!(spec.flags & SPECIAL) && i != 5)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700642 *p++ = ':';
643 }
644 *p = '\0';
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100645 spec.flags &= ~SPECIAL;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700646
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100647 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700648}
649
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100650static char *ip6_addr_string(char *buf, char *end, u8 *addr,
651 struct printf_spec spec)
Harvey Harrison689afa72008-10-28 16:04:44 -0700652{
653 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
654 char *p = ip6_addr;
655 int i;
656
657 for (i = 0; i < 8; i++) {
658 p = pack_hex_byte(p, addr[2 * i]);
659 p = pack_hex_byte(p, addr[2 * i + 1]);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100660 if (!(spec.flags & SPECIAL) && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700661 *p++ = ':';
662 }
663 *p = '\0';
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100664 spec.flags &= ~SPECIAL;
Harvey Harrison689afa72008-10-28 16:04:44 -0700665
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100666 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700667}
668
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100669static char *ip4_addr_string(char *buf, char *end, u8 *addr,
670 struct printf_spec spec)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700671{
672 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800673 char temp[3]; /* hold each IP quad in reverse order */
Harvey Harrison4aa99602008-10-29 12:49:58 -0700674 char *p = ip4_addr;
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800675 int i, digits;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700676
677 for (i = 0; i < 4; i++) {
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800678 digits = put_dec_trunc(temp, addr[i]) - temp;
679 /* reverse the digits in the quad */
680 while (digits--)
681 *p++ = temp[digits];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700682 if (i != 3)
683 *p++ = '.';
684 }
685 *p = '\0';
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100686 spec.flags &= ~SPECIAL;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700687
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100688 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700689}
690
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700691/*
692 * Show a '%p' thing. A kernel extension is that the '%p' is followed
693 * by an extra set of alphanumeric characters that are extended format
694 * specifiers.
695 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100696 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700697 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200698 * - 'F' For symbolic function descriptor pointers with offset
699 * - 'f' For simple symbolic function names without offset
Linus Torvalds332d2e72008-10-20 15:07:34 +1100700 * - 'S' For symbolic direct pointers
701 * - 'R' For a struct resource pointer, it prints the range of
702 * addresses (not the name nor the flags)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700703 * - 'M' For a 6-byte MAC address, it prints the address in the
704 * usual colon-separated hex notation
Harvey Harrison4aa99602008-10-29 12:49:58 -0700705 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
706 * decimal for v4 and colon separated network-order 16 bit hex for v6)
707 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
Harvey Harrison6b9a1062008-10-29 12:53:10 -0700708 * currently the same
Linus Torvalds332d2e72008-10-20 15:07:34 +1100709 *
710 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
711 * function pointers are really function descriptors, which contain a
712 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700713 */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100714static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
715 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700716{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800717 if (!ptr)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100718 return string(buf, end, "(null)", spec);
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800719
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700720 switch (*fmt) {
721 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200722 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700723 ptr = dereference_function_descriptor(ptr);
724 /* Fallthrough */
725 case 'S':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200726 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100727 case 'R':
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100728 return resource_string(buf, end, ptr, spec);
Harvey Harrison411c41e2008-11-25 00:40:37 -0800729 case 'm':
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100730 spec.flags |= SPECIAL;
Harvey Harrison411c41e2008-11-25 00:40:37 -0800731 /* Fallthrough */
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700732 case 'M':
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100733 return mac_address_string(buf, end, ptr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700734 case 'i':
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100735 spec.flags |= SPECIAL;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700736 /* Fallthrough */
737 case 'I':
738 if (fmt[1] == '6')
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100739 return ip6_addr_string(buf, end, ptr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700740 if (fmt[1] == '4')
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100741 return ip4_addr_string(buf, end, ptr, spec);
742 spec.flags &= ~SPECIAL;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700743 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700744 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100745 spec.flags |= SMALL;
746 if (spec.field_width == -1) {
747 spec.field_width = 2*sizeof(void *);
748 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700749 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100750 spec.base = 16;
751
752 return number(buf, end, (unsigned long) ptr, spec);
753}
754
755/*
756 * Helper function to decode printf style format.
757 * Each call decode a token from the format and return the
758 * number of characters read (or likely the delta where it wants
759 * to go on the next call).
760 * The decoded token is returned through the parameters
761 *
762 * 'h', 'l', or 'L' for integer fields
763 * 'z' support added 23/7/1999 S.H.
764 * 'z' changed to 'Z' --davidm 1/25/99
765 * 't' added for ptrdiff_t
766 *
767 * @fmt: the format string
768 * @type of the token returned
769 * @flags: various flags such as +, -, # tokens..
770 * @field_width: overwritten width
771 * @base: base of the number (octal, hex, ...)
772 * @precision: precision of a number
773 * @qualifier: qualifier of a number (long, size_t, ...)
774 */
775static int format_decode(const char *fmt, struct printf_spec *spec)
776{
777 const char *start = fmt;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100778
779 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +0100780 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100781 if (spec->field_width < 0) {
782 spec->field_width = -spec->field_width;
783 spec->flags |= LEFT;
784 }
785 spec->type = FORMAT_TYPE_NONE;
786 goto precision;
787 }
788
789 /* we finished early by reading the precision */
790 if (spec->type == FORMAT_TYPE_PRECISION) {
791 if (spec->precision < 0)
792 spec->precision = 0;
793
794 spec->type = FORMAT_TYPE_NONE;
795 goto qualifier;
796 }
797
798 /* By default */
799 spec->type = FORMAT_TYPE_NONE;
800
801 for (; *fmt ; ++fmt) {
802 if (*fmt == '%')
803 break;
804 }
805
806 /* Return the current non-format string */
807 if (fmt != start || !*fmt)
808 return fmt - start;
809
810 /* Process flags */
811 spec->flags = 0;
812
813 while (1) { /* this also skips first '%' */
814 bool found = true;
815
816 ++fmt;
817
818 switch (*fmt) {
819 case '-': spec->flags |= LEFT; break;
820 case '+': spec->flags |= PLUS; break;
821 case ' ': spec->flags |= SPACE; break;
822 case '#': spec->flags |= SPECIAL; break;
823 case '0': spec->flags |= ZEROPAD; break;
824 default: found = false;
825 }
826
827 if (!found)
828 break;
829 }
830
831 /* get field width */
832 spec->field_width = -1;
833
834 if (isdigit(*fmt))
835 spec->field_width = skip_atoi(&fmt);
836 else if (*fmt == '*') {
837 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +0100838 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100839 return ++fmt - start;
840 }
841
842precision:
843 /* get the precision */
844 spec->precision = -1;
845 if (*fmt == '.') {
846 ++fmt;
847 if (isdigit(*fmt)) {
848 spec->precision = skip_atoi(&fmt);
849 if (spec->precision < 0)
850 spec->precision = 0;
851 } else if (*fmt == '*') {
852 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +0100853 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100854 return ++fmt - start;
855 }
856 }
857
858qualifier:
859 /* get the conversion qualifier */
860 spec->qualifier = -1;
861 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
862 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +0800863 spec->qualifier = *fmt++;
864 if (unlikely(spec->qualifier == *fmt)) {
865 if (spec->qualifier == 'l') {
866 spec->qualifier = 'L';
867 ++fmt;
868 } else if (spec->qualifier == 'h') {
869 spec->qualifier = 'H';
870 ++fmt;
871 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100872 }
873 }
874
875 /* default base */
876 spec->base = 10;
877 switch (*fmt) {
878 case 'c':
879 spec->type = FORMAT_TYPE_CHAR;
880 return ++fmt - start;
881
882 case 's':
883 spec->type = FORMAT_TYPE_STR;
884 return ++fmt - start;
885
886 case 'p':
887 spec->type = FORMAT_TYPE_PTR;
888 return fmt - start;
889 /* skip alnum */
890
891 case 'n':
892 spec->type = FORMAT_TYPE_NRCHARS;
893 return ++fmt - start;
894
895 case '%':
896 spec->type = FORMAT_TYPE_PERCENT_CHAR;
897 return ++fmt - start;
898
899 /* integer number formats - set up the flags and "break" */
900 case 'o':
901 spec->base = 8;
902 break;
903
904 case 'x':
905 spec->flags |= SMALL;
906
907 case 'X':
908 spec->base = 16;
909 break;
910
911 case 'd':
912 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +0100913 spec->flags |= SIGN;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100914 case 'u':
915 break;
916
917 default:
918 spec->type = FORMAT_TYPE_INVALID;
919 return fmt - start;
920 }
921
922 if (spec->qualifier == 'L')
923 spec->type = FORMAT_TYPE_LONG_LONG;
924 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +0100925 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100926 spec->type = FORMAT_TYPE_LONG;
927 else
928 spec->type = FORMAT_TYPE_ULONG;
929 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
930 spec->type = FORMAT_TYPE_SIZE_T;
931 } else if (spec->qualifier == 't') {
932 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +0800933 } else if (spec->qualifier == 'H') {
934 if (spec->flags & SIGN)
935 spec->type = FORMAT_TYPE_BYTE;
936 else
937 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100938 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +0100939 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100940 spec->type = FORMAT_TYPE_SHORT;
941 else
942 spec->type = FORMAT_TYPE_USHORT;
943 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +0100944 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100945 spec->type = FORMAT_TYPE_INT;
946 else
947 spec->type = FORMAT_TYPE_UINT;
948 }
949
950 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/**
954 * vsnprintf - Format a string and place it in a buffer
955 * @buf: The buffer to place the result into
956 * @size: The size of the buffer, including the trailing null space
957 * @fmt: The format string to use
958 * @args: Arguments for the format string
959 *
Andi Kleen20036fd2008-10-15 22:02:02 -0700960 * This function follows C99 vsnprintf, but has some extensions:
961 * %pS output the name of a text symbol
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200962 * %pF output the name of a function pointer with its offset
963 * %pf output the name of a function pointer without its offset
Linus Torvalds332d2e72008-10-20 15:07:34 +1100964 * %pR output the address range in a struct resource
Andi Kleen20036fd2008-10-15 22:02:02 -0700965 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * The return value is the number of characters which would
967 * be generated for the given input, excluding the trailing
968 * '\0', as per ISO C99. If you want to have the exact
969 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800970 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * return is greater than or equal to @size, the resulting
972 * string is truncated.
973 *
974 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800975 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
977int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
978{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 unsigned long long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 char *str, *end, c;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100981 int read;
982 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700984 /* Reject out-of-range values early. Large positive sizes are
985 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (unlikely((int) size < 0)) {
987 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700988 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 WARN_ON(warn);
990 warn = 0;
991 return 0;
992 }
993
994 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700995 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700997 /* Make sure end is always >= buf */
998 if (end < buf) {
999 end = ((void *)-1);
1000 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001003 while (*fmt) {
1004 const char *old_fmt = fmt;
1005
1006 read = format_decode(fmt, &spec);
1007
1008 fmt += read;
1009
1010 switch (spec.type) {
1011 case FORMAT_TYPE_NONE: {
1012 int copy = read;
1013 if (str < end) {
1014 if (copy > end - str)
1015 copy = end - str;
1016 memcpy(str, old_fmt, copy);
1017 }
1018 str += read;
1019 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Vegard Nossumed681a92009-03-14 12:08:50 +01001022 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001023 spec.field_width = va_arg(args, int);
1024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001026 case FORMAT_TYPE_PRECISION:
1027 spec.precision = va_arg(args, int);
1028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001030 case FORMAT_TYPE_CHAR:
1031 if (!(spec.flags & LEFT)) {
1032 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001033 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 *str = ' ';
1035 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001038 }
1039 c = (unsigned char) va_arg(args, int);
1040 if (str < end)
1041 *str = c;
1042 ++str;
1043 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001044 if (str < end)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001045 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001047 }
1048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001050 case FORMAT_TYPE_STR:
1051 str = string(str, end, va_arg(args, char *), spec);
1052 break;
1053
1054 case FORMAT_TYPE_PTR:
1055 str = pointer(fmt+1, str, end, va_arg(args, void *),
1056 spec);
1057 while (isalnum(*fmt))
1058 fmt++;
1059 break;
1060
1061 case FORMAT_TYPE_PERCENT_CHAR:
1062 if (str < end)
1063 *str = '%';
1064 ++str;
1065 break;
1066
1067 case FORMAT_TYPE_INVALID:
1068 if (str < end)
1069 *str = '%';
1070 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001071 break;
1072
1073 case FORMAT_TYPE_NRCHARS: {
1074 int qualifier = spec.qualifier;
1075
1076 if (qualifier == 'l') {
1077 long *ip = va_arg(args, long *);
1078 *ip = (str - buf);
1079 } else if (qualifier == 'Z' ||
1080 qualifier == 'z') {
1081 size_t *ip = va_arg(args, size_t *);
1082 *ip = (str - buf);
1083 } else {
1084 int *ip = va_arg(args, int *);
1085 *ip = (str - buf);
1086 }
1087 break;
1088 }
1089
1090 default:
1091 switch (spec.type) {
1092 case FORMAT_TYPE_LONG_LONG:
1093 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001095 case FORMAT_TYPE_ULONG:
1096 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001098 case FORMAT_TYPE_LONG:
1099 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001101 case FORMAT_TYPE_SIZE_T:
1102 num = va_arg(args, size_t);
1103 break;
1104 case FORMAT_TYPE_PTRDIFF:
1105 num = va_arg(args, ptrdiff_t);
1106 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001107 case FORMAT_TYPE_UBYTE:
1108 num = (unsigned char) va_arg(args, int);
1109 break;
1110 case FORMAT_TYPE_BYTE:
1111 num = (signed char) va_arg(args, int);
1112 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001113 case FORMAT_TYPE_USHORT:
1114 num = (unsigned short) va_arg(args, int);
1115 break;
1116 case FORMAT_TYPE_SHORT:
1117 num = (short) va_arg(args, int);
1118 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001119 case FORMAT_TYPE_INT:
1120 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 default:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001123 num = va_arg(args, unsigned int);
1124 }
1125
1126 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001129
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001130 if (size > 0) {
1131 if (str < end)
1132 *str = '\0';
1133 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001134 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001135 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001136
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001137 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return str-buf;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141EXPORT_SYMBOL(vsnprintf);
1142
1143/**
1144 * vscnprintf - Format a string and place it in a buffer
1145 * @buf: The buffer to place the result into
1146 * @size: The size of the buffer, including the trailing null space
1147 * @fmt: The format string to use
1148 * @args: Arguments for the format string
1149 *
1150 * The return value is the number of characters which have been written into
1151 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1152 * returns 0.
1153 *
1154 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001155 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001156 *
1157 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
1159int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1160{
1161 int i;
1162
1163 i=vsnprintf(buf,size,fmt,args);
1164 return (i >= size) ? (size - 1) : i;
1165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166EXPORT_SYMBOL(vscnprintf);
1167
1168/**
1169 * snprintf - Format a string and place it in a buffer
1170 * @buf: The buffer to place the result into
1171 * @size: The size of the buffer, including the trailing null space
1172 * @fmt: The format string to use
1173 * @...: Arguments for the format string
1174 *
1175 * The return value is the number of characters which would be
1176 * generated for the given input, excluding the trailing null,
1177 * as per ISO C99. If the return is greater than or equal to
1178 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001179 *
1180 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
1182int snprintf(char * buf, size_t size, const char *fmt, ...)
1183{
1184 va_list args;
1185 int i;
1186
1187 va_start(args, fmt);
1188 i=vsnprintf(buf,size,fmt,args);
1189 va_end(args);
1190 return i;
1191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192EXPORT_SYMBOL(snprintf);
1193
1194/**
1195 * scnprintf - Format a string and place it in a buffer
1196 * @buf: The buffer to place the result into
1197 * @size: The size of the buffer, including the trailing null space
1198 * @fmt: The format string to use
1199 * @...: Arguments for the format string
1200 *
1201 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001202 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
1204
1205int scnprintf(char * buf, size_t size, const char *fmt, ...)
1206{
1207 va_list args;
1208 int i;
1209
1210 va_start(args, fmt);
1211 i = vsnprintf(buf, size, fmt, args);
1212 va_end(args);
1213 return (i >= size) ? (size - 1) : i;
1214}
1215EXPORT_SYMBOL(scnprintf);
1216
1217/**
1218 * vsprintf - Format a string and place it in a buffer
1219 * @buf: The buffer to place the result into
1220 * @fmt: The format string to use
1221 * @args: Arguments for the format string
1222 *
1223 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001224 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * buffer overflows.
1226 *
1227 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001228 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001229 *
1230 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
1232int vsprintf(char *buf, const char *fmt, va_list args)
1233{
1234 return vsnprintf(buf, INT_MAX, fmt, args);
1235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236EXPORT_SYMBOL(vsprintf);
1237
1238/**
1239 * sprintf - Format a string and place it in a buffer
1240 * @buf: The buffer to place the result into
1241 * @fmt: The format string to use
1242 * @...: Arguments for the format string
1243 *
1244 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001245 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001247 *
1248 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
1250int sprintf(char * buf, const char *fmt, ...)
1251{
1252 va_list args;
1253 int i;
1254
1255 va_start(args, fmt);
1256 i=vsnprintf(buf, INT_MAX, fmt, args);
1257 va_end(args);
1258 return i;
1259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260EXPORT_SYMBOL(sprintf);
1261
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001262#ifdef CONFIG_BINARY_PRINTF
1263/*
1264 * bprintf service:
1265 * vbin_printf() - VA arguments to binary data
1266 * bstr_printf() - Binary data to text string
1267 */
1268
1269/**
1270 * vbin_printf - Parse a format string and place args' binary value in a buffer
1271 * @bin_buf: The buffer to place args' binary value
1272 * @size: The size of the buffer(by words(32bits), not characters)
1273 * @fmt: The format string to use
1274 * @args: Arguments for the format string
1275 *
1276 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1277 * is skiped.
1278 *
1279 * The return value is the number of words(32bits) which would be generated for
1280 * the given input.
1281 *
1282 * NOTE:
1283 * If the return value is greater than @size, the resulting bin_buf is NOT
1284 * valid for bstr_printf().
1285 */
1286int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1287{
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001288 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001289 char *str, *end;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001290 int read;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001291
1292 str = (char *)bin_buf;
1293 end = (char *)(bin_buf + size);
1294
1295#define save_arg(type) \
1296do { \
1297 if (sizeof(type) == 8) { \
1298 unsigned long long value; \
1299 str = PTR_ALIGN(str, sizeof(u32)); \
1300 value = va_arg(args, unsigned long long); \
1301 if (str + sizeof(type) <= end) { \
1302 *(u32 *)str = *(u32 *)&value; \
1303 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1304 } \
1305 } else { \
1306 unsigned long value; \
1307 str = PTR_ALIGN(str, sizeof(type)); \
1308 value = va_arg(args, int); \
1309 if (str + sizeof(type) <= end) \
1310 *(typeof(type) *)str = (type)value; \
1311 } \
1312 str += sizeof(type); \
1313} while (0)
1314
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001315
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001316 while (*fmt) {
1317 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001318
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001319 fmt += read;
1320
1321 switch (spec.type) {
1322 case FORMAT_TYPE_NONE:
1323 break;
1324
Vegard Nossumed681a92009-03-14 12:08:50 +01001325 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001326 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001327 save_arg(int);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001328 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001329
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001330 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001331 save_arg(char);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001332 break;
1333
1334 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001335 const char *save_str = va_arg(args, char *);
1336 size_t len;
1337 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1338 || (unsigned long)save_str < PAGE_SIZE)
1339 save_str = "<NULL>";
1340 len = strlen(save_str);
1341 if (str + len + 1 < end)
1342 memcpy(str, save_str, len + 1);
1343 str += len + 1;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001344 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001345 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001346
1347 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001348 save_arg(void *);
1349 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001350 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001351 fmt++;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001352 break;
1353
1354 case FORMAT_TYPE_PERCENT_CHAR:
1355 break;
1356
1357 case FORMAT_TYPE_INVALID:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001358 break;
1359
1360 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001361 /* skip %n 's argument */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001362 int qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001363 void *skip_arg;
1364 if (qualifier == 'l')
1365 skip_arg = va_arg(args, long *);
1366 else if (qualifier == 'Z' || qualifier == 'z')
1367 skip_arg = va_arg(args, size_t *);
1368 else
1369 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001370 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001371 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001372
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001373 default:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001374 switch (spec.type) {
1375
1376 case FORMAT_TYPE_LONG_LONG:
1377 save_arg(long long);
1378 break;
1379 case FORMAT_TYPE_ULONG:
1380 case FORMAT_TYPE_LONG:
1381 save_arg(unsigned long);
1382 break;
1383 case FORMAT_TYPE_SIZE_T:
1384 save_arg(size_t);
1385 break;
1386 case FORMAT_TYPE_PTRDIFF:
1387 save_arg(ptrdiff_t);
1388 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001389 case FORMAT_TYPE_UBYTE:
1390 case FORMAT_TYPE_BYTE:
1391 save_arg(char);
1392 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001393 case FORMAT_TYPE_USHORT:
1394 case FORMAT_TYPE_SHORT:
1395 save_arg(short);
1396 break;
1397 default:
1398 save_arg(int);
1399 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001400 }
1401 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001402 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001403
1404#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001405}
1406EXPORT_SYMBOL_GPL(vbin_printf);
1407
1408/**
1409 * bstr_printf - Format a string from binary arguments and place it in a buffer
1410 * @buf: The buffer to place the result into
1411 * @size: The size of the buffer, including the trailing null space
1412 * @fmt: The format string to use
1413 * @bin_buf: Binary arguments for the format string
1414 *
1415 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1416 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1417 * a binary buffer that generated by vbin_printf.
1418 *
1419 * The format follows C99 vsnprintf, but has some extensions:
1420 * %pS output the name of a text symbol
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001421 * %pF output the name of a function pointer with its offset
1422 * %pf output the name of a function pointer without its offset
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001423 * %pR output the address range in a struct resource
1424 * %n is ignored
1425 *
1426 * The return value is the number of characters which would
1427 * be generated for the given input, excluding the trailing
1428 * '\0', as per ISO C99. If you want to have the exact
1429 * number of characters written into @buf as return value
1430 * (not including the trailing '\0'), use vscnprintf(). If the
1431 * return is greater than or equal to @size, the resulting
1432 * string is truncated.
1433 */
1434int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1435{
1436 unsigned long long num;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001437 char *str, *end, c;
1438 const char *args = (const char *)bin_buf;
1439
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001440 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001441
1442 if (unlikely((int) size < 0)) {
1443 /* There can be only one.. */
1444 static char warn = 1;
1445 WARN_ON(warn);
1446 warn = 0;
1447 return 0;
1448 }
1449
1450 str = buf;
1451 end = buf + size;
1452
1453#define get_arg(type) \
1454({ \
1455 typeof(type) value; \
1456 if (sizeof(type) == 8) { \
1457 args = PTR_ALIGN(args, sizeof(u32)); \
1458 *(u32 *)&value = *(u32 *)args; \
1459 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1460 } else { \
1461 args = PTR_ALIGN(args, sizeof(type)); \
1462 value = *(typeof(type) *)args; \
1463 } \
1464 args += sizeof(type); \
1465 value; \
1466})
1467
1468 /* Make sure end is always >= buf */
1469 if (end < buf) {
1470 end = ((void *)-1);
1471 size = end - buf;
1472 }
1473
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001474 while (*fmt) {
1475 int read;
1476 const char *old_fmt = fmt;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001477
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001478 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001479
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001480 fmt += read;
1481
1482 switch (spec.type) {
1483 case FORMAT_TYPE_NONE: {
1484 int copy = read;
1485 if (str < end) {
1486 if (copy > end - str)
1487 copy = end - str;
1488 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001489 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001490 str += read;
1491 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001492 }
1493
Vegard Nossumed681a92009-03-14 12:08:50 +01001494 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001495 spec.field_width = get_arg(int);
1496 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001497
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001498 case FORMAT_TYPE_PRECISION:
1499 spec.precision = get_arg(int);
1500 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001501
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001502 case FORMAT_TYPE_CHAR:
1503 if (!(spec.flags & LEFT)) {
1504 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001505 if (str < end)
1506 *str = ' ';
1507 ++str;
1508 }
1509 }
1510 c = (unsigned char) get_arg(char);
1511 if (str < end)
1512 *str = c;
1513 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001514 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001515 if (str < end)
1516 *str = ' ';
1517 ++str;
1518 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001519 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001520
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001521 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001522 const char *str_arg = args;
1523 size_t len = strlen(str_arg);
1524 args += len + 1;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001525 str = string(str, end, (char *)str_arg, spec);
1526 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001527 }
1528
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001529 case FORMAT_TYPE_PTR:
1530 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1531 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001532 fmt++;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001533 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001534
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001535 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001536 if (str < end)
1537 *str = '%';
1538 ++str;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001539 break;
1540
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001541 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001542 if (str < end)
1543 *str = '%';
1544 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001545 break;
1546
1547 case FORMAT_TYPE_NRCHARS:
1548 /* skip */
1549 break;
1550
1551 default:
1552 switch (spec.type) {
1553
1554 case FORMAT_TYPE_LONG_LONG:
1555 num = get_arg(long long);
1556 break;
1557 case FORMAT_TYPE_ULONG:
1558 num = get_arg(unsigned long);
1559 break;
1560 case FORMAT_TYPE_LONG:
1561 num = get_arg(unsigned long);
1562 break;
1563 case FORMAT_TYPE_SIZE_T:
1564 num = get_arg(size_t);
1565 break;
1566 case FORMAT_TYPE_PTRDIFF:
1567 num = get_arg(ptrdiff_t);
1568 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001569 case FORMAT_TYPE_UBYTE:
1570 num = get_arg(unsigned char);
1571 break;
1572 case FORMAT_TYPE_BYTE:
1573 num = get_arg(signed char);
1574 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001575 case FORMAT_TYPE_USHORT:
1576 num = get_arg(unsigned short);
1577 break;
1578 case FORMAT_TYPE_SHORT:
1579 num = get_arg(short);
1580 break;
1581 case FORMAT_TYPE_UINT:
1582 num = get_arg(unsigned int);
1583 break;
1584 default:
1585 num = get_arg(int);
1586 }
1587
1588 str = number(str, end, num, spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001589 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001590 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001591
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001592 if (size > 0) {
1593 if (str < end)
1594 *str = '\0';
1595 else
1596 end[-1] = '\0';
1597 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001598
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001599#undef get_arg
1600
1601 /* the trailing null byte doesn't count towards the total */
1602 return str - buf;
1603}
1604EXPORT_SYMBOL_GPL(bstr_printf);
1605
1606/**
1607 * bprintf - Parse a format string and place args' binary value in a buffer
1608 * @bin_buf: The buffer to place args' binary value
1609 * @size: The size of the buffer(by words(32bits), not characters)
1610 * @fmt: The format string to use
1611 * @...: Arguments for the format string
1612 *
1613 * The function returns the number of words(u32) written
1614 * into @bin_buf.
1615 */
1616int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1617{
1618 va_list args;
1619 int ret;
1620
1621 va_start(args, fmt);
1622 ret = vbin_printf(bin_buf, size, fmt, args);
1623 va_end(args);
1624 return ret;
1625}
1626EXPORT_SYMBOL_GPL(bprintf);
1627
1628#endif /* CONFIG_BINARY_PRINTF */
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/**
1631 * vsscanf - Unformat a buffer into a list of arguments
1632 * @buf: input buffer
1633 * @fmt: format of buffer
1634 * @args: arguments
1635 */
1636int vsscanf(const char * buf, const char * fmt, va_list args)
1637{
1638 const char *str = buf;
1639 char *next;
1640 char digit;
1641 int num = 0;
1642 int qualifier;
1643 int base;
1644 int field_width;
1645 int is_sign = 0;
1646
1647 while(*fmt && *str) {
1648 /* skip any white space in format */
1649 /* white space in format matchs any amount of
1650 * white space, including none, in the input.
1651 */
1652 if (isspace(*fmt)) {
1653 while (isspace(*fmt))
1654 ++fmt;
1655 while (isspace(*str))
1656 ++str;
1657 }
1658
1659 /* anything that is not a conversion must match exactly */
1660 if (*fmt != '%' && *fmt) {
1661 if (*fmt++ != *str++)
1662 break;
1663 continue;
1664 }
1665
1666 if (!*fmt)
1667 break;
1668 ++fmt;
1669
1670 /* skip this conversion.
1671 * advance both strings to next white space
1672 */
1673 if (*fmt == '*') {
1674 while (!isspace(*fmt) && *fmt)
1675 fmt++;
1676 while (!isspace(*str) && *str)
1677 str++;
1678 continue;
1679 }
1680
1681 /* get field width */
1682 field_width = -1;
1683 if (isdigit(*fmt))
1684 field_width = skip_atoi(&fmt);
1685
1686 /* get conversion qualifier */
1687 qualifier = -1;
1688 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1689 *fmt == 'Z' || *fmt == 'z') {
1690 qualifier = *fmt++;
1691 if (unlikely(qualifier == *fmt)) {
1692 if (qualifier == 'h') {
1693 qualifier = 'H';
1694 fmt++;
1695 } else if (qualifier == 'l') {
1696 qualifier = 'L';
1697 fmt++;
1698 }
1699 }
1700 }
1701 base = 10;
1702 is_sign = 0;
1703
1704 if (!*fmt || !*str)
1705 break;
1706
1707 switch(*fmt++) {
1708 case 'c':
1709 {
1710 char *s = (char *) va_arg(args,char*);
1711 if (field_width == -1)
1712 field_width = 1;
1713 do {
1714 *s++ = *str++;
1715 } while (--field_width > 0 && *str);
1716 num++;
1717 }
1718 continue;
1719 case 's':
1720 {
1721 char *s = (char *) va_arg(args, char *);
1722 if(field_width == -1)
1723 field_width = INT_MAX;
1724 /* first, skip leading white space in buffer */
1725 while (isspace(*str))
1726 str++;
1727
1728 /* now copy until next white space */
1729 while (*str && !isspace(*str) && field_width--) {
1730 *s++ = *str++;
1731 }
1732 *s = '\0';
1733 num++;
1734 }
1735 continue;
1736 case 'n':
1737 /* return number of characters read so far */
1738 {
1739 int *i = (int *)va_arg(args,int*);
1740 *i = str - buf;
1741 }
1742 continue;
1743 case 'o':
1744 base = 8;
1745 break;
1746 case 'x':
1747 case 'X':
1748 base = 16;
1749 break;
1750 case 'i':
1751 base = 0;
1752 case 'd':
1753 is_sign = 1;
1754 case 'u':
1755 break;
1756 case '%':
1757 /* looking for '%' in str */
1758 if (*str++ != '%')
1759 return num;
1760 continue;
1761 default:
1762 /* invalid format; stop here */
1763 return num;
1764 }
1765
1766 /* have some sort of integer conversion.
1767 * first, skip white space in buffer.
1768 */
1769 while (isspace(*str))
1770 str++;
1771
1772 digit = *str;
1773 if (is_sign && digit == '-')
1774 digit = *(str + 1);
1775
1776 if (!digit
1777 || (base == 16 && !isxdigit(digit))
1778 || (base == 10 && !isdigit(digit))
1779 || (base == 8 && (!isdigit(digit) || digit > '7'))
1780 || (base == 0 && !isdigit(digit)))
1781 break;
1782
1783 switch(qualifier) {
1784 case 'H': /* that's 'hh' in format */
1785 if (is_sign) {
1786 signed char *s = (signed char *) va_arg(args,signed char *);
1787 *s = (signed char) simple_strtol(str,&next,base);
1788 } else {
1789 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1790 *s = (unsigned char) simple_strtoul(str, &next, base);
1791 }
1792 break;
1793 case 'h':
1794 if (is_sign) {
1795 short *s = (short *) va_arg(args,short *);
1796 *s = (short) simple_strtol(str,&next,base);
1797 } else {
1798 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1799 *s = (unsigned short) simple_strtoul(str, &next, base);
1800 }
1801 break;
1802 case 'l':
1803 if (is_sign) {
1804 long *l = (long *) va_arg(args,long *);
1805 *l = simple_strtol(str,&next,base);
1806 } else {
1807 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1808 *l = simple_strtoul(str,&next,base);
1809 }
1810 break;
1811 case 'L':
1812 if (is_sign) {
1813 long long *l = (long long*) va_arg(args,long long *);
1814 *l = simple_strtoll(str,&next,base);
1815 } else {
1816 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1817 *l = simple_strtoull(str,&next,base);
1818 }
1819 break;
1820 case 'Z':
1821 case 'z':
1822 {
1823 size_t *s = (size_t*) va_arg(args,size_t*);
1824 *s = (size_t) simple_strtoul(str,&next,base);
1825 }
1826 break;
1827 default:
1828 if (is_sign) {
1829 int *i = (int *) va_arg(args, int*);
1830 *i = (int) simple_strtol(str,&next,base);
1831 } else {
1832 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1833 *i = (unsigned int) simple_strtoul(str,&next,base);
1834 }
1835 break;
1836 }
1837 num++;
1838
1839 if (!next)
1840 break;
1841 str = next;
1842 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001843
1844 /*
1845 * Now we've come all the way through so either the input string or the
1846 * format ended. In the former case, there can be a %n at the current
1847 * position in the format that needs to be filled.
1848 */
1849 if (*fmt == '%' && *(fmt + 1) == 'n') {
1850 int *p = (int *)va_arg(args, int *);
1851 *p = str - buf;
1852 }
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return num;
1855}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856EXPORT_SYMBOL(vsscanf);
1857
1858/**
1859 * sscanf - Unformat a buffer into a list of arguments
1860 * @buf: input buffer
1861 * @fmt: formatting of buffer
1862 * @...: resulting arguments
1863 */
1864int sscanf(const char * buf, const char * fmt, ...)
1865{
1866 va_list args;
1867 int i;
1868
1869 va_start(args,fmt);
1870 i = vsscanf(buf,fmt,args);
1871 va_end(args);
1872 return i;
1873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874EXPORT_SYMBOL(sscanf);