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