]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/vsprintf.c
get rid of special-casing the /sbin/loader on alpha
[mirror_ubuntu-artful-kernel.git] / lib / vsprintf.c
CommitLineData
1da177e4
LT
1/*
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>
0fe1ef24
LT
25#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
332d2e78 27#include <linux/ioport.h>
1da177e4 28
4e57b681 29#include <asm/page.h> /* for PAGE_SIZE */
1da177e4 30#include <asm/div64.h>
deac93df 31#include <asm/sections.h> /* for dereference_function_descriptor() */
1da177e4 32
9b706aee
DV
33/* Works only for digits and letters, but small and fast */
34#define TOLOWER(x) ((x) | 0x20)
35
aa46a63e
HH
36static unsigned int simple_guess_base(const char *cp)
37{
38 if (cp[0] == '0') {
39 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
40 return 16;
41 else
42 return 8;
43 } else {
44 return 10;
45 }
46}
47
1da177e4
LT
48/**
49 * simple_strtoul - convert a string to an unsigned long
50 * @cp: The start of the string
51 * @endp: A pointer to the end of the parsed string will be placed here
52 * @base: The number base to use
53 */
22d27051 54unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
1da177e4 55{
aa46a63e
HH
56 unsigned long result = 0;
57
58 if (!base)
59 base = simple_guess_base(cp);
60
61 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
62 cp += 2;
63
64 while (isxdigit(*cp)) {
65 unsigned int value;
66
67 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
68 if (value >= base)
69 break;
70 result = result * base + value;
1da177e4
LT
71 cp++;
72 }
aa46a63e 73
1da177e4
LT
74 if (endp)
75 *endp = (char *)cp;
76 return result;
77}
1da177e4
LT
78EXPORT_SYMBOL(simple_strtoul);
79
80/**
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
85 */
22d27051 86long simple_strtol(const char *cp, char **endp, unsigned int base)
1da177e4 87{
22d27051
HH
88 if(*cp == '-')
89 return -simple_strtoul(cp + 1, endp, base);
90 return simple_strtoul(cp, endp, base);
1da177e4 91}
1da177e4
LT
92EXPORT_SYMBOL(simple_strtol);
93
94/**
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
99 */
22d27051 100unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
1da177e4 101{
aa46a63e
HH
102 unsigned long long result = 0;
103
104 if (!base)
105 base = simple_guess_base(cp);
106
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
109
110 while (isxdigit(*cp)) {
111 unsigned int value;
112
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
114 if (value >= base)
115 break;
116 result = result * base + value;
1da177e4
LT
117 cp++;
118 }
aa46a63e 119
1da177e4
LT
120 if (endp)
121 *endp = (char *)cp;
122 return result;
123}
1da177e4
LT
124EXPORT_SYMBOL(simple_strtoull);
125
126/**
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
131 */
22d27051 132long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1da177e4
LT
133{
134 if(*cp=='-')
22d27051
HH
135 return -simple_strtoull(cp + 1, endp, base);
136 return simple_strtoull(cp, endp, base);
1da177e4
LT
137}
138
06b2a76d
YY
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 */
9d85db22
HH
161int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
162{
163 char *tail;
164 unsigned long val;
165 size_t len;
166
167 *res = 0;
168 len = strlen(cp);
169 if (len == 0)
170 return -EINVAL;
171
172 val = simple_strtoul(cp, &tail, base);
173 if ((*tail == '\0') ||
174 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
175 *res = val;
176 return 0;
177 }
178
179 return -EINVAL;
180}
181EXPORT_SYMBOL(strict_strtoul);
06b2a76d
YY
182
183/**
184 * strict_strtol - convert a string to a long strictly
185 * @cp: The string to be converted
186 * @base: The number base to use
187 * @res: The converted result value
188 *
189 * strict_strtol is similiar to strict_strtoul, but it allows the first
190 * character of a string is '-'.
191 *
192 * It returns 0 if conversion is successful and *res is set to the converted
193 * value, otherwise it returns -EINVAL and *res is set to 0.
194 */
9d85db22
HH
195int strict_strtol(const char *cp, unsigned int base, long *res)
196{
197 int ret;
198 if (*cp == '-') {
199 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
200 if (!ret)
201 *res = -(*res);
202 } else {
203 ret = strict_strtoul(cp, base, (unsigned long *)res);
204 }
205
206 return ret;
207}
208EXPORT_SYMBOL(strict_strtol);
06b2a76d
YY
209
210/**
211 * strict_strtoull - convert a string to an unsigned long long strictly
212 * @cp: The string to be converted
213 * @base: The number base to use
214 * @res: The converted result value
215 *
216 * strict_strtoull converts a string to an unsigned long long only if the
217 * string is really an unsigned long long string, any string containing
218 * any invalid char at the tail will be rejected and -EINVAL is returned,
219 * only a newline char at the tail is acceptible because people generally
220 * change a module parameter in the following way:
221 *
222 * echo 1024 > /sys/module/e1000/parameters/copybreak
223 *
224 * echo will append a newline to the tail of the string.
225 *
226 * It returns 0 if conversion is successful and *res is set to the converted
227 * value, otherwise it returns -EINVAL and *res is set to 0.
228 *
229 * simple_strtoull just ignores the successive invalid characters and
230 * return the converted value of prefix part of the string.
231 */
9d85db22
HH
232int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
233{
234 char *tail;
235 unsigned long long val;
236 size_t len;
237
238 *res = 0;
239 len = strlen(cp);
240 if (len == 0)
241 return -EINVAL;
242
243 val = simple_strtoull(cp, &tail, base);
244 if ((*tail == '\0') ||
245 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
246 *res = val;
247 return 0;
248 }
249
250 return -EINVAL;
251}
252EXPORT_SYMBOL(strict_strtoull);
06b2a76d
YY
253
254/**
255 * strict_strtoll - convert a string to a long long strictly
256 * @cp: The string to be converted
257 * @base: The number base to use
258 * @res: The converted result value
259 *
260 * strict_strtoll is similiar to strict_strtoull, but it allows the first
261 * character of a string is '-'.
262 *
263 * It returns 0 if conversion is successful and *res is set to the converted
264 * value, otherwise it returns -EINVAL and *res is set to 0.
265 */
9d85db22
HH
266int strict_strtoll(const char *cp, unsigned int base, long long *res)
267{
268 int ret;
269 if (*cp == '-') {
270 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
271 if (!ret)
272 *res = -(*res);
273 } else {
274 ret = strict_strtoull(cp, base, (unsigned long long *)res);
275 }
06b2a76d 276
9d85db22
HH
277 return ret;
278}
06b2a76d 279EXPORT_SYMBOL(strict_strtoll);
06b2a76d 280
1da177e4
LT
281static int skip_atoi(const char **s)
282{
283 int i=0;
284
285 while (isdigit(**s))
286 i = i*10 + *((*s)++) - '0';
287 return i;
288}
289
4277eedd
DV
290/* Decimal conversion is by far the most typical, and is used
291 * for /proc and /sys data. This directly impacts e.g. top performance
292 * with many processes running. We optimize it for speed
293 * using code from
294 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
295 * (with permission from the author, Douglas W. Jones). */
296
297/* Formats correctly any integer in [0,99999].
298 * Outputs from one to five digits depending on input.
299 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
300static char* put_dec_trunc(char *buf, unsigned q)
301{
302 unsigned d3, d2, d1, d0;
303 d1 = (q>>4) & 0xf;
304 d2 = (q>>8) & 0xf;
305 d3 = (q>>12);
306
307 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
308 q = (d0 * 0xcd) >> 11;
309 d0 = d0 - 10*q;
310 *buf++ = d0 + '0'; /* least significant digit */
311 d1 = q + 9*d3 + 5*d2 + d1;
312 if (d1 != 0) {
313 q = (d1 * 0xcd) >> 11;
314 d1 = d1 - 10*q;
315 *buf++ = d1 + '0'; /* next digit */
316
317 d2 = q + 2*d2;
318 if ((d2 != 0) || (d3 != 0)) {
319 q = (d2 * 0xd) >> 7;
320 d2 = d2 - 10*q;
321 *buf++ = d2 + '0'; /* next digit */
322
323 d3 = q + 4*d3;
324 if (d3 != 0) {
325 q = (d3 * 0xcd) >> 11;
326 d3 = d3 - 10*q;
327 *buf++ = d3 + '0'; /* next digit */
328 if (q != 0)
329 *buf++ = q + '0'; /* most sign. digit */
330 }
331 }
332 }
333 return buf;
334}
335/* Same with if's removed. Always emits five digits */
336static char* put_dec_full(char *buf, unsigned q)
337{
338 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
339 /* but anyway, gcc produces better code with full-sized ints */
340 unsigned d3, d2, d1, d0;
341 d1 = (q>>4) & 0xf;
342 d2 = (q>>8) & 0xf;
343 d3 = (q>>12);
344
345 /* Possible ways to approx. divide by 10 */
346 /* gcc -O2 replaces multiply with shifts and adds */
347 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
348 // (x * 0x67) >> 10: 1100111
349 // (x * 0x34) >> 9: 110100 - same
350 // (x * 0x1a) >> 8: 11010 - same
351 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
352
353 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
354 q = (d0 * 0xcd) >> 11;
355 d0 = d0 - 10*q;
356 *buf++ = d0 + '0';
357 d1 = q + 9*d3 + 5*d2 + d1;
358 q = (d1 * 0xcd) >> 11;
359 d1 = d1 - 10*q;
360 *buf++ = d1 + '0';
361
362 d2 = q + 2*d2;
363 q = (d2 * 0xd) >> 7;
364 d2 = d2 - 10*q;
365 *buf++ = d2 + '0';
366
367 d3 = q + 4*d3;
368 q = (d3 * 0xcd) >> 11; /* - shorter code */
369 /* q = (d3 * 0x67) >> 10; - would also work */
370 d3 = d3 - 10*q;
371 *buf++ = d3 + '0';
372 *buf++ = q + '0';
373 return buf;
374}
375/* No inlining helps gcc to use registers better */
376static noinline char* put_dec(char *buf, unsigned long long num)
377{
378 while (1) {
379 unsigned rem;
380 if (num < 100000)
381 return put_dec_trunc(buf, num);
382 rem = do_div(num, 100000);
383 buf = put_dec_full(buf, rem);
384 }
385}
386
1da177e4
LT
387#define ZEROPAD 1 /* pad with zero */
388#define SIGN 2 /* unsigned/signed long */
389#define PLUS 4 /* show plus */
390#define SPACE 8 /* space if plus */
391#define LEFT 16 /* left justified */
9b706aee
DV
392#define SMALL 32 /* Must be 32 == 0x20 */
393#define SPECIAL 64 /* 0x */
1da177e4 394
b39a7340 395static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
1da177e4 396{
9b706aee
DV
397 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
398 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
399
400 char tmp[66];
401 char sign;
402 char locase;
b39a7340 403 int need_pfx = ((type & SPECIAL) && base != 10);
1da177e4
LT
404 int i;
405
9b706aee
DV
406 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
407 * produces same digits or (maybe lowercased) letters */
408 locase = (type & SMALL);
1da177e4
LT
409 if (type & LEFT)
410 type &= ~ZEROPAD;
1da177e4
LT
411 sign = 0;
412 if (type & SIGN) {
413 if ((signed long long) num < 0) {
414 sign = '-';
415 num = - (signed long long) num;
416 size--;
417 } else if (type & PLUS) {
418 sign = '+';
419 size--;
420 } else if (type & SPACE) {
421 sign = ' ';
422 size--;
423 }
424 }
b39a7340
DV
425 if (need_pfx) {
426 size--;
1da177e4 427 if (base == 16)
1da177e4
LT
428 size--;
429 }
b39a7340
DV
430
431 /* generate full string in tmp[], in reverse order */
1da177e4
LT
432 i = 0;
433 if (num == 0)
b39a7340 434 tmp[i++] = '0';
4277eedd
DV
435 /* Generic code, for any base:
436 else do {
9b706aee 437 tmp[i++] = (digits[do_div(num,base)] | locase);
4277eedd
DV
438 } while (num != 0);
439 */
b39a7340
DV
440 else if (base != 10) { /* 8 or 16 */
441 int mask = base - 1;
442 int shift = 3;
443 if (base == 16) shift = 4;
444 do {
9b706aee 445 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
b39a7340
DV
446 num >>= shift;
447 } while (num);
4277eedd
DV
448 } else { /* base 10 */
449 i = put_dec(tmp, num) - tmp;
450 }
b39a7340
DV
451
452 /* printing 100 using %2d gives "100", not "00" */
1da177e4
LT
453 if (i > precision)
454 precision = i;
b39a7340 455 /* leading space padding */
1da177e4 456 size -= precision;
b39a7340
DV
457 if (!(type & (ZEROPAD+LEFT))) {
458 while(--size >= 0) {
f796937a 459 if (buf < end)
1da177e4
LT
460 *buf = ' ';
461 ++buf;
462 }
463 }
b39a7340 464 /* sign */
1da177e4 465 if (sign) {
f796937a 466 if (buf < end)
1da177e4
LT
467 *buf = sign;
468 ++buf;
469 }
b39a7340
DV
470 /* "0x" / "0" prefix */
471 if (need_pfx) {
472 if (buf < end)
473 *buf = '0';
474 ++buf;
475 if (base == 16) {
f796937a 476 if (buf < end)
9b706aee 477 *buf = ('X' | locase);
1da177e4
LT
478 ++buf;
479 }
480 }
b39a7340 481 /* zero or space padding */
1da177e4 482 if (!(type & LEFT)) {
b39a7340
DV
483 char c = (type & ZEROPAD) ? '0' : ' ';
484 while (--size >= 0) {
f796937a 485 if (buf < end)
1da177e4
LT
486 *buf = c;
487 ++buf;
488 }
489 }
b39a7340
DV
490 /* hmm even more zero padding? */
491 while (i <= --precision) {
f796937a 492 if (buf < end)
1da177e4
LT
493 *buf = '0';
494 ++buf;
495 }
b39a7340
DV
496 /* actual digits of result */
497 while (--i >= 0) {
f796937a 498 if (buf < end)
1da177e4
LT
499 *buf = tmp[i];
500 ++buf;
501 }
b39a7340
DV
502 /* trailing space padding */
503 while (--size >= 0) {
f796937a 504 if (buf < end)
1da177e4
LT
505 *buf = ' ';
506 ++buf;
507 }
508 return buf;
509}
510
0f9bfa56
LT
511static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
512{
513 int len, i;
514
515 if ((unsigned long)s < PAGE_SIZE)
516 s = "<NULL>";
517
518 len = strnlen(s, precision);
519
520 if (!(flags & LEFT)) {
521 while (len < field_width--) {
522 if (buf < end)
523 *buf = ' ';
524 ++buf;
525 }
526 }
527 for (i = 0; i < len; ++i) {
528 if (buf < end)
529 *buf = *s;
530 ++buf; ++s;
531 }
532 while (len < field_width--) {
533 if (buf < end)
534 *buf = ' ';
535 ++buf;
536 }
537 return buf;
538}
539
0fe1ef24
LT
540static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
541{
542 unsigned long value = (unsigned long) ptr;
543#ifdef CONFIG_KALLSYMS
544 char sym[KSYM_SYMBOL_LEN];
545 sprint_symbol(sym, value);
546 return string(buf, end, sym, field_width, precision, flags);
547#else
548 field_width = 2*sizeof(void *);
549 flags |= SPECIAL | SMALL | ZEROPAD;
550 return number(buf, end, value, 16, field_width, precision, flags);
551#endif
552}
553
332d2e78
LT
554static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
555{
556#ifndef IO_RSRC_PRINTK_SIZE
557#define IO_RSRC_PRINTK_SIZE 4
558#endif
559
560#ifndef MEM_RSRC_PRINTK_SIZE
561#define MEM_RSRC_PRINTK_SIZE 8
562#endif
563
564 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
565 char sym[4*sizeof(resource_size_t) + 8];
566 char *p = sym, *pend = sym + sizeof(sym);
567 int size = -1;
568
569 if (res->flags & IORESOURCE_IO)
570 size = IO_RSRC_PRINTK_SIZE;
571 else if (res->flags & IORESOURCE_MEM)
572 size = MEM_RSRC_PRINTK_SIZE;
573
574 *p++ = '[';
575 p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
576 *p++ = '-';
577 p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
578 *p++ = ']';
579 *p = 0;
580
581 return string(buf, end, sym, field_width, precision, flags);
582}
583
dd45c9cf
HH
584static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
585 int precision, int flags)
586{
587 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
588 char *p = mac_addr;
589 int i;
590
591 for (i = 0; i < 6; i++) {
592 p = pack_hex_byte(p, addr[i]);
593 if (!(flags & SPECIAL) && i != 5)
594 *p++ = ':';
595 }
596 *p = '\0';
597
598 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
599}
600
689afa7d
HH
601static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
602 int precision, int flags)
603{
604 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
605 char *p = ip6_addr;
606 int i;
607
608 for (i = 0; i < 8; i++) {
609 p = pack_hex_byte(p, addr[2 * i]);
610 p = pack_hex_byte(p, addr[2 * i + 1]);
611 if (!(flags & SPECIAL) && i != 7)
612 *p++ = ':';
613 }
614 *p = '\0';
615
616 return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
617}
618
4aa99606
HH
619static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
620 int precision, int flags)
621{
622 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
b9ac9985 623 char temp[3]; /* hold each IP quad in reverse order */
4aa99606 624 char *p = ip4_addr;
b9ac9985 625 int i, digits;
4aa99606
HH
626
627 for (i = 0; i < 4; i++) {
b9ac9985
HH
628 digits = put_dec_trunc(temp, addr[i]) - temp;
629 /* reverse the digits in the quad */
630 while (digits--)
631 *p++ = temp[digits];
4aa99606
HH
632 if (i != 3)
633 *p++ = '.';
634 }
635 *p = '\0';
636
637 return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
638}
639
4d8a743c
LT
640/*
641 * Show a '%p' thing. A kernel extension is that the '%p' is followed
642 * by an extra set of alphanumeric characters that are extended format
643 * specifiers.
644 *
332d2e78
LT
645 * Right now we handle:
646 *
647 * - 'F' For symbolic function descriptor pointers
648 * - 'S' For symbolic direct pointers
649 * - 'R' For a struct resource pointer, it prints the range of
650 * addresses (not the name nor the flags)
dd45c9cf
HH
651 * - 'M' For a 6-byte MAC address, it prints the address in the
652 * usual colon-separated hex notation
4aa99606
HH
653 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
654 * decimal for v4 and colon separated network-order 16 bit hex for v6)
655 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
6b9a1066 656 * currently the same
0fe1ef24 657 *
332d2e78
LT
658 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
659 * function pointers are really function descriptors, which contain a
660 * pointer to the real address.
4d8a743c
LT
661 */
662static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
78a8bf69 663{
0fe1ef24
LT
664 switch (*fmt) {
665 case 'F':
666 ptr = dereference_function_descriptor(ptr);
667 /* Fallthrough */
668 case 'S':
669 return symbol_string(buf, end, ptr, field_width, precision, flags);
332d2e78
LT
670 case 'R':
671 return resource_string(buf, end, ptr, field_width, precision, flags);
411c41ee
HH
672 case 'm':
673 flags |= SPECIAL;
674 /* Fallthrough */
dd45c9cf
HH
675 case 'M':
676 return mac_address_string(buf, end, ptr, field_width, precision, flags);
4aa99606
HH
677 case 'i':
678 flags |= SPECIAL;
679 /* Fallthrough */
680 case 'I':
681 if (fmt[1] == '6')
682 return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
683 if (fmt[1] == '4')
684 return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
685 flags &= ~SPECIAL;
686 break;
0fe1ef24 687 }
78a8bf69
LT
688 flags |= SMALL;
689 if (field_width == -1) {
690 field_width = 2*sizeof(void *);
691 flags |= ZEROPAD;
692 }
693 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
694}
695
1da177e4
LT
696/**
697 * vsnprintf - Format a string and place it in a buffer
698 * @buf: The buffer to place the result into
699 * @size: The size of the buffer, including the trailing null space
700 * @fmt: The format string to use
701 * @args: Arguments for the format string
702 *
20036fdc
AK
703 * This function follows C99 vsnprintf, but has some extensions:
704 * %pS output the name of a text symbol
705 * %pF output the name of a function pointer
332d2e78 706 * %pR output the address range in a struct resource
20036fdc 707 *
1da177e4
LT
708 * The return value is the number of characters which would
709 * be generated for the given input, excluding the trailing
710 * '\0', as per ISO C99. If you want to have the exact
711 * number of characters written into @buf as return value
72fd4a35 712 * (not including the trailing '\0'), use vscnprintf(). If the
1da177e4
LT
713 * return is greater than or equal to @size, the resulting
714 * string is truncated.
715 *
716 * Call this function if you are already dealing with a va_list.
72fd4a35 717 * You probably want snprintf() instead.
1da177e4
LT
718 */
719int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
720{
1da177e4 721 unsigned long long num;
0f9bfa56 722 int base;
1da177e4 723 char *str, *end, c;
1da177e4
LT
724
725 int flags; /* flags to number() */
726
727 int field_width; /* width of output field */
728 int precision; /* min. # of digits for integers; max
729 number of chars for from string */
730 int qualifier; /* 'h', 'l', or 'L' for integer fields */
731 /* 'z' support added 23/7/1999 S.H. */
732 /* 'z' changed to 'Z' --davidm 1/25/99 */
80322306 733 /* 't' added for ptrdiff_t */
1da177e4 734
f796937a
JF
735 /* Reject out-of-range values early. Large positive sizes are
736 used for unknown buffer sizes. */
1da177e4
LT
737 if (unlikely((int) size < 0)) {
738 /* There can be only one.. */
b39a7340 739 static char warn = 1;
1da177e4
LT
740 WARN_ON(warn);
741 warn = 0;
742 return 0;
743 }
744
745 str = buf;
f796937a 746 end = buf + size;
1da177e4 747
f796937a
JF
748 /* Make sure end is always >= buf */
749 if (end < buf) {
750 end = ((void *)-1);
751 size = end - buf;
1da177e4
LT
752 }
753
754 for (; *fmt ; ++fmt) {
755 if (*fmt != '%') {
f796937a 756 if (str < end)
1da177e4
LT
757 *str = *fmt;
758 ++str;
759 continue;
760 }
761
762 /* process flags */
763 flags = 0;
764 repeat:
765 ++fmt; /* this also skips first '%' */
766 switch (*fmt) {
767 case '-': flags |= LEFT; goto repeat;
768 case '+': flags |= PLUS; goto repeat;
769 case ' ': flags |= SPACE; goto repeat;
770 case '#': flags |= SPECIAL; goto repeat;
771 case '0': flags |= ZEROPAD; goto repeat;
772 }
773
774 /* get field width */
775 field_width = -1;
776 if (isdigit(*fmt))
777 field_width = skip_atoi(&fmt);
778 else if (*fmt == '*') {
779 ++fmt;
780 /* it's the next argument */
781 field_width = va_arg(args, int);
782 if (field_width < 0) {
783 field_width = -field_width;
784 flags |= LEFT;
785 }
786 }
787
788 /* get the precision */
789 precision = -1;
790 if (*fmt == '.') {
791 ++fmt;
792 if (isdigit(*fmt))
793 precision = skip_atoi(&fmt);
794 else if (*fmt == '*') {
795 ++fmt;
796 /* it's the next argument */
797 precision = va_arg(args, int);
798 }
799 if (precision < 0)
800 precision = 0;
801 }
802
803 /* get the conversion qualifier */
804 qualifier = -1;
805 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
80322306 806 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
1da177e4
LT
807 qualifier = *fmt;
808 ++fmt;
809 if (qualifier == 'l' && *fmt == 'l') {
810 qualifier = 'L';
811 ++fmt;
812 }
813 }
814
815 /* default base */
816 base = 10;
817
818 switch (*fmt) {
819 case 'c':
820 if (!(flags & LEFT)) {
821 while (--field_width > 0) {
f796937a 822 if (str < end)
1da177e4
LT
823 *str = ' ';
824 ++str;
825 }
826 }
827 c = (unsigned char) va_arg(args, int);
f796937a 828 if (str < end)
1da177e4
LT
829 *str = c;
830 ++str;
831 while (--field_width > 0) {
f796937a 832 if (str < end)
1da177e4
LT
833 *str = ' ';
834 ++str;
835 }
836 continue;
837
838 case 's':
0f9bfa56 839 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
1da177e4
LT
840 continue;
841
842 case 'p':
4d8a743c
LT
843 str = pointer(fmt+1, str, end,
844 va_arg(args, void *),
845 field_width, precision, flags);
846 /* Skip all alphanumeric pointer suffixes */
847 while (isalnum(fmt[1]))
848 fmt++;
1da177e4
LT
849 continue;
850
1da177e4
LT
851 case 'n':
852 /* FIXME:
853 * What does C99 say about the overflow case here? */
854 if (qualifier == 'l') {
855 long * ip = va_arg(args, long *);
856 *ip = (str - buf);
857 } else if (qualifier == 'Z' || qualifier == 'z') {
858 size_t * ip = va_arg(args, size_t *);
859 *ip = (str - buf);
860 } else {
861 int * ip = va_arg(args, int *);
862 *ip = (str - buf);
863 }
864 continue;
865
866 case '%':
f796937a 867 if (str < end)
1da177e4
LT
868 *str = '%';
869 ++str;
870 continue;
871
872 /* integer number formats - set up the flags and "break" */
873 case 'o':
874 base = 8;
875 break;
876
1da177e4 877 case 'x':
9b706aee
DV
878 flags |= SMALL;
879 case 'X':
1da177e4
LT
880 base = 16;
881 break;
882
883 case 'd':
884 case 'i':
885 flags |= SIGN;
886 case 'u':
887 break;
888
889 default:
f796937a 890 if (str < end)
1da177e4
LT
891 *str = '%';
892 ++str;
893 if (*fmt) {
f796937a 894 if (str < end)
1da177e4
LT
895 *str = *fmt;
896 ++str;
897 } else {
898 --fmt;
899 }
900 continue;
901 }
902 if (qualifier == 'L')
903 num = va_arg(args, long long);
904 else if (qualifier == 'l') {
905 num = va_arg(args, unsigned long);
906 if (flags & SIGN)
907 num = (signed long) num;
908 } else if (qualifier == 'Z' || qualifier == 'z') {
909 num = va_arg(args, size_t);
80322306
AV
910 } else if (qualifier == 't') {
911 num = va_arg(args, ptrdiff_t);
1da177e4
LT
912 } else if (qualifier == 'h') {
913 num = (unsigned short) va_arg(args, int);
914 if (flags & SIGN)
915 num = (signed short) num;
916 } else {
917 num = va_arg(args, unsigned int);
918 if (flags & SIGN)
919 num = (signed int) num;
920 }
921 str = number(str, end, num, base,
922 field_width, precision, flags);
923 }
f796937a
JF
924 if (size > 0) {
925 if (str < end)
926 *str = '\0';
927 else
0a6047ee 928 end[-1] = '\0';
f796937a
JF
929 }
930 /* the trailing null byte doesn't count towards the total */
1da177e4
LT
931 return str-buf;
932}
1da177e4
LT
933EXPORT_SYMBOL(vsnprintf);
934
935/**
936 * vscnprintf - Format a string and place it in a buffer
937 * @buf: The buffer to place the result into
938 * @size: The size of the buffer, including the trailing null space
939 * @fmt: The format string to use
940 * @args: Arguments for the format string
941 *
942 * The return value is the number of characters which have been written into
943 * the @buf not including the trailing '\0'. If @size is <= 0 the function
944 * returns 0.
945 *
946 * Call this function if you are already dealing with a va_list.
72fd4a35 947 * You probably want scnprintf() instead.
20036fdc
AK
948 *
949 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
950 */
951int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
952{
953 int i;
954
955 i=vsnprintf(buf,size,fmt,args);
956 return (i >= size) ? (size - 1) : i;
957}
1da177e4
LT
958EXPORT_SYMBOL(vscnprintf);
959
960/**
961 * snprintf - Format a string and place it in a buffer
962 * @buf: The buffer to place the result into
963 * @size: The size of the buffer, including the trailing null space
964 * @fmt: The format string to use
965 * @...: Arguments for the format string
966 *
967 * The return value is the number of characters which would be
968 * generated for the given input, excluding the trailing null,
969 * as per ISO C99. If the return is greater than or equal to
970 * @size, the resulting string is truncated.
20036fdc
AK
971 *
972 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
973 */
974int snprintf(char * buf, size_t size, const char *fmt, ...)
975{
976 va_list args;
977 int i;
978
979 va_start(args, fmt);
980 i=vsnprintf(buf,size,fmt,args);
981 va_end(args);
982 return i;
983}
1da177e4
LT
984EXPORT_SYMBOL(snprintf);
985
986/**
987 * scnprintf - Format a string and place it in a buffer
988 * @buf: The buffer to place the result into
989 * @size: The size of the buffer, including the trailing null space
990 * @fmt: The format string to use
991 * @...: Arguments for the format string
992 *
993 * The return value is the number of characters written into @buf not including
ea6f3281 994 * the trailing '\0'. If @size is <= 0 the function returns 0.
1da177e4
LT
995 */
996
997int scnprintf(char * buf, size_t size, const char *fmt, ...)
998{
999 va_list args;
1000 int i;
1001
1002 va_start(args, fmt);
1003 i = vsnprintf(buf, size, fmt, args);
1004 va_end(args);
1005 return (i >= size) ? (size - 1) : i;
1006}
1007EXPORT_SYMBOL(scnprintf);
1008
1009/**
1010 * vsprintf - Format a string and place it in a buffer
1011 * @buf: The buffer to place the result into
1012 * @fmt: The format string to use
1013 * @args: Arguments for the format string
1014 *
1015 * The function returns the number of characters written
72fd4a35 1016 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1da177e4
LT
1017 * buffer overflows.
1018 *
1019 * Call this function if you are already dealing with a va_list.
72fd4a35 1020 * You probably want sprintf() instead.
20036fdc
AK
1021 *
1022 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1023 */
1024int vsprintf(char *buf, const char *fmt, va_list args)
1025{
1026 return vsnprintf(buf, INT_MAX, fmt, args);
1027}
1da177e4
LT
1028EXPORT_SYMBOL(vsprintf);
1029
1030/**
1031 * sprintf - Format a string and place it in a buffer
1032 * @buf: The buffer to place the result into
1033 * @fmt: The format string to use
1034 * @...: Arguments for the format string
1035 *
1036 * The function returns the number of characters written
72fd4a35 1037 * into @buf. Use snprintf() or scnprintf() in order to avoid
1da177e4 1038 * buffer overflows.
20036fdc
AK
1039 *
1040 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1041 */
1042int sprintf(char * buf, const char *fmt, ...)
1043{
1044 va_list args;
1045 int i;
1046
1047 va_start(args, fmt);
1048 i=vsnprintf(buf, INT_MAX, fmt, args);
1049 va_end(args);
1050 return i;
1051}
1da177e4
LT
1052EXPORT_SYMBOL(sprintf);
1053
1054/**
1055 * vsscanf - Unformat a buffer into a list of arguments
1056 * @buf: input buffer
1057 * @fmt: format of buffer
1058 * @args: arguments
1059 */
1060int vsscanf(const char * buf, const char * fmt, va_list args)
1061{
1062 const char *str = buf;
1063 char *next;
1064 char digit;
1065 int num = 0;
1066 int qualifier;
1067 int base;
1068 int field_width;
1069 int is_sign = 0;
1070
1071 while(*fmt && *str) {
1072 /* skip any white space in format */
1073 /* white space in format matchs any amount of
1074 * white space, including none, in the input.
1075 */
1076 if (isspace(*fmt)) {
1077 while (isspace(*fmt))
1078 ++fmt;
1079 while (isspace(*str))
1080 ++str;
1081 }
1082
1083 /* anything that is not a conversion must match exactly */
1084 if (*fmt != '%' && *fmt) {
1085 if (*fmt++ != *str++)
1086 break;
1087 continue;
1088 }
1089
1090 if (!*fmt)
1091 break;
1092 ++fmt;
1093
1094 /* skip this conversion.
1095 * advance both strings to next white space
1096 */
1097 if (*fmt == '*') {
1098 while (!isspace(*fmt) && *fmt)
1099 fmt++;
1100 while (!isspace(*str) && *str)
1101 str++;
1102 continue;
1103 }
1104
1105 /* get field width */
1106 field_width = -1;
1107 if (isdigit(*fmt))
1108 field_width = skip_atoi(&fmt);
1109
1110 /* get conversion qualifier */
1111 qualifier = -1;
1112 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1113 *fmt == 'Z' || *fmt == 'z') {
1114 qualifier = *fmt++;
1115 if (unlikely(qualifier == *fmt)) {
1116 if (qualifier == 'h') {
1117 qualifier = 'H';
1118 fmt++;
1119 } else if (qualifier == 'l') {
1120 qualifier = 'L';
1121 fmt++;
1122 }
1123 }
1124 }
1125 base = 10;
1126 is_sign = 0;
1127
1128 if (!*fmt || !*str)
1129 break;
1130
1131 switch(*fmt++) {
1132 case 'c':
1133 {
1134 char *s = (char *) va_arg(args,char*);
1135 if (field_width == -1)
1136 field_width = 1;
1137 do {
1138 *s++ = *str++;
1139 } while (--field_width > 0 && *str);
1140 num++;
1141 }
1142 continue;
1143 case 's':
1144 {
1145 char *s = (char *) va_arg(args, char *);
1146 if(field_width == -1)
1147 field_width = INT_MAX;
1148 /* first, skip leading white space in buffer */
1149 while (isspace(*str))
1150 str++;
1151
1152 /* now copy until next white space */
1153 while (*str && !isspace(*str) && field_width--) {
1154 *s++ = *str++;
1155 }
1156 *s = '\0';
1157 num++;
1158 }
1159 continue;
1160 case 'n':
1161 /* return number of characters read so far */
1162 {
1163 int *i = (int *)va_arg(args,int*);
1164 *i = str - buf;
1165 }
1166 continue;
1167 case 'o':
1168 base = 8;
1169 break;
1170 case 'x':
1171 case 'X':
1172 base = 16;
1173 break;
1174 case 'i':
1175 base = 0;
1176 case 'd':
1177 is_sign = 1;
1178 case 'u':
1179 break;
1180 case '%':
1181 /* looking for '%' in str */
1182 if (*str++ != '%')
1183 return num;
1184 continue;
1185 default:
1186 /* invalid format; stop here */
1187 return num;
1188 }
1189
1190 /* have some sort of integer conversion.
1191 * first, skip white space in buffer.
1192 */
1193 while (isspace(*str))
1194 str++;
1195
1196 digit = *str;
1197 if (is_sign && digit == '-')
1198 digit = *(str + 1);
1199
1200 if (!digit
1201 || (base == 16 && !isxdigit(digit))
1202 || (base == 10 && !isdigit(digit))
1203 || (base == 8 && (!isdigit(digit) || digit > '7'))
1204 || (base == 0 && !isdigit(digit)))
1205 break;
1206
1207 switch(qualifier) {
1208 case 'H': /* that's 'hh' in format */
1209 if (is_sign) {
1210 signed char *s = (signed char *) va_arg(args,signed char *);
1211 *s = (signed char) simple_strtol(str,&next,base);
1212 } else {
1213 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1214 *s = (unsigned char) simple_strtoul(str, &next, base);
1215 }
1216 break;
1217 case 'h':
1218 if (is_sign) {
1219 short *s = (short *) va_arg(args,short *);
1220 *s = (short) simple_strtol(str,&next,base);
1221 } else {
1222 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1223 *s = (unsigned short) simple_strtoul(str, &next, base);
1224 }
1225 break;
1226 case 'l':
1227 if (is_sign) {
1228 long *l = (long *) va_arg(args,long *);
1229 *l = simple_strtol(str,&next,base);
1230 } else {
1231 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1232 *l = simple_strtoul(str,&next,base);
1233 }
1234 break;
1235 case 'L':
1236 if (is_sign) {
1237 long long *l = (long long*) va_arg(args,long long *);
1238 *l = simple_strtoll(str,&next,base);
1239 } else {
1240 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1241 *l = simple_strtoull(str,&next,base);
1242 }
1243 break;
1244 case 'Z':
1245 case 'z':
1246 {
1247 size_t *s = (size_t*) va_arg(args,size_t*);
1248 *s = (size_t) simple_strtoul(str,&next,base);
1249 }
1250 break;
1251 default:
1252 if (is_sign) {
1253 int *i = (int *) va_arg(args, int*);
1254 *i = (int) simple_strtol(str,&next,base);
1255 } else {
1256 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1257 *i = (unsigned int) simple_strtoul(str,&next,base);
1258 }
1259 break;
1260 }
1261 num++;
1262
1263 if (!next)
1264 break;
1265 str = next;
1266 }
c6b40d16
JB
1267
1268 /*
1269 * Now we've come all the way through so either the input string or the
1270 * format ended. In the former case, there can be a %n at the current
1271 * position in the format that needs to be filled.
1272 */
1273 if (*fmt == '%' && *(fmt + 1) == 'n') {
1274 int *p = (int *)va_arg(args, int *);
1275 *p = str - buf;
1276 }
1277
1da177e4
LT
1278 return num;
1279}
1da177e4
LT
1280EXPORT_SYMBOL(vsscanf);
1281
1282/**
1283 * sscanf - Unformat a buffer into a list of arguments
1284 * @buf: input buffer
1285 * @fmt: formatting of buffer
1286 * @...: resulting arguments
1287 */
1288int sscanf(const char * buf, const char * fmt, ...)
1289{
1290 va_list args;
1291 int i;
1292
1293 va_start(args,fmt);
1294 i = vsscanf(buf,fmt,args);
1295 va_end(args);
1296 return i;
1297}
1da177e4 1298EXPORT_SYMBOL(sscanf);