1 // string and memory functions.
3 // This software is copyright (c) 2007 Scott Wood <scott@buserror.net>.
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
18 void *memcpy(void *dest, const void *src, size_t len)
24 for (i = 0; i < len; i++)
30 void *memmove(void *dest, const void *src, size_t len)
37 return memcpy(dest, src, len);
39 for (i = len - 1; i >= 0; i--)
45 int memcmp(const void *b1, const void *b2, size_t len)
51 for (pos = 0; pos < len; pos++) {
52 if (c1[pos] != c2[pos])
53 return c1[pos] - c2[pos];
61 void *memset(void *b, int ch, size_t len)
71 size_t strnlen(const char *s, size_t n)
75 while (pos < n && *s++)
81 size_t strlen(const char *s)
91 char *strcpy(char *dest, const char *src)
102 char *strncpy(char *dest, const char *src, size_t len)
113 memset(dest, 0, len);
117 char *strcat(char *dest, const char *src)
120 dest += strlen(dest);
129 char *strncat(char *dest, const char *src, size_t len)
132 int orig_len = strlen(dest);
144 memset(dest, 0, len);
148 int strcmp(const char *s1, const char *s2)
150 while (*s1 && *s2 && *s1 == *s2) {
158 int strncmp(const char *s1, const char *s2, int n)
162 while (i < n && s1[i] && s2[i] && s1[i] == s2[i])
171 char *strchr(const char *s, int c)
173 while (*s && *s != c)