blob: 4c6674a41ed9df423fd806c48e1c66f43e019beb [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/**
36 * simple_strtoul - convert a string to an unsigned long
37 * @cp: The start of the string
38 * @endp: A pointer to the end of the parsed string will be placed here
39 * @base: The number base to use
40 */
41unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
42{
43 unsigned long result = 0,value;
44
45 if (!base) {
46 base = 10;
47 if (*cp == '0') {
48 base = 8;
49 cp++;
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010050 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 cp++;
52 base = 16;
53 }
54 }
55 } else if (base == 16) {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010056 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 cp += 2;
58 }
59 while (isxdigit(*cp) &&
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010060 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 result = result*base + value;
62 cp++;
63 }
64 if (endp)
65 *endp = (char *)cp;
66 return result;
67}
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 return simple_strtoul(cp,endp,base);
82}
83
84EXPORT_SYMBOL(simple_strtol);
85
86/**
87 * simple_strtoull - convert a string to an unsigned 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 */
92unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
93{
94 unsigned long long result = 0,value;
95
96 if (!base) {
97 base = 10;
98 if (*cp == '0') {
99 base = 8;
100 cp++;
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100101 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 cp++;
103 base = 16;
104 }
105 }
106 } else if (base == 16) {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100107 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 cp += 2;
109 }
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100110 while (isxdigit(*cp)
111 && (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 result = result*base + value;
113 cp++;
114 }
115 if (endp)
116 *endp = (char *)cp;
117 return result;
118}
119
120EXPORT_SYMBOL(simple_strtoull);
121
122/**
123 * simple_strtoll - convert a string to a signed long long
124 * @cp: The start of the string
125 * @endp: A pointer to the end of the parsed string will be placed here
126 * @base: The number base to use
127 */
128long long simple_strtoll(const char *cp,char **endp,unsigned int base)
129{
130 if(*cp=='-')
131 return -simple_strtoull(cp+1,endp,base);
132 return simple_strtoull(cp,endp,base);
133}
134
Yi Yang06b2a762008-02-08 04:21:57 -0800135
136/**
137 * strict_strtoul - convert a string to an unsigned long strictly
138 * @cp: The string to be converted
139 * @base: The number base to use
140 * @res: The converted result value
141 *
142 * strict_strtoul converts a string to an unsigned long only if the
143 * string is really an unsigned long string, any string containing
144 * any invalid char at the tail will be rejected and -EINVAL is returned,
145 * only a newline char at the tail is acceptible because people generally
146 * change a module parameter in the following way:
147 *
148 * echo 1024 > /sys/module/e1000/parameters/copybreak
149 *
150 * echo will append a newline to the tail.
151 *
152 * It returns 0 if conversion is successful and *res is set to the converted
153 * value, otherwise it returns -EINVAL and *res is set to 0.
154 *
155 * simple_strtoul just ignores the successive invalid characters and
156 * return the converted value of prefix part of the string.
157 */
158int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
159
160/**
161 * strict_strtol - convert a string to a long strictly
162 * @cp: The string to be converted
163 * @base: The number base to use
164 * @res: The converted result value
165 *
166 * strict_strtol is similiar to strict_strtoul, but it allows the first
167 * character of a string is '-'.
168 *
169 * It returns 0 if conversion is successful and *res is set to the converted
170 * value, otherwise it returns -EINVAL and *res is set to 0.
171 */
172int strict_strtol(const char *cp, unsigned int base, long *res);
173
174/**
175 * strict_strtoull - convert a string to an unsigned long long strictly
176 * @cp: The string to be converted
177 * @base: The number base to use
178 * @res: The converted result value
179 *
180 * strict_strtoull converts a string to an unsigned long long only if the
181 * string is really an unsigned long long string, any string containing
182 * any invalid char at the tail will be rejected and -EINVAL is returned,
183 * only a newline char at the tail is acceptible because people generally
184 * change a module parameter in the following way:
185 *
186 * echo 1024 > /sys/module/e1000/parameters/copybreak
187 *
188 * echo will append a newline to the tail of the string.
189 *
190 * It returns 0 if conversion is successful and *res is set to the converted
191 * value, otherwise it returns -EINVAL and *res is set to 0.
192 *
193 * simple_strtoull just ignores the successive invalid characters and
194 * return the converted value of prefix part of the string.
195 */
196int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
197
198/**
199 * strict_strtoll - convert a string to a long long strictly
200 * @cp: The string to be converted
201 * @base: The number base to use
202 * @res: The converted result value
203 *
204 * strict_strtoll is similiar to strict_strtoull, but it allows the first
205 * character of a string is '-'.
206 *
207 * It returns 0 if conversion is successful and *res is set to the converted
208 * value, otherwise it returns -EINVAL and *res is set to 0.
209 */
210int strict_strtoll(const char *cp, unsigned int base, long long *res);
211
212#define define_strict_strtoux(type, valtype) \
213int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
214{ \
215 char *tail; \
216 valtype val; \
217 size_t len; \
218 \
219 *res = 0; \
220 len = strlen(cp); \
221 if (len == 0) \
222 return -EINVAL; \
223 \
Yi Yang29a6d392008-08-12 15:08:58 -0700224 val = simple_strtou##type(cp, &tail, base); \
Yi Yang06b2a762008-02-08 04:21:57 -0800225 if ((*tail == '\0') || \
226 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
227 *res = val; \
228 return 0; \
229 } \
230 \
231 return -EINVAL; \
232} \
233
234#define define_strict_strtox(type, valtype) \
235int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
236{ \
237 int ret; \
238 if (*cp == '-') { \
239 ret = strict_strtou##type(cp+1, base, res); \
Hoang-Nam Nguyen4f9d5f42008-02-23 15:23:37 -0800240 if (!ret) \
Yi Yang06b2a762008-02-08 04:21:57 -0800241 *res = -(*res); \
242 } else \
243 ret = strict_strtou##type(cp, base, res); \
244 \
245 return ret; \
246} \
247
248define_strict_strtoux(l, unsigned long)
249define_strict_strtox(l, long)
250define_strict_strtoux(ll, unsigned long long)
251define_strict_strtox(ll, long long)
252
253EXPORT_SYMBOL(strict_strtoul);
254EXPORT_SYMBOL(strict_strtol);
255EXPORT_SYMBOL(strict_strtoll);
256EXPORT_SYMBOL(strict_strtoull);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static int skip_atoi(const char **s)
259{
260 int i=0;
261
262 while (isdigit(**s))
263 i = i*10 + *((*s)++) - '0';
264 return i;
265}
266
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700267/* Decimal conversion is by far the most typical, and is used
268 * for /proc and /sys data. This directly impacts e.g. top performance
269 * with many processes running. We optimize it for speed
270 * using code from
271 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
272 * (with permission from the author, Douglas W. Jones). */
273
274/* Formats correctly any integer in [0,99999].
275 * Outputs from one to five digits depending on input.
276 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
277static char* put_dec_trunc(char *buf, unsigned q)
278{
279 unsigned d3, d2, d1, d0;
280 d1 = (q>>4) & 0xf;
281 d2 = (q>>8) & 0xf;
282 d3 = (q>>12);
283
284 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
285 q = (d0 * 0xcd) >> 11;
286 d0 = d0 - 10*q;
287 *buf++ = d0 + '0'; /* least significant digit */
288 d1 = q + 9*d3 + 5*d2 + d1;
289 if (d1 != 0) {
290 q = (d1 * 0xcd) >> 11;
291 d1 = d1 - 10*q;
292 *buf++ = d1 + '0'; /* next digit */
293
294 d2 = q + 2*d2;
295 if ((d2 != 0) || (d3 != 0)) {
296 q = (d2 * 0xd) >> 7;
297 d2 = d2 - 10*q;
298 *buf++ = d2 + '0'; /* next digit */
299
300 d3 = q + 4*d3;
301 if (d3 != 0) {
302 q = (d3 * 0xcd) >> 11;
303 d3 = d3 - 10*q;
304 *buf++ = d3 + '0'; /* next digit */
305 if (q != 0)
306 *buf++ = q + '0'; /* most sign. digit */
307 }
308 }
309 }
310 return buf;
311}
312/* Same with if's removed. Always emits five digits */
313static char* put_dec_full(char *buf, unsigned q)
314{
315 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
316 /* but anyway, gcc produces better code with full-sized ints */
317 unsigned d3, d2, d1, d0;
318 d1 = (q>>4) & 0xf;
319 d2 = (q>>8) & 0xf;
320 d3 = (q>>12);
321
322 /* Possible ways to approx. divide by 10 */
323 /* gcc -O2 replaces multiply with shifts and adds */
324 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
325 // (x * 0x67) >> 10: 1100111
326 // (x * 0x34) >> 9: 110100 - same
327 // (x * 0x1a) >> 8: 11010 - same
328 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
329
330 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
331 q = (d0 * 0xcd) >> 11;
332 d0 = d0 - 10*q;
333 *buf++ = d0 + '0';
334 d1 = q + 9*d3 + 5*d2 + d1;
335 q = (d1 * 0xcd) >> 11;
336 d1 = d1 - 10*q;
337 *buf++ = d1 + '0';
338
339 d2 = q + 2*d2;
340 q = (d2 * 0xd) >> 7;
341 d2 = d2 - 10*q;
342 *buf++ = d2 + '0';
343
344 d3 = q + 4*d3;
345 q = (d3 * 0xcd) >> 11; /* - shorter code */
346 /* q = (d3 * 0x67) >> 10; - would also work */
347 d3 = d3 - 10*q;
348 *buf++ = d3 + '0';
349 *buf++ = q + '0';
350 return buf;
351}
352/* No inlining helps gcc to use registers better */
353static noinline char* put_dec(char *buf, unsigned long long num)
354{
355 while (1) {
356 unsigned rem;
357 if (num < 100000)
358 return put_dec_trunc(buf, num);
359 rem = do_div(num, 100000);
360 buf = put_dec_full(buf, rem);
361 }
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#define ZEROPAD 1 /* pad with zero */
365#define SIGN 2 /* unsigned/signed long */
366#define PLUS 4 /* show plus */
367#define SPACE 8 /* space if plus */
368#define LEFT 16 /* left justified */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100369#define SMALL 32 /* Must be 32 == 0x20 */
370#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700372static 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 -0700373{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100374 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
375 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
376
377 char tmp[66];
378 char sign;
379 char locase;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700380 int need_pfx = ((type & SPECIAL) && base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int i;
382
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100383 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
384 * produces same digits or (maybe lowercased) letters */
385 locase = (type & SMALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (type & LEFT)
387 type &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 sign = 0;
389 if (type & SIGN) {
390 if ((signed long long) num < 0) {
391 sign = '-';
392 num = - (signed long long) num;
393 size--;
394 } else if (type & PLUS) {
395 sign = '+';
396 size--;
397 } else if (type & SPACE) {
398 sign = ' ';
399 size--;
400 }
401 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700402 if (need_pfx) {
403 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (base == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 size--;
406 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700407
408 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 i = 0;
410 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700411 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700412 /* Generic code, for any base:
413 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100414 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700415 } while (num != 0);
416 */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700417 else if (base != 10) { /* 8 or 16 */
418 int mask = base - 1;
419 int shift = 3;
420 if (base == 16) shift = 4;
421 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100422 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700423 num >>= shift;
424 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700425 } else { /* base 10 */
426 i = put_dec(tmp, num) - tmp;
427 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700428
429 /* printing 100 using %2d gives "100", not "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (i > precision)
431 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700432 /* leading space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 size -= precision;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700434 if (!(type & (ZEROPAD+LEFT))) {
435 while(--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700436 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *buf = ' ';
438 ++buf;
439 }
440 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700441 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700443 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *buf = sign;
445 ++buf;
446 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 /* "0x" / "0" prefix */
448 if (need_pfx) {
449 if (buf < end)
450 *buf = '0';
451 ++buf;
452 if (base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700453 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100454 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ++buf;
456 }
457 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700458 /* zero or space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!(type & LEFT)) {
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460 char c = (type & ZEROPAD) ? '0' : ' ';
461 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700462 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *buf = c;
464 ++buf;
465 }
466 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700467 /* hmm even more zero padding? */
468 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700469 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *buf = '0';
471 ++buf;
472 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700473 /* actual digits of result */
474 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700475 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *buf = tmp[i];
477 ++buf;
478 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 /* trailing space padding */
480 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700481 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *buf = ' ';
483 ++buf;
484 }
485 return buf;
486}
487
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700488static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
489{
490 int len, i;
491
492 if ((unsigned long)s < PAGE_SIZE)
493 s = "<NULL>";
494
495 len = strnlen(s, precision);
496
497 if (!(flags & LEFT)) {
498 while (len < field_width--) {
499 if (buf < end)
500 *buf = ' ';
501 ++buf;
502 }
503 }
504 for (i = 0; i < len; ++i) {
505 if (buf < end)
506 *buf = *s;
507 ++buf; ++s;
508 }
509 while (len < field_width--) {
510 if (buf < end)
511 *buf = ' ';
512 ++buf;
513 }
514 return buf;
515}
516
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700517static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
518{
519 unsigned long value = (unsigned long) ptr;
520#ifdef CONFIG_KALLSYMS
521 char sym[KSYM_SYMBOL_LEN];
522 sprint_symbol(sym, value);
523 return string(buf, end, sym, field_width, precision, flags);
524#else
525 field_width = 2*sizeof(void *);
526 flags |= SPECIAL | SMALL | ZEROPAD;
527 return number(buf, end, value, 16, field_width, precision, flags);
528#endif
529}
530
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700531/*
532 * Show a '%p' thing. A kernel extension is that the '%p' is followed
533 * by an extra set of alphanumeric characters that are extended format
534 * specifiers.
535 *
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700536 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
537 * and 'S' (for Symbolic direct pointers), but this can easily be
538 * extended in the future (network address types etc).
539 *
540 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
541 * pointers are really function descriptors, which contain a pointer the
542 * real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700543 */
544static 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 -0700545{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700546 switch (*fmt) {
547 case 'F':
548 ptr = dereference_function_descriptor(ptr);
549 /* Fallthrough */
550 case 'S':
551 return symbol_string(buf, end, ptr, field_width, precision, flags);
552 }
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700553 flags |= SMALL;
554 if (field_width == -1) {
555 field_width = 2*sizeof(void *);
556 flags |= ZEROPAD;
557 }
558 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/**
562 * vsnprintf - Format a string and place it in a buffer
563 * @buf: The buffer to place the result into
564 * @size: The size of the buffer, including the trailing null space
565 * @fmt: The format string to use
566 * @args: Arguments for the format string
567 *
Andi Kleen20036fd2008-10-15 22:02:02 -0700568 * This function follows C99 vsnprintf, but has some extensions:
569 * %pS output the name of a text symbol
570 * %pF output the name of a function pointer
571 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * The return value is the number of characters which would
573 * be generated for the given input, excluding the trailing
574 * '\0', as per ISO C99. If you want to have the exact
575 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800576 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * return is greater than or equal to @size, the resulting
578 * string is truncated.
579 *
580 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800581 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 unsigned long long num;
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700586 int base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 char *str, *end, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 int flags; /* flags to number() */
590
591 int field_width; /* width of output field */
592 int precision; /* min. # of digits for integers; max
593 number of chars for from string */
594 int qualifier; /* 'h', 'l', or 'L' for integer fields */
595 /* 'z' support added 23/7/1999 S.H. */
596 /* 'z' changed to 'Z' --davidm 1/25/99 */
Al Viro80322302005-08-23 22:48:17 +0100597 /* 't' added for ptrdiff_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700599 /* Reject out-of-range values early. Large positive sizes are
600 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (unlikely((int) size < 0)) {
602 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700603 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 WARN_ON(warn);
605 warn = 0;
606 return 0;
607 }
608
609 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700610 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700612 /* Make sure end is always >= buf */
613 if (end < buf) {
614 end = ((void *)-1);
615 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 for (; *fmt ; ++fmt) {
619 if (*fmt != '%') {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700620 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *str = *fmt;
622 ++str;
623 continue;
624 }
625
626 /* process flags */
627 flags = 0;
628 repeat:
629 ++fmt; /* this also skips first '%' */
630 switch (*fmt) {
631 case '-': flags |= LEFT; goto repeat;
632 case '+': flags |= PLUS; goto repeat;
633 case ' ': flags |= SPACE; goto repeat;
634 case '#': flags |= SPECIAL; goto repeat;
635 case '0': flags |= ZEROPAD; goto repeat;
636 }
637
638 /* get field width */
639 field_width = -1;
640 if (isdigit(*fmt))
641 field_width = skip_atoi(&fmt);
642 else if (*fmt == '*') {
643 ++fmt;
644 /* it's the next argument */
645 field_width = va_arg(args, int);
646 if (field_width < 0) {
647 field_width = -field_width;
648 flags |= LEFT;
649 }
650 }
651
652 /* get the precision */
653 precision = -1;
654 if (*fmt == '.') {
655 ++fmt;
656 if (isdigit(*fmt))
657 precision = skip_atoi(&fmt);
658 else if (*fmt == '*') {
659 ++fmt;
660 /* it's the next argument */
661 precision = va_arg(args, int);
662 }
663 if (precision < 0)
664 precision = 0;
665 }
666
667 /* get the conversion qualifier */
668 qualifier = -1;
669 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Al Viro80322302005-08-23 22:48:17 +0100670 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 qualifier = *fmt;
672 ++fmt;
673 if (qualifier == 'l' && *fmt == 'l') {
674 qualifier = 'L';
675 ++fmt;
676 }
677 }
678
679 /* default base */
680 base = 10;
681
682 switch (*fmt) {
683 case 'c':
684 if (!(flags & LEFT)) {
685 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700686 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *str = ' ';
688 ++str;
689 }
690 }
691 c = (unsigned char) va_arg(args, int);
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700692 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 *str = c;
694 ++str;
695 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700696 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *str = ' ';
698 ++str;
699 }
700 continue;
701
702 case 's':
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700703 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 continue;
705
706 case 'p':
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700707 str = pointer(fmt+1, str, end,
708 va_arg(args, void *),
709 field_width, precision, flags);
710 /* Skip all alphanumeric pointer suffixes */
711 while (isalnum(fmt[1]))
712 fmt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 continue;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 case 'n':
716 /* FIXME:
717 * What does C99 say about the overflow case here? */
718 if (qualifier == 'l') {
719 long * ip = va_arg(args, long *);
720 *ip = (str - buf);
721 } else if (qualifier == 'Z' || qualifier == 'z') {
722 size_t * ip = va_arg(args, size_t *);
723 *ip = (str - buf);
724 } else {
725 int * ip = va_arg(args, int *);
726 *ip = (str - buf);
727 }
728 continue;
729
730 case '%':
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700731 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *str = '%';
733 ++str;
734 continue;
735
736 /* integer number formats - set up the flags and "break" */
737 case 'o':
738 base = 8;
739 break;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case 'x':
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100742 flags |= SMALL;
743 case 'X':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 base = 16;
745 break;
746
747 case 'd':
748 case 'i':
749 flags |= SIGN;
750 case 'u':
751 break;
752
753 default:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700754 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *str = '%';
756 ++str;
757 if (*fmt) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700758 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *str = *fmt;
760 ++str;
761 } else {
762 --fmt;
763 }
764 continue;
765 }
766 if (qualifier == 'L')
767 num = va_arg(args, long long);
768 else if (qualifier == 'l') {
769 num = va_arg(args, unsigned long);
770 if (flags & SIGN)
771 num = (signed long) num;
772 } else if (qualifier == 'Z' || qualifier == 'z') {
773 num = va_arg(args, size_t);
Al Viro80322302005-08-23 22:48:17 +0100774 } else if (qualifier == 't') {
775 num = va_arg(args, ptrdiff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 } else if (qualifier == 'h') {
777 num = (unsigned short) va_arg(args, int);
778 if (flags & SIGN)
779 num = (signed short) num;
780 } else {
781 num = va_arg(args, unsigned int);
782 if (flags & SIGN)
783 num = (signed int) num;
784 }
785 str = number(str, end, num, base,
786 field_width, precision, flags);
787 }
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700788 if (size > 0) {
789 if (str < end)
790 *str = '\0';
791 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -0700792 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700793 }
794 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return str-buf;
796}
797
798EXPORT_SYMBOL(vsnprintf);
799
800/**
801 * vscnprintf - Format a string and place it in a buffer
802 * @buf: The buffer to place the result into
803 * @size: The size of the buffer, including the trailing null space
804 * @fmt: The format string to use
805 * @args: Arguments for the format string
806 *
807 * The return value is the number of characters which have been written into
808 * the @buf not including the trailing '\0'. If @size is <= 0 the function
809 * returns 0.
810 *
811 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800812 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -0700813 *
814 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
816int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
817{
818 int i;
819
820 i=vsnprintf(buf,size,fmt,args);
821 return (i >= size) ? (size - 1) : i;
822}
823
824EXPORT_SYMBOL(vscnprintf);
825
826/**
827 * snprintf - Format a string and place it in a buffer
828 * @buf: The buffer to place the result into
829 * @size: The size of the buffer, including the trailing null space
830 * @fmt: The format string to use
831 * @...: Arguments for the format string
832 *
833 * The return value is the number of characters which would be
834 * generated for the given input, excluding the trailing null,
835 * as per ISO C99. If the return is greater than or equal to
836 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -0700837 *
838 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
840int snprintf(char * buf, size_t size, const char *fmt, ...)
841{
842 va_list args;
843 int i;
844
845 va_start(args, fmt);
846 i=vsnprintf(buf,size,fmt,args);
847 va_end(args);
848 return i;
849}
850
851EXPORT_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}
895
896EXPORT_SYMBOL(vsprintf);
897
898/**
899 * sprintf - Format a string and place it in a buffer
900 * @buf: The buffer to place the result into
901 * @fmt: The format string to use
902 * @...: Arguments for the format string
903 *
904 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800905 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -0700907 *
908 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
910int sprintf(char * buf, const char *fmt, ...)
911{
912 va_list args;
913 int i;
914
915 va_start(args, fmt);
916 i=vsnprintf(buf, INT_MAX, fmt, args);
917 va_end(args);
918 return i;
919}
920
921EXPORT_SYMBOL(sprintf);
922
923/**
924 * vsscanf - Unformat a buffer into a list of arguments
925 * @buf: input buffer
926 * @fmt: format of buffer
927 * @args: arguments
928 */
929int vsscanf(const char * buf, const char * fmt, va_list args)
930{
931 const char *str = buf;
932 char *next;
933 char digit;
934 int num = 0;
935 int qualifier;
936 int base;
937 int field_width;
938 int is_sign = 0;
939
940 while(*fmt && *str) {
941 /* skip any white space in format */
942 /* white space in format matchs any amount of
943 * white space, including none, in the input.
944 */
945 if (isspace(*fmt)) {
946 while (isspace(*fmt))
947 ++fmt;
948 while (isspace(*str))
949 ++str;
950 }
951
952 /* anything that is not a conversion must match exactly */
953 if (*fmt != '%' && *fmt) {
954 if (*fmt++ != *str++)
955 break;
956 continue;
957 }
958
959 if (!*fmt)
960 break;
961 ++fmt;
962
963 /* skip this conversion.
964 * advance both strings to next white space
965 */
966 if (*fmt == '*') {
967 while (!isspace(*fmt) && *fmt)
968 fmt++;
969 while (!isspace(*str) && *str)
970 str++;
971 continue;
972 }
973
974 /* get field width */
975 field_width = -1;
976 if (isdigit(*fmt))
977 field_width = skip_atoi(&fmt);
978
979 /* get conversion qualifier */
980 qualifier = -1;
981 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
982 *fmt == 'Z' || *fmt == 'z') {
983 qualifier = *fmt++;
984 if (unlikely(qualifier == *fmt)) {
985 if (qualifier == 'h') {
986 qualifier = 'H';
987 fmt++;
988 } else if (qualifier == 'l') {
989 qualifier = 'L';
990 fmt++;
991 }
992 }
993 }
994 base = 10;
995 is_sign = 0;
996
997 if (!*fmt || !*str)
998 break;
999
1000 switch(*fmt++) {
1001 case 'c':
1002 {
1003 char *s = (char *) va_arg(args,char*);
1004 if (field_width == -1)
1005 field_width = 1;
1006 do {
1007 *s++ = *str++;
1008 } while (--field_width > 0 && *str);
1009 num++;
1010 }
1011 continue;
1012 case 's':
1013 {
1014 char *s = (char *) va_arg(args, char *);
1015 if(field_width == -1)
1016 field_width = INT_MAX;
1017 /* first, skip leading white space in buffer */
1018 while (isspace(*str))
1019 str++;
1020
1021 /* now copy until next white space */
1022 while (*str && !isspace(*str) && field_width--) {
1023 *s++ = *str++;
1024 }
1025 *s = '\0';
1026 num++;
1027 }
1028 continue;
1029 case 'n':
1030 /* return number of characters read so far */
1031 {
1032 int *i = (int *)va_arg(args,int*);
1033 *i = str - buf;
1034 }
1035 continue;
1036 case 'o':
1037 base = 8;
1038 break;
1039 case 'x':
1040 case 'X':
1041 base = 16;
1042 break;
1043 case 'i':
1044 base = 0;
1045 case 'd':
1046 is_sign = 1;
1047 case 'u':
1048 break;
1049 case '%':
1050 /* looking for '%' in str */
1051 if (*str++ != '%')
1052 return num;
1053 continue;
1054 default:
1055 /* invalid format; stop here */
1056 return num;
1057 }
1058
1059 /* have some sort of integer conversion.
1060 * first, skip white space in buffer.
1061 */
1062 while (isspace(*str))
1063 str++;
1064
1065 digit = *str;
1066 if (is_sign && digit == '-')
1067 digit = *(str + 1);
1068
1069 if (!digit
1070 || (base == 16 && !isxdigit(digit))
1071 || (base == 10 && !isdigit(digit))
1072 || (base == 8 && (!isdigit(digit) || digit > '7'))
1073 || (base == 0 && !isdigit(digit)))
1074 break;
1075
1076 switch(qualifier) {
1077 case 'H': /* that's 'hh' in format */
1078 if (is_sign) {
1079 signed char *s = (signed char *) va_arg(args,signed char *);
1080 *s = (signed char) simple_strtol(str,&next,base);
1081 } else {
1082 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1083 *s = (unsigned char) simple_strtoul(str, &next, base);
1084 }
1085 break;
1086 case 'h':
1087 if (is_sign) {
1088 short *s = (short *) va_arg(args,short *);
1089 *s = (short) simple_strtol(str,&next,base);
1090 } else {
1091 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1092 *s = (unsigned short) simple_strtoul(str, &next, base);
1093 }
1094 break;
1095 case 'l':
1096 if (is_sign) {
1097 long *l = (long *) va_arg(args,long *);
1098 *l = simple_strtol(str,&next,base);
1099 } else {
1100 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1101 *l = simple_strtoul(str,&next,base);
1102 }
1103 break;
1104 case 'L':
1105 if (is_sign) {
1106 long long *l = (long long*) va_arg(args,long long *);
1107 *l = simple_strtoll(str,&next,base);
1108 } else {
1109 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1110 *l = simple_strtoull(str,&next,base);
1111 }
1112 break;
1113 case 'Z':
1114 case 'z':
1115 {
1116 size_t *s = (size_t*) va_arg(args,size_t*);
1117 *s = (size_t) simple_strtoul(str,&next,base);
1118 }
1119 break;
1120 default:
1121 if (is_sign) {
1122 int *i = (int *) va_arg(args, int*);
1123 *i = (int) simple_strtol(str,&next,base);
1124 } else {
1125 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1126 *i = (unsigned int) simple_strtoul(str,&next,base);
1127 }
1128 break;
1129 }
1130 num++;
1131
1132 if (!next)
1133 break;
1134 str = next;
1135 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001136
1137 /*
1138 * Now we've come all the way through so either the input string or the
1139 * format ended. In the former case, there can be a %n at the current
1140 * position in the format that needs to be filled.
1141 */
1142 if (*fmt == '%' && *(fmt + 1) == 'n') {
1143 int *p = (int *)va_arg(args, int *);
1144 *p = str - buf;
1145 }
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return num;
1148}
1149
1150EXPORT_SYMBOL(vsscanf);
1151
1152/**
1153 * sscanf - Unformat a buffer into a list of arguments
1154 * @buf: input buffer
1155 * @fmt: formatting of buffer
1156 * @...: resulting arguments
1157 */
1158int sscanf(const char * buf, const char * fmt, ...)
1159{
1160 va_list args;
1161 int i;
1162
1163 va_start(args,fmt);
1164 i = vsscanf(buf,fmt,args);
1165 va_end(args);
1166 return i;
1167}
1168
1169EXPORT_SYMBOL(sscanf);