blob: 4819c3d6cca44ea637782bcb823eedd7f8904634 [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
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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>
Joe Perches8a27f7c2009-08-17 12:29:44 +000028#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Tim Schmielau4e57b682005-10-30 15:03:48 -080030#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/div64.h>
James Bottomleydeac93d2008-09-03 20:43:36 -050032#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010034/* Works only for digits and letters, but small and fast */
35#define TOLOWER(x) ((x) | 0x20)
36
Harvey Harrisonaa46a632008-10-16 13:40:34 -070037static unsigned int simple_guess_base(const char *cp)
38{
39 if (cp[0] == '0') {
40 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
41 return 16;
42 else
43 return 8;
44 } else {
45 return 10;
46 }
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/**
50 * simple_strtoul - convert a string to an unsigned long
51 * @cp: The start of the string
52 * @endp: A pointer to the end of the parsed string will be placed here
53 * @base: The number base to use
54 */
Harvey Harrison22d27052008-10-16 13:40:35 -070055unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Harvey Harrisonaa46a632008-10-16 13:40:34 -070057 unsigned long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Harvey Harrisonaa46a632008-10-16 13:40:34 -070059 if (!base)
60 base = simple_guess_base(cp);
61
62 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
63 cp += 2;
64
65 while (isxdigit(*cp)) {
66 unsigned int value;
67
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69 if (value >= base)
70 break;
71 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 cp++;
73 }
74 if (endp)
75 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return result;
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079EXPORT_SYMBOL(simple_strtoul);
80
81/**
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
86 */
Harvey Harrison22d27052008-10-16 13:40:35 -070087long simple_strtol(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080089 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -070090 return -simple_strtoul(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080091
Harvey Harrison22d27052008-10-16 13:40:35 -070092 return simple_strtoul(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094EXPORT_SYMBOL(simple_strtol);
95
96/**
97 * simple_strtoull - convert a string to an unsigned long long
98 * @cp: The start of the string
99 * @endp: A pointer to the end of the parsed string will be placed here
100 * @base: The number base to use
101 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700102unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700104 unsigned long long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700106 if (!base)
107 base = simple_guess_base(cp);
108
109 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
110 cp += 2;
111
112 while (isxdigit(*cp)) {
113 unsigned int value;
114
115 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
116 if (value >= base)
117 break;
118 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 cp++;
120 }
121 if (endp)
122 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return result;
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126EXPORT_SYMBOL(simple_strtoull);
127
128/**
129 * simple_strtoll - convert a string to a signed long long
130 * @cp: The start of the string
131 * @endp: A pointer to the end of the parsed string will be placed here
132 * @base: The number base to use
133 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700134long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800136 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700137 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800138
Harvey Harrison22d27052008-10-16 13:40:35 -0700139 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Yi Yang06b2a762008-02-08 04:21:57 -0800142/**
143 * strict_strtoul - convert a string to an unsigned long strictly
144 * @cp: The string to be converted
145 * @base: The number base to use
146 * @res: The converted result value
147 *
148 * strict_strtoul converts a string to an unsigned long only if the
149 * string is really an unsigned long string, any string containing
150 * any invalid char at the tail will be rejected and -EINVAL is returned,
151 * only a newline char at the tail is acceptible because people generally
152 * change a module parameter in the following way:
153 *
154 * echo 1024 > /sys/module/e1000/parameters/copybreak
155 *
156 * echo will append a newline to the tail.
157 *
158 * It returns 0 if conversion is successful and *res is set to the converted
159 * value, otherwise it returns -EINVAL and *res is set to 0.
160 *
161 * simple_strtoul just ignores the successive invalid characters and
162 * return the converted value of prefix part of the string.
163 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700164int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
165{
166 char *tail;
167 unsigned long val;
168 size_t len;
169
170 *res = 0;
171 len = strlen(cp);
172 if (len == 0)
173 return -EINVAL;
174
175 val = simple_strtoul(cp, &tail, base);
Pavel Macheke899aa82009-01-06 14:40:53 -0800176 if (tail == cp)
177 return -EINVAL;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800178
Harvey Harrison9d85db22008-10-16 13:40:35 -0700179 if ((*tail == '\0') ||
180 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
181 *res = val;
182 return 0;
183 }
184
185 return -EINVAL;
186}
187EXPORT_SYMBOL(strict_strtoul);
Yi Yang06b2a762008-02-08 04:21:57 -0800188
189/**
190 * strict_strtol - convert a string to a long strictly
191 * @cp: The string to be converted
192 * @base: The number base to use
193 * @res: The converted result value
194 *
195 * strict_strtol is similiar to strict_strtoul, but it allows the first
196 * character of a string is '-'.
197 *
198 * It returns 0 if conversion is successful and *res is set to the converted
199 * value, otherwise it returns -EINVAL and *res is set to 0.
200 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700201int strict_strtol(const char *cp, unsigned int base, long *res)
202{
203 int ret;
204 if (*cp == '-') {
205 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
206 if (!ret)
207 *res = -(*res);
208 } else {
209 ret = strict_strtoul(cp, base, (unsigned long *)res);
210 }
211
212 return ret;
213}
214EXPORT_SYMBOL(strict_strtol);
Yi Yang06b2a762008-02-08 04:21:57 -0800215
216/**
217 * strict_strtoull - convert a string to an unsigned long long strictly
218 * @cp: The string to be converted
219 * @base: The number base to use
220 * @res: The converted result value
221 *
222 * strict_strtoull converts a string to an unsigned long long only if the
223 * string is really an unsigned long long string, any string containing
224 * any invalid char at the tail will be rejected and -EINVAL is returned,
225 * only a newline char at the tail is acceptible because people generally
226 * change a module parameter in the following way:
227 *
228 * echo 1024 > /sys/module/e1000/parameters/copybreak
229 *
230 * echo will append a newline to the tail of the string.
231 *
232 * It returns 0 if conversion is successful and *res is set to the converted
233 * value, otherwise it returns -EINVAL and *res is set to 0.
234 *
235 * simple_strtoull just ignores the successive invalid characters and
236 * return the converted value of prefix part of the string.
237 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700238int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
239{
240 char *tail;
241 unsigned long long val;
242 size_t len;
243
244 *res = 0;
245 len = strlen(cp);
246 if (len == 0)
247 return -EINVAL;
248
249 val = simple_strtoull(cp, &tail, base);
Pavel Macheke899aa82009-01-06 14:40:53 -0800250 if (tail == cp)
251 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700252 if ((*tail == '\0') ||
253 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
254 *res = val;
255 return 0;
256 }
257
258 return -EINVAL;
259}
260EXPORT_SYMBOL(strict_strtoull);
Yi Yang06b2a762008-02-08 04:21:57 -0800261
262/**
263 * strict_strtoll - convert a string to a long long strictly
264 * @cp: The string to be converted
265 * @base: The number base to use
266 * @res: The converted result value
267 *
268 * strict_strtoll is similiar to strict_strtoull, but it allows the first
269 * character of a string is '-'.
270 *
271 * It returns 0 if conversion is successful and *res is set to the converted
272 * value, otherwise it returns -EINVAL and *res is set to 0.
273 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700274int strict_strtoll(const char *cp, unsigned int base, long long *res)
275{
276 int ret;
277 if (*cp == '-') {
278 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
279 if (!ret)
280 *res = -(*res);
281 } else {
282 ret = strict_strtoull(cp, base, (unsigned long long *)res);
283 }
Yi Yang06b2a762008-02-08 04:21:57 -0800284
Harvey Harrison9d85db22008-10-16 13:40:35 -0700285 return ret;
286}
Yi Yang06b2a762008-02-08 04:21:57 -0800287EXPORT_SYMBOL(strict_strtoll);
Yi Yang06b2a762008-02-08 04:21:57 -0800288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int skip_atoi(const char **s)
290{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800291 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 while (isdigit(**s))
294 i = i*10 + *((*s)++) - '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return i;
297}
298
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700299/* Decimal conversion is by far the most typical, and is used
300 * for /proc and /sys data. This directly impacts e.g. top performance
301 * with many processes running. We optimize it for speed
302 * using code from
303 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
304 * (with permission from the author, Douglas W. Jones). */
305
306/* Formats correctly any integer in [0,99999].
307 * Outputs from one to five digits depending on input.
308 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800309static char *put_dec_trunc(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700310{
311 unsigned d3, d2, d1, d0;
312 d1 = (q>>4) & 0xf;
313 d2 = (q>>8) & 0xf;
314 d3 = (q>>12);
315
316 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
317 q = (d0 * 0xcd) >> 11;
318 d0 = d0 - 10*q;
319 *buf++ = d0 + '0'; /* least significant digit */
320 d1 = q + 9*d3 + 5*d2 + d1;
321 if (d1 != 0) {
322 q = (d1 * 0xcd) >> 11;
323 d1 = d1 - 10*q;
324 *buf++ = d1 + '0'; /* next digit */
325
326 d2 = q + 2*d2;
327 if ((d2 != 0) || (d3 != 0)) {
328 q = (d2 * 0xd) >> 7;
329 d2 = d2 - 10*q;
330 *buf++ = d2 + '0'; /* next digit */
331
332 d3 = q + 4*d3;
333 if (d3 != 0) {
334 q = (d3 * 0xcd) >> 11;
335 d3 = d3 - 10*q;
336 *buf++ = d3 + '0'; /* next digit */
337 if (q != 0)
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800338 *buf++ = q + '0'; /* most sign. digit */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700339 }
340 }
341 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800342
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700343 return buf;
344}
345/* Same with if's removed. Always emits five digits */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800346static char *put_dec_full(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700347{
348 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
349 /* but anyway, gcc produces better code with full-sized ints */
350 unsigned d3, d2, d1, d0;
351 d1 = (q>>4) & 0xf;
352 d2 = (q>>8) & 0xf;
353 d3 = (q>>12);
354
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800355 /*
356 * Possible ways to approx. divide by 10
357 * gcc -O2 replaces multiply with shifts and adds
358 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
359 * (x * 0x67) >> 10: 1100111
360 * (x * 0x34) >> 9: 110100 - same
361 * (x * 0x1a) >> 8: 11010 - same
362 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
363 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700364 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
365 q = (d0 * 0xcd) >> 11;
366 d0 = d0 - 10*q;
367 *buf++ = d0 + '0';
368 d1 = q + 9*d3 + 5*d2 + d1;
369 q = (d1 * 0xcd) >> 11;
370 d1 = d1 - 10*q;
371 *buf++ = d1 + '0';
372
373 d2 = q + 2*d2;
374 q = (d2 * 0xd) >> 7;
375 d2 = d2 - 10*q;
376 *buf++ = d2 + '0';
377
378 d3 = q + 4*d3;
379 q = (d3 * 0xcd) >> 11; /* - shorter code */
380 /* q = (d3 * 0x67) >> 10; - would also work */
381 d3 = d3 - 10*q;
382 *buf++ = d3 + '0';
383 *buf++ = q + '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800384
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700385 return buf;
386}
387/* No inlining helps gcc to use registers better */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800388static noinline char *put_dec(char *buf, unsigned long long num)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700389{
390 while (1) {
391 unsigned rem;
392 if (num < 100000)
393 return put_dec_trunc(buf, num);
394 rem = do_div(num, 100000);
395 buf = put_dec_full(buf, rem);
396 }
397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#define ZEROPAD 1 /* pad with zero */
400#define SIGN 2 /* unsigned/signed long */
401#define PLUS 4 /* show plus */
402#define SPACE 8 /* space if plus */
403#define LEFT 16 /* left justified */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100404#define SMALL 32 /* Must be 32 == 0x20 */
405#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100407enum format_type {
408 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100409 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100410 FORMAT_TYPE_PRECISION,
411 FORMAT_TYPE_CHAR,
412 FORMAT_TYPE_STR,
413 FORMAT_TYPE_PTR,
414 FORMAT_TYPE_PERCENT_CHAR,
415 FORMAT_TYPE_INVALID,
416 FORMAT_TYPE_LONG_LONG,
417 FORMAT_TYPE_ULONG,
418 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800419 FORMAT_TYPE_UBYTE,
420 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100421 FORMAT_TYPE_USHORT,
422 FORMAT_TYPE_SHORT,
423 FORMAT_TYPE_UINT,
424 FORMAT_TYPE_INT,
425 FORMAT_TYPE_NRCHARS,
426 FORMAT_TYPE_SIZE_T,
427 FORMAT_TYPE_PTRDIFF
428};
429
430struct printf_spec {
431 enum format_type type;
432 int flags; /* flags to number() */
433 int field_width; /* width of output field */
434 int base;
435 int precision; /* # of digits/chars */
436 int qualifier;
437};
438
439static char *number(char *buf, char *end, unsigned long long num,
440 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100442 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
443 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
444
445 char tmp[66];
446 char sign;
447 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100448 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int i;
450
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100451 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
452 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100453 locase = (spec.flags & SMALL);
454 if (spec.flags & LEFT)
455 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100457 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800458 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800460 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100461 spec.field_width--;
462 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100464 spec.field_width--;
465 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100467 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700470 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100471 spec.field_width--;
472 if (spec.base == 16)
473 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700475
476 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 i = 0;
478 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700480 /* Generic code, for any base:
481 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100482 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700483 } while (num != 0);
484 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100485 else if (spec.base != 10) { /* 8 or 16 */
486 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700487 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800488
489 if (spec.base == 16)
490 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700491 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100492 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700493 num >>= shift;
494 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700495 } else { /* base 10 */
496 i = put_dec(tmp, num) - tmp;
497 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700498
499 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100500 if (i > spec.precision)
501 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700502 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100503 spec.field_width -= spec.precision;
504 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800505 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700506 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *buf = ' ';
508 ++buf;
509 }
510 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700511 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700513 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *buf = sign;
515 ++buf;
516 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700517 /* "0x" / "0" prefix */
518 if (need_pfx) {
519 if (buf < end)
520 *buf = '0';
521 ++buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100522 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700523 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100524 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 ++buf;
526 }
527 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700528 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100529 if (!(spec.flags & LEFT)) {
530 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
531 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700532 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *buf = c;
534 ++buf;
535 }
536 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700537 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100538 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700539 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *buf = '0';
541 ++buf;
542 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700543 /* actual digits of result */
544 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700545 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *buf = tmp[i];
547 ++buf;
548 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700549 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100550 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700551 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *buf = ' ';
553 ++buf;
554 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return buf;
557}
558
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800559static char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700560{
561 int len, i;
562
563 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800564 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700565
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100566 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700567
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100568 if (!(spec.flags & LEFT)) {
569 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700570 if (buf < end)
571 *buf = ' ';
572 ++buf;
573 }
574 }
575 for (i = 0; i < len; ++i) {
576 if (buf < end)
577 *buf = *s;
578 ++buf; ++s;
579 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100580 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700581 if (buf < end)
582 *buf = ' ';
583 ++buf;
584 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800585
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700586 return buf;
587}
588
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100589static char *symbol_string(char *buf, char *end, void *ptr,
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200590 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700591{
592 unsigned long value = (unsigned long) ptr;
593#ifdef CONFIG_KALLSYMS
594 char sym[KSYM_SYMBOL_LEN];
Steven Rostedt91adcd22009-09-16 20:03:06 -0400595 if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200596 sprint_symbol(sym, value);
597 else
598 kallsyms_lookup(value, NULL, NULL, NULL, sym);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800599
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100600 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700601#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800602 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100603 spec.flags |= SPECIAL | SMALL | ZEROPAD;
604 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800605
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100606 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700607#endif
608}
609
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100610static char *resource_string(char *buf, char *end, struct resource *res,
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600611 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100612{
613#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600614#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100615#endif
616
617#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600618#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100619#endif
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600620 struct printf_spec hex_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100621 .base = 16,
622 .precision = -1,
623 .flags = SPECIAL | SMALL | ZEROPAD,
624 };
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600625 struct printf_spec dec_spec = {
626 .base = 10,
627 .precision = -1,
628 .flags = 0,
629 };
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600630 struct printf_spec str_spec = {
631 .field_width = -1,
632 .precision = 10,
633 .flags = LEFT,
634 };
635 struct printf_spec flag_spec = {
636 .base = 16,
637 .precision = -1,
638 .flags = SPECIAL | SMALL,
639 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600640
641 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
642 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
643#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
644#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
645#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref disabled]")
646#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
647 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
648 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
649
Linus Torvalds332d2e72008-10-20 15:07:34 +1100650 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600651 int size = -1, addr = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600652 int decode = (fmt[0] == 'R') ? 1 : 0;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100653
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600654 if (res->flags & IORESOURCE_IO) {
Linus Torvalds332d2e72008-10-20 15:07:34 +1100655 size = IO_RSRC_PRINTK_SIZE;
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600656 addr = 1;
657 } else if (res->flags & IORESOURCE_MEM) {
Linus Torvalds332d2e72008-10-20 15:07:34 +1100658 size = MEM_RSRC_PRINTK_SIZE;
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600659 addr = 1;
660 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100661
662 *p++ = '[';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600663 if (res->flags & IORESOURCE_IO)
664 p = string(p, pend, "io ", str_spec);
665 else if (res->flags & IORESOURCE_MEM)
666 p = string(p, pend, "mem ", str_spec);
667 else if (res->flags & IORESOURCE_IRQ)
668 p = string(p, pend, "irq ", str_spec);
669 else if (res->flags & IORESOURCE_DMA)
670 p = string(p, pend, "dma ", str_spec);
671 else {
672 p = string(p, pend, "??? ", str_spec);
673 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600674 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600675 hex_spec.field_width = size;
676 p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
677 if (res->start != res->end) {
678 *p++ = '-';
679 p = number(p, pend, res->end, addr ? hex_spec : dec_spec);
680 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600681 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600682 if (res->flags & IORESOURCE_MEM_64)
683 p = string(p, pend, " 64bit", str_spec);
684 if (res->flags & IORESOURCE_PREFETCH)
685 p = string(p, pend, " pref", str_spec);
686 if (res->flags & IORESOURCE_DISABLED)
687 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600688 } else {
689 p = string(p, pend, " flags ", str_spec);
690 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600691 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100692 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600693 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100694
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100695 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100696}
697
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100698static char *mac_address_string(char *buf, char *end, u8 *addr,
Joe Perches8a27f7c2009-08-17 12:29:44 +0000699 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700700{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000701 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700702 char *p = mac_addr;
703 int i;
704
705 for (i = 0; i < 6; i++) {
706 p = pack_hex_byte(p, addr[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000707 if (fmt[0] == 'M' && i != 5)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700708 *p++ = ':';
709 }
710 *p = '\0';
711
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100712 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700713}
714
Joe Perches8a27f7c2009-08-17 12:29:44 +0000715static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
Harvey Harrison689afa72008-10-28 16:04:44 -0700716{
Harvey Harrison689afa72008-10-28 16:04:44 -0700717 int i;
718
Joe Perches8a27f7c2009-08-17 12:29:44 +0000719 for (i = 0; i < 4; i++) {
720 char temp[3]; /* hold each IP quad in reverse order */
721 int digits = put_dec_trunc(temp, addr[i]) - temp;
722 if (leading_zeros) {
723 if (digits < 3)
724 *p++ = '0';
725 if (digits < 2)
726 *p++ = '0';
727 }
728 /* reverse the digits in the quad */
729 while (digits--)
730 *p++ = temp[digits];
731 if (i < 3)
732 *p++ = '.';
733 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000734 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800735
Joe Perches8a27f7c2009-08-17 12:29:44 +0000736 return p;
737}
738
Joe Percheseb78cd22009-09-18 13:04:06 +0000739static char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000740{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800741 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000742 unsigned char zerolength[8];
743 int longest = 1;
744 int colonpos = -1;
745 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800746 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000747 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000748 bool useIPv4;
749 struct in6_addr in6;
750
751 memcpy(&in6, addr, sizeof(struct in6_addr));
752
753 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000754
755 memset(zerolength, 0, sizeof(zerolength));
756
757 if (useIPv4)
758 range = 6;
759 else
760 range = 8;
761
762 /* find position of longest 0 run */
763 for (i = 0; i < range; i++) {
764 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000765 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000766 break;
767 zerolength[i]++;
768 }
769 }
770 for (i = 0; i < range; i++) {
771 if (zerolength[i] > longest) {
772 longest = zerolength[i];
773 colonpos = i;
774 }
775 }
776
777 /* emit address */
778 for (i = 0; i < range; i++) {
779 if (i == colonpos) {
780 if (needcolon || i == 0)
781 *p++ = ':';
782 *p++ = ':';
783 needcolon = false;
784 i += longest - 1;
785 continue;
786 }
787 if (needcolon) {
788 *p++ = ':';
789 needcolon = false;
790 }
791 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000792 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000793 hi = word >> 8;
794 lo = word & 0xff;
795 if (hi) {
796 if (hi > 0x0f)
797 p = pack_hex_byte(p, hi);
798 else
799 *p++ = hex_asc_lo(hi);
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800800 p = pack_hex_byte(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000801 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800802 else if (lo > 0x0f)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000803 p = pack_hex_byte(p, lo);
804 else
805 *p++ = hex_asc_lo(lo);
806 needcolon = true;
807 }
808
809 if (useIPv4) {
810 if (needcolon)
811 *p++ = ':';
Joe Percheseb78cd22009-09-18 13:04:06 +0000812 p = ip4_string(p, &in6.s6_addr[12], false);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000813 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000814 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800815
Joe Perches8a27f7c2009-08-17 12:29:44 +0000816 return p;
817}
818
Joe Percheseb78cd22009-09-18 13:04:06 +0000819static char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000820{
821 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800822
Harvey Harrison689afa72008-10-28 16:04:44 -0700823 for (i = 0; i < 8; i++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000824 p = pack_hex_byte(p, *addr++);
825 p = pack_hex_byte(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000826 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700827 *p++ = ':';
828 }
829 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800830
Joe Perches8a27f7c2009-08-17 12:29:44 +0000831 return p;
832}
833
834static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
835 struct printf_spec spec, const char *fmt)
836{
837 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
838
839 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000840 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000841 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000842 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700843
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100844 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700845}
846
Joe Perches8a27f7c2009-08-17 12:29:44 +0000847static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
848 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700849{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000850 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700851
Joe Perches8a27f7c2009-08-17 12:29:44 +0000852 ip4_string(ip4_addr, addr, fmt[0] == 'i');
Harvey Harrison4aa99602008-10-29 12:49:58 -0700853
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100854 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700855}
856
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700857/*
858 * Show a '%p' thing. A kernel extension is that the '%p' is followed
859 * by an extra set of alphanumeric characters that are extended format
860 * specifiers.
861 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100862 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700863 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200864 * - 'F' For symbolic function descriptor pointers with offset
865 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400866 * - 'S' For symbolic direct pointers with offset
867 * - 's' For symbolic direct pointers without offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600868 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
869 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700870 * - 'M' For a 6-byte MAC address, it prints the address in the
871 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000872 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
873 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
874 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
875 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
876 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
877 * IPv6 omits the colons (01020304...0f)
878 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
879 * - 'I6c' for IPv6 addresses printed as specified by
880 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
Linus Torvalds332d2e72008-10-20 15:07:34 +1100881 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
882 * function pointers are really function descriptors, which contain a
883 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700884 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100885static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
886 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700887{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800888 if (!ptr)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100889 return string(buf, end, "(null)", spec);
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800890
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700891 switch (*fmt) {
892 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200893 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700894 ptr = dereference_function_descriptor(ptr);
Steven Rostedt91adcd22009-09-16 20:03:06 -0400895 case 's':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700896 /* Fallthrough */
897 case 'S':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200898 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100899 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600900 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600901 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000902 case 'M': /* Colon separated: 00:01:02:03:04:05 */
903 case 'm': /* Contiguous: 000102030405 */
904 return mac_address_string(buf, end, ptr, spec, fmt);
905 case 'I': /* Formatted IP supported
906 * 4: 1.2.3.4
907 * 6: 0001:0203:...:0708
908 * 6c: 1::708 or 1::1.2.3.4
909 */
910 case 'i': /* Contiguous:
911 * 4: 001.002.003.004
912 * 6: 000102...0f
913 */
914 switch (fmt[1]) {
915 case '6':
916 return ip6_addr_string(buf, end, ptr, spec, fmt);
917 case '4':
918 return ip4_addr_string(buf, end, ptr, spec, fmt);
919 }
Harvey Harrison4aa99602008-10-29 12:49:58 -0700920 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700921 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100922 spec.flags |= SMALL;
923 if (spec.field_width == -1) {
924 spec.field_width = 2*sizeof(void *);
925 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700926 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100927 spec.base = 16;
928
929 return number(buf, end, (unsigned long) ptr, spec);
930}
931
932/*
933 * Helper function to decode printf style format.
934 * Each call decode a token from the format and return the
935 * number of characters read (or likely the delta where it wants
936 * to go on the next call).
937 * The decoded token is returned through the parameters
938 *
939 * 'h', 'l', or 'L' for integer fields
940 * 'z' support added 23/7/1999 S.H.
941 * 'z' changed to 'Z' --davidm 1/25/99
942 * 't' added for ptrdiff_t
943 *
944 * @fmt: the format string
945 * @type of the token returned
946 * @flags: various flags such as +, -, # tokens..
947 * @field_width: overwritten width
948 * @base: base of the number (octal, hex, ...)
949 * @precision: precision of a number
950 * @qualifier: qualifier of a number (long, size_t, ...)
951 */
952static int format_decode(const char *fmt, struct printf_spec *spec)
953{
954 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100955
956 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +0100957 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100958 if (spec->field_width < 0) {
959 spec->field_width = -spec->field_width;
960 spec->flags |= LEFT;
961 }
962 spec->type = FORMAT_TYPE_NONE;
963 goto precision;
964 }
965
966 /* we finished early by reading the precision */
967 if (spec->type == FORMAT_TYPE_PRECISION) {
968 if (spec->precision < 0)
969 spec->precision = 0;
970
971 spec->type = FORMAT_TYPE_NONE;
972 goto qualifier;
973 }
974
975 /* By default */
976 spec->type = FORMAT_TYPE_NONE;
977
978 for (; *fmt ; ++fmt) {
979 if (*fmt == '%')
980 break;
981 }
982
983 /* Return the current non-format string */
984 if (fmt != start || !*fmt)
985 return fmt - start;
986
987 /* Process flags */
988 spec->flags = 0;
989
990 while (1) { /* this also skips first '%' */
991 bool found = true;
992
993 ++fmt;
994
995 switch (*fmt) {
996 case '-': spec->flags |= LEFT; break;
997 case '+': spec->flags |= PLUS; break;
998 case ' ': spec->flags |= SPACE; break;
999 case '#': spec->flags |= SPECIAL; break;
1000 case '0': spec->flags |= ZEROPAD; break;
1001 default: found = false;
1002 }
1003
1004 if (!found)
1005 break;
1006 }
1007
1008 /* get field width */
1009 spec->field_width = -1;
1010
1011 if (isdigit(*fmt))
1012 spec->field_width = skip_atoi(&fmt);
1013 else if (*fmt == '*') {
1014 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001015 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001016 return ++fmt - start;
1017 }
1018
1019precision:
1020 /* get the precision */
1021 spec->precision = -1;
1022 if (*fmt == '.') {
1023 ++fmt;
1024 if (isdigit(*fmt)) {
1025 spec->precision = skip_atoi(&fmt);
1026 if (spec->precision < 0)
1027 spec->precision = 0;
1028 } else if (*fmt == '*') {
1029 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001030 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001031 return ++fmt - start;
1032 }
1033 }
1034
1035qualifier:
1036 /* get the conversion qualifier */
1037 spec->qualifier = -1;
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001038 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1039 TOLOWER(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001040 spec->qualifier = *fmt++;
1041 if (unlikely(spec->qualifier == *fmt)) {
1042 if (spec->qualifier == 'l') {
1043 spec->qualifier = 'L';
1044 ++fmt;
1045 } else if (spec->qualifier == 'h') {
1046 spec->qualifier = 'H';
1047 ++fmt;
1048 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001049 }
1050 }
1051
1052 /* default base */
1053 spec->base = 10;
1054 switch (*fmt) {
1055 case 'c':
1056 spec->type = FORMAT_TYPE_CHAR;
1057 return ++fmt - start;
1058
1059 case 's':
1060 spec->type = FORMAT_TYPE_STR;
1061 return ++fmt - start;
1062
1063 case 'p':
1064 spec->type = FORMAT_TYPE_PTR;
1065 return fmt - start;
1066 /* skip alnum */
1067
1068 case 'n':
1069 spec->type = FORMAT_TYPE_NRCHARS;
1070 return ++fmt - start;
1071
1072 case '%':
1073 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1074 return ++fmt - start;
1075
1076 /* integer number formats - set up the flags and "break" */
1077 case 'o':
1078 spec->base = 8;
1079 break;
1080
1081 case 'x':
1082 spec->flags |= SMALL;
1083
1084 case 'X':
1085 spec->base = 16;
1086 break;
1087
1088 case 'd':
1089 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001090 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001091 case 'u':
1092 break;
1093
1094 default:
1095 spec->type = FORMAT_TYPE_INVALID;
1096 return fmt - start;
1097 }
1098
1099 if (spec->qualifier == 'L')
1100 spec->type = FORMAT_TYPE_LONG_LONG;
1101 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001102 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001103 spec->type = FORMAT_TYPE_LONG;
1104 else
1105 spec->type = FORMAT_TYPE_ULONG;
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001106 } else if (TOLOWER(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001107 spec->type = FORMAT_TYPE_SIZE_T;
1108 } else if (spec->qualifier == 't') {
1109 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001110 } else if (spec->qualifier == 'H') {
1111 if (spec->flags & SIGN)
1112 spec->type = FORMAT_TYPE_BYTE;
1113 else
1114 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001115 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001116 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001117 spec->type = FORMAT_TYPE_SHORT;
1118 else
1119 spec->type = FORMAT_TYPE_USHORT;
1120 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001121 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001122 spec->type = FORMAT_TYPE_INT;
1123 else
1124 spec->type = FORMAT_TYPE_UINT;
1125 }
1126
1127 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/**
1131 * vsnprintf - Format a string and place it in a buffer
1132 * @buf: The buffer to place the result into
1133 * @size: The size of the buffer, including the trailing null space
1134 * @fmt: The format string to use
1135 * @args: Arguments for the format string
1136 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001137 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001138 * %pS output the name of a text symbol with offset
1139 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001140 * %pF output the name of a function pointer with its offset
1141 * %pf output the name of a function pointer without its offset
Linus Torvalds332d2e72008-10-20 15:07:34 +11001142 * %pR output the address range in a struct resource
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001143 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * The return value is the number of characters which would
1146 * be generated for the given input, excluding the trailing
1147 * '\0', as per ISO C99. If you want to have the exact
1148 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001149 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * return is greater than or equal to @size, the resulting
1151 * string is truncated.
1152 *
1153 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001154 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 unsigned long long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 char *str, *end, c;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001160 int read;
1161 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001163 /* Reject out-of-range values early. Large positive sizes are
1164 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001165 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001169 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001171 /* Make sure end is always >= buf */
1172 if (end < buf) {
1173 end = ((void *)-1);
1174 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001177 while (*fmt) {
1178 const char *old_fmt = fmt;
1179
1180 read = format_decode(fmt, &spec);
1181
1182 fmt += read;
1183
1184 switch (spec.type) {
1185 case FORMAT_TYPE_NONE: {
1186 int copy = read;
1187 if (str < end) {
1188 if (copy > end - str)
1189 copy = end - str;
1190 memcpy(str, old_fmt, copy);
1191 }
1192 str += read;
1193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
Vegard Nossumed681a92009-03-14 12:08:50 +01001196 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001197 spec.field_width = va_arg(args, int);
1198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001200 case FORMAT_TYPE_PRECISION:
1201 spec.precision = va_arg(args, int);
1202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001204 case FORMAT_TYPE_CHAR:
1205 if (!(spec.flags & LEFT)) {
1206 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001207 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *str = ' ';
1209 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001212 }
1213 c = (unsigned char) va_arg(args, int);
1214 if (str < end)
1215 *str = c;
1216 ++str;
1217 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001218 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001219 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001221 }
1222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001224 case FORMAT_TYPE_STR:
1225 str = string(str, end, va_arg(args, char *), spec);
1226 break;
1227
1228 case FORMAT_TYPE_PTR:
1229 str = pointer(fmt+1, str, end, va_arg(args, void *),
1230 spec);
1231 while (isalnum(*fmt))
1232 fmt++;
1233 break;
1234
1235 case FORMAT_TYPE_PERCENT_CHAR:
1236 if (str < end)
1237 *str = '%';
1238 ++str;
1239 break;
1240
1241 case FORMAT_TYPE_INVALID:
1242 if (str < end)
1243 *str = '%';
1244 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001245 break;
1246
1247 case FORMAT_TYPE_NRCHARS: {
1248 int qualifier = spec.qualifier;
1249
1250 if (qualifier == 'l') {
1251 long *ip = va_arg(args, long *);
1252 *ip = (str - buf);
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001253 } else if (TOLOWER(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001254 size_t *ip = va_arg(args, size_t *);
1255 *ip = (str - buf);
1256 } else {
1257 int *ip = va_arg(args, int *);
1258 *ip = (str - buf);
1259 }
1260 break;
1261 }
1262
1263 default:
1264 switch (spec.type) {
1265 case FORMAT_TYPE_LONG_LONG:
1266 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001268 case FORMAT_TYPE_ULONG:
1269 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001271 case FORMAT_TYPE_LONG:
1272 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001274 case FORMAT_TYPE_SIZE_T:
1275 num = va_arg(args, size_t);
1276 break;
1277 case FORMAT_TYPE_PTRDIFF:
1278 num = va_arg(args, ptrdiff_t);
1279 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001280 case FORMAT_TYPE_UBYTE:
1281 num = (unsigned char) va_arg(args, int);
1282 break;
1283 case FORMAT_TYPE_BYTE:
1284 num = (signed char) va_arg(args, int);
1285 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001286 case FORMAT_TYPE_USHORT:
1287 num = (unsigned short) va_arg(args, int);
1288 break;
1289 case FORMAT_TYPE_SHORT:
1290 num = (short) va_arg(args, int);
1291 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001292 case FORMAT_TYPE_INT:
1293 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001296 num = va_arg(args, unsigned int);
1297 }
1298
1299 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001302
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001303 if (size > 0) {
1304 if (str < end)
1305 *str = '\0';
1306 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001307 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001308 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001309
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001310 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314EXPORT_SYMBOL(vsnprintf);
1315
1316/**
1317 * vscnprintf - Format a string and place it in a buffer
1318 * @buf: The buffer to place the result into
1319 * @size: The size of the buffer, including the trailing null space
1320 * @fmt: The format string to use
1321 * @args: Arguments for the format string
1322 *
1323 * The return value is the number of characters which have been written into
1324 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1325 * returns 0.
1326 *
1327 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001328 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001329 *
1330 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
1332int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1333{
1334 int i;
1335
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001336 i = vsnprintf(buf, size, fmt, args);
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return (i >= size) ? (size - 1) : i;
1339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340EXPORT_SYMBOL(vscnprintf);
1341
1342/**
1343 * snprintf - Format a string and place it in a buffer
1344 * @buf: The buffer to place the result into
1345 * @size: The size of the buffer, including the trailing null space
1346 * @fmt: The format string to use
1347 * @...: Arguments for the format string
1348 *
1349 * The return value is the number of characters which would be
1350 * generated for the given input, excluding the trailing null,
1351 * as per ISO C99. If the return is greater than or equal to
1352 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001353 *
1354 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001356int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 va_list args;
1359 int i;
1360
1361 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001362 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return i;
1366}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367EXPORT_SYMBOL(snprintf);
1368
1369/**
1370 * scnprintf - Format a string and place it in a buffer
1371 * @buf: The buffer to place the result into
1372 * @size: The size of the buffer, including the trailing null space
1373 * @fmt: The format string to use
1374 * @...: Arguments for the format string
1375 *
1376 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001377 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
1379
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001380int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 va_list args;
1383 int i;
1384
1385 va_start(args, fmt);
1386 i = vsnprintf(buf, size, fmt, args);
1387 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return (i >= size) ? (size - 1) : i;
1390}
1391EXPORT_SYMBOL(scnprintf);
1392
1393/**
1394 * vsprintf - Format a string and place it in a buffer
1395 * @buf: The buffer to place the result into
1396 * @fmt: The format string to use
1397 * @args: Arguments for the format string
1398 *
1399 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001400 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * buffer overflows.
1402 *
1403 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001404 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001405 *
1406 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 */
1408int vsprintf(char *buf, const char *fmt, va_list args)
1409{
1410 return vsnprintf(buf, INT_MAX, fmt, args);
1411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412EXPORT_SYMBOL(vsprintf);
1413
1414/**
1415 * sprintf - Format a string and place it in a buffer
1416 * @buf: The buffer to place the result into
1417 * @fmt: The format string to use
1418 * @...: Arguments for the format string
1419 *
1420 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001421 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001423 *
1424 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001426int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 va_list args;
1429 int i;
1430
1431 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001432 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return i;
1436}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437EXPORT_SYMBOL(sprintf);
1438
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001439#ifdef CONFIG_BINARY_PRINTF
1440/*
1441 * bprintf service:
1442 * vbin_printf() - VA arguments to binary data
1443 * bstr_printf() - Binary data to text string
1444 */
1445
1446/**
1447 * vbin_printf - Parse a format string and place args' binary value in a buffer
1448 * @bin_buf: The buffer to place args' binary value
1449 * @size: The size of the buffer(by words(32bits), not characters)
1450 * @fmt: The format string to use
1451 * @args: Arguments for the format string
1452 *
1453 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1454 * is skiped.
1455 *
1456 * The return value is the number of words(32bits) which would be generated for
1457 * the given input.
1458 *
1459 * NOTE:
1460 * If the return value is greater than @size, the resulting bin_buf is NOT
1461 * valid for bstr_printf().
1462 */
1463int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1464{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001465 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001466 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001467 int read;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001468
1469 str = (char *)bin_buf;
1470 end = (char *)(bin_buf + size);
1471
1472#define save_arg(type) \
1473do { \
1474 if (sizeof(type) == 8) { \
1475 unsigned long long value; \
1476 str = PTR_ALIGN(str, sizeof(u32)); \
1477 value = va_arg(args, unsigned long long); \
1478 if (str + sizeof(type) <= end) { \
1479 *(u32 *)str = *(u32 *)&value; \
1480 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1481 } \
1482 } else { \
1483 unsigned long value; \
1484 str = PTR_ALIGN(str, sizeof(type)); \
1485 value = va_arg(args, int); \
1486 if (str + sizeof(type) <= end) \
1487 *(typeof(type) *)str = (type)value; \
1488 } \
1489 str += sizeof(type); \
1490} while (0)
1491
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001492 while (*fmt) {
1493 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001494
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001495 fmt += read;
1496
1497 switch (spec.type) {
1498 case FORMAT_TYPE_NONE:
1499 break;
1500
Vegard Nossumed681a92009-03-14 12:08:50 +01001501 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001502 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001503 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001504 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001505
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001506 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001507 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001508 break;
1509
1510 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001511 const char *save_str = va_arg(args, char *);
1512 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001513
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001514 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1515 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001516 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001517 len = strlen(save_str) + 1;
1518 if (str + len < end)
1519 memcpy(str, save_str, len);
1520 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001521 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001522 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001523
1524 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001525 save_arg(void *);
1526 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001527 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001528 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001529 break;
1530
1531 case FORMAT_TYPE_PERCENT_CHAR:
1532 break;
1533
1534 case FORMAT_TYPE_INVALID:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001535 break;
1536
1537 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001538 /* skip %n 's argument */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001539 int qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001540 void *skip_arg;
1541 if (qualifier == 'l')
1542 skip_arg = va_arg(args, long *);
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001543 else if (TOLOWER(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001544 skip_arg = va_arg(args, size_t *);
1545 else
1546 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001547 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001548 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001549
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001550 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001551 switch (spec.type) {
1552
1553 case FORMAT_TYPE_LONG_LONG:
1554 save_arg(long long);
1555 break;
1556 case FORMAT_TYPE_ULONG:
1557 case FORMAT_TYPE_LONG:
1558 save_arg(unsigned long);
1559 break;
1560 case FORMAT_TYPE_SIZE_T:
1561 save_arg(size_t);
1562 break;
1563 case FORMAT_TYPE_PTRDIFF:
1564 save_arg(ptrdiff_t);
1565 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001566 case FORMAT_TYPE_UBYTE:
1567 case FORMAT_TYPE_BYTE:
1568 save_arg(char);
1569 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001570 case FORMAT_TYPE_USHORT:
1571 case FORMAT_TYPE_SHORT:
1572 save_arg(short);
1573 break;
1574 default:
1575 save_arg(int);
1576 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001577 }
1578 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001579
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001580 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001581#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001582}
1583EXPORT_SYMBOL_GPL(vbin_printf);
1584
1585/**
1586 * bstr_printf - Format a string from binary arguments and place it in a buffer
1587 * @buf: The buffer to place the result into
1588 * @size: The size of the buffer, including the trailing null space
1589 * @fmt: The format string to use
1590 * @bin_buf: Binary arguments for the format string
1591 *
1592 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1593 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1594 * a binary buffer that generated by vbin_printf.
1595 *
1596 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001597 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001598 *
1599 * The return value is the number of characters which would
1600 * be generated for the given input, excluding the trailing
1601 * '\0', as per ISO C99. If you want to have the exact
1602 * number of characters written into @buf as return value
1603 * (not including the trailing '\0'), use vscnprintf(). If the
1604 * return is greater than or equal to @size, the resulting
1605 * string is truncated.
1606 */
1607int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1608{
1609 unsigned long long num;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001610 char *str, *end, c;
1611 const char *args = (const char *)bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001612 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001613
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001614 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001615 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001616
1617 str = buf;
1618 end = buf + size;
1619
1620#define get_arg(type) \
1621({ \
1622 typeof(type) value; \
1623 if (sizeof(type) == 8) { \
1624 args = PTR_ALIGN(args, sizeof(u32)); \
1625 *(u32 *)&value = *(u32 *)args; \
1626 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1627 } else { \
1628 args = PTR_ALIGN(args, sizeof(type)); \
1629 value = *(typeof(type) *)args; \
1630 } \
1631 args += sizeof(type); \
1632 value; \
1633})
1634
1635 /* Make sure end is always >= buf */
1636 if (end < buf) {
1637 end = ((void *)-1);
1638 size = end - buf;
1639 }
1640
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001641 while (*fmt) {
1642 int read;
1643 const char *old_fmt = fmt;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001644
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001645 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001646
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001647 fmt += read;
1648
1649 switch (spec.type) {
1650 case FORMAT_TYPE_NONE: {
1651 int copy = read;
1652 if (str < end) {
1653 if (copy > end - str)
1654 copy = end - str;
1655 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001656 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001657 str += read;
1658 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001659 }
1660
Vegard Nossumed681a92009-03-14 12:08:50 +01001661 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001662 spec.field_width = get_arg(int);
1663 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001664
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001665 case FORMAT_TYPE_PRECISION:
1666 spec.precision = get_arg(int);
1667 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001668
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001669 case FORMAT_TYPE_CHAR:
1670 if (!(spec.flags & LEFT)) {
1671 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001672 if (str < end)
1673 *str = ' ';
1674 ++str;
1675 }
1676 }
1677 c = (unsigned char) get_arg(char);
1678 if (str < end)
1679 *str = c;
1680 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001681 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001682 if (str < end)
1683 *str = ' ';
1684 ++str;
1685 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001686 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001687
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001688 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001689 const char *str_arg = args;
1690 size_t len = strlen(str_arg);
1691 args += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001692 str = string(str, end, (char *)str_arg, spec);
1693 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001694 }
1695
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001696 case FORMAT_TYPE_PTR:
1697 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1698 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001699 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001700 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001701
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001702 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001703 if (str < end)
1704 *str = '%';
1705 ++str;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001706 break;
1707
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001708 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001709 if (str < end)
1710 *str = '%';
1711 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001712 break;
1713
1714 case FORMAT_TYPE_NRCHARS:
1715 /* skip */
1716 break;
1717
1718 default:
1719 switch (spec.type) {
1720
1721 case FORMAT_TYPE_LONG_LONG:
1722 num = get_arg(long long);
1723 break;
1724 case FORMAT_TYPE_ULONG:
1725 num = get_arg(unsigned long);
1726 break;
1727 case FORMAT_TYPE_LONG:
1728 num = get_arg(unsigned long);
1729 break;
1730 case FORMAT_TYPE_SIZE_T:
1731 num = get_arg(size_t);
1732 break;
1733 case FORMAT_TYPE_PTRDIFF:
1734 num = get_arg(ptrdiff_t);
1735 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001736 case FORMAT_TYPE_UBYTE:
1737 num = get_arg(unsigned char);
1738 break;
1739 case FORMAT_TYPE_BYTE:
1740 num = get_arg(signed char);
1741 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001742 case FORMAT_TYPE_USHORT:
1743 num = get_arg(unsigned short);
1744 break;
1745 case FORMAT_TYPE_SHORT:
1746 num = get_arg(short);
1747 break;
1748 case FORMAT_TYPE_UINT:
1749 num = get_arg(unsigned int);
1750 break;
1751 default:
1752 num = get_arg(int);
1753 }
1754
1755 str = number(str, end, num, spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001756 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001757 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001758
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001759 if (size > 0) {
1760 if (str < end)
1761 *str = '\0';
1762 else
1763 end[-1] = '\0';
1764 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001765
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001766#undef get_arg
1767
1768 /* the trailing null byte doesn't count towards the total */
1769 return str - buf;
1770}
1771EXPORT_SYMBOL_GPL(bstr_printf);
1772
1773/**
1774 * bprintf - Parse a format string and place args' binary value in a buffer
1775 * @bin_buf: The buffer to place args' binary value
1776 * @size: The size of the buffer(by words(32bits), not characters)
1777 * @fmt: The format string to use
1778 * @...: Arguments for the format string
1779 *
1780 * The function returns the number of words(u32) written
1781 * into @bin_buf.
1782 */
1783int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1784{
1785 va_list args;
1786 int ret;
1787
1788 va_start(args, fmt);
1789 ret = vbin_printf(bin_buf, size, fmt, args);
1790 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001791
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001792 return ret;
1793}
1794EXPORT_SYMBOL_GPL(bprintf);
1795
1796#endif /* CONFIG_BINARY_PRINTF */
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/**
1799 * vsscanf - Unformat a buffer into a list of arguments
1800 * @buf: input buffer
1801 * @fmt: format of buffer
1802 * @args: arguments
1803 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001804int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 const char *str = buf;
1807 char *next;
1808 char digit;
1809 int num = 0;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001810 int qualifier, base, field_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int is_sign = 0;
1812
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001813 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* skip any white space in format */
1815 /* white space in format matchs any amount of
1816 * white space, including none, in the input.
1817 */
1818 if (isspace(*fmt)) {
1819 while (isspace(*fmt))
1820 ++fmt;
1821 while (isspace(*str))
1822 ++str;
1823 }
1824
1825 /* anything that is not a conversion must match exactly */
1826 if (*fmt != '%' && *fmt) {
1827 if (*fmt++ != *str++)
1828 break;
1829 continue;
1830 }
1831
1832 if (!*fmt)
1833 break;
1834 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /* skip this conversion.
1837 * advance both strings to next white space
1838 */
1839 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001840 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 fmt++;
1842 while (!isspace(*str) && *str)
1843 str++;
1844 continue;
1845 }
1846
1847 /* get field width */
1848 field_width = -1;
1849 if (isdigit(*fmt))
1850 field_width = skip_atoi(&fmt);
1851
1852 /* get conversion qualifier */
1853 qualifier = -1;
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001854 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1855 TOLOWER(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 qualifier = *fmt++;
1857 if (unlikely(qualifier == *fmt)) {
1858 if (qualifier == 'h') {
1859 qualifier = 'H';
1860 fmt++;
1861 } else if (qualifier == 'l') {
1862 qualifier = 'L';
1863 fmt++;
1864 }
1865 }
1866 }
1867 base = 10;
1868 is_sign = 0;
1869
1870 if (!*fmt || !*str)
1871 break;
1872
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001873 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 case 'c':
1875 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001876 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (field_width == -1)
1878 field_width = 1;
1879 do {
1880 *s++ = *str++;
1881 } while (--field_width > 0 && *str);
1882 num++;
1883 }
1884 continue;
1885 case 's':
1886 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001887 char *s = (char *)va_arg(args, char *);
1888 if (field_width == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 field_width = INT_MAX;
1890 /* first, skip leading white space in buffer */
1891 while (isspace(*str))
1892 str++;
1893
1894 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001895 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *s = '\0';
1898 num++;
1899 }
1900 continue;
1901 case 'n':
1902 /* return number of characters read so far */
1903 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001904 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 *i = str - buf;
1906 }
1907 continue;
1908 case 'o':
1909 base = 8;
1910 break;
1911 case 'x':
1912 case 'X':
1913 base = 16;
1914 break;
1915 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001916 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 case 'd':
1918 is_sign = 1;
1919 case 'u':
1920 break;
1921 case '%':
1922 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001923 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return num;
1925 continue;
1926 default:
1927 /* invalid format; stop here */
1928 return num;
1929 }
1930
1931 /* have some sort of integer conversion.
1932 * first, skip white space in buffer.
1933 */
1934 while (isspace(*str))
1935 str++;
1936
1937 digit = *str;
1938 if (is_sign && digit == '-')
1939 digit = *(str + 1);
1940
1941 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001942 || (base == 16 && !isxdigit(digit))
1943 || (base == 10 && !isdigit(digit))
1944 || (base == 8 && (!isdigit(digit) || digit > '7'))
1945 || (base == 0 && !isdigit(digit)))
1946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001948 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 case 'H': /* that's 'hh' in format */
1950 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001951 signed char *s = (signed char *)va_arg(args, signed char *);
1952 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001954 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
1955 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957 break;
1958 case 'h':
1959 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001960 short *s = (short *)va_arg(args, short *);
1961 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001963 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
1964 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966 break;
1967 case 'l':
1968 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001969 long *l = (long *)va_arg(args, long *);
1970 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001972 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
1973 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975 break;
1976 case 'L':
1977 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001978 long long *l = (long long *)va_arg(args, long long *);
1979 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001981 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
1982 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 break;
1985 case 'Z':
1986 case 'z':
1987 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001988 size_t *s = (size_t *)va_arg(args, size_t *);
1989 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991 break;
1992 default:
1993 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001994 int *i = (int *)va_arg(args, int *);
1995 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001997 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
1998 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000 break;
2001 }
2002 num++;
2003
2004 if (!next)
2005 break;
2006 str = next;
2007 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002008
2009 /*
2010 * Now we've come all the way through so either the input string or the
2011 * format ended. In the former case, there can be a %n at the current
2012 * position in the format that needs to be filled.
2013 */
2014 if (*fmt == '%' && *(fmt + 1) == 'n') {
2015 int *p = (int *)va_arg(args, int *);
2016 *p = str - buf;
2017 }
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return num;
2020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021EXPORT_SYMBOL(vsscanf);
2022
2023/**
2024 * sscanf - Unformat a buffer into a list of arguments
2025 * @buf: input buffer
2026 * @fmt: formatting of buffer
2027 * @...: resulting arguments
2028 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002029int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 va_list args;
2032 int i;
2033
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002034 va_start(args, fmt);
2035 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return i;
2039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040EXPORT_SYMBOL(sscanf);