diff options
Diffstat (limited to 'src/user/lib/std')
-rw-r--r-- | src/user/lib/std/stdio.c | 73 | ||||
-rw-r--r-- | src/user/lib/std/stdlib.c | 5 | ||||
-rw-r--r-- | src/user/lib/std/string.c | 2 |
3 files changed, 70 insertions, 10 deletions
diff --git a/src/user/lib/std/stdio.c b/src/user/lib/std/stdio.c index c11dd05..2bce2e3 100644 --- a/src/user/lib/std/stdio.c +++ b/src/user/lib/std/stdio.c @@ -1,9 +1,14 @@ #include <stdlib.h> #include <stdio.h> +#include <tce/syscall.h> -void printk_int(int number) { +void fprint(FILE f, char *s) { + write(f, 0, strlen(s), s); +} + +void fprint_int(FILE f, int number) { if (number == 0) { - printk("0"); + fprint(f, "0"); return; } int negative = 0; @@ -31,10 +36,10 @@ void printk_int(int number) { number /= 10; } r[order] = 0; - printk(s); + fprint(f, s); } -void printk_hex(unsigned v) { +void fprint_hex(FILE f, unsigned v) { char s[11] = {'0', 'x', 0}; int i; @@ -46,5 +51,63 @@ void printk_hex(unsigned v) { v = v << 4; } s[11] = 0; - printk(s); + fprint(f, s); +} + +void fprintf(FILE f, char* format, ...) { + va_list ap; + va_start(ap, format); + + char* start = format; + + while (*format) { + if (*format == '%') { + if (start != format) write(f, 0, format - start, start); + format++; + if (*format == 'd' || *format == 'i') { + fprint_int(f, va_arg(ap, int)); + } else if (*format == 'p') { + fprint_hex(f, va_arg(ap, uint32_t)); + } else if (*format == 's') { + fprint(f, va_arg(ap, char*)); + } + format++; + start = format; + } else { + format++; + } + } + if (start != format) write(f, 0, format - start, start); + + va_end(ap); +} + +char* freadln(FILE f) { + int i; + + char *p = (char*)malloc(256); + char *b = p; + + while (1) { + int l = read(f, 0, 255, b); + if (l < 0) { + free(b); + return 0; + } + + for (i = 0; i < l; i++) { + if (b[i] == '\n') { + b[i+1] = 0; + return p; + } + } + + int d = b - p + l; + + char* newp = (char*)malloc(d + 256); + memcpy(newp, p, d); + free(p); + p = newp; + b = p + d; + } } diff --git a/src/user/lib/std/stdlib.c b/src/user/lib/std/stdlib.c index 21753e9..9d46b5c 100644 --- a/src/user/lib/std/stdlib.c +++ b/src/user/lib/std/stdlib.c @@ -4,8 +4,5 @@ volatile int errno; void abort() { - printk("Abort - errno "); - printk_int(errno); - printk(".\n"); - process_exit(-1); + process_exit(100 + errno); } diff --git a/src/user/lib/std/string.c b/src/user/lib/std/string.c index 20fd9e8..7cd8ede 100644 --- a/src/user/lib/std/string.c +++ b/src/user/lib/std/string.c @@ -3,7 +3,7 @@ int strlen(const char *str) { int i = 0; while (str[i++]); - return i; + return i-1; } char *strchr(const char *str, char c) { |