arm/kconfig: inverse the board selection
[barebox-mini2440.git] / lib / string.c
blob520eb74f1229d7c6eb9e769321c3e1f8114a5835
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>
21 #include <malloc.h>
23 char * ___strtok;
25 #ifndef __HAVE_ARCH_STRCPY
26 /**
27 * strcpy - Copy a %NUL terminated string
28 * @dest: Where to copy the string to
29 * @src: Where to copy the string from
31 char * strcpy(char * dest,const char *src)
33 char *tmp = dest;
35 while ((*dest++ = *src++) != '\0')
36 /* nothing */;
37 return tmp;
39 #endif
40 EXPORT_SYMBOL(strcpy);
42 #ifndef __HAVE_ARCH_STRNCPY
43 /**
44 * strncpy - Copy a length-limited, %NUL-terminated string
45 * @dest: Where to copy the string to
46 * @src: Where to copy the string from
47 * @count: The maximum number of bytes to copy
49 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
50 * However, the result is not %NUL-terminated if the source exceeds
51 * @count bytes.
53 char * strncpy(char * dest,const char *src,size_t count)
55 char *tmp = dest;
57 while (count-- && (*dest++ = *src++) != '\0')
58 /* nothing */;
60 return tmp;
62 #endif
63 EXPORT_SYMBOL(strncpy);
65 #ifndef __HAVE_ARCH_STRCAT
66 /**
67 * strcat - Append one %NUL-terminated string to another
68 * @dest: The string to be appended to
69 * @src: The string to append to it
71 char * strcat(char * dest, const char * src)
73 char *tmp = dest;
75 while (*dest)
76 dest++;
77 while ((*dest++ = *src++) != '\0')
80 return tmp;
82 #endif
83 EXPORT_SYMBOL(strcat);
85 #ifndef __HAVE_ARCH_STRNCAT
86 /**
87 * strncat - Append a length-limited, %NUL-terminated string to another
88 * @dest: The string to be appended to
89 * @src: The string to append to it
90 * @count: The maximum numbers of bytes to copy
92 * Note that in contrast to strncpy, strncat ensures the result is
93 * terminated.
95 char * strncat(char *dest, const char *src, size_t count)
97 char *tmp = dest;
99 if (count) {
100 while (*dest)
101 dest++;
102 while ((*dest++ = *src++)) {
103 if (--count == 0) {
104 *dest = '\0';
105 break;
110 return tmp;
112 #endif
113 EXPORT_SYMBOL(strncat);
115 #ifndef __HAVE_ARCH_STRCMP
117 * strcmp - Compare two strings
118 * @cs: One string
119 * @ct: Another string
121 int strcmp(const char * cs,const char * ct)
123 register signed char __res;
125 while (1) {
126 if ((__res = *cs - *ct++) != 0 || !*cs++)
127 break;
130 return __res;
132 #endif
133 EXPORT_SYMBOL(strcmp);
135 #ifndef __HAVE_ARCH_STRNCMP
137 * strncmp - Compare two length-limited strings
138 * @cs: One string
139 * @ct: Another string
140 * @count: The maximum number of bytes to compare
142 int strncmp(const char * cs, const char * ct, size_t count)
144 register signed char __res = 0;
146 while (count) {
147 if ((__res = *cs - *ct++) != 0 || !*cs++)
148 break;
149 count--;
152 return __res;
154 #endif
155 EXPORT_SYMBOL(strncmp);
157 #ifndef __HAVE_ARCH_STRCHR
159 * strchr - Find the first occurrence of a character in a string
160 * @s: The string to be searched
161 * @c: The character to search for
163 char * _strchr(const char * s, int c)
165 for(; *s != (char) c; ++s)
166 if (*s == '\0')
167 return NULL;
168 return (char *) s;
170 #endif
171 EXPORT_SYMBOL(_strchr);
173 #ifndef __HAVE_ARCH_STRRCHR
175 * strrchr - Find the last occurrence of a character in a string
176 * @s: The string to be searched
177 * @c: The character to search for
179 char * _strrchr(const char * s, int c)
181 const char *p = s + strlen(s);
182 do {
183 if (*p == (char)c)
184 return (char *)p;
185 } while (--p >= s);
186 return NULL;
188 #endif
189 EXPORT_SYMBOL(strrchr);
191 #ifndef __HAVE_ARCH_STRLEN
193 * strlen - Find the length of a string
194 * @s: The string to be sized
196 size_t strlen(const char * s)
198 const char *sc;
200 for (sc = s; *sc != '\0'; ++sc)
201 /* nothing */;
202 return sc - s;
204 #endif
205 EXPORT_SYMBOL(strlen);
207 #ifndef __HAVE_ARCH_STRNLEN
209 * strnlen - Find the length of a length-limited string
210 * @s: The string to be sized
211 * @count: The maximum number of bytes to search
213 size_t strnlen(const char * s, size_t count)
215 const char *sc;
217 for (sc = s; count-- && *sc != '\0'; ++sc)
218 /* nothing */;
219 return sc - s;
221 #endif
222 EXPORT_SYMBOL(strnlen);
224 #ifndef __HAVE_ARCH_STRDUP
225 char * strdup(const char *s)
227 char *new;
229 if ((s == NULL) ||
230 ((new = malloc (strlen(s) + 1)) == NULL) ) {
231 return NULL;
234 strcpy (new, s);
235 return new;
237 #endif
238 EXPORT_SYMBOL(strdup);
240 #ifndef __HAVE_ARCH_STRSPN
242 * strspn - Calculate the length of the initial substring of @s which only
243 * contain letters in @accept
244 * @s: The string to be searched
245 * @accept: The string to search for
247 size_t strspn(const char *s, const char *accept)
249 const char *p;
250 const char *a;
251 size_t count = 0;
253 for (p = s; *p != '\0'; ++p) {
254 for (a = accept; *a != '\0'; ++a) {
255 if (*p == *a)
256 break;
258 if (*a == '\0')
259 return count;
260 ++count;
263 return count;
265 #endif
266 EXPORT_SYMBOL(strspn);
268 #ifndef __HAVE_ARCH_STRPBRK
270 * strpbrk - Find the first occurrence of a set of characters
271 * @cs: The string to be searched
272 * @ct: The characters to search for
274 char * strpbrk(const char * cs,const char * ct)
276 const char *sc1, *sc2;
278 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
279 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
280 if (*sc1 == *sc2)
281 return (char *) sc1;
284 return NULL;
286 #endif
287 EXPORT_SYMBOL(strpbrk);
289 #ifndef __HAVE_ARCH_STRTOK
291 * strtok - Split a string into tokens
292 * @s: The string to be searched
293 * @ct: The characters to search for
295 * WARNING: strtok is deprecated, use strsep instead.
297 char * strtok(char * s, const char * ct)
299 char *sbegin, *send;
301 sbegin = s ? s : ___strtok;
302 if (!sbegin) {
303 return NULL;
305 sbegin += strspn(sbegin,ct);
306 if (*sbegin == '\0') {
307 ___strtok = NULL;
308 return( NULL );
310 send = strpbrk( sbegin, ct);
311 if (send && *send != '\0')
312 *send++ = '\0';
313 ___strtok = send;
314 return (sbegin);
316 #endif
317 EXPORT_SYMBOL(strtok);
319 #ifndef __HAVE_ARCH_STRSEP
321 * strsep - Split a string into tokens
322 * @s: The string to be searched
323 * @ct: The characters to search for
325 * strsep() updates @s to point after the token, ready for the next call.
327 * It returns empty tokens, too, behaving exactly like the libc function
328 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
329 * Same semantics, slimmer shape. ;)
331 char * strsep(char **s, const char *ct)
333 char *sbegin = *s, *end;
335 if (sbegin == NULL)
336 return NULL;
338 end = strpbrk(sbegin, ct);
339 if (end)
340 *end++ = '\0';
341 *s = end;
343 return sbegin;
345 #endif
346 EXPORT_SYMBOL(strsep);
348 #ifndef __HAVE_ARCH_STRSWAB
350 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
351 * s: address of the string
353 * returns the address of the swapped string or NULL on error. If
354 * string length is odd, last byte is untouched.
356 char *strswab(const char *s)
358 char *p, *q;
360 if ((NULL == s) || ('\0' == *s)) {
361 return (NULL);
364 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
365 char tmp;
367 tmp = *p;
368 *p = *q;
369 *q = tmp;
372 return (char *) s;
374 #endif
376 #ifndef __HAVE_ARCH_MEMSET
378 * memset - Fill a region of memory with the given value
379 * @s: Pointer to the start of the area.
380 * @c: The byte to fill the area with
381 * @count: The size of the area.
383 * Do not use memset() to access IO space, use memset_io() instead.
385 void * memset(void * s,int c,size_t count)
387 char *xs = (char *) s;
389 while (count--)
390 *xs++ = c;
392 return s;
394 #endif
395 EXPORT_SYMBOL(memset);
397 #ifndef __HAVE_ARCH_BCOPY
399 * bcopy - Copy one area of memory to another
400 * @src: Where to copy from
401 * @dest: Where to copy to
402 * @count: The size of the area.
404 * Note that this is the same as memcpy(), with the arguments reversed.
405 * memcpy() is the standard, bcopy() is a legacy BSD function.
407 * You should not use this function to access IO space, use memcpy_toio()
408 * or memcpy_fromio() instead.
410 char * bcopy(const char * src, char * dest, int count)
412 char *tmp = dest;
414 while (count--)
415 *tmp++ = *src++;
417 return dest;
419 #endif
421 #ifndef __HAVE_ARCH_MEMCPY
423 * memcpy - Copy one area of memory to another
424 * @dest: Where to copy to
425 * @src: Where to copy from
426 * @count: The size of the area.
428 * You should not use this function to access IO space, use memcpy_toio()
429 * or memcpy_fromio() instead.
431 void * memcpy(void * dest,const void *src,size_t count)
433 char *tmp = (char *) dest, *s = (char *) src;
435 while (count--)
436 *tmp++ = *s++;
438 return dest;
440 #endif
441 EXPORT_SYMBOL(memcpy);
443 #ifndef __HAVE_ARCH_MEMMOVE
445 * memmove - Copy one area of memory to another
446 * @dest: Where to copy to
447 * @src: Where to copy from
448 * @count: The size of the area.
450 * Unlike memcpy(), memmove() copes with overlapping areas.
452 void * memmove(void * dest,const void *src,size_t count)
454 char *tmp, *s;
456 if (dest <= src) {
457 tmp = (char *) dest;
458 s = (char *) src;
459 while (count--)
460 *tmp++ = *s++;
462 else {
463 tmp = (char *) dest + count;
464 s = (char *) src + count;
465 while (count--)
466 *--tmp = *--s;
469 return dest;
471 #endif
472 EXPORT_SYMBOL(memmove);
474 #ifndef __HAVE_ARCH_MEMCMP
476 * memcmp - Compare two areas of memory
477 * @cs: One area of memory
478 * @ct: Another area of memory
479 * @count: The size of the area.
481 int memcmp(const void * cs,const void * ct,size_t count)
483 const unsigned char *su1, *su2;
484 int res = 0;
486 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
487 if ((res = *su1 - *su2) != 0)
488 break;
489 return res;
491 #endif
492 EXPORT_SYMBOL(memcmp);
494 #ifndef __HAVE_ARCH_MEMSCAN
496 * memscan - Find a character in an area of memory.
497 * @addr: The memory area
498 * @c: The byte to search for
499 * @size: The size of the area.
501 * returns the address of the first occurrence of @c, or 1 byte past
502 * the area if @c is not found
504 void * memscan(void * addr, int c, size_t size)
506 unsigned char * p = (unsigned char *) addr;
508 while (size) {
509 if (*p == c)
510 return (void *) p;
511 p++;
512 size--;
514 return (void *) p;
516 #endif
517 EXPORT_SYMBOL(memscan);
519 #ifndef __HAVE_ARCH_STRSTR
521 * strstr - Find the first substring in a %NUL terminated string
522 * @s1: The string to be searched
523 * @s2: The string to search for
525 char * _strstr(const char * s1,const char * s2)
527 int l1, l2;
529 l2 = strlen(s2);
530 if (!l2)
531 return (char *) s1;
532 l1 = strlen(s1);
533 while (l1 >= l2) {
534 l1--;
535 if (!memcmp(s1,s2,l2))
536 return (char *) s1;
537 s1++;
539 return NULL;
541 #endif
542 EXPORT_SYMBOL(strstr);
544 #ifndef __HAVE_ARCH_MEMCHR
546 * memchr - Find a character in an area of memory.
547 * @s: The memory area
548 * @c: The byte to search for
549 * @n: The size of the area.
551 * returns the address of the first occurrence of @c, or %NULL
552 * if @c is not found
554 void *memchr(const void *s, int c, size_t n)
556 const unsigned char *p = s;
557 while (n-- != 0) {
558 if ((unsigned char)c == *p++) {
559 return (void *)(p-1);
562 return NULL;
565 #endif
566 EXPORT_SYMBOL(memchr);