]> git.buserror.net Git - polintos/scott/priv.git/blob - lib/c/freestanding/string.c
0241f91b5c10176ef2fb8f54fc3b179e16717699
[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                 pos++;
56         }
57         
58         return 0;
59 }
60
61 void *memset(void *b, int ch, size_t len)
62 {
63         char *c = b;
64         
65         while (len--)
66                 *c++ = ch;
67
68         return b;
69 }
70
71 size_t strnlen(const char *s, size_t n)
72 {
73         size_t pos = 0;
74
75         while (pos < n && *s++)
76                 pos++;
77
78         return pos;
79 }
80
81 size_t strlen(const char *s)
82 {
83         size_t pos = 0;
84
85         while (*s++)
86                 pos++;
87
88         return pos;
89 }
90
91 char *strcpy(char *dest, const char *src)
92 {
93         char *orig = dest;
94
95         do {
96                 *dest = *src++;
97         } while (*dest++);
98
99         return orig;
100 }
101
102 char *strncpy(char *dest, const char *src, size_t len)
103 {
104         char *orig = dest;
105
106         while (len--) {
107                 *dest = *src++;
108                 
109                 if (!*dest++)
110                         break;
111         }
112         
113         memset(dest, 0, len);
114         return orig;
115 }
116
117 char *strcat(char *dest, const char *src)
118 {
119         char *orig = dest;
120         dest += strlen(dest);
121
122         do {
123                 *dest = *src++;
124         } while (*dest++);
125
126         return orig;
127 }
128
129 char *strncat(char *dest, const char *src, size_t len)
130 {
131         char *orig = dest;
132         int orig_len = strlen(dest);
133         
134         len -= orig_len;
135         dest += orig_len;
136
137         while (len--) {
138                 *dest = *src++;
139                 
140                 if (!*dest++)
141                         break;
142         }
143         
144         memset(dest, 0, len);
145         return orig;
146 }
147
148 int strcmp(const char *s1, const char *s2)
149 {
150         while (*s1 && *s2 && *s1 == *s2) {
151                 s1++;
152                 s2++;
153         }
154
155         return *s2 - *s1;
156 }
157
158 int strncmp(const char *s1, const char *s2, int n)
159 {
160         int i = 0;
161
162         while (i < n && s1[i] && s2[i] && s1[i] == s2[i])
163                 i++;
164
165         if (i == n)
166                 return 0;
167
168         return *s2 - *s1;
169 }
170
171 char *strchr(const char *s, int c)
172 {
173         while (*s && *s != c)
174                 s++;
175
176         if (*s == c) 
177                 return (char *)s;
178
179         return NULL;
180 }