]> git.buserror.net Git - polintos/scott/priv.git/blob - lib/c/freestanding/string.c
Fix stupid memcmp bug
[polintos/scott/priv.git] / lib / c / freestanding / string.c
1 // string and memory functions.
2 //
3 // This software is copyright (c) 2007 Scott Wood <scott@buserror.net>.
4 // 
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.
8 // 
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.
14
15 #include <stdint.h>
16 #include <string.h>
17
18 void *memcpy(void *dest, const void *src, size_t len)
19 {
20         const char *cs = src;
21         char *cd = dest;
22         size_t i;
23
24         for (i = 0; i < len; i++)
25                 cd[i] = cs[i];
26
27         return dest;
28 }
29
30 void *memmove(void *dest, const void *src, size_t len)
31 {
32         const char *cs = src;
33         char *cd = dest;
34         size_t i;
35
36         if (dest < src)
37                 return memcpy(dest, src, len);
38
39         for (i = len - 1; i >= 0; i--)
40                 cd[i] = cs[i];
41
42         return dest;
43 }
44
45 int memcmp(const void *b1, const void *b2, size_t len)
46 {
47         size_t pos;
48         const char *c1 = b1;
49         const char *c2 = b2;
50         
51         for (pos = 0; pos < len; pos++) {
52                 if (c1[pos] != c2[pos])
53                         return c1[pos] - c2[pos];
54         }
55         
56         return 0;
57 }
58
59 void *memset(void *b, int ch, size_t len)
60 {
61         char *c = b;
62         
63         while (len--)
64                 *c++ = ch;
65
66         return b;
67 }
68
69 size_t strnlen(const char *s, size_t n)
70 {
71         size_t pos = 0;
72
73         while (pos < n && *s++)
74                 pos++;
75
76         return pos;
77 }
78
79 size_t strlen(const char *s)
80 {
81         size_t pos = 0;
82
83         while (*s++)
84                 pos++;
85
86         return pos;
87 }
88
89 char *strcpy(char *dest, const char *src)
90 {
91         char *orig = dest;
92
93         do {
94                 *dest = *src++;
95         } while (*dest++);
96
97         return orig;
98 }
99
100 char *strncpy(char *dest, const char *src, size_t len)
101 {
102         char *orig = dest;
103
104         while (len--) {
105                 *dest = *src++;
106                 
107                 if (!*dest++)
108                         break;
109         }
110         
111         memset(dest, 0, len);
112         return orig;
113 }
114
115 char *strcat(char *dest, const char *src)
116 {
117         char *orig = dest;
118         dest += strlen(dest);
119
120         do {
121                 *dest = *src++;
122         } while (*dest++);
123
124         return orig;
125 }
126
127 char *strncat(char *dest, const char *src, size_t len)
128 {
129         char *orig = dest;
130         int orig_len = strlen(dest);
131         
132         len -= orig_len;
133         dest += orig_len;
134
135         while (len--) {
136                 *dest = *src++;
137                 
138                 if (!*dest++)
139                         break;
140         }
141         
142         memset(dest, 0, len);
143         return orig;
144 }
145
146 int strcmp(const char *s1, const char *s2)
147 {
148         while (*s1 && *s2 && *s1 == *s2) {
149                 s1++;
150                 s2++;
151         }
152
153         return *s2 - *s1;
154 }
155
156 int strncmp(const char *s1, const char *s2, int n)
157 {
158         int i = 0;
159
160         while (i < n && s1[i] && s2[i] && s1[i] == s2[i])
161                 i++;
162
163         if (i == n)
164                 return 0;
165
166         return *s2 - *s1;
167 }
168
169 char *strchr(const char *s, int c)
170 {
171         while (*s && *s != c)
172                 s++;
173
174         if (*s == c) 
175                 return (char *)s;
176
177         return NULL;
178 }