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,19 +49,30 @@ static void printnum(void (*func)(int, void*),void *handle,
} while (num /= base);
}
// Print Spacers
// Print spacers (pre-number)
spaces = width - (p - buf);
if (padc == ' ' || padc == '0') {
while (spaces > 0)
{
func(padc, handle);
spaces--;
}
}
// Print Number
while (p != buf) {
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':
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) {