Improving libc and printf

This commit is contained in:
Ali Mashtizadeh 2014-07-13 14:09:36 -07:00
parent c9750e4834
commit 6b0d427898
2 changed files with 61 additions and 14 deletions

View File

@ -4,9 +4,23 @@
* All rights reserved. * All rights reserved.
*/ */
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
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 * char *
strcpy(char *to, const char *from) strcpy(char *to, const char *from)
{ {

View File

@ -4,8 +4,9 @@
* All rights reserved. * All rights reserved.
*/ */
#include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <kassert.h> #include <kassert.h>
@ -48,12 +49,14 @@ static void printnum(void (*func)(int, void*),void *handle,
} while (num /= base); } while (num /= base);
} }
// Print Spacers // Print spacers (pre-number)
spaces = width - (p - buf); spaces = width - (p - buf);
while (spaces > 0) if (padc == ' ' || padc == '0') {
{ while (spaces > 0)
func(padc, handle); {
spaces--; func(padc, handle);
spaces--;
}
} }
// Print Number // Print Number
@ -61,6 +64,15 @@ static void printnum(void (*func)(int, void*),void *handle,
p--; p--;
func((int)*p, handle); 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) int kvprintf(char const *fmt, void (*func)(int,void *), void *handle, va_list ap)
@ -117,14 +129,35 @@ again:
case 'c': case 'c':
func(va_arg(ap, int) & 0xff, handle); func(va_arg(ap, int) & 0xff, handle);
break; break;
case 's': case 's': {
p = va_arg(ap, char *); int spaces;
ASSERT(p != 0);
while (*p != '\0') p = va_arg(ap, char *);
{ ASSERT(p != 0);
func(*p++, handle); spaces = width - strlen(p);
}
break; 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': case 'd':
num = getint(ap, lflag); num = getint(ap, lflag);
if (num < 0) { if (num < 0) {