summaryrefslogtreecommitdiff
path: root/src/user/lib/std/stdio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/lib/std/stdio.c')
-rw-r--r--src/user/lib/std/stdio.c73
1 files changed, 68 insertions, 5 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;
+ }
}