Updated email mdc's email address
[gpxe.git] / src / core / string.c
blob353abd6f6baf4996a74ea43a6dc8cb7b8455cf27
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2004 Tobias Lorenz
5 * string handling functions
6 * based on linux/lib/string.c
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * stupid library routines.. The optimized versions should generally be found
15 * as inline code in <asm-xx/string.h>
17 * These are buggy as well..
19 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
20 * - Added strsep() which will replace strtok() soon (because strsep() is
21 * reentrant and should be faster). Use only strsep() in new code, please.
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
29 /* *** FROM string.c *** */
31 #ifndef __HAVE_ARCH_STRNICMP
32 /**
33 * strnicmp - Case insensitive, length-limited string comparison
34 * @s1: One string
35 * @s2: The other string
36 * @len: the maximum number of characters to compare
38 int strnicmp(const char *s1, const char *s2, size_t len)
40 /* Yes, Virginia, it had better be unsigned */
41 unsigned char c1, c2;
43 c1 = 0; c2 = 0;
44 if (len) {
45 do {
46 c1 = *s1; c2 = *s2;
47 s1++; s2++;
48 if (!c1)
49 break;
50 if (!c2)
51 break;
52 if (c1 == c2)
53 continue;
54 c1 = tolower(c1);
55 c2 = tolower(c2);
56 if (c1 != c2)
57 break;
58 } while (--len);
60 return (int)c1 - (int)c2;
62 #endif
64 char * ___strtok;
66 #ifndef __HAVE_ARCH_STRCPY
67 /**
68 * strcpy - Copy a %NUL terminated string
69 * @dest: Where to copy the string to
70 * @src: Where to copy the string from
72 char * strcpy(char * dest,const char *src)
74 char *tmp = dest;
76 while ((*dest++ = *src++) != '\0')
77 /* nothing */;
78 return tmp;
80 #endif
82 #ifndef __HAVE_ARCH_STRNCPY
83 /**
84 * strncpy - Copy a length-limited, %NUL-terminated string
85 * @dest: Where to copy the string to
86 * @src: Where to copy the string from
87 * @count: The maximum number of bytes to copy
89 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
90 * However, the result is not %NUL-terminated if the source exceeds
91 * @count bytes.
93 char * strncpy(char * dest,const char *src,size_t count)
95 char *tmp = dest;
97 while (count-- && (*dest++ = *src++) != '\0')
98 /* nothing */;
100 return tmp;
102 #endif
104 #ifndef __HAVE_ARCH_STRCAT
106 * strcat - Append one %NUL-terminated string to another
107 * @dest: The string to be appended to
108 * @src: The string to append to it
110 char * strcat(char * dest, const char * src)
112 char *tmp = dest;
114 while (*dest)
115 dest++;
116 while ((*dest++ = *src++) != '\0')
119 return tmp;
121 #endif
123 #ifndef __HAVE_ARCH_STRNCAT
125 * strncat - Append a length-limited, %NUL-terminated string to another
126 * @dest: The string to be appended to
127 * @src: The string to append to it
128 * @count: The maximum numbers of bytes to copy
130 * Note that in contrast to strncpy, strncat ensures the result is
131 * terminated.
133 char * strncat(char *dest, const char *src, size_t count)
135 char *tmp = dest;
137 if (count) {
138 while (*dest)
139 dest++;
140 while ((*dest++ = *src++)) {
141 if (--count == 0) {
142 *dest = '\0';
143 break;
148 return tmp;
150 #endif
152 #ifndef __HAVE_ARCH_STRCMP
154 * strcmp - Compare two strings
155 * @cs: One string
156 * @ct: Another string
158 int strcmp(const char * cs,const char * ct)
160 register signed char __res;
162 while (1) {
163 if ((__res = *cs - *ct++) != 0 || !*cs++)
164 break;
167 return __res;
169 #endif
171 #ifndef __HAVE_ARCH_STRNCMP
173 * strncmp - Compare two length-limited strings
174 * @cs: One string
175 * @ct: Another string
176 * @count: The maximum number of bytes to compare
178 int strncmp(const char * cs,const char * ct,size_t count)
180 register signed char __res = 0;
182 while (count) {
183 if ((__res = *cs - *ct++) != 0 || !*cs++)
184 break;
185 count--;
188 return __res;
190 #endif
192 #ifndef __HAVE_ARCH_STRCHR
194 * strchr - Find the first occurrence of a character in a string
195 * @s: The string to be searched
196 * @c: The character to search for
198 char * strchr(const char * s, int c)
200 for(; *s != (char) c; ++s)
201 if (*s == '\0')
202 return NULL;
203 return (char *) s;
205 #endif
207 #ifndef __HAVE_ARCH_STRRCHR
209 * strrchr - Find the last occurrence of a character in a string
210 * @s: The string to be searched
211 * @c: The character to search for
213 char * strrchr(const char * s, int c)
215 const char *p = s + strlen(s);
216 do {
217 if (*p == (char)c)
218 return (char *)p;
219 } while (--p >= s);
220 return NULL;
222 #endif
224 #ifndef __HAVE_ARCH_STRLEN
226 * strlen - Find the length of a string
227 * @s: The string to be sized
229 size_t strlen(const char * s)
231 const char *sc;
233 for (sc = s; *sc != '\0'; ++sc)
234 /* nothing */;
235 return sc - s;
237 #endif
239 #ifndef __HAVE_ARCH_STRNLEN
241 * strnlen - Find the length of a length-limited string
242 * @s: The string to be sized
243 * @count: The maximum number of bytes to search
245 size_t strnlen(const char * s, size_t count)
247 const char *sc;
249 for (sc = s; count-- && *sc != '\0'; ++sc)
250 /* nothing */;
251 return sc - s;
253 #endif
255 #ifndef __HAVE_ARCH_STRSPN
257 * strspn - Calculate the length of the initial substring of @s which only
258 * contain letters in @accept
259 * @s: The string to be searched
260 * @accept: The string to search for
262 size_t strspn(const char *s, const char *accept)
264 const char *p;
265 const char *a;
266 size_t count = 0;
268 for (p = s; *p != '\0'; ++p) {
269 for (a = accept; *a != '\0'; ++a) {
270 if (*p == *a)
271 break;
273 if (*a == '\0')
274 return count;
275 ++count;
278 return count;
280 #endif
282 #ifndef __HAVE_ARCH_STRPBRK
284 * strpbrk - Find the first occurrence of a set of characters
285 * @cs: The string to be searched
286 * @ct: The characters to search for
288 char * strpbrk(const char * cs,const char * ct)
290 const char *sc1,*sc2;
292 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
293 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
294 if (*sc1 == *sc2)
295 return (char *) sc1;
298 return NULL;
300 #endif
302 #ifndef __HAVE_ARCH_STRTOK
304 * strtok - Split a string into tokens
305 * @s: The string to be searched
306 * @ct: The characters to search for
308 * WARNING: strtok is deprecated, use strsep instead.
310 char * strtok(char * s,const char * ct)
312 char *sbegin, *send;
314 sbegin = s ? s : ___strtok;
315 if (!sbegin) {
316 return NULL;
318 sbegin += strspn(sbegin,ct);
319 if (*sbegin == '\0') {
320 ___strtok = NULL;
321 return( NULL );
323 send = strpbrk( sbegin, ct);
324 if (send && *send != '\0')
325 *send++ = '\0';
326 ___strtok = send;
327 return (sbegin);
329 #endif
331 #ifndef __HAVE_ARCH_STRSEP
333 * strsep - Split a string into tokens
334 * @s: The string to be searched
335 * @ct: The characters to search for
337 * strsep() updates @s to point after the token, ready for the next call.
339 * It returns empty tokens, too, behaving exactly like the libc function
340 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
341 * Same semantics, slimmer shape. ;)
343 char * strsep(char **s, const char *ct)
345 char *sbegin = *s, *end;
347 if (sbegin == NULL)
348 return NULL;
350 end = strpbrk(sbegin, ct);
351 if (end)
352 *end++ = '\0';
353 *s = end;
355 return sbegin;
357 #endif
359 #ifndef __HAVE_ARCH_MEMSET
361 * memset - Fill a region of memory with the given value
362 * @s: Pointer to the start of the area.
363 * @c: The byte to fill the area with
364 * @count: The size of the area.
366 * Do not use memset() to access IO space, use memset_io() instead.
368 void * memset(void * s,int c,size_t count)
370 char *xs = (char *) s;
372 while (count--)
373 *xs++ = c;
375 return s;
377 #endif
379 #ifndef __HAVE_ARCH_BCOPY
381 * bcopy - Copy one area of memory to another
382 * @src: Where to copy from
383 * @dest: Where to copy to
384 * @count: The size of the area.
386 * Note that this is the same as memcpy(), with the arguments reversed.
387 * memcpy() is the standard, bcopy() is a legacy BSD function.
389 * You should not use this function to access IO space, use memcpy_toio()
390 * or memcpy_fromio() instead.
392 char * bcopy(const char * src, char * dest, int count)
394 char *tmp = dest;
396 while (count--)
397 *tmp++ = *src++;
399 return dest;
401 #endif
403 #ifndef __HAVE_ARCH_MEMCPY
405 * memcpy - Copy one area of memory to another
406 * @dest: Where to copy to
407 * @src: Where to copy from
408 * @count: The size of the area.
410 * You should not use this function to access IO space, use memcpy_toio()
411 * or memcpy_fromio() instead.
413 void * memcpy(void * dest,const void *src,size_t count)
415 char *tmp = (char *) dest, *s = (char *) src;
417 while (count--)
418 *tmp++ = *s++;
420 return dest;
422 #endif
424 #ifndef __HAVE_ARCH_MEMMOVE
426 * memmove - Copy one area of memory to another
427 * @dest: Where to copy to
428 * @src: Where to copy from
429 * @count: The size of the area.
431 * Unlike memcpy(), memmove() copes with overlapping areas.
433 void * memmove(void * dest,const void *src,size_t count)
435 char *tmp, *s;
437 if (dest <= src) {
438 tmp = (char *) dest;
439 s = (char *) src;
440 while (count--)
441 *tmp++ = *s++;
443 else {
444 tmp = (char *) dest + count;
445 s = (char *) src + count;
446 while (count--)
447 *--tmp = *--s;
450 return dest;
452 #endif
454 #ifndef __HAVE_ARCH_MEMCMP
456 * memcmp - Compare two areas of memory
457 * @cs: One area of memory
458 * @ct: Another area of memory
459 * @count: The size of the area.
461 int memcmp(const void * cs,const void * ct,size_t count)
463 const unsigned char *su1, *su2;
464 int res = 0;
466 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
467 if ((res = *su1 - *su2) != 0)
468 break;
469 return res;
471 #endif
473 #ifndef __HAVE_ARCH_MEMSCAN
475 * memscan - Find a character in an area of memory.
476 * @addr: The memory area
477 * @c: The byte to search for
478 * @size: The size of the area.
480 * returns the address of the first occurrence of @c, or 1 byte past
481 * the area if @c is not found
483 void * memscan(void * addr, int c, size_t size)
485 unsigned char * p = (unsigned char *) addr;
487 while (size) {
488 if (*p == c)
489 return (void *) p;
490 p++;
491 size--;
493 return (void *) p;
495 #endif
497 #ifndef __HAVE_ARCH_STRSTR
499 * strstr - Find the first substring in a %NUL terminated string
500 * @s1: The string to be searched
501 * @s2: The string to search for
503 char * strstr(const char * s1,const char * s2)
505 int l1, l2;
507 l2 = strlen(s2);
508 if (!l2)
509 return (char *) s1;
510 l1 = strlen(s1);
511 while (l1 >= l2) {
512 l1--;
513 if (!memcmp(s1,s2,l2))
514 return (char *) s1;
515 s1++;
517 return NULL;
519 #endif
521 #ifndef __HAVE_ARCH_MEMCHR
523 * memchr - Find a character in an area of memory.
524 * @s: The memory area
525 * @c: The byte to search for
526 * @n: The size of the area.
528 * returns the address of the first occurrence of @c, or %NULL
529 * if @c is not found
531 void * memchr(const void *s, int c, size_t n)
533 const unsigned char *p = s;
534 while (n-- != 0) {
535 if ((unsigned char)c == *p++) {
536 return (void *)(p-1);
539 return NULL;
542 #endif
544 char * strdup(const char *s) {
545 char *new = malloc(strlen(s)+1);
546 if (new)
547 strcpy(new,s);
548 return new;