1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <config.h>
19#include <linux/types.h>
20#include <linux/string.h>
21#include <linux/ctype.h>
22#include <malloc.h>
23
24
25
26
27
28
29
30
31int strncasecmp(const char *s1, const char *s2, size_t len)
32{
33
34 unsigned char c1, c2;
35
36 c1 = 0; c2 = 0;
37 if (len) {
38 do {
39 c1 = *s1; c2 = *s2;
40 s1++; s2++;
41 if (!c1)
42 break;
43 if (!c2)
44 break;
45 if (c1 == c2)
46 continue;
47 c1 = tolower(c1);
48 c2 = tolower(c2);
49 if (c1 != c2)
50 break;
51 } while (--len);
52 }
53 return (int)c1 - (int)c2;
54}
55
56
57
58
59
60
61int strcasecmp(const char *s1, const char *s2)
62{
63 return strncasecmp(s1, s2, -1U);
64}
65
66char * ___strtok;
67
68#ifndef __HAVE_ARCH_STRCPY
69
70
71
72
73
74char * strcpy(char * dest,const char *src)
75{
76 char *tmp = dest;
77
78 while ((*dest++ = *src++) != '\0')
79 ;
80 return tmp;
81}
82#endif
83
84#ifndef __HAVE_ARCH_STRNCPY
85
86
87
88
89
90
91
92
93
94
95char * strncpy(char * dest,const char *src,size_t count)
96{
97 char *tmp = dest;
98
99 while (count-- && (*dest++ = *src++) != '\0')
100 ;
101
102 return tmp;
103}
104#endif
105
106#ifndef __HAVE_ARCH_STRLCPY
107
108
109
110
111
112
113
114
115
116
117
118size_t strlcpy(char *dest, const char *src, size_t size)
119{
120 size_t ret = strlen(src);
121
122 if (size) {
123 size_t len = (ret >= size) ? size - 1 : ret;
124 memcpy(dest, src, len);
125 dest[len] = '\0';
126 }
127 return ret;
128}
129#endif
130
131#ifndef __HAVE_ARCH_STRCAT
132
133
134
135
136
137char * strcat(char * dest, const char * src)
138{
139 char *tmp = dest;
140
141 while (*dest)
142 dest++;
143 while ((*dest++ = *src++) != '\0')
144 ;
145
146 return tmp;
147}
148#endif
149
150#ifndef __HAVE_ARCH_STRNCAT
151
152
153
154
155
156
157
158
159
160char * strncat(char *dest, const char *src, size_t count)
161{
162 char *tmp = dest;
163
164 if (count) {
165 while (*dest)
166 dest++;
167 while ((*dest++ = *src++)) {
168 if (--count == 0) {
169 *dest = '\0';
170 break;
171 }
172 }
173 }
174
175 return tmp;
176}
177#endif
178
179#ifndef __HAVE_ARCH_STRCMP
180
181
182
183
184
185int strcmp(const char * cs,const char * ct)
186{
187 register signed char __res;
188
189 while (1) {
190 if ((__res = *cs - *ct++) != 0 || !*cs++)
191 break;
192 }
193
194 return __res;
195}
196#endif
197
198#ifndef __HAVE_ARCH_STRNCMP
199
200
201
202
203
204
205int strncmp(const char * cs,const char * ct,size_t count)
206{
207 register signed char __res = 0;
208
209 while (count) {
210 if ((__res = *cs - *ct++) != 0 || !*cs++)
211 break;
212 count--;
213 }
214
215 return __res;
216}
217#endif
218
219#ifndef __HAVE_ARCH_STRCHR
220
221
222
223
224
225char * strchr(const char * s, int c)
226{
227 for(; *s != (char) c; ++s)
228 if (*s == '\0')
229 return NULL;
230 return (char *) s;
231}
232#endif
233
234const char *strchrnul(const char *s, int c)
235{
236 for (; *s != (char)c; ++s)
237 if (*s == '\0')
238 break;
239 return s;
240}
241
242#ifndef __HAVE_ARCH_STRRCHR
243
244
245
246
247
248char * strrchr(const char * s, int c)
249{
250 const char *p = s + strlen(s);
251 do {
252 if (*p == (char)c)
253 return (char *)p;
254 } while (--p >= s);
255 return NULL;
256}
257#endif
258
259#ifndef __HAVE_ARCH_STRLEN
260
261
262
263
264size_t strlen(const char * s)
265{
266 const char *sc;
267
268 for (sc = s; *sc != '\0'; ++sc)
269 ;
270 return sc - s;
271}
272#endif
273
274#ifndef __HAVE_ARCH_STRNLEN
275
276
277
278
279
280size_t strnlen(const char * s, size_t count)
281{
282 const char *sc;
283
284 for (sc = s; count-- && *sc != '\0'; ++sc)
285 ;
286 return sc - s;
287}
288#endif
289
290#ifndef __HAVE_ARCH_STRCSPN
291
292
293
294
295
296
297size_t strcspn(const char *s, const char *reject)
298{
299 const char *p;
300 const char *r;
301 size_t count = 0;
302
303 for (p = s; *p != '\0'; ++p) {
304 for (r = reject; *r != '\0'; ++r) {
305 if (*p == *r)
306 return count;
307 }
308 ++count;
309 }
310 return count;
311}
312#endif
313
314#ifndef __HAVE_ARCH_STRDUP
315char * strdup(const char *s)
316{
317 char *new;
318
319 if ((s == NULL) ||
320 ((new = malloc (strlen(s) + 1)) == NULL) ) {
321 return NULL;
322 }
323
324 strcpy (new, s);
325 return new;
326}
327#endif
328
329#ifndef __HAVE_ARCH_STRSPN
330
331
332
333
334
335
336size_t strspn(const char *s, const char *accept)
337{
338 const char *p;
339 const char *a;
340 size_t count = 0;
341
342 for (p = s; *p != '\0'; ++p) {
343 for (a = accept; *a != '\0'; ++a) {
344 if (*p == *a)
345 break;
346 }
347 if (*a == '\0')
348 return count;
349 ++count;
350 }
351
352 return count;
353}
354#endif
355
356#ifndef __HAVE_ARCH_STRPBRK
357
358
359
360
361
362char * strpbrk(const char * cs,const char * ct)
363{
364 const char *sc1,*sc2;
365
366 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
367 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
368 if (*sc1 == *sc2)
369 return (char *) sc1;
370 }
371 }
372 return NULL;
373}
374#endif
375
376#ifndef __HAVE_ARCH_STRTOK
377
378
379
380
381
382
383
384char * strtok(char * s,const char * ct)
385{
386 char *sbegin, *send;
387
388 sbegin = s ? s : ___strtok;
389 if (!sbegin) {
390 return NULL;
391 }
392 sbegin += strspn(sbegin,ct);
393 if (*sbegin == '\0') {
394 ___strtok = NULL;
395 return( NULL );
396 }
397 send = strpbrk( sbegin, ct);
398 if (send && *send != '\0')
399 *send++ = '\0';
400 ___strtok = send;
401 return (sbegin);
402}
403#endif
404
405#ifndef __HAVE_ARCH_STRSEP
406
407
408
409
410
411
412
413
414
415
416
417char * strsep(char **s, const char *ct)
418{
419 char *sbegin = *s, *end;
420
421 if (sbegin == NULL)
422 return NULL;
423
424 end = strpbrk(sbegin, ct);
425 if (end)
426 *end++ = '\0';
427 *s = end;
428
429 return sbegin;
430}
431#endif
432
433#ifndef __HAVE_ARCH_STRSWAB
434
435
436
437
438
439
440
441char *strswab(const char *s)
442{
443 char *p, *q;
444
445 if ((NULL == s) || ('\0' == *s)) {
446 return (NULL);
447 }
448
449 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
450 char tmp;
451
452 tmp = *p;
453 *p = *q;
454 *q = tmp;
455 }
456
457 return (char *) s;
458}
459#endif
460
461#ifndef __HAVE_ARCH_MEMSET
462
463
464
465
466
467
468
469
470void * memset(void * s,int c,size_t count)
471{
472 unsigned long *sl = (unsigned long *) s;
473 char *s8;
474
475#if !CONFIG_IS_ENABLED(TINY_MEMSET)
476 unsigned long cl = 0;
477 int i;
478
479
480 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
481 for (i = 0; i < sizeof(*sl); i++) {
482 cl <<= 8;
483 cl |= c & 0xff;
484 }
485 while (count >= sizeof(*sl)) {
486 *sl++ = cl;
487 count -= sizeof(*sl);
488 }
489 }
490#endif
491 s8 = (char *)sl;
492 while (count--)
493 *s8++ = c;
494
495 return s;
496}
497#endif
498
499#ifndef __HAVE_ARCH_MEMCPY
500
501
502
503
504
505
506
507
508
509void * memcpy(void *dest, const void *src, size_t count)
510{
511 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
512 char *d8, *s8;
513
514 if (src == dest)
515 return dest;
516
517
518 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
519 while (count >= sizeof(*dl)) {
520 *dl++ = *sl++;
521 count -= sizeof(*dl);
522 }
523 }
524
525 d8 = (char *)dl;
526 s8 = (char *)sl;
527 while (count--)
528 *d8++ = *s8++;
529
530 return dest;
531}
532#endif
533
534#ifndef __HAVE_ARCH_MEMMOVE
535
536
537
538
539
540
541
542
543void * memmove(void * dest,const void *src,size_t count)
544{
545 char *tmp, *s;
546
547 if (dest <= src) {
548 memcpy(dest, src, count);
549 } else {
550 tmp = (char *) dest + count;
551 s = (char *) src + count;
552 while (count--)
553 *--tmp = *--s;
554 }
555
556 return dest;
557}
558#endif
559
560#ifndef __HAVE_ARCH_MEMCMP
561
562
563
564
565
566
567int memcmp(const void * cs,const void * ct,size_t count)
568{
569 const unsigned char *su1, *su2;
570 int res = 0;
571
572 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
573 if ((res = *su1 - *su2) != 0)
574 break;
575 return res;
576}
577#endif
578
579#ifndef __HAVE_ARCH_MEMSCAN
580
581
582
583
584
585
586
587
588
589void * memscan(void * addr, int c, size_t size)
590{
591 unsigned char * p = (unsigned char *) addr;
592
593 while (size) {
594 if (*p == c)
595 return (void *) p;
596 p++;
597 size--;
598 }
599 return (void *) p;
600}
601#endif
602
603#ifndef __HAVE_ARCH_STRSTR
604
605
606
607
608
609char * strstr(const char * s1,const char * s2)
610{
611 int l1, l2;
612
613 l2 = strlen(s2);
614 if (!l2)
615 return (char *) s1;
616 l1 = strlen(s1);
617 while (l1 >= l2) {
618 l1--;
619 if (!memcmp(s1,s2,l2))
620 return (char *) s1;
621 s1++;
622 }
623 return NULL;
624}
625#endif
626
627#ifndef __HAVE_ARCH_MEMCHR
628
629
630
631
632
633
634
635
636
637void *memchr(const void *s, int c, size_t n)
638{
639 const unsigned char *p = s;
640 while (n-- != 0) {
641 if ((unsigned char)c == *p++) {
642 return (void *)(p-1);
643 }
644 }
645 return NULL;
646}
647
648#endif
649#ifndef __HAVE_ARCH_MEMCHR_INV
650static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
651{
652 while (bytes) {
653 if (*start != value)
654 return (void *)start;
655 start++;
656 bytes--;
657 }
658 return NULL;
659}
660
661
662
663
664
665
666
667
668
669void *memchr_inv(const void *start, int c, size_t bytes)
670{
671 u8 value = c;
672 u64 value64;
673 unsigned int words, prefix;
674
675 if (bytes <= 16)
676 return check_bytes8(start, value, bytes);
677
678 value64 = value;
679 value64 |= value64 << 8;
680 value64 |= value64 << 16;
681 value64 |= value64 << 32;
682
683 prefix = (unsigned long)start % 8;
684 if (prefix) {
685 u8 *r;
686
687 prefix = 8 - prefix;
688 r = check_bytes8(start, value, prefix);
689 if (r)
690 return r;
691 start += prefix;
692 bytes -= prefix;
693 }
694
695 words = bytes / 8;
696
697 while (words) {
698 if (*(u64 *)start != value64)
699 return check_bytes8(start, value, 8);
700 start += 8;
701 words--;
702 }
703
704 return check_bytes8(start, value, bytes % 8);
705}
706#endif
707