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];
59 void *memset(void *b, int ch, size_t len)
69 size_t strnlen(const char *s, size_t n)
73 while (pos < n && *s++)
79 size_t strlen(const char *s)
89 char *strcpy(char *dest, const char *src)
100 char *strncpy(char *dest, const char *src, size_t len)
111 memset(dest, 0, len);
115 char *strcat(char *dest, const char *src)
118 dest += strlen(dest);
127 char *strncat(char *dest, const char *src, size_t len)
130 int orig_len = strlen(dest);
142 memset(dest, 0, len);
146 int strcmp(const char *s1, const char *s2)
148 while (*s1 && *s2 && *s1 == *s2) {
156 int strncmp(const char *s1, const char *s2, int n)
160 while (i < n && s1[i] && s2[i] && s1[i] == s2[i])
169 char *strchr(const char *s, int c)
171 while (*s && *s != c)