From 1afa20dc557019204a575166004fb5fc30cc1d52 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Sat, 22 Jan 2022 23:01:12 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E4=B8=80=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/printk.c | 41 +++++++++++++++++++++++++++++++++++++++++ kernel/printk.h | 10 ++++++++++ 2 files changed, 51 insertions(+) diff --git a/kernel/printk.c b/kernel/printk.c index cecec45c..a6191a51 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -114,6 +114,7 @@ int vsprintf(char *buf, const char *fmt, va_list args) field_width = skip_and_atoi(&fmt); //获取小数精度 + precision = -1; if (*fmt == '.') { ++fmt; @@ -162,6 +163,46 @@ int vsprintf(char *buf, const char *fmt, va_list args) ++str; } + break; + //显示一个字符串 + case 's': + s = va_arg(args, char *); + if (!s) + s = '\0'; + len = strlen(s); + if (precision < 0) + { + //未指定精度 + precision = len; + } + + else if (len > precision) + { + len = precision; + } + + //靠右对齐 + if (!(flags & LEFT)) + while (len < field_width--) + { + *str = ' '; + ++str; + } + + for (int i = 0; i < len; i++) + { + *str = *s; + ++s; + ++str; + } + + while (len