From 6b0d42789839614d19a0406b596326f2c8227f8b Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Sun, 13 Jul 2014 14:09:36 -0700 Subject: [PATCH] Improving libc and printf --- sys/kern/libc.c | 14 +++++++++++ sys/kern/printf.c | 61 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/sys/kern/libc.c b/sys/kern/libc.c index 092e1ef..8b5afe5 100644 --- a/sys/kern/libc.c +++ b/sys/kern/libc.c @@ -4,9 +4,23 @@ * All rights reserved. */ +#include #include #include +char * +strchr(const char *s, int c) +{ + int i; + for (i = 0; ; i++) + { + if (s[i] == c) + return (char *)s + i; + if (s[i] == '\0') + return NULL; + } +} + char * strcpy(char *to, const char *from) { diff --git a/sys/kern/printf.c b/sys/kern/printf.c index c0b672e..12aff0f 100644 --- a/sys/kern/printf.c +++ b/sys/kern/printf.c @@ -4,8 +4,9 @@ * All rights reserved. */ -#include #include +#include +#include #include @@ -48,12 +49,14 @@ static void printnum(void (*func)(int, void*),void *handle, } while (num /= base); } - // Print Spacers + // Print spacers (pre-number) spaces = width - (p - buf); - while (spaces > 0) - { - func(padc, handle); - spaces--; + if (padc == ' ' || padc == '0') { + while (spaces > 0) + { + func(padc, handle); + spaces--; + } } // Print Number @@ -61,6 +64,15 @@ static void printnum(void (*func)(int, void*),void *handle, p--; func((int)*p, handle); } + + // Print spacers (post-number) + if (padc == '-') { + while (spaces > 0) + { + func(' ', handle); + spaces--; + } + } } int kvprintf(char const *fmt, void (*func)(int,void *), void *handle, va_list ap) @@ -117,14 +129,35 @@ again: case 'c': func(va_arg(ap, int) & 0xff, handle); break; - case 's': - p = va_arg(ap, char *); - ASSERT(p != 0); - while (*p != '\0') - { - func(*p++, handle); - } - break; + case 's': { + int spaces; + + p = va_arg(ap, char *); + ASSERT(p != 0); + spaces = width - strlen(p); + + if (padc == ' ') { + while (spaces > 0) + { + func(' ', handle); + spaces--; + } + } + + while (*p != '\0') + { + func(*p++, handle); + } + + if (padc == '-') { + while (spaces > 0) + { + func(' ', handle); + spaces--; + } + } + break; + } case 'd': num = getint(ap, lflag); if (num < 0) {