From eff82c45c589b42061344039d5f2efc8ad7c52df Mon Sep 17 00:00:00 2001 From: stefan Date: Fri, 21 Apr 2023 19:53:04 -0400 Subject: libfdt port and the required libc functions --- lib/libc/string/memchr.c | 11 +++++++++++ lib/libc/string/memcmp.c | 15 +++++++++++++++ lib/libc/string/memcpy.c | 11 +++++++++++ lib/libc/string/memmove.c | 28 ++++++++++++++++++++++++++++ lib/libc/string/memset.c | 14 ++++++++++++++ lib/libc/string/strchr.c | 20 ++++++++++++++++++++ 6 files changed, 99 insertions(+) create mode 100644 lib/libc/string/memchr.c create mode 100644 lib/libc/string/memcmp.c create mode 100644 lib/libc/string/memcpy.c create mode 100644 lib/libc/string/memmove.c create mode 100644 lib/libc/string/memset.c create mode 100644 lib/libc/string/strchr.c (limited to 'lib/libc/string') 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 + +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 + +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 + +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 + +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 +#include + +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 + +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; +} -- cgit v1.2.3