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.
*/
#include <stddef.h>
#include <stdint.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 *
strcpy(char *to, const char *from)
{

View File

@ -4,8 +4,9 @@
* All rights reserved.
*/
#include <stdarg.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <kassert.h>
@ -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) {