From d257cf29744f732c4d9b1a741a26164940e87b90 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 5 Feb 2011 14:11:16 -0800 Subject: Adding a few more libc's string functions. --- libc/LIB.status | 23 ++++++----- libc/include/malloc.h | 8 ++-- libc/include/string.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ libc/src/malloc.c | 1 + os/src/init.c | 1 + 5 files changed, 122 insertions(+), 15 deletions(-) diff --git a/libc/LIB.status b/libc/LIB.status index 43cc8de..da42da4 100644 --- a/libc/LIB.status +++ b/libc/LIB.status @@ -94,16 +94,15 @@ fread - ok, inlined fwrite - ok, inlined fseek - ok, inlined ftell - ok, inlined -rewind - missing +rewind - ok, inlined fgetpos - missing fsetpos - missing clearerr - missing feof - ok, inlined ferror - missing -fileno - ok, inlined perror - missing -fileno - missing +fileno - ok, inlined pipe funcs - missing, can we make them ? lock funcs - missing, can we make them ? @@ -124,35 +123,35 @@ String: ------ memcpy - ok, inlined -memmove - missing +memmove - ok, inlined memset - ok, inlined memcmp - ok, inlined -memchr - missing +memchr - ok, inlined strcpy - ok, inlined strncpy - ok, inlined strcat - ok, inlined -strncat - missing -strcmp - missing -strncmp - missing +strncat - ok, inlined +strcmp - ok, inlined +strncmp - ok, inlined strcoll - missing strxfrm - missing -strdup - missing +strdup - ok, inlined strchr - ok, inlined -strrchr - missing +strrchr - ok, inlined strcspn - missing strspn - missing strpbrk - missing -strstr - missing +strstr - ok, inlined, dummy implementation (kmp eats too much stack space) strcasestr - missing strtok - missing strlen - ok, inlined -strerror - missing +strerror - missing, won't implement bzero - missing bcopy - missing diff --git a/libc/include/malloc.h b/libc/include/malloc.h index ee877a1..715c90b 100644 --- a/libc/include/malloc.h +++ b/libc/include/malloc.h @@ -3,7 +3,7 @@ #include #include -#include +#include typedef void * (*malloc_t)(size_t size); typedef void (*free_t)(void * ptr); @@ -18,8 +18,10 @@ extern free_t free; extern realloc_t realloc; static inline void * calloc(size_t nmemb, size_t size) { - void * r = malloc(nmemb * size); - memset(r, 0, nmemb * size); + uint8_t * r = malloc(nmemb * size); + size_t i; + for (i = 0; i < size; i++) + r[i] = 0; return r; } diff --git a/libc/include/string.h b/libc/include/string.h index cd58681..5505bed 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -3,6 +3,7 @@ #include #include +#include static inline void * memcpy(void * _s1, const void * _s2, size_t n) { uint8_t * s1 = (uint8_t *) _s1; @@ -15,6 +16,24 @@ static inline void * memcpy(void * _s1, const void * _s2, size_t n) { return _s1; } +static inline void * memmove(void * _s1, const void * _s2, size_t n) { + uint8_t * s1 = (uint8_t *) _s1; + const uint8_t * s2 = (uint8_t *) _s2; + size_t i; + + if (s1 < s2) { + for (i = 0; i < n; i++) + *s1++ = *s2++; + } else if (s1 > s2) { + s1 += n; + s2 += n; + for (i = 0; i < n; i++) + *--s1 = *--s2; + } + + return _s1; +} + static inline int memcmp(const void * _s1, const void * _s2, size_t n) { uint8_t * s1 = (uint8_t *) _s1; const uint8_t * s2 = (uint8_t *) _s2; @@ -41,6 +60,17 @@ static inline void * memset(void * _s, int c, size_t n) { return _s; } +static inline const void * memchr(const void * _s, int c, size_t n) { + const uint8_t * s = (uint8_t *) _s; + size_t i; + + for (i = 0; i < n; i++, s++) + if (*s == c) + return s; + + return NULL; +} + static inline char * strcat(char * s1, const char * s2) { char * r = s1; @@ -84,9 +114,22 @@ static inline const char * strchr(const char * s, char c) { return s; s++; } + return NULL; } +static inline const char * strrchr(const char * s, char c) { + const char * r = NULL; + + while (*s) { + if (*s == c) + r = s; + s++; + } + + return r; +} + static inline size_t strlen(const char * s) { size_t r = 0; @@ -96,4 +139,65 @@ static inline size_t strlen(const char * s) { return r; } +static inline char * strncat(char * s1, const char * s2, size_t n) { + char * r = s1; + + while (*s1) + s1++; + strncpy(s1, s2, n); + + return r; +} + +static inline int strcmp(const char * s1, const char * s2) { + while (*s1 && *s2) { + if (!*s1) { + return -1; + } else if (!*s2) { + return 1; + } else if (*s1 < *s2) { + return -1; + } else if (*s1 > *s2) { + return 1; + } + s1++; + s2++; + } + + return 0; +} + +static inline int strncmp(const char * s1, const char * s2, size_t n) { + while (*s1 && *s2 && n) { + if (!*s1) { + return -1; + } else if (!*s2) { + return 1; + } else if (*s1 < *s2) { + return -1; + } else if (*s1 > *s2) { + return 1; + } + s1++; + s2++; + n--; + } + + return 0; +} + +static inline char * strdup(const char * s) { + return strcpy((char *) malloc(strlen(s) + 1), s); +} + +static inline const char * strstr(const char * s1, const char * s2) { + size_t l = strlen(s2); + + while (*s1) { + if (!strncmp(s1, s2, l)) + return s1; + s1++; + } +} + #endif diff --git a/libc/src/malloc.c b/libc/src/malloc.c index fb6d954..2fae977 100644 --- a/libc/src/malloc.c +++ b/libc/src/malloc.c @@ -1,5 +1,6 @@ #include #include +#include "string.h" #include "malloc.h" void * sbrk(ptrdiff_t incr); diff --git a/os/src/init.c b/os/src/init.c index edbae0a..67f422a 100644 --- a/os/src/init.c +++ b/os/src/init.c @@ -1,5 +1,6 @@ #include #include +#include #include void __libc_init_array(); -- cgit v1.2.3