OrionLibC
Barry Importing existing Orion LibC 03048a9 (2 years, 2 months ago)diff --git a/string/memcmp.c b/string/memcmp.c new file mode 100644 index 0000000..e4c18fc --- /dev/null +++ b/string/memcmp.c @@ -0,0 +1,13 @@ +#include <stddef.h> + +/* Copy one region of memory to another */ +int +memcmp(void *s1, void *s2, size_t n) +{ + unsigned char *a = (unsigned char *) s1, + *b = (unsigned char *) s2; + while (n-- > 0) + if (*a++ != *b++) + return a[-1] - b[-1]; + return 0; +} diff --git a/string/memcpy.c b/string/memcpy.c new file mode 100644 index 0000000..4988f38 --- /dev/null +++ b/string/memcpy.c @@ -0,0 +1,12 @@ +#include <stddef.h> + +/* Copy one region of memory to another */ +void * +memcpy(void *dest, void *src, size_t n) +{ + unsigned char *a = (unsigned char *) dest, + *b = (unsigned char *) src; + while (n-- > 0) + *a++ = *b++; + return dest; +} diff --git a/string/memset.c b/string/memset.c new file mode 100644 index 0000000..f9c0902 --- /dev/null +++ b/string/memset.c @@ -0,0 +1,13 @@ +#include <stddef.h> + +/* Fill a region of memory with specified byte */ +void * +memset(void *dest, int byte, size_t len) +{ + unsigned char *a = dest; + if (len > 0) { + while (len-- > 0) + *a++ = byte; + } + return dest; +} diff --git a/string/strcmp.c b/string/strcmp.c new file mode 100644 index 0000000..37b62a4 --- /dev/null +++ b/string/strcmp.c @@ -0,0 +1,18 @@ +#include <stddef.h> + +/* Compare two strings */ +int +strcmp(char *s1, char *s2) +{ + for (; *s1 == *s2 && *s1 && *s2; s1++, s2++); + return *(unsigned char *) s1 - *(unsigned char *) s2; +} + +/* Compare two limited strings */ +int +strncmp(char *s1, char *s2, size_t n) +{ + if (!n--) return 0; + for (; *s1 == *s2 && *s1 && *s2 && n; s1++, s2++, n--); + return *(unsigned char *) s1 - *(unsigned char *) s2; +} diff --git a/string/strcpy.c b/string/strcpy.c new file mode 100644 index 0000000..ce725a2 --- /dev/null +++ b/string/strcpy.c @@ -0,0 +1,23 @@ +#include <stddef.h> + +/* Copy a string */ +char * +strcpy(char *dest, const char *src) +{ + char *ret = dest; + while (*src) + *dest++ = *src++; + *dest = '\0'; + return ret; +} + +/* Copy a limited string */ +char * +strncpy(char *dest, const char *src, size_t n) +{ + char *ret = dest; + while (*src && n--) + *dest++ = *src++; + *dest = '\0'; + return ret; +} diff --git a/string/strlen.c b/string/strlen.c new file mode 100644 index 0000000..a8d506c --- /dev/null +++ b/string/strlen.c @@ -0,0 +1,12 @@ +#include <string.h> + +/* Find length of string */ +size_t +strlen(const char *str) +{ + if (!str) + return 0; + size_t i; + for (i = 0; str[i]; i++); + return i; +}