diff options
Diffstat (limited to 'lib/libc/string')
-rw-r--r-- | lib/libc/string/memchr.c | 11 | ||||
-rw-r--r-- | lib/libc/string/memcmp.c | 15 | ||||
-rw-r--r-- | lib/libc/string/memcpy.c | 11 | ||||
-rw-r--r-- | lib/libc/string/memmove.c | 28 | ||||
-rw-r--r-- | lib/libc/string/memset.c | 14 | ||||
-rw-r--r-- | lib/libc/string/strchr.c | 20 |
6 files changed, 99 insertions, 0 deletions
diff --git a/lib/libc/string/memchr.c b/lib/libc/string/memchr.c new file mode 100644 index 0000000..9da3265 --- /dev/null +++ b/lib/libc/string/memchr.c @@ -0,0 +1,11 @@ +#include <string.h> + +void *memchr(const void *s, int c, size_t n) { + const char *p = s; + while (n--) { + if ((char)c == *p++) { + return (void*)(p-1); + } + } + return NULL; +} diff --git a/lib/libc/string/memcmp.c b/lib/libc/string/memcmp.c new file mode 100644 index 0000000..a3d47fb --- /dev/null +++ b/lib/libc/string/memcmp.c @@ -0,0 +1,15 @@ +#include <string.h> + +int +memcmp(const void *s1, const void *s2, size_t n) +{ + const char *str1 = (const char *)s1; + const char *str2 = (const char *)s2; + while (n--) { + if (*str1 != *str2) + return *str1 - *str2; + str1++; str2++; + } + return 0; +} + diff --git a/lib/libc/string/memcpy.c b/lib/libc/string/memcpy.c new file mode 100644 index 0000000..7573c01 --- /dev/null +++ b/lib/libc/string/memcpy.c @@ -0,0 +1,11 @@ +#include <string.h> + +void *memcpy(void *__restrict s1, const void *__restrict s2, size_t n) +{ + char *tmp = s1; + const char *s = s2; + + while (n--) + *tmp++ = *s++; + return s1; +} diff --git a/lib/libc/string/memmove.c b/lib/libc/string/memmove.c new file mode 100644 index 0000000..16be329 --- /dev/null +++ b/lib/libc/string/memmove.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * linux/lib/string.c + * + * Copyright (C) 1991, 1992 Linus Torvalds + */ +#include <string.h> + +void *memmove(void *dest, const void *src, size_t count) +{ + char *tmp; + const char *s; + + if (dest <= src) { + tmp = dest; + s = src; + while (count--) + *tmp++ = *s++; + } else { + tmp = dest; + tmp += count; + s = src; + s += count; + while (count--) + *--tmp = *--s; + } + return dest; +} diff --git a/lib/libc/string/memset.c b/lib/libc/string/memset.c new file mode 100644 index 0000000..59e3bea --- /dev/null +++ b/lib/libc/string/memset.c @@ -0,0 +1,14 @@ +#include <stddef.h> +#include <string.h> + +void * +memset(void *s, int c, size_t n) +{ + char *xs = s; + + while (n--) + *xs++ = c; + + return s; +} + diff --git a/lib/libc/string/strchr.c b/lib/libc/string/strchr.c new file mode 100644 index 0000000..09b4d43 --- /dev/null +++ b/lib/libc/string/strchr.c @@ -0,0 +1,20 @@ +#include <string.h> + +char *strchr(const char *s, int c) { + do { + if ((char)c == *s) { + return (char*)s; + } + } while (*s++ != '\0'); + return NULL; +} + +char *strrchr(const char *s, int c) +{ + const char *last = NULL; + do { + if (*s == (char)c) + last = s; + } while (*s++); + return (char *)last; +} |