blob: 67e74cbefa9047dc9de24dac7c6ad71520c0d0d1 [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>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050020#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070034#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * simple_strtoull - convert a string to an unsigned long long
38 * @cp: The start of the string
39 * @endp: A pointer to the end of the parsed string will be placed here
40 * @base: The number base to use
41 */
Harvey Harrison22d27052008-10-16 13:40:35 -070042unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070044 unsigned long long result;
45 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070047 cp = _parse_integer_fixup_radix(cp, &base);
48 rv = _parse_integer(cp, base, &result);
49 /* FIXME */
50 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (endp)
53 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return result;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057EXPORT_SYMBOL(simple_strtoull);
58
59/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080060 * simple_strtoul - convert a string to an unsigned long
61 * @cp: The start of the string
62 * @endp: A pointer to the end of the parsed string will be placed here
63 * @base: The number base to use
64 */
65unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
66{
67 return simple_strtoull(cp, endp, base);
68}
69EXPORT_SYMBOL(simple_strtoul);
70
71/**
72 * simple_strtol - convert a string to a signed long
73 * @cp: The start of the string
74 * @endp: A pointer to the end of the parsed string will be placed here
75 * @base: The number base to use
76 */
77long simple_strtol(const char *cp, char **endp, unsigned int base)
78{
79 if (*cp == '-')
80 return -simple_strtoul(cp + 1, endp, base);
81
82 return simple_strtoul(cp, endp, base);
83}
84EXPORT_SYMBOL(simple_strtol);
85
86/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * simple_strtoll - convert a string to a signed long long
88 * @cp: The start of the string
89 * @endp: A pointer to the end of the parsed string will be placed here
90 * @base: The number base to use
91 */
Harvey Harrison22d27052008-10-16 13:40:35 -070092long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080094 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -070095 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080096
Harvey Harrison22d27052008-10-16 13:40:35 -070097 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
Hans Verkuil98d5ce02010-04-23 13:18:04 -040099EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Joe Perchescf3b4292010-05-24 14:33:16 -0700101static noinline_for_stack
102int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800104 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 while (isdigit(**s))
107 i = i*10 + *((*s)++) - '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return i;
110}
111
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700112/* Decimal conversion is by far the most typical, and is used
113 * for /proc and /sys data. This directly impacts e.g. top performance
114 * with many processes running. We optimize it for speed
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700115 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
116 * (with permission from the author, Douglas W. Jones).
117 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700118
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700119#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
120/* Formats correctly any integer in [0, 999999999] */
Joe Perchescf3b4292010-05-24 14:33:16 -0700121static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700122char *put_dec_full9(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700123{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700124 unsigned r;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700125
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800126 /*
127 * Possible ways to approx. divide by 10
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700128 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
129 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
130 * (x * 0x6667) >> 18 x < 43699
131 * (x * 0x3334) >> 17 x < 16389
132 * (x * 0x199a) >> 16 x < 16389
133 * (x * 0x0ccd) >> 15 x < 16389
134 * (x * 0x0667) >> 14 x < 2739
135 * (x * 0x0334) >> 13 x < 1029
136 * (x * 0x019a) >> 12 x < 1029
137 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
138 * (x * 0x0067) >> 10 x < 179
139 * (x * 0x0034) >> 9 x < 69 same
140 * (x * 0x001a) >> 8 x < 69 same
141 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
142 * (x * 0x0007) >> 6 x < 19
143 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800144 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700145 r = (q * (uint64_t)0x1999999a) >> 32;
146 *buf++ = (q - 10 * r) + '0'; /* 1 */
147 q = (r * (uint64_t)0x1999999a) >> 32;
148 *buf++ = (r - 10 * q) + '0'; /* 2 */
149 r = (q * (uint64_t)0x1999999a) >> 32;
150 *buf++ = (q - 10 * r) + '0'; /* 3 */
151 q = (r * (uint64_t)0x1999999a) >> 32;
152 *buf++ = (r - 10 * q) + '0'; /* 4 */
153 r = (q * (uint64_t)0x1999999a) >> 32;
154 *buf++ = (q - 10 * r) + '0'; /* 5 */
155 /* Now value is under 10000, can avoid 64-bit multiply */
156 q = (r * 0x199a) >> 16;
157 *buf++ = (r - 10 * q) + '0'; /* 6 */
158 r = (q * 0xcd) >> 11;
159 *buf++ = (q - 10 * r) + '0'; /* 7 */
160 q = (r * 0xcd) >> 11;
161 *buf++ = (r - 10 * q) + '0'; /* 8 */
162 *buf++ = q + '0'; /* 9 */
163 return buf;
164}
165#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700166
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700167/* Similar to above but do not pad with zeros.
168 * Code can be easily arranged to print 9 digits too, but our callers
169 * always call put_dec_full9() instead when the number has 9 decimal digits.
170 */
171static noinline_for_stack
172char *put_dec_trunc8(char *buf, unsigned r)
173{
174 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700175
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700176 /* Copy of previous function's body with added early returns */
177 q = (r * (uint64_t)0x1999999a) >> 32;
178 *buf++ = (r - 10 * q) + '0'; /* 2 */
179 if (q == 0)
180 return buf;
181 r = (q * (uint64_t)0x1999999a) >> 32;
182 *buf++ = (q - 10 * r) + '0'; /* 3 */
183 if (r == 0)
184 return buf;
185 q = (r * (uint64_t)0x1999999a) >> 32;
186 *buf++ = (r - 10 * q) + '0'; /* 4 */
187 if (q == 0)
188 return buf;
189 r = (q * (uint64_t)0x1999999a) >> 32;
190 *buf++ = (q - 10 * r) + '0'; /* 5 */
191 if (r == 0)
192 return buf;
193 q = (r * 0x199a) >> 16;
194 *buf++ = (r - 10 * q) + '0'; /* 6 */
195 if (q == 0)
196 return buf;
197 r = (q * 0xcd) >> 11;
198 *buf++ = (q - 10 * r) + '0'; /* 7 */
199 if (r == 0)
200 return buf;
201 q = (r * 0xcd) >> 11;
202 *buf++ = (r - 10 * q) + '0'; /* 8 */
203 if (q == 0)
204 return buf;
205 *buf++ = q + '0'; /* 9 */
206 return buf;
207}
208
209/* There are two algorithms to print larger numbers.
210 * One is generic: divide by 1000000000 and repeatedly print
211 * groups of (up to) 9 digits. It's conceptually simple,
212 * but requires a (unsigned long long) / 1000000000 division.
213 *
214 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
215 * manipulates them cleverly and generates groups of 4 decimal digits.
216 * It so happens that it does NOT require long long division.
217 *
218 * If long is > 32 bits, division of 64-bit values is relatively easy,
219 * and we will use the first algorithm.
220 * If long long is > 64 bits (strange architecture with VERY large long long),
221 * second algorithm can't be used, and we again use the first one.
222 *
223 * Else (if long is 32 bits and long long is 64 bits) we use second one.
224 */
225
226#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
227
228/* First algorithm: generic */
229
230static
231char *put_dec(char *buf, unsigned long long n)
232{
233 if (n >= 100*1000*1000) {
234 while (n >= 1000*1000*1000)
235 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
236 if (n >= 100*1000*1000)
237 return put_dec_full9(buf, n);
238 }
239 return put_dec_trunc8(buf, n);
240}
241
242#else
243
244/* Second algorithm: valid only for 64-bit long longs */
245
George Spelvine49317d2012-10-04 17:12:27 -0700246/* See comment in put_dec_full9 for choice of constants */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700247static noinline_for_stack
248char *put_dec_full4(char *buf, unsigned q)
249{
250 unsigned r;
George Spelvine49317d2012-10-04 17:12:27 -0700251 r = (q * 0xccd) >> 15;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700252 *buf++ = (q - 10 * r) + '0';
George Spelvine49317d2012-10-04 17:12:27 -0700253 q = (r * 0xcd) >> 11;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700254 *buf++ = (r - 10 * q) + '0';
255 r = (q * 0xcd) >> 11;
256 *buf++ = (q - 10 * r) + '0';
257 *buf++ = r + '0';
258 return buf;
259}
260
261/* Based on code by Douglas W. Jones found at
262 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
263 * (with permission from the author).
264 * Performs no 64-bit division and hence should be fast on 32-bit machines.
265 */
266static
267char *put_dec(char *buf, unsigned long long n)
268{
269 uint32_t d3, d2, d1, q, h;
270
271 if (n < 100*1000*1000)
272 return put_dec_trunc8(buf, n);
273
274 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
275 h = (n >> 32);
276 d2 = (h ) & 0xffff;
277 d3 = (h >> 16); /* implicit "& 0xffff" */
278
279 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
280
281 buf = put_dec_full4(buf, q % 10000);
282 q = q / 10000;
283
284 d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
285 buf = put_dec_full4(buf, d1 % 10000);
286 q = d1 / 10000;
287
288 d2 = q + 4749 * d3 + 42 * d2;
289 buf = put_dec_full4(buf, d2 % 10000);
290 q = d2 / 10000;
291
292 d3 = q + 281 * d3;
293 if (!d3)
294 goto done;
295 buf = put_dec_full4(buf, d3 % 10000);
296 q = d3 / 10000;
297 if (!q)
298 goto done;
299 buf = put_dec_full4(buf, q);
300 done:
301 while (buf[-1] == '0')
302 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800303
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700304 return buf;
305}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700306
307#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700308
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700309/*
310 * Convert passed number to decimal string.
311 * Returns the length of string. On buffer overflow, returns 0.
312 *
313 * If speed is not important, use snprintf(). It's easy to read the code.
314 */
315int num_to_str(char *buf, int size, unsigned long long num)
316{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700317 char tmp[sizeof(num) * 3];
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700318 int idx, len;
319
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700320 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
321 if (num <= 9) {
322 tmp[0] = '0' + num;
323 len = 1;
324 } else {
325 len = put_dec(tmp, num) - tmp;
326 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700327
328 if (len > size)
329 return 0;
330 for (idx = 0; idx < len; ++idx)
331 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700332 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#define ZEROPAD 1 /* pad with zero */
336#define SIGN 2 /* unsigned/signed long */
337#define PLUS 4 /* show plus */
338#define SPACE 8 /* space if plus */
339#define LEFT 16 /* left justified */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700340#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
341#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100343enum format_type {
344 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100345 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100346 FORMAT_TYPE_PRECISION,
347 FORMAT_TYPE_CHAR,
348 FORMAT_TYPE_STR,
349 FORMAT_TYPE_PTR,
350 FORMAT_TYPE_PERCENT_CHAR,
351 FORMAT_TYPE_INVALID,
352 FORMAT_TYPE_LONG_LONG,
353 FORMAT_TYPE_ULONG,
354 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800355 FORMAT_TYPE_UBYTE,
356 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100357 FORMAT_TYPE_USHORT,
358 FORMAT_TYPE_SHORT,
359 FORMAT_TYPE_UINT,
360 FORMAT_TYPE_INT,
361 FORMAT_TYPE_NRCHARS,
362 FORMAT_TYPE_SIZE_T,
363 FORMAT_TYPE_PTRDIFF
364};
365
366struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700367 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800368 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700369 u8 base; /* number base, 8, 10 or 16 only */
370 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
371 s16 field_width; /* width of output field */
372 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100373};
374
Joe Perchescf3b4292010-05-24 14:33:16 -0700375static noinline_for_stack
376char *number(char *buf, char *end, unsigned long long num,
377 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100379 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
380 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
381
382 char tmp[66];
383 char sign;
384 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100385 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700387 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100389 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
390 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100391 locase = (spec.flags & SMALL);
392 if (spec.flags & LEFT)
393 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100395 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800396 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800398 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100399 spec.field_width--;
400 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100402 spec.field_width--;
403 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100405 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700408 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100409 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700410 spec.field_width -= 2;
411 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100412 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700414
415 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700417 if (num < spec.base)
418 tmp[i++] = digits[num] | locase;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700419 /* Generic code, for any base:
420 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100421 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700422 } while (num != 0);
423 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100424 else if (spec.base != 10) { /* 8 or 16 */
425 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700426 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800427
428 if (spec.base == 16)
429 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700430 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100431 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700432 num >>= shift;
433 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700434 } else { /* base 10 */
435 i = put_dec(tmp, num) - tmp;
436 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700437
438 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100439 if (i > spec.precision)
440 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700441 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100442 spec.field_width -= spec.precision;
443 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800444 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700445 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *buf = ' ';
447 ++buf;
448 }
449 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700452 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *buf = sign;
454 ++buf;
455 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700456 /* "0x" / "0" prefix */
457 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700458 if (spec.base == 16 || !is_zero) {
459 if (buf < end)
460 *buf = '0';
461 ++buf;
462 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100463 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700464 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100465 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ++buf;
467 }
468 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700469 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100470 if (!(spec.flags & LEFT)) {
471 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
472 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700473 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *buf = c;
475 ++buf;
476 }
477 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700478 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100479 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700480 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *buf = '0';
482 ++buf;
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 /* actual digits of result */
485 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700486 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *buf = tmp[i];
488 ++buf;
489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100491 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700492 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *buf = ' ';
494 ++buf;
495 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return buf;
498}
499
Joe Perchescf3b4292010-05-24 14:33:16 -0700500static noinline_for_stack
501char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700502{
503 int len, i;
504
505 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800506 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700507
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100508 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700509
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100510 if (!(spec.flags & LEFT)) {
511 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700512 if (buf < end)
513 *buf = ' ';
514 ++buf;
515 }
516 }
517 for (i = 0; i < len; ++i) {
518 if (buf < end)
519 *buf = *s;
520 ++buf; ++s;
521 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100522 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700523 if (buf < end)
524 *buf = ' ';
525 ++buf;
526 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800527
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700528 return buf;
529}
530
Joe Perchescf3b4292010-05-24 14:33:16 -0700531static noinline_for_stack
532char *symbol_string(char *buf, char *end, void *ptr,
533 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700534{
535 unsigned long value = (unsigned long) ptr;
536#ifdef CONFIG_KALLSYMS
537 char sym[KSYM_SYMBOL_LEN];
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900538 if (ext == 'B')
539 sprint_backtrace(sym, value);
540 else if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200541 sprint_symbol(sym, value);
542 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700543 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800544
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100545 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700546#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800547 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100548 spec.flags |= SPECIAL | SMALL | ZEROPAD;
549 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800550
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100551 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700552#endif
553}
554
Joe Perchescf3b4292010-05-24 14:33:16 -0700555static noinline_for_stack
556char *resource_string(char *buf, char *end, struct resource *res,
557 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100558{
559#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600560#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100561#endif
562
563#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600564#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100565#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700566 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100567 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700568 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100569 .precision = -1,
570 .flags = SPECIAL | SMALL | ZEROPAD,
571 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700572 static const struct printf_spec mem_spec = {
573 .base = 16,
574 .field_width = MEM_RSRC_PRINTK_SIZE,
575 .precision = -1,
576 .flags = SPECIAL | SMALL | ZEROPAD,
577 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700578 static const struct printf_spec bus_spec = {
579 .base = 16,
580 .field_width = 2,
581 .precision = -1,
582 .flags = SMALL | ZEROPAD,
583 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700584 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600585 .base = 10,
586 .precision = -1,
587 .flags = 0,
588 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700589 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600590 .field_width = -1,
591 .precision = 10,
592 .flags = LEFT,
593 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700594 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600595 .base = 16,
596 .precision = -1,
597 .flags = SPECIAL | SMALL,
598 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600599
600 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
601 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
602#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
603#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700604#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600605#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
606 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
607 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
608
Linus Torvalds332d2e72008-10-20 15:07:34 +1100609 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600610 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700611 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100612
613 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700614 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600615 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700616 specp = &io_spec;
617 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600618 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700619 specp = &mem_spec;
620 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600621 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700622 specp = &dec_spec;
623 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600624 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700625 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700626 } else if (res->flags & IORESOURCE_BUS) {
627 p = string(p, pend, "bus ", str_spec);
628 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700629 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600630 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700631 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600632 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600633 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700634 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600635 if (res->start != res->end) {
636 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700637 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600638 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600639 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600640 if (res->flags & IORESOURCE_MEM_64)
641 p = string(p, pend, " 64bit", str_spec);
642 if (res->flags & IORESOURCE_PREFETCH)
643 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700644 if (res->flags & IORESOURCE_WINDOW)
645 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600646 if (res->flags & IORESOURCE_DISABLED)
647 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600648 } else {
649 p = string(p, pend, " flags ", str_spec);
650 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600651 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100652 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600653 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100654
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100655 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100656}
657
Joe Perchescf3b4292010-05-24 14:33:16 -0700658static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700659char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
660 const char *fmt)
661{
662 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
663 negative value, fallback to the default */
664 char separator;
665
666 if (spec.field_width == 0)
667 /* nothing to print */
668 return buf;
669
670 if (ZERO_OR_NULL_PTR(addr))
671 /* NULL pointer */
672 return string(buf, end, NULL, spec);
673
674 switch (fmt[1]) {
675 case 'C':
676 separator = ':';
677 break;
678 case 'D':
679 separator = '-';
680 break;
681 case 'N':
682 separator = 0;
683 break;
684 default:
685 separator = ' ';
686 break;
687 }
688
689 if (spec.field_width > 0)
690 len = min_t(int, spec.field_width, 64);
691
692 for (i = 0; i < len && buf < end - 1; i++) {
693 buf = hex_byte_pack(buf, addr[i]);
694
695 if (buf < end && separator && i != len - 1)
696 *buf++ = separator;
697 }
698
699 return buf;
700}
701
702static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700703char *mac_address_string(char *buf, char *end, u8 *addr,
704 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700705{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000706 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700707 char *p = mac_addr;
708 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000709 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700710 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000711
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700712 switch (fmt[1]) {
713 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000714 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700715 break;
716
717 case 'R':
718 reversed = true;
719 /* fall through */
720
721 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000722 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700723 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000724 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700725
726 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700727 if (reversed)
728 p = hex_byte_pack(p, addr[5 - i]);
729 else
730 p = hex_byte_pack(p, addr[i]);
731
Joe Perches8a27f7c2009-08-17 12:29:44 +0000732 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000733 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700734 }
735 *p = '\0';
736
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100737 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700738}
739
Joe Perchescf3b4292010-05-24 14:33:16 -0700740static noinline_for_stack
741char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700742{
Harvey Harrison689afa72008-10-28 16:04:44 -0700743 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800744 bool leading_zeros = (fmt[0] == 'i');
745 int index;
746 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700747
Joe Perches0159f242010-01-13 20:23:30 -0800748 switch (fmt[2]) {
749 case 'h':
750#ifdef __BIG_ENDIAN
751 index = 0;
752 step = 1;
753#else
754 index = 3;
755 step = -1;
756#endif
757 break;
758 case 'l':
759 index = 3;
760 step = -1;
761 break;
762 case 'n':
763 case 'b':
764 default:
765 index = 0;
766 step = 1;
767 break;
768 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000769 for (i = 0; i < 4; i++) {
770 char temp[3]; /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700771 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000772 if (leading_zeros) {
773 if (digits < 3)
774 *p++ = '0';
775 if (digits < 2)
776 *p++ = '0';
777 }
778 /* reverse the digits in the quad */
779 while (digits--)
780 *p++ = temp[digits];
781 if (i < 3)
782 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800783 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000784 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000785 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800786
Joe Perches8a27f7c2009-08-17 12:29:44 +0000787 return p;
788}
789
Joe Perchescf3b4292010-05-24 14:33:16 -0700790static noinline_for_stack
791char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000792{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800793 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000794 unsigned char zerolength[8];
795 int longest = 1;
796 int colonpos = -1;
797 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800798 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000799 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000800 bool useIPv4;
801 struct in6_addr in6;
802
803 memcpy(&in6, addr, sizeof(struct in6_addr));
804
805 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000806
807 memset(zerolength, 0, sizeof(zerolength));
808
809 if (useIPv4)
810 range = 6;
811 else
812 range = 8;
813
814 /* find position of longest 0 run */
815 for (i = 0; i < range; i++) {
816 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000817 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000818 break;
819 zerolength[i]++;
820 }
821 }
822 for (i = 0; i < range; i++) {
823 if (zerolength[i] > longest) {
824 longest = zerolength[i];
825 colonpos = i;
826 }
827 }
Joe Perches29cf5192011-06-09 11:23:37 -0700828 if (longest == 1) /* don't compress a single 0 */
829 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000830
831 /* emit address */
832 for (i = 0; i < range; i++) {
833 if (i == colonpos) {
834 if (needcolon || i == 0)
835 *p++ = ':';
836 *p++ = ':';
837 needcolon = false;
838 i += longest - 1;
839 continue;
840 }
841 if (needcolon) {
842 *p++ = ':';
843 needcolon = false;
844 }
845 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000846 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000847 hi = word >> 8;
848 lo = word & 0xff;
849 if (hi) {
850 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700851 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000852 else
853 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700854 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000855 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800856 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700857 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000858 else
859 *p++ = hex_asc_lo(lo);
860 needcolon = true;
861 }
862
863 if (useIPv4) {
864 if (needcolon)
865 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800866 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000867 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000868 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800869
Joe Perches8a27f7c2009-08-17 12:29:44 +0000870 return p;
871}
872
Joe Perchescf3b4292010-05-24 14:33:16 -0700873static noinline_for_stack
874char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000875{
876 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800877
Harvey Harrison689afa72008-10-28 16:04:44 -0700878 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700879 p = hex_byte_pack(p, *addr++);
880 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000881 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700882 *p++ = ':';
883 }
884 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800885
Joe Perches8a27f7c2009-08-17 12:29:44 +0000886 return p;
887}
888
Joe Perchescf3b4292010-05-24 14:33:16 -0700889static noinline_for_stack
890char *ip6_addr_string(char *buf, char *end, const u8 *addr,
891 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000892{
893 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
894
895 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000896 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000897 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000898 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700899
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100900 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700901}
902
Joe Perchescf3b4292010-05-24 14:33:16 -0700903static noinline_for_stack
904char *ip4_addr_string(char *buf, char *end, const u8 *addr,
905 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700906{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000907 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700908
Joe Perches0159f242010-01-13 20:23:30 -0800909 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700910
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100911 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700912}
913
Joe Perchescf3b4292010-05-24 14:33:16 -0700914static noinline_for_stack
915char *uuid_string(char *buf, char *end, const u8 *addr,
916 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800917{
918 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
919 char *p = uuid;
920 int i;
921 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
922 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
923 const u8 *index = be;
924 bool uc = false;
925
926 switch (*(++fmt)) {
927 case 'L':
928 uc = true; /* fall-through */
929 case 'l':
930 index = le;
931 break;
932 case 'B':
933 uc = true;
934 break;
935 }
936
937 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700938 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -0800939 switch (i) {
940 case 3:
941 case 5:
942 case 7:
943 case 9:
944 *p++ = '-';
945 break;
946 }
947 }
948
949 *p = 0;
950
951 if (uc) {
952 p = uuid;
953 do {
954 *p = toupper(*p);
955 } while (*(++p));
956 }
957
958 return string(buf, end, uuid, spec);
959}
960
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000961static
962char *netdev_feature_string(char *buf, char *end, const u8 *addr,
963 struct printf_spec spec)
964{
965 spec.flags |= SPECIAL | SMALL | ZEROPAD;
966 if (spec.field_width == -1)
967 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
968 spec.base = 16;
969
970 return number(buf, end, *(const netdev_features_t *)addr, spec);
971}
972
Ingo Molnar411f05f2011-05-12 23:00:28 +0200973int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800974
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700975/*
976 * Show a '%p' thing. A kernel extension is that the '%p' is followed
977 * by an extra set of alphanumeric characters that are extended format
978 * specifiers.
979 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100980 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700981 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200982 * - 'F' For symbolic function descriptor pointers with offset
983 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400984 * - 'S' For symbolic direct pointers with offset
985 * - 's' For symbolic direct pointers without offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900986 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600987 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
988 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700989 * - 'M' For a 6-byte MAC address, it prints the address in the
990 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000991 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +0000992 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -0800993 * with a dash-separated hex notation
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700994 * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000995 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
996 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
997 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
998 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
999 * IPv6 omits the colons (01020304...0f)
1000 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -08001001 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +00001002 * - 'I6c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001003 * http://tools.ietf.org/html/rfc5952
Joe Perches9ac6e442009-12-14 18:01:09 -08001004 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1005 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1006 * Options for %pU are:
1007 * b big endian lower case hex (default)
1008 * B big endian UPPER case hex
1009 * l little endian lower case hex
1010 * L little endian UPPER case hex
1011 * big endian output byte order is:
1012 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1013 * little endian output byte order is:
1014 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001015 * - 'V' For a struct va_format which contains a format string * and va_list *,
1016 * call vsnprintf(->format, *->va_list).
1017 * Implements a "recursive vsnprintf".
1018 * Do not use this feature without some mechanism to verify the
1019 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001020 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001021 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001022 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1023 * a certain separator (' ' by default):
1024 * C colon
1025 * D dash
1026 * N no separator
1027 * The maximum supported length is 64 bytes of the input. Consider
1028 * to use print_hex_dump() for the larger input.
Joe Perches9ac6e442009-12-14 18:01:09 -08001029 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001030 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1031 * function pointers are really function descriptors, which contain a
1032 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001033 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001034static noinline_for_stack
1035char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1036 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001037{
Grant Likely725fe002012-05-31 16:26:08 -07001038 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1039
Kees Cook9f36e2c2011-03-22 16:34:22 -07001040 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001041 /*
1042 * Print (null) with the same width as a pointer so it makes
1043 * tabular output look nice.
1044 */
1045 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001046 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001047 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001048 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001049
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001050 switch (*fmt) {
1051 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001052 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001053 ptr = dereference_function_descriptor(ptr);
1054 /* Fallthrough */
1055 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001056 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001057 case 'B':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001058 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001059 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001060 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001061 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001062 case 'h':
1063 return hex_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001064 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1065 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001066 /* [mM]F (FDDI) */
1067 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001068 return mac_address_string(buf, end, ptr, spec, fmt);
1069 case 'I': /* Formatted IP supported
1070 * 4: 1.2.3.4
1071 * 6: 0001:0203:...:0708
1072 * 6c: 1::708 or 1::1.2.3.4
1073 */
1074 case 'i': /* Contiguous:
1075 * 4: 001.002.003.004
1076 * 6: 000102...0f
1077 */
1078 switch (fmt[1]) {
1079 case '6':
1080 return ip6_addr_string(buf, end, ptr, spec, fmt);
1081 case '4':
1082 return ip4_addr_string(buf, end, ptr, spec, fmt);
1083 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001084 break;
Joe Perches9ac6e442009-12-14 18:01:09 -08001085 case 'U':
1086 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001087 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001088 {
1089 va_list va;
1090
1091 va_copy(va, *((struct va_format *)ptr)->va);
1092 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1093 ((struct va_format *)ptr)->fmt, va);
1094 va_end(va);
1095 return buf;
1096 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001097 case 'K':
1098 /*
1099 * %pK cannot be used in IRQ context because its test
1100 * for CAP_SYSLOG would be meaningless.
1101 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001102 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1103 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001104 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001105 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001106 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001107 }
Joe Perches26297602011-03-22 16:34:19 -07001108 if (!((kptr_restrict == 0) ||
1109 (kptr_restrict == 1 &&
1110 has_capability_noaudit(current, CAP_SYSLOG))))
1111 ptr = NULL;
1112 break;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001113 case 'N':
1114 switch (fmt[1]) {
1115 case 'F':
1116 return netdev_feature_string(buf, end, ptr, spec);
1117 }
1118 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001119 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001120 spec.flags |= SMALL;
1121 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001122 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001123 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001124 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001125 spec.base = 16;
1126
1127 return number(buf, end, (unsigned long) ptr, spec);
1128}
1129
1130/*
1131 * Helper function to decode printf style format.
1132 * Each call decode a token from the format and return the
1133 * number of characters read (or likely the delta where it wants
1134 * to go on the next call).
1135 * The decoded token is returned through the parameters
1136 *
1137 * 'h', 'l', or 'L' for integer fields
1138 * 'z' support added 23/7/1999 S.H.
1139 * 'z' changed to 'Z' --davidm 1/25/99
1140 * 't' added for ptrdiff_t
1141 *
1142 * @fmt: the format string
1143 * @type of the token returned
1144 * @flags: various flags such as +, -, # tokens..
1145 * @field_width: overwritten width
1146 * @base: base of the number (octal, hex, ...)
1147 * @precision: precision of a number
1148 * @qualifier: qualifier of a number (long, size_t, ...)
1149 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001150static noinline_for_stack
1151int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001152{
1153 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001154
1155 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001156 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001157 if (spec->field_width < 0) {
1158 spec->field_width = -spec->field_width;
1159 spec->flags |= LEFT;
1160 }
1161 spec->type = FORMAT_TYPE_NONE;
1162 goto precision;
1163 }
1164
1165 /* we finished early by reading the precision */
1166 if (spec->type == FORMAT_TYPE_PRECISION) {
1167 if (spec->precision < 0)
1168 spec->precision = 0;
1169
1170 spec->type = FORMAT_TYPE_NONE;
1171 goto qualifier;
1172 }
1173
1174 /* By default */
1175 spec->type = FORMAT_TYPE_NONE;
1176
1177 for (; *fmt ; ++fmt) {
1178 if (*fmt == '%')
1179 break;
1180 }
1181
1182 /* Return the current non-format string */
1183 if (fmt != start || !*fmt)
1184 return fmt - start;
1185
1186 /* Process flags */
1187 spec->flags = 0;
1188
1189 while (1) { /* this also skips first '%' */
1190 bool found = true;
1191
1192 ++fmt;
1193
1194 switch (*fmt) {
1195 case '-': spec->flags |= LEFT; break;
1196 case '+': spec->flags |= PLUS; break;
1197 case ' ': spec->flags |= SPACE; break;
1198 case '#': spec->flags |= SPECIAL; break;
1199 case '0': spec->flags |= ZEROPAD; break;
1200 default: found = false;
1201 }
1202
1203 if (!found)
1204 break;
1205 }
1206
1207 /* get field width */
1208 spec->field_width = -1;
1209
1210 if (isdigit(*fmt))
1211 spec->field_width = skip_atoi(&fmt);
1212 else if (*fmt == '*') {
1213 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001214 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001215 return ++fmt - start;
1216 }
1217
1218precision:
1219 /* get the precision */
1220 spec->precision = -1;
1221 if (*fmt == '.') {
1222 ++fmt;
1223 if (isdigit(*fmt)) {
1224 spec->precision = skip_atoi(&fmt);
1225 if (spec->precision < 0)
1226 spec->precision = 0;
1227 } else if (*fmt == '*') {
1228 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001229 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001230 return ++fmt - start;
1231 }
1232 }
1233
1234qualifier:
1235 /* get the conversion qualifier */
1236 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001237 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1238 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001239 spec->qualifier = *fmt++;
1240 if (unlikely(spec->qualifier == *fmt)) {
1241 if (spec->qualifier == 'l') {
1242 spec->qualifier = 'L';
1243 ++fmt;
1244 } else if (spec->qualifier == 'h') {
1245 spec->qualifier = 'H';
1246 ++fmt;
1247 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001248 }
1249 }
1250
1251 /* default base */
1252 spec->base = 10;
1253 switch (*fmt) {
1254 case 'c':
1255 spec->type = FORMAT_TYPE_CHAR;
1256 return ++fmt - start;
1257
1258 case 's':
1259 spec->type = FORMAT_TYPE_STR;
1260 return ++fmt - start;
1261
1262 case 'p':
1263 spec->type = FORMAT_TYPE_PTR;
1264 return fmt - start;
1265 /* skip alnum */
1266
1267 case 'n':
1268 spec->type = FORMAT_TYPE_NRCHARS;
1269 return ++fmt - start;
1270
1271 case '%':
1272 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1273 return ++fmt - start;
1274
1275 /* integer number formats - set up the flags and "break" */
1276 case 'o':
1277 spec->base = 8;
1278 break;
1279
1280 case 'x':
1281 spec->flags |= SMALL;
1282
1283 case 'X':
1284 spec->base = 16;
1285 break;
1286
1287 case 'd':
1288 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001289 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001290 case 'u':
1291 break;
1292
1293 default:
1294 spec->type = FORMAT_TYPE_INVALID;
1295 return fmt - start;
1296 }
1297
1298 if (spec->qualifier == 'L')
1299 spec->type = FORMAT_TYPE_LONG_LONG;
1300 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001301 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001302 spec->type = FORMAT_TYPE_LONG;
1303 else
1304 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001305 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001306 spec->type = FORMAT_TYPE_SIZE_T;
1307 } else if (spec->qualifier == 't') {
1308 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001309 } else if (spec->qualifier == 'H') {
1310 if (spec->flags & SIGN)
1311 spec->type = FORMAT_TYPE_BYTE;
1312 else
1313 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001314 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001315 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001316 spec->type = FORMAT_TYPE_SHORT;
1317 else
1318 spec->type = FORMAT_TYPE_USHORT;
1319 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001320 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001321 spec->type = FORMAT_TYPE_INT;
1322 else
1323 spec->type = FORMAT_TYPE_UINT;
1324 }
1325
1326 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/**
1330 * vsnprintf - Format a string and place it in a buffer
1331 * @buf: The buffer to place the result into
1332 * @size: The size of the buffer, including the trailing null space
1333 * @fmt: The format string to use
1334 * @args: Arguments for the format string
1335 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001336 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001337 * %pS output the name of a text symbol with offset
1338 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001339 * %pF output the name of a function pointer with its offset
1340 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001341 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001342 * %pR output the address range in a struct resource with decoded flags
1343 * %pr output the address range in a struct resource with raw flags
1344 * %pM output a 6-byte MAC address with colons
1345 * %pm output a 6-byte MAC address without colons
1346 * %pI4 print an IPv4 address without leading zeros
1347 * %pi4 print an IPv4 address with leading zeros
1348 * %pI6 print an IPv6 address with colons
1349 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001350 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001351 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1352 * case.
Andy Shevchenko31550a12012-07-30 14:40:27 -07001353 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1354 * bytes of the input)
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001355 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001356 *
Andrew Morton80f548e2012-07-30 14:40:25 -07001357 * ** Please update Documentation/printk-formats.txt when making changes **
1358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * The return value is the number of characters which would
1360 * be generated for the given input, excluding the trailing
1361 * '\0', as per ISO C99. If you want to have the exact
1362 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001363 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * return is greater than or equal to @size, the resulting
1365 * string is truncated.
1366 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001367 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
1369int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1370{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001372 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001373 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001375 /* Reject out-of-range values early. Large positive sizes are
1376 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001377 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001381 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001383 /* Make sure end is always >= buf */
1384 if (end < buf) {
1385 end = ((void *)-1);
1386 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001389 while (*fmt) {
1390 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001391 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001392
1393 fmt += read;
1394
1395 switch (spec.type) {
1396 case FORMAT_TYPE_NONE: {
1397 int copy = read;
1398 if (str < end) {
1399 if (copy > end - str)
1400 copy = end - str;
1401 memcpy(str, old_fmt, copy);
1402 }
1403 str += read;
1404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Vegard Nossumed681a92009-03-14 12:08:50 +01001407 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001408 spec.field_width = va_arg(args, int);
1409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001411 case FORMAT_TYPE_PRECISION:
1412 spec.precision = va_arg(args, int);
1413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
André Goddard Rosad4be1512009-12-14 18:00:59 -08001415 case FORMAT_TYPE_CHAR: {
1416 char c;
1417
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001418 if (!(spec.flags & LEFT)) {
1419 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001420 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 *str = ' ';
1422 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001425 }
1426 c = (unsigned char) va_arg(args, int);
1427 if (str < end)
1428 *str = c;
1429 ++str;
1430 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001431 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001432 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001434 }
1435 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001438 case FORMAT_TYPE_STR:
1439 str = string(str, end, va_arg(args, char *), spec);
1440 break;
1441
1442 case FORMAT_TYPE_PTR:
1443 str = pointer(fmt+1, str, end, va_arg(args, void *),
1444 spec);
1445 while (isalnum(*fmt))
1446 fmt++;
1447 break;
1448
1449 case FORMAT_TYPE_PERCENT_CHAR:
1450 if (str < end)
1451 *str = '%';
1452 ++str;
1453 break;
1454
1455 case FORMAT_TYPE_INVALID:
1456 if (str < end)
1457 *str = '%';
1458 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001459 break;
1460
1461 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001462 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001463
1464 if (qualifier == 'l') {
1465 long *ip = va_arg(args, long *);
1466 *ip = (str - buf);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001467 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001468 size_t *ip = va_arg(args, size_t *);
1469 *ip = (str - buf);
1470 } else {
1471 int *ip = va_arg(args, int *);
1472 *ip = (str - buf);
1473 }
1474 break;
1475 }
1476
1477 default:
1478 switch (spec.type) {
1479 case FORMAT_TYPE_LONG_LONG:
1480 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001482 case FORMAT_TYPE_ULONG:
1483 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001485 case FORMAT_TYPE_LONG:
1486 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001488 case FORMAT_TYPE_SIZE_T:
1489 num = va_arg(args, size_t);
1490 break;
1491 case FORMAT_TYPE_PTRDIFF:
1492 num = va_arg(args, ptrdiff_t);
1493 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001494 case FORMAT_TYPE_UBYTE:
1495 num = (unsigned char) va_arg(args, int);
1496 break;
1497 case FORMAT_TYPE_BYTE:
1498 num = (signed char) va_arg(args, int);
1499 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001500 case FORMAT_TYPE_USHORT:
1501 num = (unsigned short) va_arg(args, int);
1502 break;
1503 case FORMAT_TYPE_SHORT:
1504 num = (short) va_arg(args, int);
1505 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001506 case FORMAT_TYPE_INT:
1507 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001510 num = va_arg(args, unsigned int);
1511 }
1512
1513 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001516
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001517 if (size > 0) {
1518 if (str < end)
1519 *str = '\0';
1520 else
Linus Torvalds0a6047ee2006-06-28 17:09:34 -07001521 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001522 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001523
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001524 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528EXPORT_SYMBOL(vsnprintf);
1529
1530/**
1531 * vscnprintf - Format a string and place it in a buffer
1532 * @buf: The buffer to place the result into
1533 * @size: The size of the buffer, including the trailing null space
1534 * @fmt: The format string to use
1535 * @args: Arguments for the format string
1536 *
1537 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001538 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * returns 0.
1540 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001541 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001542 *
1543 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
1545int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1546{
1547 int i;
1548
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001549 i = vsnprintf(buf, size, fmt, args);
1550
Anton Arapovb921c692011-01-12 16:59:49 -08001551 if (likely(i < size))
1552 return i;
1553 if (size != 0)
1554 return size - 1;
1555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557EXPORT_SYMBOL(vscnprintf);
1558
1559/**
1560 * snprintf - Format a string and place it in a buffer
1561 * @buf: The buffer to place the result into
1562 * @size: The size of the buffer, including the trailing null space
1563 * @fmt: The format string to use
1564 * @...: Arguments for the format string
1565 *
1566 * The return value is the number of characters which would be
1567 * generated for the given input, excluding the trailing null,
1568 * as per ISO C99. If the return is greater than or equal to
1569 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001570 *
1571 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001573int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 va_list args;
1576 int i;
1577
1578 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001579 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return i;
1583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584EXPORT_SYMBOL(snprintf);
1585
1586/**
1587 * scnprintf - Format a string and place it in a buffer
1588 * @buf: The buffer to place the result into
1589 * @size: The size of the buffer, including the trailing null space
1590 * @fmt: The format string to use
1591 * @...: Arguments for the format string
1592 *
1593 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001594 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
1596
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001597int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 va_list args;
1600 int i;
1601
1602 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001603 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001605
Anton Arapovb921c692011-01-12 16:59:49 -08001606 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608EXPORT_SYMBOL(scnprintf);
1609
1610/**
1611 * vsprintf - Format a string and place it in a buffer
1612 * @buf: The buffer to place the result into
1613 * @fmt: The format string to use
1614 * @args: Arguments for the format string
1615 *
1616 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001617 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 * buffer overflows.
1619 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001620 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001621 *
1622 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 */
1624int vsprintf(char *buf, const char *fmt, va_list args)
1625{
1626 return vsnprintf(buf, INT_MAX, fmt, args);
1627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628EXPORT_SYMBOL(vsprintf);
1629
1630/**
1631 * sprintf - Format a string and place it in a buffer
1632 * @buf: The buffer to place the result into
1633 * @fmt: The format string to use
1634 * @...: Arguments for the format string
1635 *
1636 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001637 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001639 *
1640 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001642int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 va_list args;
1645 int i;
1646
1647 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001648 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return i;
1652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653EXPORT_SYMBOL(sprintf);
1654
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001655#ifdef CONFIG_BINARY_PRINTF
1656/*
1657 * bprintf service:
1658 * vbin_printf() - VA arguments to binary data
1659 * bstr_printf() - Binary data to text string
1660 */
1661
1662/**
1663 * vbin_printf - Parse a format string and place args' binary value in a buffer
1664 * @bin_buf: The buffer to place args' binary value
1665 * @size: The size of the buffer(by words(32bits), not characters)
1666 * @fmt: The format string to use
1667 * @args: Arguments for the format string
1668 *
1669 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1670 * is skiped.
1671 *
1672 * The return value is the number of words(32bits) which would be generated for
1673 * the given input.
1674 *
1675 * NOTE:
1676 * If the return value is greater than @size, the resulting bin_buf is NOT
1677 * valid for bstr_printf().
1678 */
1679int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1680{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001681 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001682 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001683
1684 str = (char *)bin_buf;
1685 end = (char *)(bin_buf + size);
1686
1687#define save_arg(type) \
1688do { \
1689 if (sizeof(type) == 8) { \
1690 unsigned long long value; \
1691 str = PTR_ALIGN(str, sizeof(u32)); \
1692 value = va_arg(args, unsigned long long); \
1693 if (str + sizeof(type) <= end) { \
1694 *(u32 *)str = *(u32 *)&value; \
1695 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1696 } \
1697 } else { \
1698 unsigned long value; \
1699 str = PTR_ALIGN(str, sizeof(type)); \
1700 value = va_arg(args, int); \
1701 if (str + sizeof(type) <= end) \
1702 *(typeof(type) *)str = (type)value; \
1703 } \
1704 str += sizeof(type); \
1705} while (0)
1706
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001707 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001708 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001709
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001710 fmt += read;
1711
1712 switch (spec.type) {
1713 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001714 case FORMAT_TYPE_INVALID:
1715 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001716 break;
1717
Vegard Nossumed681a92009-03-14 12:08:50 +01001718 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001719 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001720 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001721 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001722
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001723 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001724 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001725 break;
1726
1727 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001728 const char *save_str = va_arg(args, char *);
1729 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001730
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001731 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1732 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001733 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001734 len = strlen(save_str) + 1;
1735 if (str + len < end)
1736 memcpy(str, save_str, len);
1737 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001738 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001739 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001740
1741 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001742 save_arg(void *);
1743 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001744 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001745 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001746 break;
1747
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001748 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001749 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001750 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001751 void *skip_arg;
1752 if (qualifier == 'l')
1753 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001754 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001755 skip_arg = va_arg(args, size_t *);
1756 else
1757 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001758 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001759 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001760
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001761 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001762 switch (spec.type) {
1763
1764 case FORMAT_TYPE_LONG_LONG:
1765 save_arg(long long);
1766 break;
1767 case FORMAT_TYPE_ULONG:
1768 case FORMAT_TYPE_LONG:
1769 save_arg(unsigned long);
1770 break;
1771 case FORMAT_TYPE_SIZE_T:
1772 save_arg(size_t);
1773 break;
1774 case FORMAT_TYPE_PTRDIFF:
1775 save_arg(ptrdiff_t);
1776 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001777 case FORMAT_TYPE_UBYTE:
1778 case FORMAT_TYPE_BYTE:
1779 save_arg(char);
1780 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001781 case FORMAT_TYPE_USHORT:
1782 case FORMAT_TYPE_SHORT:
1783 save_arg(short);
1784 break;
1785 default:
1786 save_arg(int);
1787 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001788 }
1789 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001790
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001791 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001792#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001793}
1794EXPORT_SYMBOL_GPL(vbin_printf);
1795
1796/**
1797 * bstr_printf - Format a string from binary arguments and place it in a buffer
1798 * @buf: The buffer to place the result into
1799 * @size: The size of the buffer, including the trailing null space
1800 * @fmt: The format string to use
1801 * @bin_buf: Binary arguments for the format string
1802 *
1803 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1804 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1805 * a binary buffer that generated by vbin_printf.
1806 *
1807 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001808 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001809 *
1810 * The return value is the number of characters which would
1811 * be generated for the given input, excluding the trailing
1812 * '\0', as per ISO C99. If you want to have the exact
1813 * number of characters written into @buf as return value
1814 * (not including the trailing '\0'), use vscnprintf(). If the
1815 * return is greater than or equal to @size, the resulting
1816 * string is truncated.
1817 */
1818int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1819{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001820 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001821 char *str, *end;
1822 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001823
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001824 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001825 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001826
1827 str = buf;
1828 end = buf + size;
1829
1830#define get_arg(type) \
1831({ \
1832 typeof(type) value; \
1833 if (sizeof(type) == 8) { \
1834 args = PTR_ALIGN(args, sizeof(u32)); \
1835 *(u32 *)&value = *(u32 *)args; \
1836 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1837 } else { \
1838 args = PTR_ALIGN(args, sizeof(type)); \
1839 value = *(typeof(type) *)args; \
1840 } \
1841 args += sizeof(type); \
1842 value; \
1843})
1844
1845 /* Make sure end is always >= buf */
1846 if (end < buf) {
1847 end = ((void *)-1);
1848 size = end - buf;
1849 }
1850
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001851 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001852 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001853 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001854
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001855 fmt += read;
1856
1857 switch (spec.type) {
1858 case FORMAT_TYPE_NONE: {
1859 int copy = read;
1860 if (str < end) {
1861 if (copy > end - str)
1862 copy = end - str;
1863 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001864 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001865 str += read;
1866 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001867 }
1868
Vegard Nossumed681a92009-03-14 12:08:50 +01001869 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001870 spec.field_width = get_arg(int);
1871 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001872
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001873 case FORMAT_TYPE_PRECISION:
1874 spec.precision = get_arg(int);
1875 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001876
André Goddard Rosad4be1512009-12-14 18:00:59 -08001877 case FORMAT_TYPE_CHAR: {
1878 char c;
1879
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001880 if (!(spec.flags & LEFT)) {
1881 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001882 if (str < end)
1883 *str = ' ';
1884 ++str;
1885 }
1886 }
1887 c = (unsigned char) get_arg(char);
1888 if (str < end)
1889 *str = c;
1890 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001891 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001892 if (str < end)
1893 *str = ' ';
1894 ++str;
1895 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001896 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001897 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001898
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001899 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001900 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001901 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001902 str = string(str, end, (char *)str_arg, spec);
1903 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001904 }
1905
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001906 case FORMAT_TYPE_PTR:
1907 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1908 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001909 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001910 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001911
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001912 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001913 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001914 if (str < end)
1915 *str = '%';
1916 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001917 break;
1918
1919 case FORMAT_TYPE_NRCHARS:
1920 /* skip */
1921 break;
1922
André Goddard Rosad4be1512009-12-14 18:00:59 -08001923 default: {
1924 unsigned long long num;
1925
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001926 switch (spec.type) {
1927
1928 case FORMAT_TYPE_LONG_LONG:
1929 num = get_arg(long long);
1930 break;
1931 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001932 case FORMAT_TYPE_LONG:
1933 num = get_arg(unsigned long);
1934 break;
1935 case FORMAT_TYPE_SIZE_T:
1936 num = get_arg(size_t);
1937 break;
1938 case FORMAT_TYPE_PTRDIFF:
1939 num = get_arg(ptrdiff_t);
1940 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001941 case FORMAT_TYPE_UBYTE:
1942 num = get_arg(unsigned char);
1943 break;
1944 case FORMAT_TYPE_BYTE:
1945 num = get_arg(signed char);
1946 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001947 case FORMAT_TYPE_USHORT:
1948 num = get_arg(unsigned short);
1949 break;
1950 case FORMAT_TYPE_SHORT:
1951 num = get_arg(short);
1952 break;
1953 case FORMAT_TYPE_UINT:
1954 num = get_arg(unsigned int);
1955 break;
1956 default:
1957 num = get_arg(int);
1958 }
1959
1960 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001961 } /* default: */
1962 } /* switch(spec.type) */
1963 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001964
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001965 if (size > 0) {
1966 if (str < end)
1967 *str = '\0';
1968 else
1969 end[-1] = '\0';
1970 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001971
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001972#undef get_arg
1973
1974 /* the trailing null byte doesn't count towards the total */
1975 return str - buf;
1976}
1977EXPORT_SYMBOL_GPL(bstr_printf);
1978
1979/**
1980 * bprintf - Parse a format string and place args' binary value in a buffer
1981 * @bin_buf: The buffer to place args' binary value
1982 * @size: The size of the buffer(by words(32bits), not characters)
1983 * @fmt: The format string to use
1984 * @...: Arguments for the format string
1985 *
1986 * The function returns the number of words(u32) written
1987 * into @bin_buf.
1988 */
1989int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1990{
1991 va_list args;
1992 int ret;
1993
1994 va_start(args, fmt);
1995 ret = vbin_printf(bin_buf, size, fmt, args);
1996 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001997
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001998 return ret;
1999}
2000EXPORT_SYMBOL_GPL(bprintf);
2001
2002#endif /* CONFIG_BINARY_PRINTF */
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/**
2005 * vsscanf - Unformat a buffer into a list of arguments
2006 * @buf: input buffer
2007 * @fmt: format of buffer
2008 * @args: arguments
2009 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002010int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 const char *str = buf;
2013 char *next;
2014 char digit;
2015 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002016 u8 qualifier;
2017 u8 base;
2018 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002019 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002021 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 /* skip any white space in format */
2023 /* white space in format matchs any amount of
2024 * white space, including none, in the input.
2025 */
2026 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002027 fmt = skip_spaces(++fmt);
2028 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030
2031 /* anything that is not a conversion must match exactly */
2032 if (*fmt != '%' && *fmt) {
2033 if (*fmt++ != *str++)
2034 break;
2035 continue;
2036 }
2037
2038 if (!*fmt)
2039 break;
2040 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* skip this conversion.
2043 * advance both strings to next white space
2044 */
2045 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07002046 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 fmt++;
2048 while (!isspace(*str) && *str)
2049 str++;
2050 continue;
2051 }
2052
2053 /* get field width */
2054 field_width = -1;
2055 if (isdigit(*fmt))
2056 field_width = skip_atoi(&fmt);
2057
2058 /* get conversion qualifier */
2059 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002060 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2061 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 qualifier = *fmt++;
2063 if (unlikely(qualifier == *fmt)) {
2064 if (qualifier == 'h') {
2065 qualifier = 'H';
2066 fmt++;
2067 } else if (qualifier == 'l') {
2068 qualifier = 'L';
2069 fmt++;
2070 }
2071 }
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 if (!*fmt || !*str)
2075 break;
2076
André Goddard Rosad4be1512009-12-14 18:00:59 -08002077 base = 10;
2078 is_sign = 0;
2079
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002080 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 case 'c':
2082 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002083 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (field_width == -1)
2085 field_width = 1;
2086 do {
2087 *s++ = *str++;
2088 } while (--field_width > 0 && *str);
2089 num++;
2090 }
2091 continue;
2092 case 's':
2093 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002094 char *s = (char *)va_arg(args, char *);
2095 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002096 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002098 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002101 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 *s = '\0';
2104 num++;
2105 }
2106 continue;
2107 case 'n':
2108 /* return number of characters read so far */
2109 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002110 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 *i = str - buf;
2112 }
2113 continue;
2114 case 'o':
2115 base = 8;
2116 break;
2117 case 'x':
2118 case 'X':
2119 base = 16;
2120 break;
2121 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002122 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 case 'd':
2124 is_sign = 1;
2125 case 'u':
2126 break;
2127 case '%':
2128 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002129 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return num;
2131 continue;
2132 default:
2133 /* invalid format; stop here */
2134 return num;
2135 }
2136
2137 /* have some sort of integer conversion.
2138 * first, skip white space in buffer.
2139 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002140 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 digit = *str;
2143 if (is_sign && digit == '-')
2144 digit = *(str + 1);
2145
2146 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002147 || (base == 16 && !isxdigit(digit))
2148 || (base == 10 && !isdigit(digit))
2149 || (base == 8 && (!isdigit(digit) || digit > '7'))
2150 || (base == 0 && !isdigit(digit)))
2151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002153 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 case 'H': /* that's 'hh' in format */
2155 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002156 signed char *s = (signed char *)va_arg(args, signed char *);
2157 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002159 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2160 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162 break;
2163 case 'h':
2164 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002165 short *s = (short *)va_arg(args, short *);
2166 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002168 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2169 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171 break;
2172 case 'l':
2173 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002174 long *l = (long *)va_arg(args, long *);
2175 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002177 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2178 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 break;
2181 case 'L':
2182 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002183 long long *l = (long long *)va_arg(args, long long *);
2184 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002186 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2187 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189 break;
2190 case 'Z':
2191 case 'z':
2192 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002193 size_t *s = (size_t *)va_arg(args, size_t *);
2194 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196 break;
2197 default:
2198 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002199 int *i = (int *)va_arg(args, int *);
2200 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002202 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2203 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205 break;
2206 }
2207 num++;
2208
2209 if (!next)
2210 break;
2211 str = next;
2212 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002213
2214 /*
2215 * Now we've come all the way through so either the input string or the
2216 * format ended. In the former case, there can be a %n at the current
2217 * position in the format that needs to be filled.
2218 */
2219 if (*fmt == '%' && *(fmt + 1) == 'n') {
2220 int *p = (int *)va_arg(args, int *);
2221 *p = str - buf;
2222 }
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return num;
2225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226EXPORT_SYMBOL(vsscanf);
2227
2228/**
2229 * sscanf - Unformat a buffer into a list of arguments
2230 * @buf: input buffer
2231 * @fmt: formatting of buffer
2232 * @...: resulting arguments
2233 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002234int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 va_list args;
2237 int i;
2238
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002239 va_start(args, fmt);
2240 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return i;
2244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245EXPORT_SYMBOL(sscanf);