From Geert: Quickfix to get DDB compile.
[linux-2.6/linux-mips.git] / lib / string.c
blob71d65325b09adaf97a135d70d6c3510c1269ff20
1 /*
2 * linux/lib/string.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * stupid library routines.. The optimized versions should generally be found
9 * as inline code in <asm-xx/string.h>
11 * These are buggy as well..
13 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
14 * - Added strsep() which will replace strtok() soon (because strsep() is
15 * reentrant and should be faster). Use only strsep() in new code, please.
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
22 #ifndef __HAVE_ARCH_STRNICMP
23 int strnicmp(const char *s1, const char *s2, size_t len)
25 /* Yes, Virginia, it had better be unsigned */
26 unsigned char c1, c2;
28 c1 = 0; c2 = 0;
29 if (len) {
30 do {
31 c1 = *s1; c2 = *s2;
32 s1++; s2++;
33 if (!c1)
34 break;
35 if (!c2)
36 break;
37 if (c1 == c2)
38 continue;
39 c1 = tolower(c1);
40 c2 = tolower(c2);
41 if (c1 != c2)
42 break;
43 } while (--len);
45 return (int)c1 - (int)c2;
47 #endif
49 char * ___strtok;
51 #ifndef __HAVE_ARCH_STRCPY
52 char * strcpy(char * dest,const char *src)
54 char *tmp = dest;
56 while ((*dest++ = *src++) != '\0')
57 /* nothing */;
58 return tmp;
60 #endif
62 #ifndef __HAVE_ARCH_STRNCPY
63 char * strncpy(char * dest,const char *src,size_t count)
65 char *tmp = dest;
67 while (count-- && (*dest++ = *src++) != '\0')
68 /* nothing */;
70 return tmp;
72 #endif
74 #ifndef __HAVE_ARCH_STRCAT
75 char * strcat(char * dest, const char * src)
77 char *tmp = dest;
79 while (*dest)
80 dest++;
81 while ((*dest++ = *src++) != '\0')
84 return tmp;
86 #endif
88 #ifndef __HAVE_ARCH_STRNCAT
89 char * strncat(char *dest, const char *src, size_t count)
91 char *tmp = dest;
93 if (count) {
94 while (*dest)
95 dest++;
96 while ((*dest++ = *src++)) {
97 if (--count == 0) {
98 *dest = '\0';
99 break;
104 return tmp;
106 #endif
108 #ifndef __HAVE_ARCH_STRCMP
109 int strcmp(const char * cs,const char * ct)
111 register signed char __res;
113 while (1) {
114 if ((__res = *cs - *ct++) != 0 || !*cs++)
115 break;
118 return __res;
120 #endif
122 #ifndef __HAVE_ARCH_STRNCMP
123 int strncmp(const char * cs,const char * ct,size_t count)
125 register signed char __res = 0;
127 while (count) {
128 if ((__res = *cs - *ct++) != 0 || !*cs++)
129 break;
130 count--;
133 return __res;
135 #endif
137 #ifndef __HAVE_ARCH_STRCHR
138 char * strchr(const char * s, int c)
140 for(; *s != (char) c; ++s)
141 if (*s == '\0')
142 return NULL;
143 return (char *) s;
145 #endif
147 #ifndef __HAVE_ARCH_STRRCHR
148 char * strrchr(const char * s, int c)
150 const char *p = s + strlen(s);
151 do {
152 if (*p == (char)c)
153 return (char *)p;
154 } while (--p >= s);
155 return NULL;
157 #endif
159 #ifndef __HAVE_ARCH_STRLEN
160 size_t strlen(const char * s)
162 const char *sc;
164 for (sc = s; *sc != '\0'; ++sc)
165 /* nothing */;
166 return sc - s;
168 #endif
170 #ifndef __HAVE_ARCH_STRNLEN
171 size_t strnlen(const char * s, size_t count)
173 const char *sc;
175 for (sc = s; count-- && *sc != '\0'; ++sc)
176 /* nothing */;
177 return sc - s;
179 #endif
181 #ifndef __HAVE_ARCH_STRSPN
182 size_t strspn(const char *s, const char *accept)
184 const char *p;
185 const char *a;
186 size_t count = 0;
188 for (p = s; *p != '\0'; ++p) {
189 for (a = accept; *a != '\0'; ++a) {
190 if (*p == *a)
191 break;
193 if (*a == '\0')
194 return count;
195 ++count;
198 return count;
200 #endif
202 #ifndef __HAVE_ARCH_STRPBRK
203 char * strpbrk(const char * cs,const char * ct)
205 const char *sc1,*sc2;
207 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
208 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
209 if (*sc1 == *sc2)
210 return (char *) sc1;
213 return NULL;
215 #endif
217 #ifndef __HAVE_ARCH_STRTOK
218 char * strtok(char * s,const char * ct)
220 char *sbegin, *send;
222 sbegin = s ? s : ___strtok;
223 if (!sbegin) {
224 return NULL;
226 sbegin += strspn(sbegin,ct);
227 if (*sbegin == '\0') {
228 ___strtok = NULL;
229 return( NULL );
231 send = strpbrk( sbegin, ct);
232 if (send && *send != '\0')
233 *send++ = '\0';
234 ___strtok = send;
235 return (sbegin);
237 #endif
239 #ifndef __HAVE_ARCH_STRSEP
241 char * strsep(char **s, const char * ct)
243 char *sbegin=*s;
244 if (!sbegin)
245 return NULL;
247 sbegin += strspn(sbegin,ct);
248 if (*sbegin == '\0')
249 return NULL;
251 *s = strpbrk( sbegin, ct);
252 if (*s && **s != '\0')
253 **s++ = '\0';
254 return (sbegin);
256 #endif
258 #ifndef __HAVE_ARCH_MEMSET
259 void * memset(void * s,char c, size_t count)
261 char *xs = (char *) s;
263 while (count--)
264 *xs++ = c;
266 return s;
268 #endif
270 #ifndef __HAVE_ARCH_BCOPY
271 char * bcopy(const char * src, char * dest, int count)
273 char *tmp = dest;
275 while (count--)
276 *tmp++ = *src++;
278 return dest;
280 #endif
282 #ifndef __HAVE_ARCH_MEMCPY
283 void * memcpy(void * dest,const void *src,size_t count)
285 char *tmp = (char *) dest, *s = (char *) src;
287 while (count--)
288 *tmp++ = *s++;
290 return dest;
292 #endif
294 #ifndef __HAVE_ARCH_MEMMOVE
295 void * memmove(void * dest,const void *src,size_t count)
297 char *tmp, *s;
299 if (dest <= src) {
300 tmp = (char *) dest;
301 s = (char *) src;
302 while (count--)
303 *tmp++ = *s++;
305 else {
306 tmp = (char *) dest + count;
307 s = (char *) src + count;
308 while (count--)
309 *--tmp = *--s;
312 return dest;
314 #endif
316 #ifndef __HAVE_ARCH_MEMCMP
317 int memcmp(const void * cs,const void * ct,size_t count)
319 const unsigned char *su1, *su2;
320 signed char res = 0;
322 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
323 if ((res = *su1 - *su2) != 0)
324 break;
325 return res;
327 #endif
330 * find the first occurrence of byte 'c', or 1 past the area if none
332 #ifndef __HAVE_ARCH_MEMSCAN
333 void * memscan(void * addr, int c, size_t size)
335 unsigned char * p = (unsigned char *) addr;
336 unsigned char * e = p + size;
338 while (p != e) {
339 if (*p == c)
340 return (void *) p;
341 p++;
344 return (void *) p;
346 #endif
348 #ifndef __HAVE_ARCH_STRSTR
349 char * strstr(const char * s1,const char * s2)
351 int l1, l2;
353 l2 = strlen(s2);
354 if (!l2)
355 return (char *) s1;
356 l1 = strlen(s1);
357 while (l1 >= l2) {
358 l1--;
359 if (!memcmp(s1,s2,l2))
360 return (char *) s1;
361 s1++;
363 return NULL;
365 #endif
367 #ifndef __HAVE_ARCH_MEMCHR
368 void *memchr(const void *s, int c, size_t n)
370 const unsigned char *p = s;
371 while (n-- != 0) {
372 if ((unsigned char)c == *p++) {
373 return (void *)(p-1);
376 return NULL;
379 #endif