blob: 0ddf928fe4d5bf4efbad83f69b382db9576a509c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
12/*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070025#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Tim Schmielau4e57b682005-10-30 15:03:48 -080028#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/div64.h>
James Bottomleydeac93d2008-09-03 20:43:36 -050030#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010032/* Works only for digits and letters, but small and fast */
33#define TOLOWER(x) ((x) | 0x20)
34
Harvey Harrisonaa46a632008-10-16 13:40:34 -070035static unsigned int simple_guess_base(const char *cp)
36{
37 if (cp[0] == '0') {
38 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
39 return 16;
40 else
41 return 8;
42 } else {
43 return 10;
44 }
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/**
48 * simple_strtoul - convert a string to an unsigned long
49 * @cp: The start of the string
50 * @endp: A pointer to the end of the parsed string will be placed here
51 * @base: The number base to use
52 */
Harvey Harrison22d27052008-10-16 13:40:35 -070053unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Harvey Harrisonaa46a632008-10-16 13:40:34 -070055 unsigned long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Harvey Harrisonaa46a632008-10-16 13:40:34 -070057 if (!base)
58 base = simple_guess_base(cp);
59
60 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
61 cp += 2;
62
63 while (isxdigit(*cp)) {
64 unsigned int value;
65
66 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
67 if (value >= base)
68 break;
69 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 cp++;
71 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (endp)
74 *endp = (char *)cp;
75 return result;
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077EXPORT_SYMBOL(simple_strtoul);
78
79/**
80 * simple_strtol - convert a string to a signed long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
84 */
Harvey Harrison22d27052008-10-16 13:40:35 -070085long simple_strtol(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Harvey Harrison22d27052008-10-16 13:40:35 -070087 if(*cp == '-')
88 return -simple_strtoul(cp + 1, endp, base);
89 return simple_strtoul(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091EXPORT_SYMBOL(simple_strtol);
92
93/**
94 * simple_strtoull - convert a string to an unsigned long long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
98 */
Harvey Harrison22d27052008-10-16 13:40:35 -070099unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700101 unsigned long long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700103 if (!base)
104 base = simple_guess_base(cp);
105
106 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
107 cp += 2;
108
109 while (isxdigit(*cp)) {
110 unsigned int value;
111
112 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
113 if (value >= base)
114 break;
115 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 cp++;
117 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (endp)
120 *endp = (char *)cp;
121 return result;
122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123EXPORT_SYMBOL(simple_strtoull);
124
125/**
126 * simple_strtoll - convert a string to a signed long long
127 * @cp: The start of the string
128 * @endp: A pointer to the end of the parsed string will be placed here
129 * @base: The number base to use
130 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700131long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 if(*cp=='-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700134 return -simple_strtoull(cp + 1, endp, base);
135 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Yi Yang06b2a762008-02-08 04:21:57 -0800138
139/**
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
144 *
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
150 *
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
152 *
153 * echo will append a newline to the tail.
154 *
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
157 *
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
160 */
161int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
162
163/**
164 * strict_strtol - convert a string to a long strictly
165 * @cp: The string to be converted
166 * @base: The number base to use
167 * @res: The converted result value
168 *
169 * strict_strtol is similiar to strict_strtoul, but it allows the first
170 * character of a string is '-'.
171 *
172 * It returns 0 if conversion is successful and *res is set to the converted
173 * value, otherwise it returns -EINVAL and *res is set to 0.
174 */
175int strict_strtol(const char *cp, unsigned int base, long *res);
176
177/**
178 * strict_strtoull - convert a string to an unsigned long long strictly
179 * @cp: The string to be converted
180 * @base: The number base to use
181 * @res: The converted result value
182 *
183 * strict_strtoull converts a string to an unsigned long long only if the
184 * string is really an unsigned long long string, any string containing
185 * any invalid char at the tail will be rejected and -EINVAL is returned,
186 * only a newline char at the tail is acceptible because people generally
187 * change a module parameter in the following way:
188 *
189 * echo 1024 > /sys/module/e1000/parameters/copybreak
190 *
191 * echo will append a newline to the tail of the string.
192 *
193 * It returns 0 if conversion is successful and *res is set to the converted
194 * value, otherwise it returns -EINVAL and *res is set to 0.
195 *
196 * simple_strtoull just ignores the successive invalid characters and
197 * return the converted value of prefix part of the string.
198 */
199int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
200
201/**
202 * strict_strtoll - convert a string to a long long strictly
203 * @cp: The string to be converted
204 * @base: The number base to use
205 * @res: The converted result value
206 *
207 * strict_strtoll is similiar to strict_strtoull, but it allows the first
208 * character of a string is '-'.
209 *
210 * It returns 0 if conversion is successful and *res is set to the converted
211 * value, otherwise it returns -EINVAL and *res is set to 0.
212 */
213int strict_strtoll(const char *cp, unsigned int base, long long *res);
214
215#define define_strict_strtoux(type, valtype) \
216int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
217{ \
218 char *tail; \
219 valtype val; \
220 size_t len; \
221 \
222 *res = 0; \
223 len = strlen(cp); \
224 if (len == 0) \
225 return -EINVAL; \
226 \
Yi Yang29a6d392008-08-12 15:08:58 -0700227 val = simple_strtou##type(cp, &tail, base); \
Yi Yang06b2a762008-02-08 04:21:57 -0800228 if ((*tail == '\0') || \
229 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
230 *res = val; \
231 return 0; \
232 } \
233 \
234 return -EINVAL; \
235} \
236
237#define define_strict_strtox(type, valtype) \
238int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
239{ \
240 int ret; \
241 if (*cp == '-') { \
242 ret = strict_strtou##type(cp+1, base, res); \
Hoang-Nam Nguyen4f9d5f42008-02-23 15:23:37 -0800243 if (!ret) \
Yi Yang06b2a762008-02-08 04:21:57 -0800244 *res = -(*res); \
245 } else \
246 ret = strict_strtou##type(cp, base, res); \
247 \
248 return ret; \
249} \
250
251define_strict_strtoux(l, unsigned long)
252define_strict_strtox(l, long)
253define_strict_strtoux(ll, unsigned long long)
254define_strict_strtox(ll, long long)
255
256EXPORT_SYMBOL(strict_strtoul);
257EXPORT_SYMBOL(strict_strtol);
258EXPORT_SYMBOL(strict_strtoll);
259EXPORT_SYMBOL(strict_strtoull);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static int skip_atoi(const char **s)
262{
263 int i=0;
264
265 while (isdigit(**s))
266 i = i*10 + *((*s)++) - '0';
267 return i;
268}
269
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700270/* Decimal conversion is by far the most typical, and is used
271 * for /proc and /sys data. This directly impacts e.g. top performance
272 * with many processes running. We optimize it for speed
273 * using code from
274 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
275 * (with permission from the author, Douglas W. Jones). */
276
277/* Formats correctly any integer in [0,99999].
278 * Outputs from one to five digits depending on input.
279 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
280static char* put_dec_trunc(char *buf, unsigned q)
281{
282 unsigned d3, d2, d1, d0;
283 d1 = (q>>4) & 0xf;
284 d2 = (q>>8) & 0xf;
285 d3 = (q>>12);
286
287 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
288 q = (d0 * 0xcd) >> 11;
289 d0 = d0 - 10*q;
290 *buf++ = d0 + '0'; /* least significant digit */
291 d1 = q + 9*d3 + 5*d2 + d1;
292 if (d1 != 0) {
293 q = (d1 * 0xcd) >> 11;
294 d1 = d1 - 10*q;
295 *buf++ = d1 + '0'; /* next digit */
296
297 d2 = q + 2*d2;
298 if ((d2 != 0) || (d3 != 0)) {
299 q = (d2 * 0xd) >> 7;
300 d2 = d2 - 10*q;
301 *buf++ = d2 + '0'; /* next digit */
302
303 d3 = q + 4*d3;
304 if (d3 != 0) {
305 q = (d3 * 0xcd) >> 11;
306 d3 = d3 - 10*q;
307 *buf++ = d3 + '0'; /* next digit */
308 if (q != 0)
309 *buf++ = q + '0'; /* most sign. digit */
310 }
311 }
312 }
313 return buf;
314}
315/* Same with if's removed. Always emits five digits */
316static char* put_dec_full(char *buf, unsigned q)
317{
318 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
319 /* but anyway, gcc produces better code with full-sized ints */
320 unsigned d3, d2, d1, d0;
321 d1 = (q>>4) & 0xf;
322 d2 = (q>>8) & 0xf;
323 d3 = (q>>12);
324
325 /* Possible ways to approx. divide by 10 */
326 /* gcc -O2 replaces multiply with shifts and adds */
327 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
328 // (x * 0x67) >> 10: 1100111
329 // (x * 0x34) >> 9: 110100 - same
330 // (x * 0x1a) >> 8: 11010 - same
331 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
332
333 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
334 q = (d0 * 0xcd) >> 11;
335 d0 = d0 - 10*q;
336 *buf++ = d0 + '0';
337 d1 = q + 9*d3 + 5*d2 + d1;
338 q = (d1 * 0xcd) >> 11;
339 d1 = d1 - 10*q;
340 *buf++ = d1 + '0';
341
342 d2 = q + 2*d2;
343 q = (d2 * 0xd) >> 7;
344 d2 = d2 - 10*q;
345 *buf++ = d2 + '0';
346
347 d3 = q + 4*d3;
348 q = (d3 * 0xcd) >> 11; /* - shorter code */
349 /* q = (d3 * 0x67) >> 10; - would also work */
350 d3 = d3 - 10*q;
351 *buf++ = d3 + '0';
352 *buf++ = q + '0';
353 return buf;
354}
355/* No inlining helps gcc to use registers better */
356static noinline char* put_dec(char *buf, unsigned long long num)
357{
358 while (1) {
359 unsigned rem;
360 if (num < 100000)
361 return put_dec_trunc(buf, num);
362 rem = do_div(num, 100000);
363 buf = put_dec_full(buf, rem);
364 }
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#define ZEROPAD 1 /* pad with zero */
368#define SIGN 2 /* unsigned/signed long */
369#define PLUS 4 /* show plus */
370#define SPACE 8 /* space if plus */
371#define LEFT 16 /* left justified */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100372#define SMALL 32 /* Must be 32 == 0x20 */
373#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700375static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100377 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
378 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
379
380 char tmp[66];
381 char sign;
382 char locase;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700383 int need_pfx = ((type & SPECIAL) && base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int i;
385
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100386 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
387 * produces same digits or (maybe lowercased) letters */
388 locase = (type & SMALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (type & LEFT)
390 type &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 sign = 0;
392 if (type & SIGN) {
393 if ((signed long long) num < 0) {
394 sign = '-';
395 num = - (signed long long) num;
396 size--;
397 } else if (type & PLUS) {
398 sign = '+';
399 size--;
400 } else if (type & SPACE) {
401 sign = ' ';
402 size--;
403 }
404 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700405 if (need_pfx) {
406 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (base == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 size--;
409 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700410
411 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 i = 0;
413 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700414 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700415 /* Generic code, for any base:
416 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100417 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700418 } while (num != 0);
419 */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700420 else if (base != 10) { /* 8 or 16 */
421 int mask = base - 1;
422 int shift = 3;
423 if (base == 16) shift = 4;
424 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100425 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700426 num >>= shift;
427 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700428 } else { /* base 10 */
429 i = put_dec(tmp, num) - tmp;
430 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700431
432 /* printing 100 using %2d gives "100", not "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (i > precision)
434 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700435 /* leading space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 size -= precision;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700437 if (!(type & (ZEROPAD+LEFT))) {
438 while(--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700439 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *buf = ' ';
441 ++buf;
442 }
443 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700444 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700446 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *buf = sign;
448 ++buf;
449 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450 /* "0x" / "0" prefix */
451 if (need_pfx) {
452 if (buf < end)
453 *buf = '0';
454 ++buf;
455 if (base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700456 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100457 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 ++buf;
459 }
460 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700461 /* zero or space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!(type & LEFT)) {
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700463 char c = (type & ZEROPAD) ? '0' : ' ';
464 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700465 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *buf = c;
467 ++buf;
468 }
469 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700470 /* hmm even more zero padding? */
471 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700472 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *buf = '0';
474 ++buf;
475 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700476 /* actual digits of result */
477 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700478 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *buf = tmp[i];
480 ++buf;
481 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700482 /* trailing space padding */
483 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700484 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *buf = ' ';
486 ++buf;
487 }
488 return buf;
489}
490
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700491static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
492{
493 int len, i;
494
495 if ((unsigned long)s < PAGE_SIZE)
496 s = "<NULL>";
497
498 len = strnlen(s, precision);
499
500 if (!(flags & LEFT)) {
501 while (len < field_width--) {
502 if (buf < end)
503 *buf = ' ';
504 ++buf;
505 }
506 }
507 for (i = 0; i < len; ++i) {
508 if (buf < end)
509 *buf = *s;
510 ++buf; ++s;
511 }
512 while (len < field_width--) {
513 if (buf < end)
514 *buf = ' ';
515 ++buf;
516 }
517 return buf;
518}
519
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700520static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
521{
522 unsigned long value = (unsigned long) ptr;
523#ifdef CONFIG_KALLSYMS
524 char sym[KSYM_SYMBOL_LEN];
525 sprint_symbol(sym, value);
526 return string(buf, end, sym, field_width, precision, flags);
527#else
528 field_width = 2*sizeof(void *);
529 flags |= SPECIAL | SMALL | ZEROPAD;
530 return number(buf, end, value, 16, field_width, precision, flags);
531#endif
532}
533
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700534/*
535 * Show a '%p' thing. A kernel extension is that the '%p' is followed
536 * by an extra set of alphanumeric characters that are extended format
537 * specifiers.
538 *
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700539 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
540 * and 'S' (for Symbolic direct pointers), but this can easily be
541 * extended in the future (network address types etc).
542 *
543 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
544 * pointers are really function descriptors, which contain a pointer the
545 * real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700546 */
547static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700548{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700549 switch (*fmt) {
550 case 'F':
551 ptr = dereference_function_descriptor(ptr);
552 /* Fallthrough */
553 case 'S':
554 return symbol_string(buf, end, ptr, field_width, precision, flags);
555 }
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700556 flags |= SMALL;
557 if (field_width == -1) {
558 field_width = 2*sizeof(void *);
559 flags |= ZEROPAD;
560 }
561 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/**
565 * vsnprintf - Format a string and place it in a buffer
566 * @buf: The buffer to place the result into
567 * @size: The size of the buffer, including the trailing null space
568 * @fmt: The format string to use
569 * @args: Arguments for the format string
570 *
Andi Kleen20036fd2008-10-15 22:02:02 -0700571 * This function follows C99 vsnprintf, but has some extensions:
572 * %pS output the name of a text symbol
573 * %pF output the name of a function pointer
574 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * The return value is the number of characters which would
576 * be generated for the given input, excluding the trailing
577 * '\0', as per ISO C99. If you want to have the exact
578 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800579 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * return is greater than or equal to @size, the resulting
581 * string is truncated.
582 *
583 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800584 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
586int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
587{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long long num;
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700589 int base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 char *str, *end, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 int flags; /* flags to number() */
593
594 int field_width; /* width of output field */
595 int precision; /* min. # of digits for integers; max
596 number of chars for from string */
597 int qualifier; /* 'h', 'l', or 'L' for integer fields */
598 /* 'z' support added 23/7/1999 S.H. */
599 /* 'z' changed to 'Z' --davidm 1/25/99 */
Al Viro80322302005-08-23 22:48:17 +0100600 /* 't' added for ptrdiff_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700602 /* Reject out-of-range values early. Large positive sizes are
603 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (unlikely((int) size < 0)) {
605 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700606 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 WARN_ON(warn);
608 warn = 0;
609 return 0;
610 }
611
612 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700613 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700615 /* Make sure end is always >= buf */
616 if (end < buf) {
617 end = ((void *)-1);
618 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621 for (; *fmt ; ++fmt) {
622 if (*fmt != '%') {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700623 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *str = *fmt;
625 ++str;
626 continue;
627 }
628
629 /* process flags */
630 flags = 0;
631 repeat:
632 ++fmt; /* this also skips first '%' */
633 switch (*fmt) {
634 case '-': flags |= LEFT; goto repeat;
635 case '+': flags |= PLUS; goto repeat;
636 case ' ': flags |= SPACE; goto repeat;
637 case '#': flags |= SPECIAL; goto repeat;
638 case '0': flags |= ZEROPAD; goto repeat;
639 }
640
641 /* get field width */
642 field_width = -1;
643 if (isdigit(*fmt))
644 field_width = skip_atoi(&fmt);
645 else if (*fmt == '*') {
646 ++fmt;
647 /* it's the next argument */
648 field_width = va_arg(args, int);
649 if (field_width < 0) {
650 field_width = -field_width;
651 flags |= LEFT;
652 }
653 }
654
655 /* get the precision */
656 precision = -1;
657 if (*fmt == '.') {
658 ++fmt;
659 if (isdigit(*fmt))
660 precision = skip_atoi(&fmt);
661 else if (*fmt == '*') {
662 ++fmt;
663 /* it's the next argument */
664 precision = va_arg(args, int);
665 }
666 if (precision < 0)
667 precision = 0;
668 }
669
670 /* get the conversion qualifier */
671 qualifier = -1;
672 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Al Viro80322302005-08-23 22:48:17 +0100673 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 qualifier = *fmt;
675 ++fmt;
676 if (qualifier == 'l' && *fmt == 'l') {
677 qualifier = 'L';
678 ++fmt;
679 }
680 }
681
682 /* default base */
683 base = 10;
684
685 switch (*fmt) {
686 case 'c':
687 if (!(flags & LEFT)) {
688 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700689 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 *str = ' ';
691 ++str;
692 }
693 }
694 c = (unsigned char) va_arg(args, int);
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700695 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *str = c;
697 ++str;
698 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700699 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *str = ' ';
701 ++str;
702 }
703 continue;
704
705 case 's':
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700706 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 continue;
708
709 case 'p':
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700710 str = pointer(fmt+1, str, end,
711 va_arg(args, void *),
712 field_width, precision, flags);
713 /* Skip all alphanumeric pointer suffixes */
714 while (isalnum(fmt[1]))
715 fmt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 continue;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 case 'n':
719 /* FIXME:
720 * What does C99 say about the overflow case here? */
721 if (qualifier == 'l') {
722 long * ip = va_arg(args, long *);
723 *ip = (str - buf);
724 } else if (qualifier == 'Z' || qualifier == 'z') {
725 size_t * ip = va_arg(args, size_t *);
726 *ip = (str - buf);
727 } else {
728 int * ip = va_arg(args, int *);
729 *ip = (str - buf);
730 }
731 continue;
732
733 case '%':
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700734 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 *str = '%';
736 ++str;
737 continue;
738
739 /* integer number formats - set up the flags and "break" */
740 case 'o':
741 base = 8;
742 break;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case 'x':
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100745 flags |= SMALL;
746 case 'X':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 base = 16;
748 break;
749
750 case 'd':
751 case 'i':
752 flags |= SIGN;
753 case 'u':
754 break;
755
756 default:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700757 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *str = '%';
759 ++str;
760 if (*fmt) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700761 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *str = *fmt;
763 ++str;
764 } else {
765 --fmt;
766 }
767 continue;
768 }
769 if (qualifier == 'L')
770 num = va_arg(args, long long);
771 else if (qualifier == 'l') {
772 num = va_arg(args, unsigned long);
773 if (flags & SIGN)
774 num = (signed long) num;
775 } else if (qualifier == 'Z' || qualifier == 'z') {
776 num = va_arg(args, size_t);
Al Viro80322302005-08-23 22:48:17 +0100777 } else if (qualifier == 't') {
778 num = va_arg(args, ptrdiff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 } else if (qualifier == 'h') {
780 num = (unsigned short) va_arg(args, int);
781 if (flags & SIGN)
782 num = (signed short) num;
783 } else {
784 num = va_arg(args, unsigned int);
785 if (flags & SIGN)
786 num = (signed int) num;
787 }
788 str = number(str, end, num, base,
789 field_width, precision, flags);
790 }
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700791 if (size > 0) {
792 if (str < end)
793 *str = '\0';
794 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -0700795 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700796 }
797 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return str-buf;
799}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800EXPORT_SYMBOL(vsnprintf);
801
802/**
803 * vscnprintf - Format a string and place it in a buffer
804 * @buf: The buffer to place the result into
805 * @size: The size of the buffer, including the trailing null space
806 * @fmt: The format string to use
807 * @args: Arguments for the format string
808 *
809 * The return value is the number of characters which have been written into
810 * the @buf not including the trailing '\0'. If @size is <= 0 the function
811 * returns 0.
812 *
813 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800814 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -0700815 *
816 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
818int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
819{
820 int i;
821
822 i=vsnprintf(buf,size,fmt,args);
823 return (i >= size) ? (size - 1) : i;
824}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825EXPORT_SYMBOL(vscnprintf);
826
827/**
828 * snprintf - Format a string and place it in a buffer
829 * @buf: The buffer to place the result into
830 * @size: The size of the buffer, including the trailing null space
831 * @fmt: The format string to use
832 * @...: Arguments for the format string
833 *
834 * The return value is the number of characters which would be
835 * generated for the given input, excluding the trailing null,
836 * as per ISO C99. If the return is greater than or equal to
837 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -0700838 *
839 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
841int snprintf(char * buf, size_t size, const char *fmt, ...)
842{
843 va_list args;
844 int i;
845
846 va_start(args, fmt);
847 i=vsnprintf(buf,size,fmt,args);
848 va_end(args);
849 return i;
850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851EXPORT_SYMBOL(snprintf);
852
853/**
854 * scnprintf - Format a string and place it in a buffer
855 * @buf: The buffer to place the result into
856 * @size: The size of the buffer, including the trailing null space
857 * @fmt: The format string to use
858 * @...: Arguments for the format string
859 *
860 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -0800861 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
863
864int scnprintf(char * buf, size_t size, const char *fmt, ...)
865{
866 va_list args;
867 int i;
868
869 va_start(args, fmt);
870 i = vsnprintf(buf, size, fmt, args);
871 va_end(args);
872 return (i >= size) ? (size - 1) : i;
873}
874EXPORT_SYMBOL(scnprintf);
875
876/**
877 * vsprintf - Format a string and place it in a buffer
878 * @buf: The buffer to place the result into
879 * @fmt: The format string to use
880 * @args: Arguments for the format string
881 *
882 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800883 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * buffer overflows.
885 *
886 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800887 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -0700888 *
889 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891int vsprintf(char *buf, const char *fmt, va_list args)
892{
893 return vsnprintf(buf, INT_MAX, fmt, args);
894}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895EXPORT_SYMBOL(vsprintf);
896
897/**
898 * sprintf - Format a string and place it in a buffer
899 * @buf: The buffer to place the result into
900 * @fmt: The format string to use
901 * @...: Arguments for the format string
902 *
903 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800904 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -0700906 *
907 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
909int sprintf(char * buf, const char *fmt, ...)
910{
911 va_list args;
912 int i;
913
914 va_start(args, fmt);
915 i=vsnprintf(buf, INT_MAX, fmt, args);
916 va_end(args);
917 return i;
918}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919EXPORT_SYMBOL(sprintf);
920
921/**
922 * vsscanf - Unformat a buffer into a list of arguments
923 * @buf: input buffer
924 * @fmt: format of buffer
925 * @args: arguments
926 */
927int vsscanf(const char * buf, const char * fmt, va_list args)
928{
929 const char *str = buf;
930 char *next;
931 char digit;
932 int num = 0;
933 int qualifier;
934 int base;
935 int field_width;
936 int is_sign = 0;
937
938 while(*fmt && *str) {
939 /* skip any white space in format */
940 /* white space in format matchs any amount of
941 * white space, including none, in the input.
942 */
943 if (isspace(*fmt)) {
944 while (isspace(*fmt))
945 ++fmt;
946 while (isspace(*str))
947 ++str;
948 }
949
950 /* anything that is not a conversion must match exactly */
951 if (*fmt != '%' && *fmt) {
952 if (*fmt++ != *str++)
953 break;
954 continue;
955 }
956
957 if (!*fmt)
958 break;
959 ++fmt;
960
961 /* skip this conversion.
962 * advance both strings to next white space
963 */
964 if (*fmt == '*') {
965 while (!isspace(*fmt) && *fmt)
966 fmt++;
967 while (!isspace(*str) && *str)
968 str++;
969 continue;
970 }
971
972 /* get field width */
973 field_width = -1;
974 if (isdigit(*fmt))
975 field_width = skip_atoi(&fmt);
976
977 /* get conversion qualifier */
978 qualifier = -1;
979 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
980 *fmt == 'Z' || *fmt == 'z') {
981 qualifier = *fmt++;
982 if (unlikely(qualifier == *fmt)) {
983 if (qualifier == 'h') {
984 qualifier = 'H';
985 fmt++;
986 } else if (qualifier == 'l') {
987 qualifier = 'L';
988 fmt++;
989 }
990 }
991 }
992 base = 10;
993 is_sign = 0;
994
995 if (!*fmt || !*str)
996 break;
997
998 switch(*fmt++) {
999 case 'c':
1000 {
1001 char *s = (char *) va_arg(args,char*);
1002 if (field_width == -1)
1003 field_width = 1;
1004 do {
1005 *s++ = *str++;
1006 } while (--field_width > 0 && *str);
1007 num++;
1008 }
1009 continue;
1010 case 's':
1011 {
1012 char *s = (char *) va_arg(args, char *);
1013 if(field_width == -1)
1014 field_width = INT_MAX;
1015 /* first, skip leading white space in buffer */
1016 while (isspace(*str))
1017 str++;
1018
1019 /* now copy until next white space */
1020 while (*str && !isspace(*str) && field_width--) {
1021 *s++ = *str++;
1022 }
1023 *s = '\0';
1024 num++;
1025 }
1026 continue;
1027 case 'n':
1028 /* return number of characters read so far */
1029 {
1030 int *i = (int *)va_arg(args,int*);
1031 *i = str - buf;
1032 }
1033 continue;
1034 case 'o':
1035 base = 8;
1036 break;
1037 case 'x':
1038 case 'X':
1039 base = 16;
1040 break;
1041 case 'i':
1042 base = 0;
1043 case 'd':
1044 is_sign = 1;
1045 case 'u':
1046 break;
1047 case '%':
1048 /* looking for '%' in str */
1049 if (*str++ != '%')
1050 return num;
1051 continue;
1052 default:
1053 /* invalid format; stop here */
1054 return num;
1055 }
1056
1057 /* have some sort of integer conversion.
1058 * first, skip white space in buffer.
1059 */
1060 while (isspace(*str))
1061 str++;
1062
1063 digit = *str;
1064 if (is_sign && digit == '-')
1065 digit = *(str + 1);
1066
1067 if (!digit
1068 || (base == 16 && !isxdigit(digit))
1069 || (base == 10 && !isdigit(digit))
1070 || (base == 8 && (!isdigit(digit) || digit > '7'))
1071 || (base == 0 && !isdigit(digit)))
1072 break;
1073
1074 switch(qualifier) {
1075 case 'H': /* that's 'hh' in format */
1076 if (is_sign) {
1077 signed char *s = (signed char *) va_arg(args,signed char *);
1078 *s = (signed char) simple_strtol(str,&next,base);
1079 } else {
1080 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1081 *s = (unsigned char) simple_strtoul(str, &next, base);
1082 }
1083 break;
1084 case 'h':
1085 if (is_sign) {
1086 short *s = (short *) va_arg(args,short *);
1087 *s = (short) simple_strtol(str,&next,base);
1088 } else {
1089 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1090 *s = (unsigned short) simple_strtoul(str, &next, base);
1091 }
1092 break;
1093 case 'l':
1094 if (is_sign) {
1095 long *l = (long *) va_arg(args,long *);
1096 *l = simple_strtol(str,&next,base);
1097 } else {
1098 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1099 *l = simple_strtoul(str,&next,base);
1100 }
1101 break;
1102 case 'L':
1103 if (is_sign) {
1104 long long *l = (long long*) va_arg(args,long long *);
1105 *l = simple_strtoll(str,&next,base);
1106 } else {
1107 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1108 *l = simple_strtoull(str,&next,base);
1109 }
1110 break;
1111 case 'Z':
1112 case 'z':
1113 {
1114 size_t *s = (size_t*) va_arg(args,size_t*);
1115 *s = (size_t) simple_strtoul(str,&next,base);
1116 }
1117 break;
1118 default:
1119 if (is_sign) {
1120 int *i = (int *) va_arg(args, int*);
1121 *i = (int) simple_strtol(str,&next,base);
1122 } else {
1123 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1124 *i = (unsigned int) simple_strtoul(str,&next,base);
1125 }
1126 break;
1127 }
1128 num++;
1129
1130 if (!next)
1131 break;
1132 str = next;
1133 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001134
1135 /*
1136 * Now we've come all the way through so either the input string or the
1137 * format ended. In the former case, there can be a %n at the current
1138 * position in the format that needs to be filled.
1139 */
1140 if (*fmt == '%' && *(fmt + 1) == 'n') {
1141 int *p = (int *)va_arg(args, int *);
1142 *p = str - buf;
1143 }
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return num;
1146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147EXPORT_SYMBOL(vsscanf);
1148
1149/**
1150 * sscanf - Unformat a buffer into a list of arguments
1151 * @buf: input buffer
1152 * @fmt: formatting of buffer
1153 * @...: resulting arguments
1154 */
1155int sscanf(const char * buf, const char * fmt, ...)
1156{
1157 va_list args;
1158 int i;
1159
1160 va_start(args,fmt);
1161 i = vsscanf(buf,fmt,args);
1162 va_end(args);
1163 return i;
1164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165EXPORT_SYMBOL(sscanf);