From 3814aed56039dcb7d6d2f72eb5fd692669e09dcd Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Tue, 23 Dec 2014 14:58:03 +0000 Subject: [PATCH] libsa: add string precision support to vprintf We already had a hack to ignore the precision, but the ACPI driver requires an actual implementation--it prints garbage at the end of some strings otherwise. This patch adds support for precision for strings only, limiting printing to the given number of characters. For all other specifiers, precision is still unsupported. Change-Id: I1d41fc70a0d0494db695c22ba609262a50b86e08 --- sys/lib/libsa/subr_prf.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/sys/lib/libsa/subr_prf.c b/sys/lib/libsa/subr_prf.c index 93f101728..d6fc0defc 100644 --- a/sys/lib/libsa/subr_prf.c +++ b/sys/lib/libsa/subr_prf.c @@ -191,6 +191,9 @@ kdoprnt(void (*put)(int), const char *fmt, va_list ap) #ifdef LIBSA_PRINTF_WIDTH_SUPPORT int width; char *q; +#if defined(__minix) + int max; +#endif /* defined(__minix) */ #endif for (;;) { @@ -202,18 +205,20 @@ kdoprnt(void (*put)(int), const char *fmt, va_list ap) lflag = 0; #ifdef LIBSA_PRINTF_WIDTH_SUPPORT width = 0; +#if defined(__minix) + max = -1; +#endif /* defined(__minix) */ #endif reswitch: switch (ch = *fmt++) { #ifdef LIBSA_PRINTF_WIDTH_SUPPORT #if defined(__minix) - /* LSC: FIXME: this is a simple hack which ignores the thing for now. */ case '.': - /* eat up digits */ - while( ((('1' >= *fmt) && ( *fmt <= '9')) - || (*fmt == '*')) ) - fmt++; - fmt++; + if (*fmt == '*') { + max = va_arg(ap, int); + fmt++; + } else for (max = 0; *fmt >= '0' && *fmt <= '9'; fmt++) + max = max * 10 + *fmt - '0'; goto reswitch; #endif /* defined(__minix) */ case '#': @@ -274,10 +279,19 @@ reswitch: #ifdef LIBSA_PRINTF_WIDTH_SUPPORT for (q = p; *q != '\0'; ++q) continue; +#if defined(__minix) + if (max >= 0 && q - p > max) + q = &p[max]; +#endif /* defined(__minix) */ width -= q - p; #endif RPAD(); +#if defined(LIBSA_PRINTF_WIDTH_SUPPORT) && defined(__minix) + while ((max < 0 || max-- > 0) && + (ch = (unsigned char)*p++)) +#else /* !defined(LIBSA_PRINTF_WIDTH_SUPPORT) || !defined(__minix) */ while ((ch = (unsigned char)*p++)) +#endif /* !defined(LIBSA_PRINTF_WIDTH_SUPPORT) || !defined(__minix) */ put(ch); LPAD(); break;