menu: simplify usage for clients
[barebox-mini2440.git] / lib / string.c
blob77435aad793c58cf1f32f77a5b3c3aa88a049c6f
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_STRLCPY
66 /**
67 * strlcpy - Copy a %NUL terminated string into a sized buffer
68 * @dest: Where to copy the string to
69 * @src: Where to copy the string from
70 * @size: size of destination buffer
72 * Compatible with *BSD: the result is always a valid
73 * NUL-terminated string that fits in the buffer (unless,
74 * of course, the buffer size is zero). It does not pad
75 * out the result like strncpy() does.
77 size_t strlcpy(char *dest, const char *src, size_t size)
79 size_t ret = strlen(src);
81 if (size) {
82 size_t len = (ret >= size) ? size - 1 : ret;
83 memcpy(dest, src, len);
84 dest[len] = '\0';
86 return ret;
88 EXPORT_SYMBOL(strlcpy);
89 #endif
91 #ifndef __HAVE_ARCH_STRCAT
92 /**
93 * strcat - Append one %NUL-terminated string to another
94 * @dest: The string to be appended to
95 * @src: The string to append to it
97 char * strcat(char * dest, const char * src)
99 char *tmp = dest;
101 while (*dest)
102 dest++;
103 while ((*dest++ = *src++) != '\0')
106 return tmp;
108 #endif
109 EXPORT_SYMBOL(strcat);
111 #ifndef __HAVE_ARCH_STRNCAT
113 * strncat - Append a length-limited, %NUL-terminated string to another
114 * @dest: The string to be appended to
115 * @src: The string to append to it
116 * @count: The maximum numbers of bytes to copy
118 * Note that in contrast to strncpy, strncat ensures the result is
119 * terminated.
121 char * strncat(char *dest, const char *src, size_t count)
123 char *tmp = dest;
125 if (count) {
126 while (*dest)
127 dest++;
128 while ((*dest++ = *src++)) {
129 if (--count == 0) {
130 *dest = '\0';
131 break;
136 return tmp;
138 #endif
139 EXPORT_SYMBOL(strncat);
141 #ifndef __HAVE_ARCH_STRCMP
143 * strcmp - Compare two strings
144 * @cs: One string
145 * @ct: Another string
147 int strcmp(const char * cs,const char * ct)
149 register signed char __res;
151 while (1) {
152 if ((__res = *cs - *ct++) != 0 || !*cs++)
153 break;
156 return __res;
158 #endif
159 EXPORT_SYMBOL(strcmp);
161 #ifndef __HAVE_ARCH_STRNCMP
163 * strncmp - Compare two length-limited strings
164 * @cs: One string
165 * @ct: Another string
166 * @count: The maximum number of bytes to compare
168 int strncmp(const char * cs, const char * ct, size_t count)
170 register signed char __res = 0;
172 while (count) {
173 if ((__res = *cs - *ct++) != 0 || !*cs++)
174 break;
175 count--;
178 return __res;
180 #endif
181 EXPORT_SYMBOL(strncmp);
183 #ifndef __HAVE_ARCH_STRCHR
185 * strchr - Find the first occurrence of a character in a string
186 * @s: The string to be searched
187 * @c: The character to search for
189 char * _strchr(const char * s, int c)
191 for(; *s != (char) c; ++s)
192 if (*s == '\0')
193 return NULL;
194 return (char *) s;
196 #endif
197 EXPORT_SYMBOL(_strchr);
199 #ifndef __HAVE_ARCH_STRRCHR
201 * strrchr - Find the last occurrence of a character in a string
202 * @s: The string to be searched
203 * @c: The character to search for
205 char * _strrchr(const char * s, int c)
207 const char *p = s + strlen(s);
208 do {
209 if (*p == (char)c)
210 return (char *)p;
211 } while (--p >= s);
212 return NULL;
214 #endif
215 EXPORT_SYMBOL(_strrchr);
217 #ifndef __HAVE_ARCH_STRLEN
219 * strlen - Find the length of a string
220 * @s: The string to be sized
222 size_t strlen(const char * s)
224 const char *sc;
226 for (sc = s; *sc != '\0'; ++sc)
227 /* nothing */;
228 return sc - s;
230 #endif
231 EXPORT_SYMBOL(strlen);
233 #ifndef __HAVE_ARCH_STRNLEN
235 * strnlen - Find the length of a length-limited string
236 * @s: The string to be sized
237 * @count: The maximum number of bytes to search
239 size_t strnlen(const char * s, size_t count)
241 const char *sc;
243 for (sc = s; count-- && *sc != '\0'; ++sc)
244 /* nothing */;
245 return sc - s;
247 #endif
248 EXPORT_SYMBOL(strnlen);
250 #ifndef __HAVE_ARCH_STRDUP
251 char * strdup(const char *s)
253 char *new;
255 if ((s == NULL) ||
256 ((new = malloc (strlen(s) + 1)) == NULL) ) {
257 return NULL;
260 strcpy (new, s);
261 return new;
263 #endif
264 EXPORT_SYMBOL(strdup);
266 #ifndef __HAVE_ARCH_STRSPN
268 * strspn - Calculate the length of the initial substring of @s which only
269 * contain letters in @accept
270 * @s: The string to be searched
271 * @accept: The string to search for
273 size_t strspn(const char *s, const char *accept)
275 const char *p;
276 const char *a;
277 size_t count = 0;
279 for (p = s; *p != '\0'; ++p) {
280 for (a = accept; *a != '\0'; ++a) {
281 if (*p == *a)
282 break;
284 if (*a == '\0')
285 return count;
286 ++count;
289 return count;
291 #endif
292 EXPORT_SYMBOL(strspn);
294 #ifndef __HAVE_ARCH_STRPBRK
296 * strpbrk - Find the first occurrence of a set of characters
297 * @cs: The string to be searched
298 * @ct: The characters to search for
300 char * strpbrk(const char * cs,const char * ct)
302 const char *sc1, *sc2;
304 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
305 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
306 if (*sc1 == *sc2)
307 return (char *) sc1;
310 return NULL;
312 #endif
313 EXPORT_SYMBOL(strpbrk);
315 #ifndef __HAVE_ARCH_STRTOK
317 * strtok - Split a string into tokens
318 * @s: The string to be searched
319 * @ct: The characters to search for
321 * WARNING: strtok is deprecated, use strsep instead.
323 char * strtok(char * s, const char * ct)
325 char *sbegin, *send;
327 sbegin = s ? s : ___strtok;
328 if (!sbegin) {
329 return NULL;
331 sbegin += strspn(sbegin,ct);
332 if (*sbegin == '\0') {
333 ___strtok = NULL;
334 return( NULL );
336 send = strpbrk( sbegin, ct);
337 if (send && *send != '\0')
338 *send++ = '\0';
339 ___strtok = send;
340 return (sbegin);
342 #endif
343 EXPORT_SYMBOL(strtok);
345 #ifndef __HAVE_ARCH_STRSEP
347 * strsep - Split a string into tokens
348 * @s: The string to be searched
349 * @ct: The characters to search for
351 * strsep() updates @s to point after the token, ready for the next call.
353 * It returns empty tokens, too, behaving exactly like the libc function
354 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
355 * Same semantics, slimmer shape. ;)
357 char * strsep(char **s, const char *ct)
359 char *sbegin = *s, *end;
361 if (sbegin == NULL)
362 return NULL;
364 end = strpbrk(sbegin, ct);
365 if (end)
366 *end++ = '\0';
367 *s = end;
369 return sbegin;
371 #endif
372 EXPORT_SYMBOL(strsep);
374 #ifndef __HAVE_ARCH_STRSWAB
376 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
377 * s: address of the string
379 * returns the address of the swapped string or NULL on error. If
380 * string length is odd, last byte is untouched.
382 char *strswab(const char *s)
384 char *p, *q;
386 if ((NULL == s) || ('\0' == *s)) {
387 return (NULL);
390 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
391 char tmp;
393 tmp = *p;
394 *p = *q;
395 *q = tmp;
398 return (char *) s;
400 #endif
402 #ifndef __HAVE_ARCH_MEMSET
404 * memset - Fill a region of memory with the given value
405 * @s: Pointer to the start of the area.
406 * @c: The byte to fill the area with
407 * @count: The size of the area.
409 * Do not use memset() to access IO space, use memset_io() instead.
411 void * memset(void * s,int c,size_t count)
413 char *xs = (char *) s;
415 while (count--)
416 *xs++ = c;
418 return s;
420 #endif
421 EXPORT_SYMBOL(memset);
423 #ifndef __HAVE_ARCH_BCOPY
425 * bcopy - Copy one area of memory to another
426 * @src: Where to copy from
427 * @dest: Where to copy to
428 * @count: The size of the area.
430 * Note that this is the same as memcpy(), with the arguments reversed.
431 * memcpy() is the standard, bcopy() is a legacy BSD function.
433 * You should not use this function to access IO space, use memcpy_toio()
434 * or memcpy_fromio() instead.
436 char * bcopy(const char * src, char * dest, int count)
438 char *tmp = dest;
440 while (count--)
441 *tmp++ = *src++;
443 return dest;
445 #endif
447 #ifndef __HAVE_ARCH_MEMCPY
449 * memcpy - Copy one area of memory to another
450 * @dest: Where to copy to
451 * @src: Where to copy from
452 * @count: The size of the area.
454 * You should not use this function to access IO space, use memcpy_toio()
455 * or memcpy_fromio() instead.
457 void * memcpy(void * dest,const void *src,size_t count)
459 char *tmp = (char *) dest, *s = (char *) src;
461 while (count--)
462 *tmp++ = *s++;
464 return dest;
466 #endif
467 EXPORT_SYMBOL(memcpy);
469 #ifndef __HAVE_ARCH_MEMMOVE
471 * memmove - Copy one area of memory to another
472 * @dest: Where to copy to
473 * @src: Where to copy from
474 * @count: The size of the area.
476 * Unlike memcpy(), memmove() copes with overlapping areas.
478 void * memmove(void * dest,const void *src,size_t count)
480 char *tmp, *s;
482 if (dest <= src) {
483 tmp = (char *) dest;
484 s = (char *) src;
485 while (count--)
486 *tmp++ = *s++;
488 else {
489 tmp = (char *) dest + count;
490 s = (char *) src + count;
491 while (count--)
492 *--tmp = *--s;
495 return dest;
497 #endif
498 EXPORT_SYMBOL(memmove);
500 #ifndef __HAVE_ARCH_MEMCMP
502 * memcmp - Compare two areas of memory
503 * @cs: One area of memory
504 * @ct: Another area of memory
505 * @count: The size of the area.
507 int memcmp(const void * cs,const void * ct,size_t count)
509 const unsigned char *su1, *su2;
510 int res = 0;
512 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
513 if ((res = *su1 - *su2) != 0)
514 break;
515 return res;
517 #endif
518 EXPORT_SYMBOL(memcmp);
520 #ifndef __HAVE_ARCH_MEMSCAN
522 * memscan - Find a character in an area of memory.
523 * @addr: The memory area
524 * @c: The byte to search for
525 * @size: The size of the area.
527 * returns the address of the first occurrence of @c, or 1 byte past
528 * the area if @c is not found
530 void * memscan(void * addr, int c, size_t size)
532 unsigned char * p = (unsigned char *) addr;
534 while (size) {
535 if (*p == c)
536 return (void *) p;
537 p++;
538 size--;
540 return (void *) p;
542 #endif
543 EXPORT_SYMBOL(memscan);
545 #ifndef __HAVE_ARCH_STRSTR
547 * strstr - Find the first substring in a %NUL terminated string
548 * @s1: The string to be searched
549 * @s2: The string to search for
551 char * _strstr(const char * s1,const char * s2)
553 int l1, l2;
555 l2 = strlen(s2);
556 if (!l2)
557 return (char *) s1;
558 l1 = strlen(s1);
559 while (l1 >= l2) {
560 l1--;
561 if (!memcmp(s1,s2,l2))
562 return (char *) s1;
563 s1++;
565 return NULL;
567 #endif
568 EXPORT_SYMBOL(_strstr);
570 #ifndef __HAVE_ARCH_MEMCHR
572 * memchr - Find a character in an area of memory.
573 * @s: The memory area
574 * @c: The byte to search for
575 * @n: The size of the area.
577 * returns the address of the first occurrence of @c, or %NULL
578 * if @c is not found
580 void *memchr(const void *s, int c, size_t n)
582 const unsigned char *p = s;
583 while (n-- != 0) {
584 if ((unsigned char)c == *p++) {
585 return (void *)(p-1);
588 return NULL;
591 #endif
592 EXPORT_SYMBOL(memchr);