From ee348973b0cb0f6481e4fd6e7494c63167c9759c Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Wed, 7 Apr 2010 11:37:21 +0200 Subject: More work on manager --- src/library/std/stdio.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ src/library/std/string.c | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/library/std/stdio.c (limited to 'src/library/std') diff --git a/src/library/std/stdio.c b/src/library/std/stdio.c new file mode 100644 index 0000000..3b24da1 --- /dev/null +++ b/src/library/std/stdio.c @@ -0,0 +1,51 @@ +#include + +void printk_int(int number) { + if (number == 0) { + printk("0"); + return; + } + int negative = 0; + if (number < 0) { + negative = 1; + number = 0 - number; + } + int order = 0, temp = number, i; + char numbers[] = "0123456789"; + while (temp > 0) { + order++; + temp /= 10; + } + + char *s, *r; + s = malloc(order + (negative ? 2 : 1)); + if (negative) { + s[0] = '-'; + r = s + 1; + } else { + r = s; + } + + for (i = order; i > 0; i--) { + r[i - 1] = numbers[number % 10]; + number /= 10; + } + r[order] = 0; + printk(s); + free(s); +} + +void printk_hex(unsigned v) { + char s[11] = {'0', 'x', 0}; + + int i; + + char hexdigits[] = "0123456789ABCDEF"; + + for (i = 0; i < 8; i++) { + s[i + 2] = (hexdigits[v >> 28]); + v = v << 4; + } + s[11] = 0; + printk(s); +} diff --git a/src/library/std/string.c b/src/library/std/string.c index 869256a..4c374ad 100644 --- a/src/library/std/string.c +++ b/src/library/std/string.c @@ -6,6 +6,46 @@ int strlen(const char *str) { return i; } +char *strchr(const char *str, char c) { + while (*str) { + if (*str == c) return (char*)str; + str++; + } + return NULL; +} + +char *strcpy(char *dest, const char *src) { + memcpy(dest, src, strlen(src) + 1); + return (char*)src; +} + +char *strdup(const char *src) { + char* ret = malloc(strlen(src) + 1); + if (ret == NULL) return ret; + strcpy(ret, src); + return ret; +} + +char *strcat(char *dest, const char *src) { + char *dest2 = dest; + dest2 += strlen(dest) - 1; + while (*src) { + *dest2 = *src; + src++; + dest2++; + } + *dest2 = 0; + return dest; +} + +int strcmp(const char *s1, const char *s2) { + while ((*s1) && (*s1 == *s2)) { + s1++; + s2++; + } + return (* (unsigned char*)s1 - *(unsigned char*)s2); +} + void *memcpy(void *vd, const void *vs, int count) { uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; uint32_t f = count % 4, n = count / 4, i; -- cgit v1.2.3