diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2008-07-06 19:06:25 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-07-06 19:06:25 -0400 |
commit | 0f9bfa569d46f2346a53a940b2b9e49a38635732 (patch) | |
tree | ca301743d54bc7f4e0fea3f541db1b23768f9934 /lib | |
parent | 1b40a895df6c7d5a80e71f65674060b03d84bbef (diff) |
vsprintf: split out '%s' handling logic
The actual code is the same, just split out into a helper function.
This makes it easier to read, and allows for future sharing of the
string code.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/vsprintf.c | 57 |
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 | ||
485 | static 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 | */ |
503 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | 532 | int 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': |