blob: 09dfae03c247554deb48c29ce98693ffd9c25f9d [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
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
wdenkc6097192002-11-03 00:24:07 +000023
wdenkc6097192002-11-03 00:24:07 +000024/**
Simon Glassb1f17bf2012-12-05 14:46:35 +000025 * strncasecmp - Case insensitive, length-limited string comparison
wdenkc6097192002-11-03 00:24:07 +000026 * @s1: One string
27 * @s2: The other string
28 * @len: the maximum number of characters to compare
29 */
Simon Glassb1f17bf2012-12-05 14:46:35 +000030int strncasecmp(const char *s1, const char *s2, size_t len)
wdenkc6097192002-11-03 00:24:07 +000031{
32 /* Yes, Virginia, it had better be unsigned */
33 unsigned char c1, c2;
34
35 c1 = 0; c2 = 0;
36 if (len) {
37 do {
38 c1 = *s1; c2 = *s2;
39 s1++; s2++;
40 if (!c1)
41 break;
42 if (!c2)
43 break;
44 if (c1 == c2)
45 continue;
46 c1 = tolower(c1);
47 c2 = tolower(c2);
48 if (c1 != c2)
49 break;
50 } while (--len);
51 }
52 return (int)c1 - (int)c2;
53}
Simon Glassb1f17bf2012-12-05 14:46:35 +000054
55/**
56 * strcasecmp - Case insensitive string comparison
57 * @s1: One string
58 * @s2: The other string
59 */
60int strcasecmp(const char *s1, const char *s2)
61{
62 return strncasecmp(s1, s2, -1U);
63}
wdenkc6097192002-11-03 00:24:07 +000064
65char * ___strtok;
66
67#ifndef __HAVE_ARCH_STRCPY
68/**
69 * strcpy - Copy a %NUL terminated string
70 * @dest: Where to copy the string to
71 * @src: Where to copy the string from
72 */
73char * strcpy(char * dest,const char *src)
74{
75 char *tmp = dest;
76
77 while ((*dest++ = *src++) != '\0')
78 /* nothing */;
79 return tmp;
80}
81#endif
82
83#ifndef __HAVE_ARCH_STRNCPY
84/**
85 * strncpy - Copy a length-limited, %NUL-terminated string
86 * @dest: Where to copy the string to
87 * @src: Where to copy the string from
88 * @count: The maximum number of bytes to copy
89 *
90 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
91 * However, the result is not %NUL-terminated if the source exceeds
92 * @count bytes.
93 */
94char * strncpy(char * dest,const char *src,size_t count)
95{
96 char *tmp = dest;
97
98 while (count-- && (*dest++ = *src++) != '\0')
99 /* nothing */;
100
101 return tmp;
102}
103#endif
104
105#ifndef __HAVE_ARCH_STRCAT
106/**
107 * strcat - Append one %NUL-terminated string to another
108 * @dest: The string to be appended to
109 * @src: The string to append to it
110 */
111char * strcat(char * dest, const char * src)
112{
113 char *tmp = dest;
114
115 while (*dest)
116 dest++;
117 while ((*dest++ = *src++) != '\0')
118 ;
119
120 return tmp;
121}
122#endif
123
124#ifndef __HAVE_ARCH_STRNCAT
125/**
126 * strncat - Append a length-limited, %NUL-terminated string to another
127 * @dest: The string to be appended to
128 * @src: The string to append to it
129 * @count: The maximum numbers of bytes to copy
130 *
131 * Note that in contrast to strncpy, strncat ensures the result is
132 * terminated.
133 */
134char * strncat(char *dest, const char *src, size_t count)
135{
136 char *tmp = dest;
137
138 if (count) {
139 while (*dest)
140 dest++;
141 while ((*dest++ = *src++)) {
142 if (--count == 0) {
143 *dest = '\0';
144 break;
145 }
146 }
147 }
148
149 return tmp;
150}
151#endif
152
153#ifndef __HAVE_ARCH_STRCMP
154/**
155 * strcmp - Compare two strings
156 * @cs: One string
157 * @ct: Another string
158 */
159int strcmp(const char * cs,const char * ct)
160{
161 register signed char __res;
162
163 while (1) {
164 if ((__res = *cs - *ct++) != 0 || !*cs++)
165 break;
166 }
167
168 return __res;
169}
170#endif
171
172#ifndef __HAVE_ARCH_STRNCMP
173/**
174 * strncmp - Compare two length-limited strings
175 * @cs: One string
176 * @ct: Another string
177 * @count: The maximum number of bytes to compare
178 */
179int strncmp(const char * cs,const char * ct,size_t count)
180{
181 register signed char __res = 0;
182
183 while (count) {
184 if ((__res = *cs - *ct++) != 0 || !*cs++)
185 break;
186 count--;
187 }
188
189 return __res;
190}
191#endif
192
193#ifndef __HAVE_ARCH_STRCHR
194/**
195 * strchr - Find the first occurrence of a character in a string
196 * @s: The string to be searched
197 * @c: The character to search for
198 */
199char * strchr(const char * s, int c)
200{
201 for(; *s != (char) c; ++s)
202 if (*s == '\0')
203 return NULL;
204 return (char *) s;
205}
206#endif
207
208#ifndef __HAVE_ARCH_STRRCHR
209/**
210 * strrchr - Find the last occurrence of a character in a string
211 * @s: The string to be searched
212 * @c: The character to search for
213 */
214char * strrchr(const char * s, int c)
215{
216 const char *p = s + strlen(s);
217 do {
wdenk8bde7f72003-06-27 21:31:46 +0000218 if (*p == (char)c)
219 return (char *)p;
wdenkc6097192002-11-03 00:24:07 +0000220 } while (--p >= s);
221 return NULL;
222}
223#endif
224
225#ifndef __HAVE_ARCH_STRLEN
226/**
227 * strlen - Find the length of a string
228 * @s: The string to be sized
229 */
230size_t strlen(const char * s)
231{
232 const char *sc;
233
234 for (sc = s; *sc != '\0'; ++sc)
235 /* nothing */;
236 return sc - s;
237}
238#endif
239
240#ifndef __HAVE_ARCH_STRNLEN
241/**
242 * strnlen - Find the length of a length-limited string
243 * @s: The string to be sized
244 * @count: The maximum number of bytes to search
245 */
246size_t strnlen(const char * s, size_t count)
247{
248 const char *sc;
249
250 for (sc = s; count-- && *sc != '\0'; ++sc)
251 /* nothing */;
252 return sc - s;
253}
254#endif
255
256#ifndef __HAVE_ARCH_STRDUP
257char * strdup(const char *s)
258{
259 char *new;
260
261 if ((s == NULL) ||
262 ((new = malloc (strlen(s) + 1)) == NULL) ) {
263 return NULL;
264 }
265
266 strcpy (new, s);
267 return new;
268}
269#endif
270
271#ifndef __HAVE_ARCH_STRSPN
272/**
273 * strspn - Calculate the length of the initial substring of @s which only
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200274 * contain letters in @accept
wdenkc6097192002-11-03 00:24:07 +0000275 * @s: The string to be searched
276 * @accept: The string to search for
277 */
278size_t strspn(const char *s, const char *accept)
279{
280 const char *p;
281 const char *a;
282 size_t count = 0;
283
284 for (p = s; *p != '\0'; ++p) {
285 for (a = accept; *a != '\0'; ++a) {
286 if (*p == *a)
287 break;
288 }
289 if (*a == '\0')
290 return count;
291 ++count;
292 }
293
294 return count;
295}
296#endif
297
298#ifndef __HAVE_ARCH_STRPBRK
299/**
300 * strpbrk - Find the first occurrence of a set of characters
301 * @cs: The string to be searched
302 * @ct: The characters to search for
303 */
304char * strpbrk(const char * cs,const char * ct)
305{
306 const char *sc1,*sc2;
307
308 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
309 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
310 if (*sc1 == *sc2)
311 return (char *) sc1;
312 }
313 }
314 return NULL;
315}
316#endif
317
318#ifndef __HAVE_ARCH_STRTOK
319/**
320 * strtok - Split a string into tokens
321 * @s: The string to be searched
322 * @ct: The characters to search for
323 *
324 * WARNING: strtok is deprecated, use strsep instead.
325 */
326char * strtok(char * s,const char * ct)
327{
328 char *sbegin, *send;
329
330 sbegin = s ? s : ___strtok;
331 if (!sbegin) {
332 return NULL;
333 }
334 sbegin += strspn(sbegin,ct);
335 if (*sbegin == '\0') {
336 ___strtok = NULL;
337 return( NULL );
338 }
339 send = strpbrk( sbegin, ct);
340 if (send && *send != '\0')
341 *send++ = '\0';
342 ___strtok = send;
343 return (sbegin);
344}
345#endif
346
347#ifndef __HAVE_ARCH_STRSEP
348/**
349 * strsep - Split a string into tokens
350 * @s: The string to be searched
351 * @ct: The characters to search for
352 *
353 * strsep() updates @s to point after the token, ready for the next call.
354 *
355 * It returns empty tokens, too, behaving exactly like the libc function
356 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
357 * Same semantics, slimmer shape. ;)
358 */
359char * strsep(char **s, const char *ct)
360{
361 char *sbegin = *s, *end;
362
363 if (sbegin == NULL)
364 return NULL;
365
366 end = strpbrk(sbegin, ct);
367 if (end)
368 *end++ = '\0';
369 *s = end;
370
371 return sbegin;
372}
373#endif
374
wdenkc3f9d492004-03-14 00:59:59 +0000375#ifndef __HAVE_ARCH_STRSWAB
376/**
377 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
378 * s: address of the string
379 *
380 * returns the address of the swapped string or NULL on error. If
381 * string length is odd, last byte is untouched.
382 */
383char *strswab(const char *s)
384{
Wolfgang Denk389db1f2005-09-25 16:15:17 +0200385 char *p, *q;
wdenkc3f9d492004-03-14 00:59:59 +0000386
387 if ((NULL == s) || ('\0' == *s)) {
388 return (NULL);
389 }
390
Wolfgang Denke5e98ed2005-10-04 23:38:07 +0200391 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
wdenkc3f9d492004-03-14 00:59:59 +0000392 char tmp;
Wolfgang Denk389db1f2005-09-25 16:15:17 +0200393
394 tmp = *p;
395 *p = *q;
396 *q = tmp;
wdenkc3f9d492004-03-14 00:59:59 +0000397 }
398
399 return (char *) s;
400}
401#endif
402
wdenkc6097192002-11-03 00:24:07 +0000403#ifndef __HAVE_ARCH_MEMSET
404/**
405 * memset - Fill a region of memory with the given value
406 * @s: Pointer to the start of the area.
407 * @c: The byte to fill the area with
408 * @count: The size of the area.
409 *
410 * Do not use memset() to access IO space, use memset_io() instead.
411 */
412void * memset(void * s,int c,size_t count)
413{
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200414 unsigned long *sl = (unsigned long *) s;
415 unsigned long cl = 0;
416 char *s8;
417 int i;
wdenkc6097192002-11-03 00:24:07 +0000418
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200419 /* do it one word at a time (32 bits or 64 bits) while possible */
420 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
421 for (i = 0; i < sizeof(*sl); i++) {
422 cl <<= 8;
423 cl |= c & 0xff;
424 }
425 while (count >= sizeof(*sl)) {
426 *sl++ = cl;
427 count -= sizeof(*sl);
428 }
429 }
430 /* fill 8 bits at a time */
431 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000432 while (count--)
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200433 *s8++ = c;
wdenkc6097192002-11-03 00:24:07 +0000434
435 return s;
436}
437#endif
438
439#ifndef __HAVE_ARCH_BCOPY
440/**
441 * bcopy - Copy one area of memory to another
442 * @src: Where to copy from
443 * @dest: Where to copy to
444 * @count: The size of the area.
445 *
446 * Note that this is the same as memcpy(), with the arguments reversed.
447 * memcpy() is the standard, bcopy() is a legacy BSD function.
448 *
449 * You should not use this function to access IO space, use memcpy_toio()
450 * or memcpy_fromio() instead.
451 */
452char * bcopy(const char * src, char * dest, int count)
453{
454 char *tmp = dest;
455
456 while (count--)
457 *tmp++ = *src++;
458
459 return dest;
460}
461#endif
462
463#ifndef __HAVE_ARCH_MEMCPY
464/**
465 * memcpy - Copy one area of memory to another
466 * @dest: Where to copy to
467 * @src: Where to copy from
468 * @count: The size of the area.
469 *
470 * You should not use this function to access IO space, use memcpy_toio()
471 * or memcpy_fromio() instead.
472 */
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200473void * memcpy(void *dest, const void *src, size_t count)
wdenkc6097192002-11-03 00:24:07 +0000474{
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200475 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
476 char *d8, *s8;
wdenkc6097192002-11-03 00:24:07 +0000477
Matthias Weisserb038db82011-05-22 23:03:55 +0000478 if (src == dest)
479 return dest;
480
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200481 /* while all data is aligned (common case), copy a word at a time */
482 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
483 while (count >= sizeof(*dl)) {
484 *dl++ = *sl++;
485 count -= sizeof(*dl);
486 }
487 }
488 /* copy the reset one byte at a time */
489 d8 = (char *)dl;
490 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000491 while (count--)
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200492 *d8++ = *s8++;
wdenkc6097192002-11-03 00:24:07 +0000493
494 return dest;
495}
496#endif
497
498#ifndef __HAVE_ARCH_MEMMOVE
499/**
500 * memmove - Copy one area of memory to another
501 * @dest: Where to copy to
502 * @src: Where to copy from
503 * @count: The size of the area.
504 *
505 * Unlike memcpy(), memmove() copes with overlapping areas.
506 */
507void * memmove(void * dest,const void *src,size_t count)
508{
509 char *tmp, *s;
510
Matthias Weisserb038db82011-05-22 23:03:55 +0000511 if (src == dest)
512 return dest;
513
wdenkc6097192002-11-03 00:24:07 +0000514 if (dest <= src) {
515 tmp = (char *) dest;
516 s = (char *) src;
517 while (count--)
518 *tmp++ = *s++;
519 }
520 else {
521 tmp = (char *) dest + count;
522 s = (char *) src + count;
523 while (count--)
524 *--tmp = *--s;
525 }
526
527 return dest;
528}
529#endif
530
531#ifndef __HAVE_ARCH_MEMCMP
532/**
533 * memcmp - Compare two areas of memory
534 * @cs: One area of memory
535 * @ct: Another area of memory
536 * @count: The size of the area.
537 */
538int memcmp(const void * cs,const void * ct,size_t count)
539{
540 const unsigned char *su1, *su2;
541 int res = 0;
542
543 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
544 if ((res = *su1 - *su2) != 0)
545 break;
546 return res;
547}
548#endif
549
550#ifndef __HAVE_ARCH_MEMSCAN
551/**
552 * memscan - Find a character in an area of memory.
553 * @addr: The memory area
554 * @c: The byte to search for
555 * @size: The size of the area.
556 *
557 * returns the address of the first occurrence of @c, or 1 byte past
558 * the area if @c is not found
559 */
560void * memscan(void * addr, int c, size_t size)
561{
562 unsigned char * p = (unsigned char *) addr;
563
564 while (size) {
565 if (*p == c)
566 return (void *) p;
567 p++;
568 size--;
569 }
wdenk8bde7f72003-06-27 21:31:46 +0000570 return (void *) p;
wdenkc6097192002-11-03 00:24:07 +0000571}
572#endif
573
574#ifndef __HAVE_ARCH_STRSTR
575/**
576 * strstr - Find the first substring in a %NUL terminated string
577 * @s1: The string to be searched
578 * @s2: The string to search for
579 */
580char * strstr(const char * s1,const char * s2)
581{
582 int l1, l2;
583
584 l2 = strlen(s2);
585 if (!l2)
586 return (char *) s1;
587 l1 = strlen(s1);
588 while (l1 >= l2) {
589 l1--;
590 if (!memcmp(s1,s2,l2))
591 return (char *) s1;
592 s1++;
593 }
594 return NULL;
595}
596#endif
597
598#ifndef __HAVE_ARCH_MEMCHR
599/**
600 * memchr - Find a character in an area of memory.
601 * @s: The memory area
602 * @c: The byte to search for
603 * @n: The size of the area.
604 *
605 * returns the address of the first occurrence of @c, or %NULL
606 * if @c is not found
607 */
608void *memchr(const void *s, int c, size_t n)
609{
610 const unsigned char *p = s;
611 while (n-- != 0) {
wdenk8bde7f72003-06-27 21:31:46 +0000612 if ((unsigned char)c == *p++) {
wdenkc6097192002-11-03 00:24:07 +0000613 return (void *)(p-1);
614 }
615 }
616 return NULL;
617}
618
619#endif