blob: 954fb01e209790f759e34494bcc176054cc5cc1e [file] [log] [blame]
wdenkc7de8292002-11-19 11:04:11 +00001/*
2 * linux/lib/string.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * stupid library routines.. The optimized versions should generally be found
9 * as inline code in <asm-xx/string.h>
10 *
11 * These are buggy as well..
12 */
13
14#include <linux/types.h>
15#include <linux/string.h>
16#include <malloc.h>
17
18#define __HAVE_ARCH_BCOPY
19#define __HAVE_ARCH_MEMCMP
20#define __HAVE_ARCH_MEMCPY
21#define __HAVE_ARCH_MEMMOVE
22#define __HAVE_ARCH_MEMSET
23#define __HAVE_ARCH_STRCAT
24#define __HAVE_ARCH_STRCMP
25#define __HAVE_ARCH_STRCPY
26#define __HAVE_ARCH_STRLEN
27#define __HAVE_ARCH_STRNCPY
28
29char * ___strtok = NULL;
30
31#ifndef __HAVE_ARCH_STRCPY
32char * strcpy(char * dest,const char *src)
33{
34 char *tmp = dest;
35
36 while ((*dest++ = *src++) != '\0')
37 /* nothing */;
38 return tmp;
39}
40#endif
41
42#ifndef __HAVE_ARCH_STRNCPY
43char * strncpy(char * dest,const char *src,size_t count)
44{
45 char *tmp = dest;
46
47 while (count-- && (*dest++ = *src++) != '\0')
48 /* nothing */;
49
50 return tmp;
51}
52#endif
53
54#ifndef __HAVE_ARCH_STRCAT
55char * strcat(char * dest, const char * src)
56{
57 char *tmp = dest;
58
59 while (*dest)
60 dest++;
61 while ((*dest++ = *src++) != '\0')
62 ;
63
64 return tmp;
65}
66#endif
67
68#ifndef __HAVE_ARCH_STRNCAT
69char * strncat(char *dest, const char *src, size_t count)
70{
71 char *tmp = dest;
72
73 if (count) {
74 while (*dest)
75 dest++;
76 while ((*dest++ = *src++)) {
77 if (--count == 0) {
78 *dest = '\0';
79 break;
80 }
81 }
82 }
83
84 return tmp;
85}
86#endif
87
88#ifndef __HAVE_ARCH_STRCMP
89int strcmp(const char * cs,const char * ct)
90{
91 register signed char __res;
92
93 while (1) {
94 if ((__res = *cs - *ct++) != 0 || !*cs++)
95 break;
96 }
97
98 return __res;
99}
100#endif
101
102#ifndef __HAVE_ARCH_STRNCMP
103int strncmp(const char * cs,const char * ct,size_t count)
104{
105 register signed char __res = 0;
106
107 while (count) {
108 if ((__res = *cs - *ct++) != 0 || !*cs++)
109 break;
110 count--;
111 }
112
113 return __res;
114}
115#endif
116
117#ifndef __HAVE_ARCH_STRCHR
118char * strchr(const char * s, int c)
119{
120 for(; *s != (char) c; ++s)
121 if (*s == '\0')
122 return NULL;
123 return (char *) s;
124}
125#endif
126
127#ifndef __HAVE_ARCH_STRRCHR
128char * strrchr(const char * s, int c)
129{
130 const char *p = s + strlen(s);
131 do {
wdenk8bde7f72003-06-27 21:31:46 +0000132 if (*p == (char)c)
133 return (char *)p;
wdenkc7de8292002-11-19 11:04:11 +0000134 } while (--p >= s);
135 return NULL;
136}
137#endif
138
139#ifndef __HAVE_ARCH_STRLEN
140size_t strlen(const char * s)
141{
142 const char *sc;
143
144 for (sc = s; *sc != '\0'; ++sc)
145 /* nothing */;
146 return sc - s;
147}
148#endif
149
150#ifndef __HAVE_ARCH_STRNLEN
151size_t strnlen(const char * s, size_t count)
152{
153 const char *sc;
154
155 for (sc = s; count-- && *sc != '\0'; ++sc)
156 /* nothing */;
157 return sc - s;
158}
159#endif
160
161#ifndef __HAVE_ARCH_STRDUP
162char * strdup(const char *s)
163{
164 char *new;
165
166 if ((s == NULL) ||
wdenk27b207f2003-07-24 23:38:38 +0000167 ((new = malloc (strlen(s) + 1)) == NULL) ) {
wdenkc7de8292002-11-19 11:04:11 +0000168 return NULL;
169 }
170
171 strcpy (new, s);
172 return new;
173}
174#endif
175
176#ifndef __HAVE_ARCH_STRSPN
177size_t strspn(const char *s, const char *accept)
178{
179 const char *p;
180 const char *a;
181 size_t count = 0;
182
183 for (p = s; *p != '\0'; ++p) {
184 for (a = accept; *a != '\0'; ++a) {
185 if (*p == *a)
186 break;
187 }
188 if (*a == '\0')
189 return count;
190 ++count;
191 }
192
193 return count;
194}
195#endif
196
197#ifndef __HAVE_ARCH_STRPBRK
198char * strpbrk(const char * cs,const char * ct)
199{
200 const char *sc1,*sc2;
201
202 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
203 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
204 if (*sc1 == *sc2)
205 return (char *) sc1;
206 }
207 }
208 return NULL;
209}
210#endif
211
212#ifndef __HAVE_ARCH_STRTOK
213char * strtok(char * s,const char * ct)
214{
215 char *sbegin, *send;
216
217 sbegin = s ? s : ___strtok;
218 if (!sbegin) {
219 return NULL;
220 }
221 sbegin += strspn(sbegin,ct);
222 if (*sbegin == '\0') {
223 ___strtok = NULL;
224 return( NULL );
225 }
226 send = strpbrk( sbegin, ct);
227 if (send && *send != '\0')
228 *send++ = '\0';
229 ___strtok = send;
230 return (sbegin);
231}
232#endif
233
234#ifndef __HAVE_ARCH_MEMSET
235void * memset(void * s,char c,size_t count)
236{
237 char *xs = (char *) s;
238
239 while (count--)
240 *xs++ = c;
241
242 return s;
243}
244#endif
245
246#ifndef __HAVE_ARCH_BCOPY
247char * bcopy(const char * src, char * dest, int count)
248{
249 char *tmp = dest;
250
251 while (count--)
252 *tmp++ = *src++;
253
254 return dest;
255}
256#endif
257
258#ifndef __HAVE_ARCH_MEMCPY
259void * memcpy(void * dest,const void *src,size_t count)
260{
261 char *tmp = (char *) dest, *s = (char *) src;
262
263 while (count--)
264 *tmp++ = *s++;
265
266 return dest;
267}
268#endif
269
270#ifndef __HAVE_ARCH_MEMMOVE
271void * memmove(void * dest,const void *src,size_t count)
272{
273 char *tmp, *s;
274
275 if (dest <= src) {
276 tmp = (char *) dest;
277 s = (char *) src;
278 while (count--)
279 *tmp++ = *s++;
280 }
281 else {
282 tmp = (char *) dest + count;
283 s = (char *) src + count;
284 while (count--)
285 *--tmp = *--s;
286 }
287
288 return dest;
289}
290#endif
291
292#ifndef __HAVE_ARCH_MEMCMP
293int memcmp(const void * cs,const void * ct,size_t count)
294{
295 const unsigned char *su1, *su2;
296 signed char res = 0;
297
298 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
299 if ((res = *su1 - *su2) != 0)
300 break;
301 return res;
302}
303#endif
304
305/*
306 * find the first occurrence of byte 'c', or 1 past the area if none
307 */
308#ifndef __HAVE_ARCH_MEMSCAN
309void * memscan(void * addr, int c, size_t size)
310{
311 unsigned char * p = (unsigned char *) addr;
312
313 while (size) {
314 if (*p == c)
315 return (void *) p;
316 p++;
317 size--;
318 }
wdenk8bde7f72003-06-27 21:31:46 +0000319 return (void *) p;
wdenkc7de8292002-11-19 11:04:11 +0000320}
321#endif
322
323#ifndef __HAVE_ARCH_STRSTR
324char * strstr(const char * s1,const char * s2)
325{
326 int l1, l2;
327
328 l2 = strlen(s2);
329 if (!l2)
330 return (char *) s1;
331 l1 = strlen(s1);
332 while (l1 >= l2) {
333 l1--;
334 if (!memcmp(s1,s2,l2))
335 return (char *) s1;
336 s1++;
337 }
338 return NULL;
339}
340#endif