summaryrefslogtreecommitdiff
path: root/src/user/lib/std
diff options
context:
space:
mode:
authorAlex AUVOLAT <alexis211@gmail.com>2012-05-18 19:06:35 +0200
committerAlex AUVOLAT <alexis211@gmail.com>2012-05-18 19:06:35 +0200
commit478c691187fbc9ba4ccaacf92f57828eef20041c (patch)
tree4cb4b00d7da1fd533cebc347f3641cc0455935f5 /src/user/lib/std
parent7e6454020ed1143e05e83a683606f318995458e5 (diff)
downloadTCE-478c691187fbc9ba4ccaacf92f57828eef20041c.tar.gz
TCE-478c691187fbc9ba4ccaacf92f57828eef20041c.zip
Simple shell added. Simple fprintf function added too.
Diffstat (limited to 'src/user/lib/std')
-rw-r--r--src/user/lib/std/stdio.c73
-rw-r--r--src/user/lib/std/stdlib.c5
-rw-r--r--src/user/lib/std/string.c2
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) {