diff options
author | Alex Auvolat <alex.auvolat@ens.fr> | 2015-02-13 20:28:54 +0100 |
---|---|---|
committer | Alex Auvolat <alex.auvolat@ens.fr> | 2015-02-13 20:28:54 +0100 |
commit | cf0b8a52287ee7c747b1d5a7d77abdef1fb46f94 (patch) | |
tree | d06ada519003a0794d3107fea1e882b737b5a00c /src/common/libc | |
parent | e484c92ff08e54e7cbfdb815a5b254507dade003 (diff) | |
download | kogata-cf0b8a52287ee7c747b1d5a7d77abdef1fb46f94.tar.gz kogata-cf0b8a52287ee7c747b1d5a7d77abdef1fb46f94.zip |
Reorganize code in preparation for user apps.
Diffstat (limited to 'src/common/libc')
-rw-r--r-- | src/common/libc/Makefile | 11 | ||||
-rw-r--r-- | src/common/libc/printf.c | 120 | ||||
-rw-r--r-- | src/common/libc/string.c | 126 |
3 files changed, 257 insertions, 0 deletions
diff --git a/src/common/libc/Makefile b/src/common/libc/Makefile new file mode 100644 index 0000000..54fa9f1 --- /dev/null +++ b/src/common/libc/Makefile @@ -0,0 +1,11 @@ +OBJ = string.o printf.o + +LIB = + +CFLAGS = -I ../include + +LDFLAGS = + +OUT = libc.lib + +include ../../rules.make diff --git a/src/common/libc/printf.c b/src/common/libc/printf.c new file mode 100644 index 0000000..68e08d8 --- /dev/null +++ b/src/common/libc/printf.c @@ -0,0 +1,120 @@ +#include <printf.h> +#include <stdarg.h> + +int snprintf(char * buff, size_t len, const char *format, ...) { + va_list ap; + + va_start(ap, format); + len = vsnprintf(buff, len, format, ap); + va_end(ap); + + return len; +} + +int vsnprintf(char *buff, size_t len, const char* format, va_list ap){ + size_t i, result; + + if (!buff || !format) + return -1; + +#define PUTCHAR(thechar) \ + do { \ + if (result < len-1) \ + *buff++ = (thechar); \ + result++; \ + } while (0) + + result = 0; + for(i = 0; format[i] != '\0' ; i++) { + if (format[i] == '%') { + i++; + switch(format[i]) { + case '%': + PUTCHAR('%'); + break; + + case 'i':; + case 'd': { + int integer = va_arg(ap,int); + int cpt2 = 0; + char buff_int[16]; + + if (integer<0) + PUTCHAR('-'); + + do { + int m10 = integer%10; + m10 = (m10 < 0)? -m10:m10; + buff_int[cpt2++] = (char)('0'+ m10); + integer = integer/10; + } while(integer != 0); + + for(cpt2 = cpt2 - 1; cpt2 >= 0; cpt2--) + PUTCHAR(buff_int[cpt2]); + + break; + } + case 'c': { + int value = va_arg(ap,int); + PUTCHAR((char)value); + break; + } + case 's': { + char *string = va_arg(ap,char *); + if (!string) + string = "(null)"; + for(; *string != '\0' ; string++) + PUTCHAR(*string); + break; + } + case 'x': { + unsigned int hexa = va_arg(ap,int); + unsigned int nb; + int j, had_nonzero = 0; + for(j = 0; j < 8; j++) { + nb = (unsigned int)(hexa << (j*4)); + nb = (nb >> 28) & 0xf; + // Skip the leading zeros + if (nb == 0) { + if (had_nonzero) + PUTCHAR('0'); + } else { + had_nonzero = 1; + if (nb < 10) + PUTCHAR('0'+nb); + else + PUTCHAR('a'+(nb-10)); + } + } + if (!had_nonzero) + PUTCHAR('0'); + break; + } + case 'p': { + unsigned int hexa = va_arg(ap,int); + unsigned int nb; + int j; + for (j = 0; j < 8; j++) { + nb = (unsigned int)(hexa << (j*4)); + nb = (nb >> 28) & 0xf; + if (nb < 10) + PUTCHAR('0'+nb); + else + PUTCHAR('a'+(nb-10)); + } + break; + } + default: + PUTCHAR('%'); + PUTCHAR(format[i]); + } + } else { + PUTCHAR(format[i]); + } + } + + *buff = '\0'; + return result; +} + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/common/libc/string.c b/src/common/libc/string.c new file mode 100644 index 0000000..f6c27b4 --- /dev/null +++ b/src/common/libc/string.c @@ -0,0 +1,126 @@ +#include <string.h> +#include <malloc.h> + + +size_t strlen(const char* str) { + size_t ret = 0; + while (str[ret] != 0) + ret++; + return ret; +} + +char *strchr(const char *str, char c) { + do { + if (*str == c) return (char*)str; + } while (*(str++)); + return NULL; +} + +char *strrchr(const char *str, char c) { + char* ret = NULL; + do { + if (*str == c) ret = (char*)str; + } while (*(str++)); + return ret; +} + +char *strcpy(char *dest, const char *src) { + memcpy(dest, src, strlen(src) + 1); + return (char*)dest; +} + +char *strncpy(char *dest, const char *src, size_t n) { + size_t x = strlen(src + 1); + if (n < x) x = n; + memcpy(dest, src, x); + if (n > x) memset(dest + n, 0, n - x); + return (char*)dest; +} + +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); +} + +int strncmp(const char *s1, const char *s2, size_t n) { + size_t i = 0; + while ((*s1) && (*s1 == *s2) && i != n) { + s1++; + s2++; + i++; + } + return (*(unsigned char*)s1 - *(unsigned char*)s2); +} + +void *memcpy(void *vd, const void *vs, size_t count) { + uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; + size_t f = count % 4, n = count / 4, i; + const uint32_t* s = (uint32_t*)src; + uint32_t* d = (uint32_t*)dest; + for (i = 0; i < n; i++) { + d[i] = s[i]; + } + if (f != 0) { + for (i = count - f; i < count; i++) { + dest[i] = src[i]; + } + } + return vd; +} + +void *memmove(void *vd, const void *vs, size_t count) { + uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; + + if (vd < vs) { + for (size_t i = 0; i < count; i++) + dest[i] = src[i]; + } else { + for (size_t i = 0; i < count; i++) + dest[count - i] = src[count - i]; + } + return vd; +} + +int memcmp(const void *va, const void *vb, size_t count) { + uint8_t *a = (uint8_t*)va; + uint8_t *b = (uint8_t*)vb; + for (size_t i = 0; i < count; i++) { + if (a[i] != b[i]) return (int)a[i] - (int)b[i]; + } + return 0; +} + +void *memset(void *dest, int val, size_t count) { + uint8_t *dest_c = (uint8_t*)dest; + for (size_t i = 0; i < count; i++) { + dest_c[i] = val; + } + return dest; +} + +char *strdup(const char* str) { + int len = strlen(str) + 1; + + char* ret = (char*)malloc(len); + if (ret == 0) return 0; + + memcpy(ret, str, len); + return ret; +} + +/* vim: set ts=4 sw=4 tw=0 noet :*/ |