vsprintf.c (6c356634111c5a7a48264d7c9ec28559e4be11a2) vsprintf.c (7b9186f5eb0b1705abf7b2fbf33076a6b83f9d89)
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
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/*
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>

--- 45 unchanged lines hidden (view full) ---

66 unsigned int value;
67
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69 if (value >= base)
70 break;
71 result = result * base + value;
72 cp++;
73 }
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>

--- 45 unchanged lines hidden (view full) ---

66 unsigned int value;
67
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69 if (value >= base)
70 break;
71 result = result * base + value;
72 cp++;
73 }
74
75 if (endp)
76 *endp = (char *)cp;
74 if (endp)
75 *endp = (char *)cp;
76
77 return result;
78}
79EXPORT_SYMBOL(simple_strtoul);
80
81/**
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
86 */
87long simple_strtol(const char *cp, char **endp, unsigned int base)
88{
77 return result;
78}
79EXPORT_SYMBOL(simple_strtoul);
80
81/**
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
86 */
87long simple_strtol(const char *cp, char **endp, unsigned int base)
88{
89 if(*cp == '-')
89 if (*cp == '-')
90 return -simple_strtoul(cp + 1, endp, base);
90 return -simple_strtoul(cp + 1, endp, base);
91
91 return simple_strtoul(cp, endp, base);
92}
93EXPORT_SYMBOL(simple_strtol);
94
95/**
96 * simple_strtoull - convert a string to an unsigned long long
97 * @cp: The start of the string
98 * @endp: A pointer to the end of the parsed string will be placed here

--- 13 unchanged lines hidden (view full) ---

112 unsigned int value;
113
114 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
115 if (value >= base)
116 break;
117 result = result * base + value;
118 cp++;
119 }
92 return simple_strtoul(cp, endp, base);
93}
94EXPORT_SYMBOL(simple_strtol);
95
96/**
97 * simple_strtoull - convert a string to an unsigned long long
98 * @cp: The start of the string
99 * @endp: A pointer to the end of the parsed string will be placed here

--- 13 unchanged lines hidden (view full) ---

113 unsigned int value;
114
115 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
116 if (value >= base)
117 break;
118 result = result * base + value;
119 cp++;
120 }
120
121 if (endp)
122 *endp = (char *)cp;
121 if (endp)
122 *endp = (char *)cp;
123
123 return result;
124}
125EXPORT_SYMBOL(simple_strtoull);
126
127/**
128 * simple_strtoll - convert a string to a signed long long
129 * @cp: The start of the string
130 * @endp: A pointer to the end of the parsed string will be placed here
131 * @base: The number base to use
132 */
133long long simple_strtoll(const char *cp, char **endp, unsigned int base)
134{
124 return result;
125}
126EXPORT_SYMBOL(simple_strtoull);
127
128/**
129 * simple_strtoll - convert a string to a signed long long
130 * @cp: The start of the string
131 * @endp: A pointer to the end of the parsed string will be placed here
132 * @base: The number base to use
133 */
134long long simple_strtoll(const char *cp, char **endp, unsigned int base)
135{
135 if(*cp=='-')
136 if (*cp == '-')
136 return -simple_strtoull(cp + 1, endp, base);
137 return -simple_strtoull(cp + 1, endp, base);
138
137 return simple_strtoull(cp, endp, base);
138}
139
140/**
141 * strict_strtoul - convert a string to an unsigned long strictly
142 * @cp: The string to be converted
143 * @base: The number base to use
144 * @res: The converted result value

--- 23 unchanged lines hidden (view full) ---

168 *res = 0;
169 len = strlen(cp);
170 if (len == 0)
171 return -EINVAL;
172
173 val = simple_strtoul(cp, &tail, base);
174 if (tail == cp)
175 return -EINVAL;
139 return simple_strtoull(cp, endp, base);
140}
141
142/**
143 * strict_strtoul - convert a string to an unsigned long strictly
144 * @cp: The string to be converted
145 * @base: The number base to use
146 * @res: The converted result value

--- 23 unchanged lines hidden (view full) ---

170 *res = 0;
171 len = strlen(cp);
172 if (len == 0)
173 return -EINVAL;
174
175 val = simple_strtoul(cp, &tail, base);
176 if (tail == cp)
177 return -EINVAL;
178
176 if ((*tail == '\0') ||
177 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
178 *res = val;
179 return 0;
180 }
181
182 return -EINVAL;
183}

--- 96 unchanged lines hidden (view full) ---

280 }
281
282 return ret;
283}
284EXPORT_SYMBOL(strict_strtoll);
285
286static int skip_atoi(const char **s)
287{
179 if ((*tail == '\0') ||
180 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
181 *res = val;
182 return 0;
183 }
184
185 return -EINVAL;
186}

--- 96 unchanged lines hidden (view full) ---

283 }
284
285 return ret;
286}
287EXPORT_SYMBOL(strict_strtoll);
288
289static int skip_atoi(const char **s)
290{
288 int i=0;
291 int i = 0;
289
290 while (isdigit(**s))
291 i = i*10 + *((*s)++) - '0';
292
293 while (isdigit(**s))
294 i = i*10 + *((*s)++) - '0';
295
292 return i;
293}
294
295/* Decimal conversion is by far the most typical, and is used
296 * for /proc and /sys data. This directly impacts e.g. top performance
297 * with many processes running. We optimize it for speed
298 * using code from
299 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
300 * (with permission from the author, Douglas W. Jones). */
301
302/* Formats correctly any integer in [0,99999].
303 * Outputs from one to five digits depending on input.
304 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
296 return i;
297}
298
299/* Decimal conversion is by far the most typical, and is used
300 * for /proc and /sys data. This directly impacts e.g. top performance
301 * with many processes running. We optimize it for speed
302 * using code from
303 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
304 * (with permission from the author, Douglas W. Jones). */
305
306/* Formats correctly any integer in [0,99999].
307 * Outputs from one to five digits depending on input.
308 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
305static char* put_dec_trunc(char *buf, unsigned q)
309static char *put_dec_trunc(char *buf, unsigned q)
306{
307 unsigned d3, d2, d1, d0;
308 d1 = (q>>4) & 0xf;
309 d2 = (q>>8) & 0xf;
310 d3 = (q>>12);
311
312 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
313 q = (d0 * 0xcd) >> 11;

--- 12 unchanged lines hidden (view full) ---

326 *buf++ = d2 + '0'; /* next digit */
327
328 d3 = q + 4*d3;
329 if (d3 != 0) {
330 q = (d3 * 0xcd) >> 11;
331 d3 = d3 - 10*q;
332 *buf++ = d3 + '0'; /* next digit */
333 if (q != 0)
310{
311 unsigned d3, d2, d1, d0;
312 d1 = (q>>4) & 0xf;
313 d2 = (q>>8) & 0xf;
314 d3 = (q>>12);
315
316 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
317 q = (d0 * 0xcd) >> 11;

--- 12 unchanged lines hidden (view full) ---

330 *buf++ = d2 + '0'; /* next digit */
331
332 d3 = q + 4*d3;
333 if (d3 != 0) {
334 q = (d3 * 0xcd) >> 11;
335 d3 = d3 - 10*q;
336 *buf++ = d3 + '0'; /* next digit */
337 if (q != 0)
334 *buf++ = q + '0'; /* most sign. digit */
338 *buf++ = q + '0'; /* most sign. digit */
335 }
336 }
337 }
339 }
340 }
341 }
342
338 return buf;
339}
340/* Same with if's removed. Always emits five digits */
343 return buf;
344}
345/* Same with if's removed. Always emits five digits */
341static char* put_dec_full(char *buf, unsigned q)
346static char *put_dec_full(char *buf, unsigned q)
342{
343 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
344 /* but anyway, gcc produces better code with full-sized ints */
345 unsigned d3, d2, d1, d0;
346 d1 = (q>>4) & 0xf;
347 d2 = (q>>8) & 0xf;
348 d3 = (q>>12);
349
347{
348 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
349 /* but anyway, gcc produces better code with full-sized ints */
350 unsigned d3, d2, d1, d0;
351 d1 = (q>>4) & 0xf;
352 d2 = (q>>8) & 0xf;
353 d3 = (q>>12);
354
350 /* Possible ways to approx. divide by 10 */
351 /* gcc -O2 replaces multiply with shifts and adds */
352 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
353 // (x * 0x67) >> 10: 1100111
354 // (x * 0x34) >> 9: 110100 - same
355 // (x * 0x1a) >> 8: 11010 - same
356 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
357
355 /*
356 * Possible ways to approx. divide by 10
357 * gcc -O2 replaces multiply with shifts and adds
358 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
359 * (x * 0x67) >> 10: 1100111
360 * (x * 0x34) >> 9: 110100 - same
361 * (x * 0x1a) >> 8: 11010 - same
362 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
363 */
358 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
359 q = (d0 * 0xcd) >> 11;
360 d0 = d0 - 10*q;
361 *buf++ = d0 + '0';
362 d1 = q + 9*d3 + 5*d2 + d1;
363 q = (d1 * 0xcd) >> 11;
364 d1 = d1 - 10*q;
365 *buf++ = d1 + '0';

--- 4 unchanged lines hidden (view full) ---

370 *buf++ = d2 + '0';
371
372 d3 = q + 4*d3;
373 q = (d3 * 0xcd) >> 11; /* - shorter code */
374 /* q = (d3 * 0x67) >> 10; - would also work */
375 d3 = d3 - 10*q;
376 *buf++ = d3 + '0';
377 *buf++ = q + '0';
364 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
365 q = (d0 * 0xcd) >> 11;
366 d0 = d0 - 10*q;
367 *buf++ = d0 + '0';
368 d1 = q + 9*d3 + 5*d2 + d1;
369 q = (d1 * 0xcd) >> 11;
370 d1 = d1 - 10*q;
371 *buf++ = d1 + '0';

--- 4 unchanged lines hidden (view full) ---

376 *buf++ = d2 + '0';
377
378 d3 = q + 4*d3;
379 q = (d3 * 0xcd) >> 11; /* - shorter code */
380 /* q = (d3 * 0x67) >> 10; - would also work */
381 d3 = d3 - 10*q;
382 *buf++ = d3 + '0';
383 *buf++ = q + '0';
384
378 return buf;
379}
380/* No inlining helps gcc to use registers better */
385 return buf;
386}
387/* No inlining helps gcc to use registers better */
381static noinline char* put_dec(char *buf, unsigned long long num)
388static noinline char *put_dec(char *buf, unsigned long long num)
382{
383 while (1) {
384 unsigned rem;
385 if (num < 100000)
386 return put_dec_trunc(buf, num);
387 rem = do_div(num, 100000);
388 buf = put_dec_full(buf, rem);
389 }

--- 53 unchanged lines hidden (view full) ---

443
444 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
445 * produces same digits or (maybe lowercased) letters */
446 locase = (spec.flags & SMALL);
447 if (spec.flags & LEFT)
448 spec.flags &= ~ZEROPAD;
449 sign = 0;
450 if (spec.flags & SIGN) {
389{
390 while (1) {
391 unsigned rem;
392 if (num < 100000)
393 return put_dec_trunc(buf, num);
394 rem = do_div(num, 100000);
395 buf = put_dec_full(buf, rem);
396 }

--- 53 unchanged lines hidden (view full) ---

450
451 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
452 * produces same digits or (maybe lowercased) letters */
453 locase = (spec.flags & SMALL);
454 if (spec.flags & LEFT)
455 spec.flags &= ~ZEROPAD;
456 sign = 0;
457 if (spec.flags & SIGN) {
451 if ((signed long long) num < 0) {
458 if ((signed long long)num < 0) {
452 sign = '-';
459 sign = '-';
453 num = - (signed long long) num;
460 num = -(signed long long)num;
454 spec.field_width--;
455 } else if (spec.flags & PLUS) {
456 sign = '+';
457 spec.field_width--;
458 } else if (spec.flags & SPACE) {
459 sign = ' ';
460 spec.field_width--;
461 }

--- 11 unchanged lines hidden (view full) ---

473 /* Generic code, for any base:
474 else do {
475 tmp[i++] = (digits[do_div(num,base)] | locase);
476 } while (num != 0);
477 */
478 else if (spec.base != 10) { /* 8 or 16 */
479 int mask = spec.base - 1;
480 int shift = 3;
461 spec.field_width--;
462 } else if (spec.flags & PLUS) {
463 sign = '+';
464 spec.field_width--;
465 } else if (spec.flags & SPACE) {
466 sign = ' ';
467 spec.field_width--;
468 }

--- 11 unchanged lines hidden (view full) ---

480 /* Generic code, for any base:
481 else do {
482 tmp[i++] = (digits[do_div(num,base)] | locase);
483 } while (num != 0);
484 */
485 else if (spec.base != 10) { /* 8 or 16 */
486 int mask = spec.base - 1;
487 int shift = 3;
481 if (spec.base == 16) shift = 4;
488
489 if (spec.base == 16)
490 shift = 4;
482 do {
483 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
484 num >>= shift;
485 } while (num);
486 } else { /* base 10 */
487 i = put_dec(tmp, num) - tmp;
488 }
489
490 /* printing 100 using %2d gives "100", not "00" */
491 if (i > spec.precision)
492 spec.precision = i;
493 /* leading space padding */
494 spec.field_width -= spec.precision;
495 if (!(spec.flags & (ZEROPAD+LEFT))) {
491 do {
492 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
493 num >>= shift;
494 } while (num);
495 } else { /* base 10 */
496 i = put_dec(tmp, num) - tmp;
497 }
498
499 /* printing 100 using %2d gives "100", not "00" */
500 if (i > spec.precision)
501 spec.precision = i;
502 /* leading space padding */
503 spec.field_width -= spec.precision;
504 if (!(spec.flags & (ZEROPAD+LEFT))) {
496 while(--spec.field_width >= 0) {
505 while (--spec.field_width >= 0) {
497 if (buf < end)
498 *buf = ' ';
499 ++buf;
500 }
501 }
502 /* sign */
503 if (sign) {
504 if (buf < end)

--- 33 unchanged lines hidden (view full) ---

538 ++buf;
539 }
540 /* trailing space padding */
541 while (--spec.field_width >= 0) {
542 if (buf < end)
543 *buf = ' ';
544 ++buf;
545 }
506 if (buf < end)
507 *buf = ' ';
508 ++buf;
509 }
510 }
511 /* sign */
512 if (sign) {
513 if (buf < end)

--- 33 unchanged lines hidden (view full) ---

547 ++buf;
548 }
549 /* trailing space padding */
550 while (--spec.field_width >= 0) {
551 if (buf < end)
552 *buf = ' ';
553 ++buf;
554 }
555
546 return buf;
547}
548
549static char *string(char *buf, char *end, const char *s, struct printf_spec spec)
550{
551 int len, i;
552
553 if ((unsigned long)s < PAGE_SIZE)

--- 13 unchanged lines hidden (view full) ---

567 *buf = *s;
568 ++buf; ++s;
569 }
570 while (len < spec.field_width--) {
571 if (buf < end)
572 *buf = ' ';
573 ++buf;
574 }
556 return buf;
557}
558
559static char *string(char *buf, char *end, const char *s, struct printf_spec spec)
560{
561 int len, i;
562
563 if ((unsigned long)s < PAGE_SIZE)

--- 13 unchanged lines hidden (view full) ---

577 *buf = *s;
578 ++buf; ++s;
579 }
580 while (len < spec.field_width--) {
581 if (buf < end)
582 *buf = ' ';
583 ++buf;
584 }
585
575 return buf;
576}
577
578static char *symbol_string(char *buf, char *end, void *ptr,
579 struct printf_spec spec, char ext)
580{
581 unsigned long value = (unsigned long) ptr;
582#ifdef CONFIG_KALLSYMS
583 char sym[KSYM_SYMBOL_LEN];
584 if (ext != 'f' && ext != 's')
585 sprint_symbol(sym, value);
586 else
587 kallsyms_lookup(value, NULL, NULL, NULL, sym);
586 return buf;
587}
588
589static char *symbol_string(char *buf, char *end, void *ptr,
590 struct printf_spec spec, char ext)
591{
592 unsigned long value = (unsigned long) ptr;
593#ifdef CONFIG_KALLSYMS
594 char sym[KSYM_SYMBOL_LEN];
595 if (ext != 'f' && ext != 's')
596 sprint_symbol(sym, value);
597 else
598 kallsyms_lookup(value, NULL, NULL, NULL, sym);
599
588 return string(buf, end, sym, spec);
589#else
600 return string(buf, end, sym, spec);
601#else
590 spec.field_width = 2*sizeof(void *);
602 spec.field_width = 2 * sizeof(void *);
591 spec.flags |= SPECIAL | SMALL | ZEROPAD;
592 spec.base = 16;
603 spec.flags |= SPECIAL | SMALL | ZEROPAD;
604 spec.base = 16;
605
593 return number(buf, end, value, spec);
594#endif
595}
596
597static char *resource_string(char *buf, char *end, struct resource *res,
598 struct printf_spec spec, const char *fmt)
599{
600#ifndef IO_RSRC_PRINTK_SIZE

--- 112 unchanged lines hidden (view full) ---

713 *p++ = '0';
714 }
715 /* reverse the digits in the quad */
716 while (digits--)
717 *p++ = temp[digits];
718 if (i < 3)
719 *p++ = '.';
720 }
606 return number(buf, end, value, spec);
607#endif
608}
609
610static char *resource_string(char *buf, char *end, struct resource *res,
611 struct printf_spec spec, const char *fmt)
612{
613#ifndef IO_RSRC_PRINTK_SIZE

--- 112 unchanged lines hidden (view full) ---

726 *p++ = '0';
727 }
728 /* reverse the digits in the quad */
729 while (digits--)
730 *p++ = temp[digits];
731 if (i < 3)
732 *p++ = '.';
733 }
721
722 *p = '\0';
734 *p = '\0';
735
723 return p;
724}
725
726static char *ip6_compressed_string(char *p, const char *addr)
727{
736 return p;
737}
738
739static char *ip6_compressed_string(char *p, const char *addr)
740{
728 int i;
729 int j;
730 int range;
741 int i, j, range;
731 unsigned char zerolength[8];
732 int longest = 1;
733 int colonpos = -1;
734 u16 word;
742 unsigned char zerolength[8];
743 int longest = 1;
744 int colonpos = -1;
745 u16 word;
735 u8 hi;
736 u8 lo;
746 u8 hi, lo;
737 bool needcolon = false;
738 bool useIPv4;
739 struct in6_addr in6;
740
741 memcpy(&in6, addr, sizeof(struct in6_addr));
742
743 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
744

--- 50 unchanged lines hidden (view full) ---

795 needcolon = true;
796 }
797
798 if (useIPv4) {
799 if (needcolon)
800 *p++ = ':';
801 p = ip4_string(p, &in6.s6_addr[12], false);
802 }
747 bool needcolon = false;
748 bool useIPv4;
749 struct in6_addr in6;
750
751 memcpy(&in6, addr, sizeof(struct in6_addr));
752
753 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
754

