diff --git a/include/metalsvm/stdio.h b/include/metalsvm/stdio.h index ca276766..4a314b57 100644 --- a/include/metalsvm/stdio.h +++ b/include/metalsvm/stdio.h @@ -22,11 +22,17 @@ #include #include +#include #ifdef __cplusplus extern "C" { #endif +/* + * Forces all write operations + */ +int kflush(void); + /* * Works like the ANSI C function puts */ @@ -38,20 +44,30 @@ int kputs(const char *); int kputchar(int); /* - * Work like the ANSI C function printf + * Works like the ANSI C function printf */ int kprintf(const char*, ...); -/* - * Forces all write operations +/* + * Works like the ANSI c function sprintf */ -int kflush(void); +int ksprintf(char *str, const char *format, ...); + +/* + * Works like the ANSI c function sprintf + */ +int ksnprintf(char *str, size_t size, const char *format, ...); /* * Initialize the I/O functions */ int koutput_init(void); +/* + * Scaled down version of printf(3) + */ +int kvprintf(char const *fmt, void (*func) (int, void *), void *arg, int radix, va_list ap); + #ifdef __cplusplus } #endif diff --git a/libkern/Makefile b/libkern/Makefile index 3d93ffcf..5bcc2212 100644 --- a/libkern/Makefile +++ b/libkern/Makefile @@ -1,4 +1,4 @@ -C_source = string.c stdio.c printf.c qdivrem.c udivdi3.c umoddi3.c +C_source = string.c stdio.c printf.c sprintf.c qdivrem.c udivdi3.c umoddi3.c OBJS += $(patsubst %.c, %.o, $(filter %.c, $(C_source))) diff --git a/libkern/printf.c b/libkern/printf.c index 097e5c32..780184bb 100644 --- a/libkern/printf.c +++ b/libkern/printf.c @@ -44,7 +44,7 @@ * all typedefs required have been added. */ -#include +#include #define __64BIT__ diff --git a/libkern/sprintf.c b/libkern/sprintf.c new file mode 100644 index 00000000..cb2581c9 --- /dev/null +++ b/libkern/sprintf.c @@ -0,0 +1,74 @@ +/* + * Copyright 2010 Stefan Lankes, Chair for Operating Systems, + * RWTH Aachen University + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This file is part of MetalSVM. + */ + +#include + +typedef struct { + char *str; + size_t pos; + size_t max; +} sputchar_arg_t; + +static void sputchar(int c, void *arg) +{ + sputchar_arg_t *dest = (sputchar_arg_t *) arg; + + if (dest->pos < dest->max) { + dest->str[dest->pos] = (char)c; + dest->pos++; + } +} + +int ksnprintf(char *str, size_t size, const char *format, ...) +{ + int ret; + va_list ap; + sputchar_arg_t dest; + + dest.str = str; + dest.pos = 0; + dest.max = size; + + va_start(ap, format); + ret = kvprintf(format, sputchar, &dest, 10, ap); + va_end(ap); + + str[ret] = 0; + + return ret; +} + +int ksprintf(char *str, const char *format, ...) +{ + int ret; + va_list ap; + sputchar_arg_t dest; + + dest.str = str; + dest.pos = 0; + dest.max = (size_t) -1; + + va_start(ap, format); + ret = kvprintf(format, sputchar, &dest, 10, ap); + va_end(ap); + + str[ret] = 0; + + return ret; +}