aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/vsprintf.c57
1 files changed, 31 insertions, 26 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 6021757a4496..926c7e00e2dc 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -482,6 +482,35 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
482 return buf; 482 return buf;
483} 483}
484 484
485static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
486{
487 int len, i;
488
489 if ((unsigned long)s < PAGE_SIZE)
490 s = "<NULL>";
491
492 len = strnlen(s, precision);
493
494 if (!(flags & LEFT)) {
495 while (len < field_width--) {
496 if (buf < end)
497 *buf = ' ';
498 ++buf;
499 }
500 }
501 for (i = 0; i < len; ++i) {
502 if (buf < end)
503 *buf = *s;
504 ++buf; ++s;
505 }
506 while (len < field_width--) {
507 if (buf < end)
508 *buf = ' ';
509 ++buf;
510 }
511 return buf;
512}
513
485/** 514/**
486 * vsnprintf - Format a string and place it in a buffer 515 * vsnprintf - Format a string and place it in a buffer
487 * @buf: The buffer to place the result into 516 * @buf: The buffer to place the result into
@@ -502,11 +531,9 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
502 */ 531 */
503int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 532int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
504{ 533{
505 int len;
506 unsigned long long num; 534 unsigned long long num;
507 int i, base; 535 int base;
508 char *str, *end, c; 536 char *str, *end, c;
509 const char *s;
510 537
511 int flags; /* flags to number() */ 538 int flags; /* flags to number() */
512 539
@@ -622,29 +649,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
622 continue; 649 continue;
623 650
624 case 's': 651 case 's':
625 s = va_arg(args, char *); 652 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
626 if ((unsigned long)s < PAGE_SIZE)
627 s = "<NULL>";
628
629 len = strnlen(s, precision);
630
631 if (!(flags & LEFT)) {
632 while (len < field_width--) {
633 if (str < end)
634 *str = ' ';
635 ++str;
636 }
637 }
638 for (i = 0; i < len; ++i) {
639 if (str < end)
640 *str = *s;
641 ++str; ++s;
642 }
643 while (len < field_width--) {
644 if (str < end)
645 *str = ' ';
646 ++str;
647 }
648 continue; 653 continue;
649 654
650 case 'p': 655 case 'p':