Use kernel fmt.cc for userspace

上级 c576c80d
......@@ -80,7 +80,7 @@ OBJS = \
incbin.o
OBJS := $(addprefix $(O)/, $(OBJS))
ULIB = ulib.o usys.o printf.o umalloc.o uthread.o
ULIB = ulib.o usys.o printf.o umalloc.o uthread.o fmt.o
ULIB := $(addprefix $(O)/, $(ULIB))
UPROGS= \
......
......@@ -2,3 +2,11 @@
#define __mpalign__ __attribute__((aligned(CACHELINE)))
#define __noret__ __attribute__((noreturn))
#define barrier() __asm volatile("" ::: "memory")
#ifdef __cplusplus
#define BEGIN_DECLS extern "C" {
#define END_DECLS }
#else
#define BEGIN_DECLS
#define END_DECLS
#endif
extern "C" {
#include "types.h"
#include "kernel.h"
#include <stddef.h>
#include <stdarg.h>
#include "fmt.h"
#include "lib.h"
unsigned int strlen(const char*);
}
//
......
......@@ -294,7 +294,7 @@ void* memmove(void*, const void*, u32);
void* memset(void*, int, u32);
void* memcpy(void*, const void *, u32);
char* safestrcpy(char*, const char*, u32);
int strlen(const char*);
unsigned int strlen(const char*);
int strncmp(const char*, const char*, u32);
char* strncpy(char*, const char*, u32);
int strcmp(const char *p, const char *q);
......
#include "types.h"
#include "stat.h"
#include "user.h"
extern "C" {
#include <stdarg.h>
static void
printint(void (*putch) (void*, char), void *putarg,
s64 xx, int base, int sgn)
{
const static char digits[] = "0123456789ABCDEF";
char buf[21];
int i, neg;
s64 x;
neg = 0;
if(sgn && xx < 0){
neg = 1;
x = -xx;
} else {
x = xx;
}
i = 0;
do{
buf[i++] = digits[x % base];
}while((x /= base) != 0);
if(neg)
buf[i++] = '-';
while(--i >= 0)
putch(putarg, buf[i]);
}
// Only understands %d, %x, %p, %s.
void
vprintfmt(void (*putch) (void*, char), void *putarg,
const char *fmt, va_list ap)
{
const char *s;
int c, i, state;
state = 0;
for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff;
if(state == 0){
if(c == '%'){
state = '%';
} else {
putch(putarg, c);
}
} else if(state == '%'){
if(c == 'd'){
printint(putch, putarg, va_arg(ap, u32), 10, 1);
} else if(c == 'x') {
printint(putch, putarg, va_arg(ap, u32), 16, 0);
} else if(c == 'l') {
state = 'l';
continue;
} else if(c == 's'){
s = (const char*) va_arg(ap, const char*);
if(s == 0)
s = "(null)";
while(*s != 0){
putch(putarg, *s);
s++;
}
} else if(c == 'c'){
putch(putarg, va_arg(ap, u32));
} else if(c == '%'){
putch(putarg, c);
} else {
// Unknown % sequence. Print it to draw attention.
putch(putarg, '%');
putch(putarg, c);
}
state = 0;
} else if(state == 'l') {
if(c == 'x') {
printint(putch, putarg, va_arg(ap, u64), 16, 0);
}
else if(c == 'u') {
printint(putch, putarg, va_arg(ap, u64), 10, 0);
}
else {
// Unknown % sequence. Print it to draw attention.
putch(putarg, '%');
putch(putarg, c);
}
state = 0;
}
}
#include "fmt.h"
}
// Print to the given fd.
static void
writec(void *arg, char c)
writec(int c, void *arg)
{
int fd = (int) (u64) arg;
int fd = (int) (u64) arg;
write(fd, &c, 1);
}
......@@ -117,7 +31,7 @@ struct bufstate {
};
static void
writebuf(void *arg, char c)
writebuf(int c, void *arg)
{
struct bufstate *bs = (bufstate*) arg;
if (bs->p < bs->e) {
......
......@@ -92,7 +92,7 @@ safestrcpy(char *s, const char *t, u32 n)
return os;
}
int
unsigned int
strlen(const char *s)
{
int n;
......
BEGIN_DECLS
struct stat;
// system calls
extern "C" {
int fork(int);
int exit(void) __attribute__((noreturn));
int wait(void);
......@@ -28,7 +28,6 @@ int unmap(void *addr, int len);
void halt(void);
ssize_t pread(int, void*, size_t, off_t);
int kernlet(int, size_t, off_t);
}
// ulib.c
int stat(char*, struct stat*);
......@@ -46,11 +45,10 @@ void free(void*);
int atoi(const char*);
// uthread.S
extern "C" {
int forkt(void *sp, void *pc, void *arg);
}
// printf.c
void printf(int, const char*, ...);
void snprintf(char *buf, unsigned int n, const char *fmt, ...);
void die(const char* errstr, ...) __attribute__((noreturn));
END_DECLS
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论