From a02dca9e41fd8d41990fc2e341ad1f1566fac0e7 Mon Sep 17 00:00:00 2001 From: TomAwezome Date: Thu, 8 Sep 2022 18:47:59 -0400 Subject: [PATCH] Add zealbooter library files for strcpy, strncpy, strcmp, strncmp, strlen. --- zealbooter/lib.h | 5 +++++ zealbooter/lib/strcmp.c | 19 +++++++++++++++++++ zealbooter/lib/strcmp.h | 6 ++++++ zealbooter/lib/strcpy.c | 19 +++++++++++++++++++ zealbooter/lib/strcpy.h | 6 ++++++ zealbooter/lib/strlen.c | 19 +++++++++++++++++++ zealbooter/lib/strlen.h | 6 ++++++ zealbooter/lib/strncmp.c | 26 ++++++++++++++++++++++++++ zealbooter/lib/strncmp.h | 6 ++++++ zealbooter/lib/strncpy.c | 31 +++++++++++++++++++++++++++++++ zealbooter/lib/strncpy.h | 6 ++++++ 11 files changed, 149 insertions(+) create mode 100644 zealbooter/lib/strcmp.c create mode 100644 zealbooter/lib/strcmp.h create mode 100644 zealbooter/lib/strcpy.c create mode 100644 zealbooter/lib/strcpy.h create mode 100644 zealbooter/lib/strlen.c create mode 100644 zealbooter/lib/strlen.h create mode 100644 zealbooter/lib/strncmp.c create mode 100644 zealbooter/lib/strncmp.h create mode 100644 zealbooter/lib/strncpy.c create mode 100644 zealbooter/lib/strncpy.h diff --git a/zealbooter/lib.h b/zealbooter/lib.h index 4092ce5e..b8b407fb 100644 --- a/zealbooter/lib.h +++ b/zealbooter/lib.h @@ -6,6 +6,11 @@ #include #include #include +#include +#include +#include +#include +#include #include uint64_t div_roundup_u64(uint64_t a, uint64_t b); diff --git a/zealbooter/lib/strcmp.c b/zealbooter/lib/strcmp.c new file mode 100644 index 00000000..6451e1d5 --- /dev/null +++ b/zealbooter/lib/strcmp.c @@ -0,0 +1,19 @@ +/* strcmp( const char *, const char * ) + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +int strcmp(const char *s1, const char *s2) +{ + while ((*s1) && (*s1 == *s2)) + { + ++s1; + ++s2; + } + + return (*(unsigned char *)s1 - *(unsigned char *)s2); +} + + diff --git a/zealbooter/lib/strcmp.h b/zealbooter/lib/strcmp.h new file mode 100644 index 00000000..8343f792 --- /dev/null +++ b/zealbooter/lib/strcmp.h @@ -0,0 +1,6 @@ +#ifndef __STRCMP_H__ +#define __STRCMP_H__ + +int strcmp(const char *s1, const char *s2); + +#endif // __STRCMP_H__ diff --git a/zealbooter/lib/strcpy.c b/zealbooter/lib/strcpy.c new file mode 100644 index 00000000..1f2d59ac --- /dev/null +++ b/zealbooter/lib/strcpy.c @@ -0,0 +1,19 @@ +/* strcpy( char *, const char * ) + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +char *strcpy(char *s1, const char *s2) +{ + char *rc = s1; + + while ((*s1++ = *s2++)) + { + /* EMPTY */ + } + + return rc; +} + diff --git a/zealbooter/lib/strcpy.h b/zealbooter/lib/strcpy.h new file mode 100644 index 00000000..6ae7ea87 --- /dev/null +++ b/zealbooter/lib/strcpy.h @@ -0,0 +1,6 @@ +#ifndef __STRCPY_H__ +#define __STRCPY_H__ + +char *strcpy(char *s1, const char *s2); + +#endif // __STRCPY_H__ diff --git a/zealbooter/lib/strlen.c b/zealbooter/lib/strlen.c new file mode 100644 index 00000000..142a435d --- /dev/null +++ b/zealbooter/lib/strlen.c @@ -0,0 +1,19 @@ +/* strlen( const char * ) + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +size_t strlen(const char *s) +{ + size_t rc = 0; + + while (s[rc]) + { + ++rc; + } + + return rc; +} + diff --git a/zealbooter/lib/strlen.h b/zealbooter/lib/strlen.h new file mode 100644 index 00000000..859589bd --- /dev/null +++ b/zealbooter/lib/strlen.h @@ -0,0 +1,6 @@ +#ifndef __STRLEN_H__ +#define __STRLEN_H__ + +size_t strlen(const char *s); + +#endif // __STRLEN_H__ diff --git a/zealbooter/lib/strncmp.c b/zealbooter/lib/strncmp.c new file mode 100644 index 00000000..e330938e --- /dev/null +++ b/zealbooter/lib/strncmp.c @@ -0,0 +1,26 @@ +/* strncmp( const char *, const char *, size_t ) + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +int strncmp(const char *s1, const char *s2, size_t n) +{ + while (n && *s1 && (*s1 == *s2)) + { + ++s1; + ++s2; + --n; + } + + if (n == 0) + { + return 0; + } + else + { + return (*(unsigned char *)s1 - *(unsigned char *)s2); + } +} + diff --git a/zealbooter/lib/strncmp.h b/zealbooter/lib/strncmp.h new file mode 100644 index 00000000..8c37ccac --- /dev/null +++ b/zealbooter/lib/strncmp.h @@ -0,0 +1,6 @@ +#ifndef __STRNCMP_H__ +#define __STRNCMP_H__ + +int strncmp(const char *s1, const char *s2, size_t n); + +#endif // __STRNCMP_H__ diff --git a/zealbooter/lib/strncpy.c b/zealbooter/lib/strncpy.c new file mode 100644 index 00000000..45248289 --- /dev/null +++ b/zealbooter/lib/strncpy.c @@ -0,0 +1,31 @@ +/* strncpy( char *, const char *, size_t ) + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + + +char *strncpy(char *s1, const char *s2, size_t n) +{ + char *rc = s1; + + while (n && (*s1++ = *s2++)) + { + /* Cannot do "n--" in the conditional as size_t is unsigned and we have + to check it again for >0 in the next loop below, so we must not risk + underflow. + */ + --n; + } + + /* Checking against 1 as we missed the last --n in the loop above. */ + while (n-- > 1) + { + *s1++ = '\0'; + } + + return rc; +} + + diff --git a/zealbooter/lib/strncpy.h b/zealbooter/lib/strncpy.h new file mode 100644 index 00000000..fbaf449f --- /dev/null +++ b/zealbooter/lib/strncpy.h @@ -0,0 +1,6 @@ +#ifndef __STRNCPY_H__ +#define __STRNCPY_H__ + +char *strncpy(char *s1, const char *s2, size_t n); + +#endif // __STRNCPY_H__