]> git.ipfire.org Git - thirdparty/u-boot.git/blame - lib_generic/string.c
Initial revision
[thirdparty/u-boot.git] / lib_generic / string.c
CommitLineData
c609719b
WD
1/*
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 * * 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.
16 */
17
18#include <linux/types.h>
19#include <linux/string.h>
20#include <linux/ctype.h>
21#include <malloc.h>
22
23#ifdef CONFIG_ARM
24#undef __HAVE_ARCH_MEMCMP
25#undef __HAVE_ARCH_MEMCPY
26#undef __HAVE_ARCH_MEMMOVE
27#undef __HAVE_ARCH_MEMSET
28#undef __HAVE_ARCH_BCOPY
29#undef __HAVE_ARCH_STRCAT
30#undef __HAVE_ARCH_STRCHR
31#undef __HAVE_ARCH_STRCMP
32#undef __HAVE_ARCH_STRCPY
33#undef __HAVE_ARCH_STRLEN
34#undef __HAVE_ARCH_STRNCPY
35#else
36#define __HAVE_ARCH_MEMCMP
37#define __HAVE_ARCH_MEMCPY
38#define __HAVE_ARCH_MEMMOVE
39#define __HAVE_ARCH_MEMSET
40#define __HAVE_ARCH_BCOPY
41#define __HAVE_ARCH_STRCAT
42#define __HAVE_ARCH_STRCMP
43#define __HAVE_ARCH_STRCPY
44#define __HAVE_ARCH_STRLEN
45#define __HAVE_ARCH_STRNCPY
46#endif
47
48#ifndef __HAVE_ARCH_STRNICMP
49/**
50 * strnicmp - Case insensitive, length-limited string comparison
51 * @s1: One string
52 * @s2: The other string
53 * @len: the maximum number of characters to compare
54 */
55int strnicmp(const char *s1, const char *s2, size_t len)
56{
57 /* Yes, Virginia, it had better be unsigned */
58 unsigned char c1, c2;
59
60 c1 = 0; c2 = 0;
61 if (len) {
62 do {
63 c1 = *s1; c2 = *s2;
64 s1++; s2++;
65 if (!c1)
66 break;
67 if (!c2)
68 break;
69 if (c1 == c2)
70 continue;
71 c1 = tolower(c1);
72 c2 = tolower(c2);
73 if (c1 != c2)
74 break;
75 } while (--len);
76 }
77 return (int)c1 - (int)c2;
78}
79#endif
80
81char * ___strtok;
82
83#ifndef __HAVE_ARCH_STRCPY
84/**
85 * strcpy - Copy a %NUL terminated string
86 * @dest: Where to copy the string to
87 * @src: Where to copy the string from
88 */
89char * strcpy(char * dest,const char *src)
90{
91 char *tmp = dest;
92
93 while ((*dest++ = *src++) != '\0')
94 /* nothing */;
95 return tmp;
96}
97#endif
98
99#ifndef __HAVE_ARCH_STRNCPY
100/**
101 * strncpy - Copy a length-limited, %NUL-terminated string
102 * @dest: Where to copy the string to
103 * @src: Where to copy the string from
104 * @count: The maximum number of bytes to copy
105 *
106 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
107 * However, the result is not %NUL-terminated if the source exceeds
108 * @count bytes.
109 */
110char * strncpy(char * dest,const char *src,size_t count)
111{
112 char *tmp = dest;
113
114 while (count-- && (*dest++ = *src++) != '\0')
115 /* nothing */;
116
117 return tmp;
118}
119#endif
120
121#ifndef __HAVE_ARCH_STRCAT
122/**
123 * strcat - Append one %NUL-terminated string to another
124 * @dest: The string to be appended to
125 * @src: The string to append to it
126 */
127char * strcat(char * dest, const char * src)
128{
129 char *tmp = dest;
130
131 while (*dest)
132 dest++;
133 while ((*dest++ = *src++) != '\0')
134 ;
135
136 return tmp;
137}
138#endif
139
140#ifndef __HAVE_ARCH_STRNCAT
141/**
142 * strncat - Append a length-limited, %NUL-terminated string to another
143 * @dest: The string to be appended to
144 * @src: The string to append to it
145 * @count: The maximum numbers of bytes to copy
146 *
147 * Note that in contrast to strncpy, strncat ensures the result is
148 * terminated.
149 */
150char * strncat(char *dest, const char *src, size_t count)
151{
152 char *tmp = dest;
153
154 if (count) {
155 while (*dest)
156 dest++;
157 while ((*dest++ = *src++)) {
158 if (--count == 0) {
159 *dest = '\0';
160 break;
161 }
162 }
163 }
164
165 return tmp;
166}
167#endif
168
169#ifndef __HAVE_ARCH_STRCMP
170/**
171 * strcmp - Compare two strings
172 * @cs: One string
173 * @ct: Another string
174 */
175int strcmp(const char * cs,const char * ct)
176{
177 register signed char __res;
178
179 while (1) {
180 if ((__res = *cs - *ct++) != 0 || !*cs++)
181 break;
182 }
183
184 return __res;
185}
186#endif
187
188#ifndef __HAVE_ARCH_STRNCMP
189/**
190 * strncmp - Compare two length-limited strings
191 * @cs: One string
192 * @ct: Another string
193 * @count: The maximum number of bytes to compare
194 */
195int strncmp(const char * cs,const char * ct,size_t count)
196{
197 register signed char __res = 0;
198
199 while (count) {
200 if ((__res = *cs - *ct++) != 0 || !*cs++)
201 break;
202 count--;
203 }
204
205 return __res;
206}
207#endif
208
209#ifndef __HAVE_ARCH_STRCHR
210/**
211 * strchr - Find the first occurrence of a character in a string
212 * @s: The string to be searched
213 * @c: The character to search for
214 */
215char * strchr(const char * s, int c)
216{
217 for(; *s != (char) c; ++s)
218 if (*s == '\0')
219 return NULL;
220 return (char *) s;
221}
222#endif
223
224#ifndef __HAVE_ARCH_STRRCHR
225/**
226 * strrchr - Find the last occurrence of a character in a string
227 * @s: The string to be searched
228 * @c: The character to search for
229 */
230char * strrchr(const char * s, int c)
231{
232 const char *p = s + strlen(s);
233 do {
234 if (*p == (char)c)
235 return (char *)p;
236 } while (--p >= s);
237 return NULL;
238}
239#endif
240
241#ifndef __HAVE_ARCH_STRLEN
242/**
243 * strlen - Find the length of a string
244 * @s: The string to be sized
245 */
246size_t strlen(const char * s)
247{
248 const char *sc;
249
250 for (sc = s; *sc != '\0'; ++sc)
251 /* nothing */;
252 return sc - s;
253}
254#endif
255
256#ifndef __HAVE_ARCH_STRNLEN
257/**
258 * strnlen - Find the length of a length-limited string
259 * @s: The string to be sized
260 * @count: The maximum number of bytes to search
261 */
262size_t strnlen(const char * s, size_t count)
263{
264 const char *sc;
265
266 for (sc = s; count-- && *sc != '\0'; ++sc)
267 /* nothing */;
268 return sc - s;
269}
270#endif
271
272#ifndef __HAVE_ARCH_STRDUP
273char * strdup(const char *s)
274{
275 char *new;
276
277 if ((s == NULL) ||
278 ((new = malloc (strlen(s) + 1)) == NULL) ) {
279 return NULL;
280 }
281
282 strcpy (new, s);
283 return new;
284}
285#endif
286
287#ifndef __HAVE_ARCH_STRSPN
288/**
289 * strspn - Calculate the length of the initial substring of @s which only
290 * contain letters in @accept
291 * @s: The string to be searched
292 * @accept: The string to search for
293 */
294size_t strspn(const char *s, const char *accept)
295{
296 const char *p;
297 const char *a;
298 size_t count = 0;
299
300 for (p = s; *p != '\0'; ++p) {
301 for (a = accept; *a != '\0'; ++a) {
302 if (*p == *a)
303 break;
304 }
305 if (*a == '\0')
306 return count;
307 ++count;
308 }
309
310 return count;
311}
312#endif
313
314#ifndef __HAVE_ARCH_STRPBRK
315/**
316 * strpbrk - Find the first occurrence of a set of characters
317 * @cs: The string to be searched
318 * @ct: The characters to search for
319 */
320char * strpbrk(const char * cs,const char * ct)
321{
322 const char *sc1,*sc2;
323
324 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
325 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
326 if (*sc1 == *sc2)
327 return (char *) sc1;
328 }
329 }
330 return NULL;
331}
332#endif
333
334#ifndef __HAVE_ARCH_STRTOK
335/**
336 * strtok - Split a string into tokens
337 * @s: The string to be searched
338 * @ct: The characters to search for
339 *
340 * WARNING: strtok is deprecated, use strsep instead.
341 */
342char * strtok(char * s,const char * ct)
343{
344 char *sbegin, *send;
345
346 sbegin = s ? s : ___strtok;
347 if (!sbegin) {
348 return NULL;
349 }
350 sbegin += strspn(sbegin,ct);
351 if (*sbegin == '\0') {
352 ___strtok = NULL;
353 return( NULL );
354 }
355 send = strpbrk( sbegin, ct);
356 if (send && *send != '\0')
357 *send++ = '\0';
358 ___strtok = send;
359 return (sbegin);
360}
361#endif
362
363#ifndef __HAVE_ARCH_STRSEP
364/**
365 * strsep - Split a string into tokens
366 * @s: The string to be searched
367 * @ct: The characters to search for
368 *
369 * strsep() updates @s to point after the token, ready for the next call.
370 *
371 * It returns empty tokens, too, behaving exactly like the libc function
372 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
373 * Same semantics, slimmer shape. ;)
374 */
375char * strsep(char **s, const char *ct)
376{
377 char *sbegin = *s, *end;
378
379 if (sbegin == NULL)
380 return NULL;
381
382 end = strpbrk(sbegin, ct);
383 if (end)
384 *end++ = '\0';
385 *s = end;
386
387 return sbegin;
388}
389#endif
390
391#ifndef __HAVE_ARCH_MEMSET
392/**
393 * memset - Fill a region of memory with the given value
394 * @s: Pointer to the start of the area.
395 * @c: The byte to fill the area with
396 * @count: The size of the area.
397 *
398 * Do not use memset() to access IO space, use memset_io() instead.
399 */
400void * memset(void * s,int c,size_t count)
401{
402 char *xs = (char *) s;
403
404 while (count--)
405 *xs++ = c;
406
407 return s;
408}
409#endif
410
411#ifndef __HAVE_ARCH_BCOPY
412/**
413 * bcopy - Copy one area of memory to another
414 * @src: Where to copy from
415 * @dest: Where to copy to
416 * @count: The size of the area.
417 *
418 * Note that this is the same as memcpy(), with the arguments reversed.
419 * memcpy() is the standard, bcopy() is a legacy BSD function.
420 *
421 * You should not use this function to access IO space, use memcpy_toio()
422 * or memcpy_fromio() instead.
423 */
424char * bcopy(const char * src, char * dest, int count)
425{
426 char *tmp = dest;
427
428 while (count--)
429 *tmp++ = *src++;
430
431 return dest;
432}
433#endif
434
435#ifndef __HAVE_ARCH_MEMCPY
436/**
437 * memcpy - Copy one area of memory to another
438 * @dest: Where to copy to
439 * @src: Where to copy from
440 * @count: The size of the area.
441 *
442 * You should not use this function to access IO space, use memcpy_toio()
443 * or memcpy_fromio() instead.
444 */
445void * memcpy(void * dest,const void *src,size_t count)
446{
447 char *tmp = (char *) dest, *s = (char *) src;
448
449 while (count--)
450 *tmp++ = *s++;
451
452 return dest;
453}
454#endif
455
456#ifndef __HAVE_ARCH_MEMMOVE
457/**
458 * memmove - Copy one area of memory to another
459 * @dest: Where to copy to
460 * @src: Where to copy from
461 * @count: The size of the area.
462 *
463 * Unlike memcpy(), memmove() copes with overlapping areas.
464 */
465void * memmove(void * dest,const void *src,size_t count)
466{
467 char *tmp, *s;
468
469 if (dest <= src) {
470 tmp = (char *) dest;
471 s = (char *) src;
472 while (count--)
473 *tmp++ = *s++;
474 }
475 else {
476 tmp = (char *) dest + count;
477 s = (char *) src + count;
478 while (count--)
479 *--tmp = *--s;
480 }
481
482 return dest;
483}
484#endif
485
486#ifndef __HAVE_ARCH_MEMCMP
487/**
488 * memcmp - Compare two areas of memory
489 * @cs: One area of memory
490 * @ct: Another area of memory
491 * @count: The size of the area.
492 */
493int memcmp(const void * cs,const void * ct,size_t count)
494{
495 const unsigned char *su1, *su2;
496 int res = 0;
497
498 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
499 if ((res = *su1 - *su2) != 0)
500 break;
501 return res;
502}
503#endif
504
505#ifndef __HAVE_ARCH_MEMSCAN
506/**
507 * memscan - Find a character in an area of memory.
508 * @addr: The memory area
509 * @c: The byte to search for
510 * @size: The size of the area.
511 *
512 * returns the address of the first occurrence of @c, or 1 byte past
513 * the area if @c is not found
514 */
515void * memscan(void * addr, int c, size_t size)
516{
517 unsigned char * p = (unsigned char *) addr;
518
519 while (size) {
520 if (*p == c)
521 return (void *) p;
522 p++;
523 size--;
524 }
525 return (void *) p;
526}
527#endif
528
529#ifndef __HAVE_ARCH_STRSTR
530/**
531 * strstr - Find the first substring in a %NUL terminated string
532 * @s1: The string to be searched
533 * @s2: The string to search for
534 */
535char * strstr(const char * s1,const char * s2)
536{
537 int l1, l2;
538
539 l2 = strlen(s2);
540 if (!l2)
541 return (char *) s1;
542 l1 = strlen(s1);
543 while (l1 >= l2) {
544 l1--;
545 if (!memcmp(s1,s2,l2))
546 return (char *) s1;
547 s1++;
548 }
549 return NULL;
550}
551#endif
552
553#ifndef __HAVE_ARCH_MEMCHR
554/**
555 * memchr - Find a character in an area of memory.
556 * @s: The memory area
557 * @c: The byte to search for
558 * @n: The size of the area.
559 *
560 * returns the address of the first occurrence of @c, or %NULL
561 * if @c is not found
562 */
563void *memchr(const void *s, int c, size_t n)
564{
565 const unsigned char *p = s;
566 while (n-- != 0) {
567 if ((unsigned char)c == *p++) {
568 return (void *)(p-1);
569 }
570 }
571 return NULL;
572}
573
574#endif