blob: 8cb7635b2ce305118b7491506122ba4bf5afa522 [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
George Spelvin23591722012-10-04 17:12:29 -0700248void put_dec_full4(char *buf, unsigned q)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700249{
250 unsigned r;
George Spelvine49317d2012-10-04 17:12:27 -0700251 r = (q * 0xccd) >> 15;
George Spelvin23591722012-10-04 17:12:29 -0700252 buf[0] = (q - 10 * r) + '0';
George Spelvine49317d2012-10-04 17:12:27 -0700253 q = (r * 0xcd) >> 11;
George Spelvin23591722012-10-04 17:12:29 -0700254 buf[1] = (r - 10 * q) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700255 r = (q * 0xcd) >> 11;
George Spelvin23591722012-10-04 17:12:29 -0700256 buf[2] = (q - 10 * r) + '0';
257 buf[3] = r + '0';
258}
259
260/*
261 * Call put_dec_full4 on x % 10000, return x / 10000.
262 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
263 * holds for all x < 1,128,869,999. The largest value this
264 * helper will ever be asked to convert is 1,125,520,955.
265 * (d1 in the put_dec code, assuming n is all-ones).
266 */
267static
268unsigned put_dec_helper4(char *buf, unsigned x)
269{
270 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
271
272 put_dec_full4(buf, x - q * 10000);
273 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700274}
275
276/* Based on code by Douglas W. Jones found at
277 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
278 * (with permission from the author).
279 * Performs no 64-bit division and hence should be fast on 32-bit machines.
280 */
281static
282char *put_dec(char *buf, unsigned long long n)
283{
284 uint32_t d3, d2, d1, q, h;
285
286 if (n < 100*1000*1000)
287 return put_dec_trunc8(buf, n);
288
289 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
290 h = (n >> 32);
291 d2 = (h ) & 0xffff;
292 d3 = (h >> 16); /* implicit "& 0xffff" */
293
294 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700295 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700296
George Spelvin23591722012-10-04 17:12:29 -0700297 q += 7671 * d3 + 9496 * d2 + 6 * d1;
298 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700299
George Spelvin23591722012-10-04 17:12:29 -0700300 q += 4749 * d3 + 42 * d2;
301 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700302
George Spelvin23591722012-10-04 17:12:29 -0700303 q += 281 * d3;
304 buf += 12;
305 if (q)
306 buf = put_dec_trunc8(buf, q);
307 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700308 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800309
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700310 return buf;
311}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700312
313#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700314
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700315/*
316 * Convert passed number to decimal string.
317 * Returns the length of string. On buffer overflow, returns 0.
318 *
319 * If speed is not important, use snprintf(). It's easy to read the code.
320 */
321int num_to_str(char *buf, int size, unsigned long long num)
322{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700323 char tmp[sizeof(num) * 3];
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700324 int idx, len;
325
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700326 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
327 if (num <= 9) {
328 tmp[0] = '0' + num;
329 len = 1;
330 } else {
331 len = put_dec(tmp, num) - tmp;
332 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700333
334 if (len > size)
335 return 0;
336 for (idx = 0; idx < len; ++idx)
337 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700338 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#define ZEROPAD 1 /* pad with zero */
342#define SIGN 2 /* unsigned/signed long */
343#define PLUS 4 /* show plus */
344#define SPACE 8 /* space if plus */
345#define LEFT 16 /* left justified */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700346#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
347#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100349enum format_type {
350 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100351 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100352 FORMAT_TYPE_PRECISION,
353 FORMAT_TYPE_CHAR,
354 FORMAT_TYPE_STR,
355 FORMAT_TYPE_PTR,
356 FORMAT_TYPE_PERCENT_CHAR,
357 FORMAT_TYPE_INVALID,
358 FORMAT_TYPE_LONG_LONG,
359 FORMAT_TYPE_ULONG,
360 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800361 FORMAT_TYPE_UBYTE,
362 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100363 FORMAT_TYPE_USHORT,
364 FORMAT_TYPE_SHORT,
365 FORMAT_TYPE_UINT,
366 FORMAT_TYPE_INT,
367 FORMAT_TYPE_NRCHARS,
368 FORMAT_TYPE_SIZE_T,
369 FORMAT_TYPE_PTRDIFF
370};
371
372struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700373 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800374 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700375 u8 base; /* number base, 8, 10 or 16 only */
376 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
377 s16 field_width; /* width of output field */
378 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100379};
380
Joe Perchescf3b4292010-05-24 14:33:16 -0700381static noinline_for_stack
382char *number(char *buf, char *end, unsigned long long num,
383 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100385 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
386 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
387
388 char tmp[66];
389 char sign;
390 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100391 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700393 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100395 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
396 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100397 locase = (spec.flags & SMALL);
398 if (spec.flags & LEFT)
399 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100401 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800402 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800404 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100405 spec.field_width--;
406 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100408 spec.field_width--;
409 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100411 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700414 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100415 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700416 spec.field_width -= 2;
417 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100418 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700420
421 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700423 if (num < spec.base)
424 tmp[i++] = digits[num] | locase;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700425 /* Generic code, for any base:
426 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100427 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700428 } while (num != 0);
429 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100430 else if (spec.base != 10) { /* 8 or 16 */
431 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700432 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800433
434 if (spec.base == 16)
435 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700436 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100437 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700438 num >>= shift;
439 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700440 } else { /* base 10 */
441 i = put_dec(tmp, num) - tmp;
442 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700443
444 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100445 if (i > spec.precision)
446 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100448 spec.field_width -= spec.precision;
449 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800450 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700451 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *buf = ' ';
453 ++buf;
454 }
455 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700456 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700458 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *buf = sign;
460 ++buf;
461 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700462 /* "0x" / "0" prefix */
463 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700464 if (spec.base == 16 || !is_zero) {
465 if (buf < end)
466 *buf = '0';
467 ++buf;
468 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100469 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700470 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100471 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 ++buf;
473 }
474 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700475 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100476 if (!(spec.flags & LEFT)) {
477 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
478 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700479 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *buf = c;
481 ++buf;
482 }
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100485 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700486 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *buf = '0';
488 ++buf;
489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* actual digits of result */
491 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700492 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *buf = tmp[i];
494 ++buf;
495 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700496 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100497 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700498 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *buf = ' ';
500 ++buf;
501 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return buf;
504}
505
Joe Perchescf3b4292010-05-24 14:33:16 -0700506static noinline_for_stack
507char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700508{
509 int len, i;
510
511 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800512 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700513
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100514 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700515
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100516 if (!(spec.flags & LEFT)) {
517 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700518 if (buf < end)
519 *buf = ' ';
520 ++buf;
521 }
522 }
523 for (i = 0; i < len; ++i) {
524 if (buf < end)
525 *buf = *s;
526 ++buf; ++s;
527 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100528 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700529 if (buf < end)
530 *buf = ' ';
531 ++buf;
532 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800533
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700534 return buf;
535}
536
Joe Perchescf3b4292010-05-24 14:33:16 -0700537static noinline_for_stack
538char *symbol_string(char *buf, char *end, void *ptr,
539 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700540{
541 unsigned long value = (unsigned long) ptr;
542#ifdef CONFIG_KALLSYMS
543 char sym[KSYM_SYMBOL_LEN];
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900544 if (ext == 'B')
545 sprint_backtrace(sym, value);
546 else if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200547 sprint_symbol(sym, value);
548 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700549 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800550
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100551 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700552#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800553 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100554 spec.flags |= SPECIAL | SMALL | ZEROPAD;
555 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800556
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100557 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700558#endif
559}
560
Joe Perchescf3b4292010-05-24 14:33:16 -0700561static noinline_for_stack
562char *resource_string(char *buf, char *end, struct resource *res,
563 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100564{
565#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600566#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100567#endif
568
569#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600570#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100571#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700572 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100573 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700574 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100575 .precision = -1,
576 .flags = SPECIAL | SMALL | ZEROPAD,
577 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700578 static const struct printf_spec mem_spec = {
579 .base = 16,
580 .field_width = MEM_RSRC_PRINTK_SIZE,
581 .precision = -1,
582 .flags = SPECIAL | SMALL | ZEROPAD,
583 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700584 static const struct printf_spec bus_spec = {
585 .base = 16,
586 .field_width = 2,
587 .precision = -1,
588 .flags = SMALL | ZEROPAD,
589 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700590 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600591 .base = 10,
592 .precision = -1,
593 .flags = 0,
594 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700595 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600596 .field_width = -1,
597 .precision = 10,
598 .flags = LEFT,
599 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700600 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600601 .base = 16,
602 .precision = -1,
603 .flags = SPECIAL | SMALL,
604 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600605
606 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
607 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
608#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
609#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700610#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600611#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
612 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
613 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
614
Linus Torvalds332d2e72008-10-20 15:07:34 +1100615 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600616 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700617 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100618
619 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700620 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600621 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700622 specp = &io_spec;
623 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600624 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700625 specp = &mem_spec;
626 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600627 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700628 specp = &dec_spec;
629 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600630 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700631 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700632 } else if (res->flags & IORESOURCE_BUS) {
633 p = string(p, pend, "bus ", str_spec);
634 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700635 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600636 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700637 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600638 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600639 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700640 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600641 if (res->start != res->end) {
642 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700643 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600644 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600645 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600646 if (res->flags & IORESOURCE_MEM_64)
647 p = string(p, pend, " 64bit", str_spec);
648 if (res->flags & IORESOURCE_PREFETCH)
649 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700650 if (res->flags & IORESOURCE_WINDOW)
651 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600652 if (res->flags & IORESOURCE_DISABLED)
653 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600654 } else {
655 p = string(p, pend, " flags ", str_spec);
656 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600657 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100658 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600659 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100660
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100661 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100662}
663
Joe Perchescf3b4292010-05-24 14:33:16 -0700664static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700665char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
666 const char *fmt)
667{
668 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
669 negative value, fallback to the default */
670 char separator;
671
672 if (spec.field_width == 0)
673 /* nothing to print */
674 return buf;
675
676 if (ZERO_OR_NULL_PTR(addr))
677 /* NULL pointer */
678 return string(buf, end, NULL, spec);
679
680 switch (fmt[1]) {
681 case 'C':
682 separator = ':';
683 break;
684 case 'D':
685 separator = '-';
686 break;
687 case 'N':
688 separator = 0;
689 break;
690 default:
691 separator = ' ';
692 break;
693 }
694
695 if (spec.field_width > 0)
696 len = min_t(int, spec.field_width, 64);
697
698 for (i = 0; i < len && buf < end - 1; i++) {
699 buf = hex_byte_pack(buf, addr[i]);
700
701 if (buf < end && separator && i != len - 1)
702 *buf++ = separator;
703 }
704
705 return buf;
706}
707
708static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700709char *mac_address_string(char *buf, char *end, u8 *addr,
710 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700711{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000712 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700713 char *p = mac_addr;
714 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000715 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700716 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000717
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700718 switch (fmt[1]) {
719 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000720 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700721 break;
722
723 case 'R':
724 reversed = true;
725 /* fall through */
726
727 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000728 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700729 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000730 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700731
732 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700733 if (reversed)
734 p = hex_byte_pack(p, addr[5 - i]);
735 else
736 p = hex_byte_pack(p, addr[i]);
737
Joe Perches8a27f7c2009-08-17 12:29:44 +0000738 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000739 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700740 }
741 *p = '\0';
742
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100743 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700744}
745
Joe Perchescf3b4292010-05-24 14:33:16 -0700746static noinline_for_stack
747char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700748{
Harvey Harrison689afa72008-10-28 16:04:44 -0700749 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800750 bool leading_zeros = (fmt[0] == 'i');
751 int index;
752 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700753
Joe Perches0159f242010-01-13 20:23:30 -0800754 switch (fmt[2]) {
755 case 'h':
756#ifdef __BIG_ENDIAN
757 index = 0;
758 step = 1;
759#else
760 index = 3;
761 step = -1;
762#endif
763 break;
764 case 'l':
765 index = 3;
766 step = -1;
767 break;
768 case 'n':
769 case 'b':
770 default:
771 index = 0;
772 step = 1;
773 break;
774 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000775 for (i = 0; i < 4; i++) {
776 char temp[3]; /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700777 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000778 if (leading_zeros) {
779 if (digits < 3)
780 *p++ = '0';
781 if (digits < 2)
782 *p++ = '0';
783 }
784 /* reverse the digits in the quad */
785 while (digits--)
786 *p++ = temp[digits];
787 if (i < 3)
788 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800789 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000790 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000791 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800792
Joe Perches8a27f7c2009-08-17 12:29:44 +0000793 return p;
794}
795
Joe Perchescf3b4292010-05-24 14:33:16 -0700796static noinline_for_stack
797char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000798{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800799 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000800 unsigned char zerolength[8];
801 int longest = 1;
802 int colonpos = -1;
803 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800804 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000805 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000806 bool useIPv4;
807 struct in6_addr in6;
808
809 memcpy(&in6, addr, sizeof(struct in6_addr));
810
811 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000812
813 memset(zerolength, 0, sizeof(zerolength));
814
815 if (useIPv4)
816 range = 6;
817 else
818 range = 8;
819
820 /* find position of longest 0 run */
821 for (i = 0; i < range; i++) {
822 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000823 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000824 break;
825 zerolength[i]++;
826 }
827 }
828 for (i = 0; i < range; i++) {
829 if (zerolength[i] > longest) {
830 longest = zerolength[i];
831 colonpos = i;
832 }
833 }
Joe Perches29cf5192011-06-09 11:23:37 -0700834 if (longest == 1) /* don't compress a single 0 */
835 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000836
837 /* emit address */
838 for (i = 0; i < range; i++) {
839 if (i == colonpos) {
840 if (needcolon || i == 0)
841 *p++ = ':';
842 *p++ = ':';
843 needcolon = false;
844 i += longest - 1;
845 continue;
846 }
847 if (needcolon) {
848 *p++ = ':';
849 needcolon = false;
850 }
851 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000852 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000853 hi = word >> 8;
854 lo = word & 0xff;
855 if (hi) {
856 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700857 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000858 else
859 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700860 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000861 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800862 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700863 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000864 else
865 *p++ = hex_asc_lo(lo);
866 needcolon = true;
867 }
868
869 if (useIPv4) {
870 if (needcolon)
871 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800872 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000873 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000874 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800875
Joe Perches8a27f7c2009-08-17 12:29:44 +0000876 return p;
877}
878
Joe Perchescf3b4292010-05-24 14:33:16 -0700879static noinline_for_stack
880char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000881{
882 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800883
Harvey Harrison689afa72008-10-28 16:04:44 -0700884 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700885 p = hex_byte_pack(p, *addr++);
886 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000887 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700888 *p++ = ':';
889 }
890 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800891
Joe Perches8a27f7c2009-08-17 12:29:44 +0000892 return p;
893}
894
Joe Perchescf3b4292010-05-24 14:33:16 -0700895static noinline_for_stack
896char *ip6_addr_string(char *buf, char *end, const u8 *addr,
897 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000898{
899 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
900
901 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000902 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000903 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000904 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700905
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100906 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700907}
908
Joe Perchescf3b4292010-05-24 14:33:16 -0700909static noinline_for_stack
910char *ip4_addr_string(char *buf, char *end, const u8 *addr,
911 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700912{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000913 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700914
Joe Perches0159f242010-01-13 20:23:30 -0800915 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700916
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100917 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700918}
919
Joe Perchescf3b4292010-05-24 14:33:16 -0700920static noinline_for_stack
921char *uuid_string(char *buf, char *end, const u8 *addr,
922 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800923{
924 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
925 char *p = uuid;
926 int i;
927 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
928 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
929 const u8 *index = be;
930 bool uc = false;
931
932 switch (*(++fmt)) {
933 case 'L':
934 uc = true; /* fall-through */
935 case 'l':
936 index = le;
937 break;
938 case 'B':
939 uc = true;
940 break;
941 }
942
943 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700944 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -0800945 switch (i) {
946 case 3:
947 case 5:
948 case 7:
949 case 9:
950 *p++ = '-';
951 break;
952 }
953 }
954
955 *p = 0;
956
957 if (uc) {
958 p = uuid;
959 do {
960 *p = toupper(*p);
961 } while (*(++p));
962 }
963
964 return string(buf, end, uuid, spec);
965}
966
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000967static
968char *netdev_feature_string(char *buf, char *end, const u8 *addr,
969 struct printf_spec spec)
970{
971 spec.flags |= SPECIAL | SMALL | ZEROPAD;
972 if (spec.field_width == -1)
973 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
974 spec.base = 16;
975
976 return number(buf, end, *(const netdev_features_t *)addr, spec);
977}
978
Ingo Molnar411f05f2011-05-12 23:00:28 +0200979int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800980
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700981/*
982 * Show a '%p' thing. A kernel extension is that the '%p' is followed
983 * by an extra set of alphanumeric characters that are extended format
984 * specifiers.
985 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100986 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700987 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200988 * - 'F' For symbolic function descriptor pointers with offset
989 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400990 * - 'S' For symbolic direct pointers with offset
991 * - 's' For symbolic direct pointers without offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900992 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600993 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
994 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700995 * - 'M' For a 6-byte MAC address, it prints the address in the
996 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000997 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +0000998 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -0800999 * with a dash-separated hex notation
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001000 * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001001 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1002 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1003 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1004 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1005 * IPv6 omits the colons (01020304...0f)
1006 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -08001007 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +00001008 * - 'I6c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001009 * http://tools.ietf.org/html/rfc5952
Joe Perches9ac6e442009-12-14 18:01:09 -08001010 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1011 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1012 * Options for %pU are:
1013 * b big endian lower case hex (default)
1014 * B big endian UPPER case hex
1015 * l little endian lower case hex
1016 * L little endian UPPER case hex
1017 * big endian output byte order is:
1018 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1019 * little endian output byte order is:
1020 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001021 * - 'V' For a struct va_format which contains a format string * and va_list *,
1022 * call vsnprintf(->format, *->va_list).
1023 * Implements a "recursive vsnprintf".
1024 * Do not use this feature without some mechanism to verify the
1025 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001026 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001027 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001028 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1029 * a certain separator (' ' by default):
1030 * C colon
1031 * D dash
1032 * N no separator
1033 * The maximum supported length is 64 bytes of the input. Consider
1034 * to use print_hex_dump() for the larger input.
Joe Perches9ac6e442009-12-14 18:01:09 -08001035 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001036 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1037 * function pointers are really function descriptors, which contain a
1038 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001039 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001040static noinline_for_stack
1041char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1042 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001043{
Grant Likely725fe002012-05-31 16:26:08 -07001044 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1045
Kees Cook9f36e2c2011-03-22 16:34:22 -07001046 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001047 /*
1048 * Print (null) with the same width as a pointer so it makes
1049 * tabular output look nice.
1050 */
1051 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001052 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001053 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001054 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001055
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001056 switch (*fmt) {
1057 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001058 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001059 ptr = dereference_function_descriptor(ptr);
1060 /* Fallthrough */
1061 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001062 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001063 case 'B':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001064 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001065 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001066 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001067 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001068 case 'h':
1069 return hex_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001070 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1071 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001072 /* [mM]F (FDDI) */
1073 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001074 return mac_address_string(buf, end, ptr, spec, fmt);
1075 case 'I': /* Formatted IP supported
1076 * 4: 1.2.3.4
1077 * 6: 0001:0203:...:0708
1078 * 6c: 1::708 or 1::1.2.3.4
1079 */
1080 case 'i': /* Contiguous:
1081 * 4: 001.002.003.004
1082 * 6: 000102...0f
1083 */
1084 switch (fmt[1]) {
1085 case '6':
1086 return ip6_addr_string(buf, end, ptr, spec, fmt);
1087 case '4':
1088 return ip4_addr_string(buf, end, ptr, spec, fmt);
1089 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001090 break;
Joe Perches9ac6e442009-12-14 18:01:09 -08001091 case 'U':
1092 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001093 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001094 {
1095 va_list va;
1096
1097 va_copy(va, *((struct va_format *)ptr)->va);
1098 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1099 ((struct va_format *)ptr)->fmt, va);
1100 va_end(va);
1101 return buf;
1102 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001103 case 'K':
1104 /*
1105 * %pK cannot be used in IRQ context because its test
1106 * for CAP_SYSLOG would be meaningless.
1107 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001108 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1109 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001110 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001111 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001112 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001113 }
Joe Perches26297602011-03-22 16:34:19 -07001114 if (!((kptr_restrict == 0) ||
1115 (kptr_restrict == 1 &&
1116 has_capability_noaudit(current, CAP_SYSLOG))))
1117 ptr = NULL;
1118 break;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001119 case 'N':
1120 switch (fmt[1]) {
1121 case 'F':
1122 return netdev_feature_string(buf, end, ptr, spec);
1123 }
1124 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001125 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001126 spec.flags |= SMALL;
1127 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001128 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001129 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001130 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001131 spec.base = 16;
1132
1133 return number(buf, end, (unsigned long) ptr, spec);
1134}
1135
1136/*
1137 * Helper function to decode printf style format.
1138 * Each call decode a token from the format and return the
1139 * number of characters read (or likely the delta where it wants
1140 * to go on the next call).
1141 * The decoded token is returned through the parameters
1142 *
1143 * 'h', 'l', or 'L' for integer fields
1144 * 'z' support added 23/7/1999 S.H.
1145 * 'z' changed to 'Z' --davidm 1/25/99
1146 * 't' added for ptrdiff_t
1147 *
1148 * @fmt: the format string
1149 * @type of the token returned
1150 * @flags: various flags such as +, -, # tokens..
1151 * @field_width: overwritten width
1152 * @base: base of the number (octal, hex, ...)
1153 * @precision: precision of a number
1154 * @qualifier: qualifier of a number (long, size_t, ...)
1155 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001156static noinline_for_stack
1157int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001158{
1159 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001160
1161 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001162 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001163 if (spec->field_width < 0) {
1164 spec->field_width = -spec->field_width;
1165 spec->flags |= LEFT;
1166 }
1167 spec->type = FORMAT_TYPE_NONE;
1168 goto precision;
1169 }
1170
1171 /* we finished early by reading the precision */
1172 if (spec->type == FORMAT_TYPE_PRECISION) {
1173 if (spec->precision < 0)
1174 spec->precision = 0;
1175
1176 spec->type = FORMAT_TYPE_NONE;
1177 goto qualifier;
1178 }
1179
1180 /* By default */
1181 spec->type = FORMAT_TYPE_NONE;
1182
1183 for (; *fmt ; ++fmt) {
1184 if (*fmt == '%')
1185 break;
1186 }
1187
1188 /* Return the current non-format string */
1189 if (fmt != start || !*fmt)
1190 return fmt - start;
1191
1192 /* Process flags */
1193 spec->flags = 0;
1194
1195 while (1) { /* this also skips first '%' */
1196 bool found = true;
1197
1198 ++fmt;
1199
1200 switch (*fmt) {
1201 case '-': spec->flags |= LEFT; break;
1202 case '+': spec->flags |= PLUS; break;
1203 case ' ': spec->flags |= SPACE; break;
1204 case '#': spec->flags |= SPECIAL; break;
1205 case '0': spec->flags |= ZEROPAD; break;
1206 default: found = false;
1207 }
1208
1209 if (!found)
1210 break;
1211 }
1212
1213 /* get field width */
1214 spec->field_width = -1;
1215
1216 if (isdigit(*fmt))
1217 spec->field_width = skip_atoi(&fmt);
1218 else if (*fmt == '*') {
1219 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001220 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001221 return ++fmt - start;
1222 }
1223
1224precision:
1225 /* get the precision */
1226 spec->precision = -1;
1227 if (*fmt == '.') {
1228 ++fmt;
1229 if (isdigit(*fmt)) {
1230 spec->precision = skip_atoi(&fmt);
1231 if (spec->precision < 0)
1232 spec->precision = 0;
1233 } else if (*fmt == '*') {
1234 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001235 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001236 return ++fmt - start;
1237 }
1238 }
1239
1240qualifier:
1241 /* get the conversion qualifier */
1242 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001243 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1244 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001245 spec->qualifier = *fmt++;
1246 if (unlikely(spec->qualifier == *fmt)) {
1247 if (spec->qualifier == 'l') {
1248 spec->qualifier = 'L';
1249 ++fmt;
1250 } else if (spec->qualifier == 'h') {
1251 spec->qualifier = 'H';
1252 ++fmt;
1253 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001254 }
1255 }
1256
1257 /* default base */
1258 spec->base = 10;
1259 switch (*fmt) {
1260 case 'c':
1261 spec->type = FORMAT_TYPE_CHAR;
1262 return ++fmt - start;
1263
1264 case 's':
1265 spec->type = FORMAT_TYPE_STR;
1266 return ++fmt - start;
1267
1268 case 'p':
1269 spec->type = FORMAT_TYPE_PTR;
1270 return fmt - start;
1271 /* skip alnum */
1272
1273 case 'n':
1274 spec->type = FORMAT_TYPE_NRCHARS;
1275 return ++fmt - start;
1276
1277 case '%':
1278 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1279 return ++fmt - start;
1280
1281 /* integer number formats - set up the flags and "break" */
1282 case 'o':
1283 spec->base = 8;
1284 break;
1285
1286 case 'x':
1287 spec->flags |= SMALL;
1288
1289 case 'X':
1290 spec->base = 16;
1291 break;
1292
1293 case 'd':
1294 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001295 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001296 case 'u':
1297 break;
1298
1299 default:
1300 spec->type = FORMAT_TYPE_INVALID;
1301 return fmt - start;
1302 }
1303
1304 if (spec->qualifier == 'L')
1305 spec->type = FORMAT_TYPE_LONG_LONG;
1306 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001307 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001308 spec->type = FORMAT_TYPE_LONG;
1309 else
1310 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001311 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001312 spec->type = FORMAT_TYPE_SIZE_T;
1313 } else if (spec->qualifier == 't') {
1314 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001315 } else if (spec->qualifier == 'H') {
1316 if (spec->flags & SIGN)
1317 spec->type = FORMAT_TYPE_BYTE;
1318 else
1319 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001320 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001321 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001322 spec->type = FORMAT_TYPE_SHORT;
1323 else
1324 spec->type = FORMAT_TYPE_USHORT;
1325 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001326 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001327 spec->type = FORMAT_TYPE_INT;
1328 else
1329 spec->type = FORMAT_TYPE_UINT;
1330 }
1331
1332 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001333}
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/**
1336 * vsnprintf - Format a string and place it in a buffer
1337 * @buf: The buffer to place the result into
1338 * @size: The size of the buffer, including the trailing null space
1339 * @fmt: The format string to use
1340 * @args: Arguments for the format string
1341 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001342 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001343 * %pS output the name of a text symbol with offset
1344 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001345 * %pF output the name of a function pointer with its offset
1346 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001347 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001348 * %pR output the address range in a struct resource with decoded flags
1349 * %pr output the address range in a struct resource with raw flags
1350 * %pM output a 6-byte MAC address with colons
1351 * %pm output a 6-byte MAC address without colons
1352 * %pI4 print an IPv4 address without leading zeros
1353 * %pi4 print an IPv4 address with leading zeros
1354 * %pI6 print an IPv6 address with colons
1355 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001356 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001357 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1358 * case.
Andy Shevchenko31550a12012-07-30 14:40:27 -07001359 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1360 * bytes of the input)
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001361 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001362 *
Andrew Morton80f548e2012-07-30 14:40:25 -07001363 * ** Please update Documentation/printk-formats.txt when making changes **
1364 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * The return value is the number of characters which would
1366 * be generated for the given input, excluding the trailing
1367 * '\0', as per ISO C99. If you want to have the exact
1368 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001369 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 * return is greater than or equal to @size, the resulting
1371 * string is truncated.
1372 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001373 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
1375int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001378 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001379 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001381 /* Reject out-of-range values early. Large positive sizes are
1382 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001383 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001387 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001389 /* Make sure end is always >= buf */
1390 if (end < buf) {
1391 end = ((void *)-1);
1392 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001395 while (*fmt) {
1396 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001397 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001398
1399 fmt += read;
1400
1401 switch (spec.type) {
1402 case FORMAT_TYPE_NONE: {
1403 int copy = read;
1404 if (str < end) {
1405 if (copy > end - str)
1406 copy = end - str;
1407 memcpy(str, old_fmt, copy);
1408 }
1409 str += read;
1410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Vegard Nossumed681a92009-03-14 12:08:50 +01001413 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001414 spec.field_width = va_arg(args, int);
1415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001417 case FORMAT_TYPE_PRECISION:
1418 spec.precision = va_arg(args, int);
1419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
André Goddard Rosad4be1512009-12-14 18:00:59 -08001421 case FORMAT_TYPE_CHAR: {
1422 char c;
1423
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001424 if (!(spec.flags & LEFT)) {
1425 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001426 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 *str = ' ';
1428 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001431 }
1432 c = (unsigned char) va_arg(args, int);
1433 if (str < end)
1434 *str = c;
1435 ++str;
1436 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001437 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001438 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001440 }
1441 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001444 case FORMAT_TYPE_STR:
1445 str = string(str, end, va_arg(args, char *), spec);
1446 break;
1447
1448 case FORMAT_TYPE_PTR:
1449 str = pointer(fmt+1, str, end, va_arg(args, void *),
1450 spec);
1451 while (isalnum(*fmt))
1452 fmt++;
1453 break;
1454
1455 case FORMAT_TYPE_PERCENT_CHAR:
1456 if (str < end)
1457 *str = '%';
1458 ++str;
1459 break;
1460
1461 case FORMAT_TYPE_INVALID:
1462 if (str < end)
1463 *str = '%';
1464 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001465 break;
1466
1467 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001468 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001469
1470 if (qualifier == 'l') {
1471 long *ip = va_arg(args, long *);
1472 *ip = (str - buf);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001473 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001474 size_t *ip = va_arg(args, size_t *);
1475 *ip = (str - buf);
1476 } else {
1477 int *ip = va_arg(args, int *);
1478 *ip = (str - buf);
1479 }
1480 break;
1481 }
1482
1483 default:
1484 switch (spec.type) {
1485 case FORMAT_TYPE_LONG_LONG:
1486 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001488 case FORMAT_TYPE_ULONG:
1489 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001491 case FORMAT_TYPE_LONG:
1492 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001494 case FORMAT_TYPE_SIZE_T:
1495 num = va_arg(args, size_t);
1496 break;
1497 case FORMAT_TYPE_PTRDIFF:
1498 num = va_arg(args, ptrdiff_t);
1499 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001500 case FORMAT_TYPE_UBYTE:
1501 num = (unsigned char) va_arg(args, int);
1502 break;
1503 case FORMAT_TYPE_BYTE:
1504 num = (signed char) va_arg(args, int);
1505 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001506 case FORMAT_TYPE_USHORT:
1507 num = (unsigned short) va_arg(args, int);
1508 break;
1509 case FORMAT_TYPE_SHORT:
1510 num = (short) va_arg(args, int);
1511 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001512 case FORMAT_TYPE_INT:
1513 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001516 num = va_arg(args, unsigned int);
1517 }
1518
1519 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001522
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001523 if (size > 0) {
1524 if (str < end)
1525 *str = '\0';
1526 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001527 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001528 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001529
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001530 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534EXPORT_SYMBOL(vsnprintf);
1535
1536/**
1537 * vscnprintf - Format a string and place it in a buffer
1538 * @buf: The buffer to place the result into
1539 * @size: The size of the buffer, including the trailing null space
1540 * @fmt: The format string to use
1541 * @args: Arguments for the format string
1542 *
1543 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001544 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * returns 0.
1546 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001547 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001548 *
1549 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
1551int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1552{
1553 int i;
1554
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001555 i = vsnprintf(buf, size, fmt, args);
1556
Anton Arapovb921c692011-01-12 16:59:49 -08001557 if (likely(i < size))
1558 return i;
1559 if (size != 0)
1560 return size - 1;
1561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563EXPORT_SYMBOL(vscnprintf);
1564
1565/**
1566 * snprintf - Format a string and place it in a buffer
1567 * @buf: The buffer to place the result into
1568 * @size: The size of the buffer, including the trailing null space
1569 * @fmt: The format string to use
1570 * @...: Arguments for the format string
1571 *
1572 * The return value is the number of characters which would be
1573 * generated for the given input, excluding the trailing null,
1574 * as per ISO C99. If the return is greater than or equal to
1575 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001576 *
1577 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001579int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 va_list args;
1582 int i;
1583
1584 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001585 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return i;
1589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590EXPORT_SYMBOL(snprintf);
1591
1592/**
1593 * scnprintf - Format a string and place it in a buffer
1594 * @buf: The buffer to place the result into
1595 * @size: The size of the buffer, including the trailing null space
1596 * @fmt: The format string to use
1597 * @...: Arguments for the format string
1598 *
1599 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001600 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
1602
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001603int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 va_list args;
1606 int i;
1607
1608 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001609 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001611
Anton Arapovb921c692011-01-12 16:59:49 -08001612 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614EXPORT_SYMBOL(scnprintf);
1615
1616/**
1617 * vsprintf - Format a string and place it in a buffer
1618 * @buf: The buffer to place the result into
1619 * @fmt: The format string to use
1620 * @args: Arguments for the format string
1621 *
1622 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001623 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * buffer overflows.
1625 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001626 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001627 *
1628 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
1630int vsprintf(char *buf, const char *fmt, va_list args)
1631{
1632 return vsnprintf(buf, INT_MAX, fmt, args);
1633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634EXPORT_SYMBOL(vsprintf);
1635
1636/**
1637 * sprintf - Format a string and place it in a buffer
1638 * @buf: The buffer to place the result into
1639 * @fmt: The format string to use
1640 * @...: Arguments for the format string
1641 *
1642 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001643 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001645 *
1646 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001648int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 va_list args;
1651 int i;
1652
1653 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001654 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return i;
1658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659EXPORT_SYMBOL(sprintf);
1660
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001661#ifdef CONFIG_BINARY_PRINTF
1662/*
1663 * bprintf service:
1664 * vbin_printf() - VA arguments to binary data
1665 * bstr_printf() - Binary data to text string
1666 */
1667
1668/**
1669 * vbin_printf - Parse a format string and place args' binary value in a buffer
1670 * @bin_buf: The buffer to place args' binary value
1671 * @size: The size of the buffer(by words(32bits), not characters)
1672 * @fmt: The format string to use
1673 * @args: Arguments for the format string
1674 *
1675 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1676 * is skiped.
1677 *
1678 * The return value is the number of words(32bits) which would be generated for
1679 * the given input.
1680 *
1681 * NOTE:
1682 * If the return value is greater than @size, the resulting bin_buf is NOT
1683 * valid for bstr_printf().
1684 */
1685int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1686{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001687 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001688 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001689
1690 str = (char *)bin_buf;
1691 end = (char *)(bin_buf + size);
1692
1693#define save_arg(type) \
1694do { \
1695 if (sizeof(type) == 8) { \
1696 unsigned long long value; \
1697 str = PTR_ALIGN(str, sizeof(u32)); \
1698 value = va_arg(args, unsigned long long); \
1699 if (str + sizeof(type) <= end) { \
1700 *(u32 *)str = *(u32 *)&value; \
1701 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1702 } \
1703 } else { \
1704 unsigned long value; \
1705 str = PTR_ALIGN(str, sizeof(type)); \
1706 value = va_arg(args, int); \
1707 if (str + sizeof(type) <= end) \
1708 *(typeof(type) *)str = (type)value; \
1709 } \
1710 str += sizeof(type); \
1711} while (0)
1712
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001713 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001714 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001715
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001716 fmt += read;
1717
1718 switch (spec.type) {
1719 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001720 case FORMAT_TYPE_INVALID:
1721 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001722 break;
1723
Vegard Nossumed681a92009-03-14 12:08:50 +01001724 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001725 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001726 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001727 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001728
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001729 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001730 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001731 break;
1732
1733 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001734 const char *save_str = va_arg(args, char *);
1735 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001736
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001737 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1738 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001739 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001740 len = strlen(save_str) + 1;
1741 if (str + len < end)
1742 memcpy(str, save_str, len);
1743 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001744 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001745 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001746
1747 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001748 save_arg(void *);
1749 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001750 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001751 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001752 break;
1753
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001754 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001755 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001756 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001757 void *skip_arg;
1758 if (qualifier == 'l')
1759 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001760 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001761 skip_arg = va_arg(args, size_t *);
1762 else
1763 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001764 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001765 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001766
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001767 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001768 switch (spec.type) {
1769
1770 case FORMAT_TYPE_LONG_LONG:
1771 save_arg(long long);
1772 break;
1773 case FORMAT_TYPE_ULONG:
1774 case FORMAT_TYPE_LONG:
1775 save_arg(unsigned long);
1776 break;
1777 case FORMAT_TYPE_SIZE_T:
1778 save_arg(size_t);
1779 break;
1780 case FORMAT_TYPE_PTRDIFF:
1781 save_arg(ptrdiff_t);
1782 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001783 case FORMAT_TYPE_UBYTE:
1784 case FORMAT_TYPE_BYTE:
1785 save_arg(char);
1786 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001787 case FORMAT_TYPE_USHORT:
1788 case FORMAT_TYPE_SHORT:
1789 save_arg(short);
1790 break;
1791 default:
1792 save_arg(int);
1793 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001794 }
1795 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001796
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001797 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001798#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001799}
1800EXPORT_SYMBOL_GPL(vbin_printf);
1801
1802/**
1803 * bstr_printf - Format a string from binary arguments and place it in a buffer
1804 * @buf: The buffer to place the result into
1805 * @size: The size of the buffer, including the trailing null space
1806 * @fmt: The format string to use
1807 * @bin_buf: Binary arguments for the format string
1808 *
1809 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1810 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1811 * a binary buffer that generated by vbin_printf.
1812 *
1813 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001814 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001815 *
1816 * The return value is the number of characters which would
1817 * be generated for the given input, excluding the trailing
1818 * '\0', as per ISO C99. If you want to have the exact
1819 * number of characters written into @buf as return value
1820 * (not including the trailing '\0'), use vscnprintf(). If the
1821 * return is greater than or equal to @size, the resulting
1822 * string is truncated.
1823 */
1824int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1825{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001826 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001827 char *str, *end;
1828 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001829
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001830 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001831 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001832
1833 str = buf;
1834 end = buf + size;
1835
1836#define get_arg(type) \
1837({ \
1838 typeof(type) value; \
1839 if (sizeof(type) == 8) { \
1840 args = PTR_ALIGN(args, sizeof(u32)); \
1841 *(u32 *)&value = *(u32 *)args; \
1842 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1843 } else { \
1844 args = PTR_ALIGN(args, sizeof(type)); \
1845 value = *(typeof(type) *)args; \
1846 } \
1847 args += sizeof(type); \
1848 value; \
1849})
1850
1851 /* Make sure end is always >= buf */
1852 if (end < buf) {
1853 end = ((void *)-1);
1854 size = end - buf;
1855 }
1856
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001857 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001858 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001859 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001860
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001861 fmt += read;
1862
1863 switch (spec.type) {
1864 case FORMAT_TYPE_NONE: {
1865 int copy = read;
1866 if (str < end) {
1867 if (copy > end - str)
1868 copy = end - str;
1869 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001870 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001871 str += read;
1872 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001873 }
1874
Vegard Nossumed681a92009-03-14 12:08:50 +01001875 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001876 spec.field_width = get_arg(int);
1877 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001878
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001879 case FORMAT_TYPE_PRECISION:
1880 spec.precision = get_arg(int);
1881 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001882
André Goddard Rosad4be1512009-12-14 18:00:59 -08001883 case FORMAT_TYPE_CHAR: {
1884 char c;
1885
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001886 if (!(spec.flags & LEFT)) {
1887 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001888 if (str < end)
1889 *str = ' ';
1890 ++str;
1891 }
1892 }
1893 c = (unsigned char) get_arg(char);
1894 if (str < end)
1895 *str = c;
1896 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001897 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001898 if (str < end)
1899 *str = ' ';
1900 ++str;
1901 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001902 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001903 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001904
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001905 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001906 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001907 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001908 str = string(str, end, (char *)str_arg, spec);
1909 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001910 }
1911
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001912 case FORMAT_TYPE_PTR:
1913 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1914 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001915 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001916 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001917
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001918 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001919 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001920 if (str < end)
1921 *str = '%';
1922 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001923 break;
1924
1925 case FORMAT_TYPE_NRCHARS:
1926 /* skip */
1927 break;
1928
André Goddard Rosad4be1512009-12-14 18:00:59 -08001929 default: {
1930 unsigned long long num;
1931
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001932 switch (spec.type) {
1933
1934 case FORMAT_TYPE_LONG_LONG:
1935 num = get_arg(long long);
1936 break;
1937 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001938 case FORMAT_TYPE_LONG:
1939 num = get_arg(unsigned long);
1940 break;
1941 case FORMAT_TYPE_SIZE_T:
1942 num = get_arg(size_t);
1943 break;
1944 case FORMAT_TYPE_PTRDIFF:
1945 num = get_arg(ptrdiff_t);
1946 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001947 case FORMAT_TYPE_UBYTE:
1948 num = get_arg(unsigned char);
1949 break;
1950 case FORMAT_TYPE_BYTE:
1951 num = get_arg(signed char);
1952 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001953 case FORMAT_TYPE_USHORT:
1954 num = get_arg(unsigned short);
1955 break;
1956 case FORMAT_TYPE_SHORT:
1957 num = get_arg(short);
1958 break;
1959 case FORMAT_TYPE_UINT:
1960 num = get_arg(unsigned int);
1961 break;
1962 default:
1963 num = get_arg(int);
1964 }
1965
1966 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001967 } /* default: */
1968 } /* switch(spec.type) */
1969 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001970
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001971 if (size > 0) {
1972 if (str < end)
1973 *str = '\0';
1974 else
1975 end[-1] = '\0';
1976 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001977
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001978#undef get_arg
1979
1980 /* the trailing null byte doesn't count towards the total */
1981 return str - buf;
1982}
1983EXPORT_SYMBOL_GPL(bstr_printf);
1984
1985/**
1986 * bprintf - Parse a format string and place args' binary value in a buffer
1987 * @bin_buf: The buffer to place args' binary value
1988 * @size: The size of the buffer(by words(32bits), not characters)
1989 * @fmt: The format string to use
1990 * @...: Arguments for the format string
1991 *
1992 * The function returns the number of words(u32) written
1993 * into @bin_buf.
1994 */
1995int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1996{
1997 va_list args;
1998 int ret;
1999
2000 va_start(args, fmt);
2001 ret = vbin_printf(bin_buf, size, fmt, args);
2002 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002003
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002004 return ret;
2005}
2006EXPORT_SYMBOL_GPL(bprintf);
2007
2008#endif /* CONFIG_BINARY_PRINTF */
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010/**
2011 * vsscanf - Unformat a buffer into a list of arguments
2012 * @buf: input buffer
2013 * @fmt: format of buffer
2014 * @args: arguments
2015 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002016int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 const char *str = buf;
2019 char *next;
2020 char digit;
2021 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002022 u8 qualifier;
2023 u8 base;
2024 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002025 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002027 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /* skip any white space in format */
2029 /* white space in format matchs any amount of
2030 * white space, including none, in the input.
2031 */
2032 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002033 fmt = skip_spaces(++fmt);
2034 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
2037 /* anything that is not a conversion must match exactly */
2038 if (*fmt != '%' && *fmt) {
2039 if (*fmt++ != *str++)
2040 break;
2041 continue;
2042 }
2043
2044 if (!*fmt)
2045 break;
2046 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /* skip this conversion.
2049 * advance both strings to next white space
2050 */
2051 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07002052 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 fmt++;
2054 while (!isspace(*str) && *str)
2055 str++;
2056 continue;
2057 }
2058
2059 /* get field width */
2060 field_width = -1;
2061 if (isdigit(*fmt))
2062 field_width = skip_atoi(&fmt);
2063
2064 /* get conversion qualifier */
2065 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002066 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2067 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 qualifier = *fmt++;
2069 if (unlikely(qualifier == *fmt)) {
2070 if (qualifier == 'h') {
2071 qualifier = 'H';
2072 fmt++;
2073 } else if (qualifier == 'l') {
2074 qualifier = 'L';
2075 fmt++;
2076 }
2077 }
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (!*fmt || !*str)
2081 break;
2082
André Goddard Rosad4be1512009-12-14 18:00:59 -08002083 base = 10;
2084 is_sign = 0;
2085
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002086 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 case 'c':
2088 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002089 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (field_width == -1)
2091 field_width = 1;
2092 do {
2093 *s++ = *str++;
2094 } while (--field_width > 0 && *str);
2095 num++;
2096 }
2097 continue;
2098 case 's':
2099 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002100 char *s = (char *)va_arg(args, char *);
2101 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002102 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002104 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002107 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *s = '\0';
2110 num++;
2111 }
2112 continue;
2113 case 'n':
2114 /* return number of characters read so far */
2115 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002116 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 *i = str - buf;
2118 }
2119 continue;
2120 case 'o':
2121 base = 8;
2122 break;
2123 case 'x':
2124 case 'X':
2125 base = 16;
2126 break;
2127 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002128 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 case 'd':
2130 is_sign = 1;
2131 case 'u':
2132 break;
2133 case '%':
2134 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002135 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return num;
2137 continue;
2138 default:
2139 /* invalid format; stop here */
2140 return num;
2141 }
2142
2143 /* have some sort of integer conversion.
2144 * first, skip white space in buffer.
2145 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002146 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 digit = *str;
2149 if (is_sign && digit == '-')
2150 digit = *(str + 1);
2151
2152 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002153 || (base == 16 && !isxdigit(digit))
2154 || (base == 10 && !isdigit(digit))
2155 || (base == 8 && (!isdigit(digit) || digit > '7'))
2156 || (base == 0 && !isdigit(digit)))
2157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002159 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 case 'H': /* that's 'hh' in format */
2161 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002162 signed char *s = (signed char *)va_arg(args, signed char *);
2163 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002165 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2166 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 break;
2169 case 'h':
2170 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002171 short *s = (short *)va_arg(args, short *);
2172 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002174 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2175 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177 break;
2178 case 'l':
2179 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002180 long *l = (long *)va_arg(args, long *);
2181 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002183 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2184 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 break;
2187 case 'L':
2188 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002189 long long *l = (long long *)va_arg(args, long long *);
2190 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002192 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2193 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195 break;
2196 case 'Z':
2197 case 'z':
2198 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002199 size_t *s = (size_t *)va_arg(args, size_t *);
2200 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202 break;
2203 default:
2204 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002205 int *i = (int *)va_arg(args, int *);
2206 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002208 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2209 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 break;
2212 }
2213 num++;
2214
2215 if (!next)
2216 break;
2217 str = next;
2218 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002219
2220 /*
2221 * Now we've come all the way through so either the input string or the
2222 * format ended. In the former case, there can be a %n at the current
2223 * position in the format that needs to be filled.
2224 */
2225 if (*fmt == '%' && *(fmt + 1) == 'n') {
2226 int *p = (int *)va_arg(args, int *);
2227 *p = str - buf;
2228 }
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return num;
2231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232EXPORT_SYMBOL(vsscanf);
2233
2234/**
2235 * sscanf - Unformat a buffer into a list of arguments
2236 * @buf: input buffer
2237 * @fmt: formatting of buffer
2238 * @...: resulting arguments
2239 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002240int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 va_list args;
2243 int i;
2244
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002245 va_start(args, fmt);
2246 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return i;
2250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251EXPORT_SYMBOL(sscanf);