--- 50 unchanged lines hidden (view full) ---

805 needcolon = true;
806 }
807
808 if (useIPv4) {
809 if (needcolon)
810 *p++ = ':';
811 p = ip4_string(p, &in6.s6_addr[12], false);
812 }
803
804 *p = '\0';
813 *p = '\0';
814
805 return p;
806}
807
808static char *ip6_string(char *p, const char *addr, const char *fmt)
809{
810 int i;
815 return p;
816}
817
818static char *ip6_string(char *p, const char *addr, const char *fmt)
819{
820 int i;
821
811 for (i = 0; i < 8; i++) {
812 p = pack_hex_byte(p, *addr++);
813 p = pack_hex_byte(p, *addr++);
814 if (fmt[0] == 'I' && i != 7)
815 *p++ = ':';
816 }
822 for (i = 0; i < 8; i++) {
823 p = pack_hex_byte(p, *addr++);
824 p = pack_hex_byte(p, *addr++);
825 if (fmt[0] == 'I' && i != 7)
826 *p++ = ':';
827 }
817
818 *p = '\0';
828 *p = '\0';
829
819 return p;
820}
821
822static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
823 struct printf_spec spec, const char *fmt)
824{
825 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
826

--- 490 unchanged lines hidden (view full) ---

1317 * You probably want scnprintf() instead.
1318 *
1319 * See the vsnprintf() documentation for format string extensions over C99.
1320 */
1321int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1322{
1323 int i;
1324
830 return p;
831}
832
833static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
834 struct printf_spec spec, const char *fmt)
835{
836 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
837

--- 490 unchanged lines hidden (view full) ---

1328 * You probably want scnprintf() instead.
1329 *
1330 * See the vsnprintf() documentation for format string extensions over C99.
1331 */
1332int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1333{
1334 int i;
1335
1325 i=vsnprintf(buf,size,fmt,args);
1336 i = vsnprintf(buf, size, fmt, args);
1337
1326 return (i >= size) ? (size - 1) : i;
1327}
1328EXPORT_SYMBOL(vscnprintf);
1329
1330/**
1331 * snprintf - Format a string and place it in a buffer
1332 * @buf: The buffer to place the result into
1333 * @size: The size of the buffer, including the trailing null space
1334 * @fmt: The format string to use
1335 * @...: Arguments for the format string
1336 *
1337 * The return value is the number of characters which would be
1338 * generated for the given input, excluding the trailing null,
1339 * as per ISO C99. If the return is greater than or equal to
1340 * @size, the resulting string is truncated.
1341 *
1342 * See the vsnprintf() documentation for format string extensions over C99.
1343 */
1338 return (i >= size) ? (size - 1) : i;
1339}
1340EXPORT_SYMBOL(vscnprintf);
1341
1342/**
1343 * snprintf - Format a string and place it in a buffer
1344 * @buf: The buffer to place the result into
1345 * @size: The size of the buffer, including the trailing null space
1346 * @fmt: The format string to use
1347 * @...: Arguments for the format string
1348 *
1349 * The return value is the number of characters which would be
1350 * generated for the given input, excluding the trailing null,
1351 * as per ISO C99. If the return is greater than or equal to
1352 * @size, the resulting string is truncated.
1353 *
1354 * See the vsnprintf() documentation for format string extensions over C99.
1355 */
1344int snprintf(char * buf, size_t size, const char *fmt, ...)
1356int snprintf(char *buf, size_t size, const char *fmt, ...)
1345{
1346 va_list args;
1347 int i;
1348
1349 va_start(args, fmt);
1357{
1358 va_list args;
1359 int i;
1360
1361 va_start(args, fmt);
1350 i=vsnprintf(buf,size,fmt,args);
1362 i = vsnprintf(buf, size, fmt, args);
1351 va_end(args);
1363 va_end(args);
1364
1352 return i;
1353}
1354EXPORT_SYMBOL(snprintf);
1355
1356/**
1357 * scnprintf - Format a string and place it in a buffer
1358 * @buf: The buffer to place the result into
1359 * @size: The size of the buffer, including the trailing null space
1360 * @fmt: The format string to use
1361 * @...: Arguments for the format string
1362 *
1363 * The return value is the number of characters written into @buf not including
1364 * the trailing '\0'. If @size is <= 0 the function returns 0.
1365 */
1366
1365 return i;
1366}
1367EXPORT_SYMBOL(snprintf);
1368
1369/**
1370 * scnprintf - Format a string and place it in a buffer
1371 * @buf: The buffer to place the result into
1372 * @size: The size of the buffer, including the trailing null space
1373 * @fmt: The format string to use
1374 * @...: Arguments for the format string
1375 *
1376 * The return value is the number of characters written into @buf not including
1377 * the trailing '\0'. If @size is <= 0 the function returns 0.
1378 */
1379
1367int scnprintf(char * buf, size_t size, const char *fmt, ...)
1380int scnprintf(char *buf, size_t size, const char *fmt, ...)
1368{
1369 va_list args;
1370 int i;
1371
1372 va_start(args, fmt);
1373 i = vsnprintf(buf, size, fmt, args);
1374 va_end(args);
1381{
1382 va_list args;
1383 int i;
1384
1385 va_start(args, fmt);
1386 i = vsnprintf(buf, size, fmt, args);
1387 va_end(args);
1388
1375 return (i >= size) ? (size - 1) : i;
1376}
1377EXPORT_SYMBOL(scnprintf);
1378
1379/**
1380 * vsprintf - Format a string and place it in a buffer
1381 * @buf: The buffer to place the result into
1382 * @fmt: The format string to use

--- 21 unchanged lines hidden (view full) ---

1404 * @...: Arguments for the format string
1405 *
1406 * The function returns the number of characters written
1407 * into @buf. Use snprintf() or scnprintf() in order to avoid
1408 * buffer overflows.
1409 *
1410 * See the vsnprintf() documentation for format string extensions over C99.
1411 */
1389 return (i >= size) ? (size - 1) : i;
1390}
1391EXPORT_SYMBOL(scnprintf);
1392
1393/**
1394 * vsprintf - Format a string and place it in a buffer
1395 * @buf: The buffer to place the result into
1396 * @fmt: The format string to use

--- 21 unchanged lines hidden (view full) ---

1418 * @...: Arguments for the format string
1419 *
1420 * The function returns the number of characters written
1421 * into @buf. Use snprintf() or scnprintf() in order to avoid
1422 * buffer overflows.
1423 *
1424 * See the vsnprintf() documentation for format string extensions over C99.
1425 */
1412int sprintf(char * buf, const char *fmt, ...)
1426int sprintf(char *buf, const char *fmt, ...)
1413{
1414 va_list args;
1415 int i;
1416
1417 va_start(args, fmt);
1427{
1428 va_list args;
1429 int i;
1430
1431 va_start(args, fmt);
1418 i=vsnprintf(buf, INT_MAX, fmt, args);
1432 i = vsnprintf(buf, INT_MAX, fmt, args);
1419 va_end(args);
1433 va_end(args);
1434
1420 return i;
1421}
1422EXPORT_SYMBOL(sprintf);
1423
1424#ifdef CONFIG_BINARY_PRINTF
1425/*
1426 * bprintf service:
1427 * vbin_printf() - VA arguments to binary data

--- 41 unchanged lines hidden (view full) ---

1469 str = PTR_ALIGN(str, sizeof(type)); \
1470 value = va_arg(args, int); \
1471 if (str + sizeof(type) <= end) \
1472 *(typeof(type) *)str = (type)value; \
1473 } \
1474 str += sizeof(type); \
1475} while (0)
1476
1435 return i;
1436}
1437EXPORT_SYMBOL(sprintf);
1438
1439#ifdef CONFIG_BINARY_PRINTF
1440/*
1441 * bprintf service:
1442 * vbin_printf() - VA arguments to binary data

--- 41 unchanged lines hidden (view full) ---

1484 str = PTR_ALIGN(str, sizeof(type)); \
1485 value = va_arg(args, int); \
1486 if (str + sizeof(type) <= end) \
1487 *(typeof(type) *)str = (type)value; \
1488 } \
1489 str += sizeof(type); \
1490} while (0)
1491
1477
1478 while (*fmt) {
1479 read = format_decode(fmt, &spec);
1480
1481 fmt += read;
1482
1483 switch (spec.type) {
1484 case FORMAT_TYPE_NONE:
1485 break;

--- 71 unchanged lines hidden (view full) ---

1557 case FORMAT_TYPE_SHORT:
1558 save_arg(short);
1559 break;
1560 default:
1561 save_arg(int);
1562 }
1563 }
1564 }
1492 while (*fmt) {
1493 read = format_decode(fmt, &spec);
1494
1495 fmt += read;
1496
1497 switch (spec.type) {
1498 case FORMAT_TYPE_NONE:
1499 break;

--- 71 unchanged lines hidden (view full) ---

1571 case FORMAT_TYPE_SHORT:
1572 save_arg(short);
1573 break;
1574 default:
1575 save_arg(int);
1576 }
1577 }
1578 }
1565 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1566
1579
1580 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1567#undef save_arg
1568}
1569EXPORT_SYMBOL_GPL(vbin_printf);
1570
1571/**
1572 * bstr_printf - Format a string from binary arguments and place it in a buffer
1573 * @buf: The buffer to place the result into
1574 * @size: The size of the buffer, including the trailing null space

--- 15 unchanged lines hidden (view full) ---

1590 * return is greater than or equal to @size, the resulting
1591 * string is truncated.
1592 */
1593int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1594{
1595 unsigned long long num;
1596 char *str, *end, c;
1597 const char *args = (const char *)bin_buf;
1581#undef save_arg
1582}
1583EXPORT_SYMBOL_GPL(vbin_printf);
1584
1585/**
1586 * bstr_printf - Format a string from binary arguments and place it in a buffer
1587 * @buf: The buffer to place the result into
1588 * @size: The size of the buffer, including the trailing null space

--- 15 unchanged lines hidden (view full) ---

1604 * return is greater than or equal to @size, the resulting
1605 * string is truncated.
1606 */
1607int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1608{
1609 unsigned long long num;
1610 char *str, *end, c;
1611 const char *args = (const char *)bin_buf;
1598
1599 struct printf_spec spec = {0};
1600
1601 if (WARN_ON_ONCE((int) size < 0))
1602 return 0;
1603
1604 str = buf;
1605 end = buf + size;
1606

--- 163 unchanged lines hidden (view full) ---

1770int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1771{
1772 va_list args;
1773 int ret;
1774
1775 va_start(args, fmt);
1776 ret = vbin_printf(bin_buf, size, fmt, args);
1777 va_end(args);
1612 struct printf_spec spec = {0};
1613
1614 if (WARN_ON_ONCE((int) size < 0))
1615 return 0;
1616
1617 str = buf;
1618 end = buf + size;
1619

--- 163 unchanged lines hidden (view full) ---

1783int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1784{
1785 va_list args;
1786 int ret;
1787
1788 va_start(args, fmt);
1789 ret = vbin_printf(bin_buf, size, fmt, args);
1790 va_end(args);
1791
1778 return ret;
1779}
1780EXPORT_SYMBOL_GPL(bprintf);
1781
1782#endif /* CONFIG_BINARY_PRINTF */
1783
1784/**
1785 * vsscanf - Unformat a buffer into a list of arguments
1786 * @buf: input buffer
1787 * @fmt: format of buffer
1788 * @args: arguments
1789 */
1792 return ret;
1793}
1794EXPORT_SYMBOL_GPL(bprintf);
1795
1796#endif /* CONFIG_BINARY_PRINTF */
1797
1798/**
1799 * vsscanf - Unformat a buffer into a list of arguments
1800 * @buf: input buffer
1801 * @fmt: format of buffer
1802 * @args: arguments
1803 */
1790int vsscanf(const char * buf, const char * fmt, va_list args)
1804int vsscanf(const char *buf, const char *fmt, va_list args)
1791{
1792 const char *str = buf;
1793 char *next;
1794 char digit;
1795 int num = 0;
1805{
1806 const char *str = buf;
1807 char *next;
1808 char digit;
1809 int num = 0;
1796 int qualifier;
1797 int base;
1798 int field_width;
1810 int qualifier, base, field_width;
1799 int is_sign = 0;
1800
1811 int is_sign = 0;
1812
1801 while(*fmt && *str) {
1813 while (*fmt && *str) {
1802 /* skip any white space in format */
1803 /* white space in format matchs any amount of
1804 * white space, including none, in the input.
1805 */
1806 if (isspace(*fmt)) {
1807 while (isspace(*fmt))
1808 ++fmt;
1809 while (isspace(*str))

--- 5 unchanged lines hidden (view full) ---

1815 if (*fmt++ != *str++)
1816 break;
1817 continue;
1818 }
1819
1820 if (!*fmt)
1821 break;
1822 ++fmt;
1814 /* skip any white space in format */
1815 /* white space in format matchs any amount of
1816 * white space, including none, in the input.
1817 */
1818 if (isspace(*fmt)) {
1819 while (isspace(*fmt))
1820 ++fmt;
1821 while (isspace(*str))

--- 5 unchanged lines hidden (view full) ---

1827 if (*fmt++ != *str++)
1828 break;
1829 continue;
1830 }
1831
1832 if (!*fmt)
1833 break;
1834 ++fmt;
1823
1835
1824 /* skip this conversion.
1825 * advance both strings to next white space
1826 */
1827 if (*fmt == '*') {
1828 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1829 fmt++;
1830 while (!isspace(*str) && *str)
1831 str++;

--- 21 unchanged lines hidden (view full) ---

1853 }
1854 }
1855 base = 10;
1856 is_sign = 0;
1857
1858 if (!*fmt || !*str)
1859 break;
1860
1836 /* skip this conversion.
1837 * advance both strings to next white space
1838 */
1839 if (*fmt == '*') {
1840 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1841 fmt++;
1842 while (!isspace(*str) && *str)
1843 str++;

--- 21 unchanged lines hidden (view full) ---

1865 }
1866 }
1867 base = 10;
1868 is_sign = 0;
1869
1870 if (!*fmt || !*str)
1871 break;
1872
1861 switch(*fmt++) {
1873 switch (*fmt++) {
1862 case 'c':
1863 {
1874 case 'c':
1875 {
1864 char *s = (char *) va_arg(args,char*);
1876 char *s = (char *)va_arg(args, char*);
1865 if (field_width == -1)
1866 field_width = 1;
1867 do {
1868 *s++ = *str++;
1869 } while (--field_width > 0 && *str);
1870 num++;
1871 }
1872 continue;
1873 case 's':
1874 {
1877 if (field_width == -1)
1878 field_width = 1;
1879 do {
1880 *s++ = *str++;
1881 } while (--field_width > 0 && *str);
1882 num++;
1883 }
1884 continue;
1885 case 's':
1886 {
1875 char *s = (char *) va_arg(args, char *);
1876 if(field_width == -1)
1887 char *s = (char *)va_arg(args, char *);
1888 if (field_width == -1)
1877 field_width = INT_MAX;
1878 /* first, skip leading white space in buffer */
1879 while (isspace(*str))
1880 str++;
1881
1882 /* now copy until next white space */
1889 field_width = INT_MAX;
1890 /* first, skip leading white space in buffer */
1891 while (isspace(*str))
1892 str++;
1893
1894 /* now copy until next white space */
1883 while (*str && !isspace(*str) && field_width--) {
1895 while (*str && !isspace(*str) && field_width--)
1884 *s++ = *str++;
1896 *s++ = *str++;
1885 }
1886 *s = '\0';
1887 num++;
1888 }
1889 continue;
1890 case 'n':
1891 /* return number of characters read so far */
1892 {
1897 *s = '\0';
1898 num++;
1899 }
1900 continue;
1901 case 'n':
1902 /* return number of characters read so far */
1903 {
1893 int *i = (int *)va_arg(args,int*);
1904 int *i = (int *)va_arg(args, int*);
1894 *i = str - buf;
1895 }
1896 continue;
1897 case 'o':
1898 base = 8;
1899 break;
1900 case 'x':
1901 case 'X':
1902 base = 16;
1903 break;
1904 case 'i':
1905 *i = str - buf;
1906 }
1907 continue;
1908 case 'o':
1909 base = 8;
1910 break;
1911 case 'x':
1912 case 'X':
1913 base = 16;
1914 break;
1915 case 'i':
1905 base = 0;
1916 base = 0;
1906 case 'd':
1907 is_sign = 1;
1908 case 'u':
1909 break;
1910 case '%':
1911 /* looking for '%' in str */
1917 case 'd':
1918 is_sign = 1;
1919 case 'u':
1920 break;
1921 case '%':
1922 /* looking for '%' in str */
1912 if (*str++ != '%')
1923 if (*str++ != '%')
1913 return num;
1914 continue;
1915 default:
1916 /* invalid format; stop here */
1917 return num;
1918 }
1919
1920 /* have some sort of integer conversion.
1921 * first, skip white space in buffer.
1922 */
1923 while (isspace(*str))
1924 str++;
1925
1926 digit = *str;
1927 if (is_sign && digit == '-')
1928 digit = *(str + 1);
1929
1930 if (!digit
1924 return num;
1925 continue;
1926 default:
1927 /* invalid format; stop here */
1928 return num;
1929 }
1930
1931 /* have some sort of integer conversion.
1932 * first, skip white space in buffer.
1933 */
1934 while (isspace(*str))
1935 str++;
1936
1937 digit = *str;
1938 if (is_sign && digit == '-')
1939 digit = *(str + 1);
1940
1941 if (!digit
1931 || (base == 16 && !isxdigit(digit))
1932 || (base == 10 && !isdigit(digit))
1933 || (base == 8 && (!isdigit(digit) || digit > '7'))
1934 || (base == 0 && !isdigit(digit)))
1935 break;
1942 || (base == 16 && !isxdigit(digit))
1943 || (base == 10 && !isdigit(digit))
1944 || (base == 8 && (!isdigit(digit) || digit > '7'))
1945 || (base == 0 && !isdigit(digit)))
1946 break;
1936
1947
1937 switch(qualifier) {
1948 switch (qualifier) {
1938 case 'H': /* that's 'hh' in format */
1939 if (is_sign) {
1949 case 'H': /* that's 'hh' in format */
1950 if (is_sign) {
1940 signed char *s = (signed char *) va_arg(args,signed char *);
1941 *s = (signed char) simple_strtol(str,&next,base);
1951 signed char *s = (signed char *)va_arg(args, signed char *);
1952 *s = (signed char)simple_strtol(str, &next, base);
1942 } else {
1953 } else {
1943 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1944 *s = (unsigned char) simple_strtoul(str, &next, base);
1954 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
1955 *s = (unsigned char)simple_strtoul(str, &next, base);
1945 }
1946 break;
1947 case 'h':
1948 if (is_sign) {
1956 }
1957 break;
1958 case 'h':
1959 if (is_sign) {
1949 short *s = (short *) va_arg(args,short *);
1950 *s = (short) simple_strtol(str,&next,base);
1960 short *s = (short *)va_arg(args, short *);
1961 *s = (short)simple_strtol(str, &next, base);
1951 } else {
1962 } else {
1952 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1953 *s = (unsigned short) simple_strtoul(str, &next, base);
1963 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
1964 *s = (unsigned short)simple_strtoul(str, &next, base);
1954 }
1955 break;
1956 case 'l':
1957 if (is_sign) {
1965 }
1966 break;
1967 case 'l':
1968 if (is_sign) {
1958 long *l = (long *) va_arg(args,long *);
1959 *l = simple_strtol(str,&next,base);
1969 long *l = (long *)va_arg(args, long *);
1970 *l = simple_strtol(str, &next, base);
1960 } else {
1971 } else {
1961 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1962 *l = simple_strtoul(str,&next,base);
1972 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
1973 *l = simple_strtoul(str, &next, base);
1963 }
1964 break;
1965 case 'L':
1966 if (is_sign) {
1974 }
1975 break;
1976 case 'L':
1977 if (is_sign) {
1967 long long *l = (long long*) va_arg(args,long long *);
1968 *l = simple_strtoll(str,&next,base);
1978 long long *l = (long long *)va_arg(args, long long *);
1979 *l = simple_strtoll(str, &next, base);
1969 } else {
1980 } else {
1970 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1971 *l = simple_strtoull(str,&next,base);
1981 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
1982 *l = simple_strtoull(str, &next, base);
1972 }
1973 break;
1974 case 'Z':
1975 case 'z':
1976 {
1983 }
1984 break;
1985 case 'Z':
1986 case 'z':
1987 {
1977 size_t *s = (size_t*) va_arg(args,size_t*);
1978 *s = (size_t) simple_strtoul(str,&next,base);
1988 size_t *s = (size_t *)va_arg(args, size_t *);
1989 *s = (size_t)simple_strtoul(str, &next, base);
1979 }
1980 break;
1981 default:
1982 if (is_sign) {
1990 }
1991 break;
1992 default:
1993 if (is_sign) {
1983 int *i = (int *) va_arg(args, int*);
1984 *i = (int) simple_strtol(str,&next,base);
1994 int *i = (int *)va_arg(args, int *);
1995 *i = (int)simple_strtol(str, &next, base);
1985 } else {
1996 } else {
1986 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1987 *i = (unsigned int) simple_strtoul(str,&next,base);
1997 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
1998 *i = (unsigned int)simple_strtoul(str, &next, base);
1988 }
1989 break;
1990 }
1991 num++;
1992
1993 if (!next)
1994 break;
1995 str = next;

--- 14 unchanged lines hidden (view full) ---

2010EXPORT_SYMBOL(vsscanf);
2011
2012/**
2013 * sscanf - Unformat a buffer into a list of arguments
2014 * @buf: input buffer
2015 * @fmt: formatting of buffer
2016 * @...: resulting arguments
2017 */
1999 }
2000 break;
2001 }
2002 num++;
2003
2004 if (!next)
2005 break;
2006 str = next;

--- 14 unchanged lines hidden (view full) ---

2021EXPORT_SYMBOL(vsscanf);
2022
2023/**
2024 * sscanf - Unformat a buffer into a list of arguments
2025 * @buf: input buffer
2026 * @fmt: formatting of buffer
2027 * @...: resulting arguments
2028 */
2018int sscanf(const char * buf, const char * fmt, ...)
2029int sscanf(const char *buf, const char *fmt, ...)
2019{
2020 va_list args;
2021 int i;
2022
2030{
2031 va_list args;
2032 int i;
2033
2023 va_start(args,fmt);
2024 i = vsscanf(buf,fmt,args);
2034 va_start(args, fmt);
2035 i = vsscanf(buf, fmt, args);
2025 va_end(args);
2036 va_end(args);
2037
2026 return i;
2027}
2028EXPORT_SYMBOL(sscanf);
2038 return i;
2039}
2040EXPORT_SYMBOL(sscanf);