1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/types.h>
19#include <linux/string.h>
20#include <linux/ctype.h>
21
22#ifndef __HAVE_ARCH_STRNICMP
23
24
25
26
27
28
29int strnicmp(const char *s1, const char *s2, size_t len)
30{
31
32 unsigned char c1, c2;
33
34 c1 = 0; c2 = 0;
35 if (len) {
36 do {
37 c1 = *s1; c2 = *s2;
38 s1++; s2++;
39 if (!c1)
40 break;
41 if (!c2)
42 break;
43 if (c1 == c2)
44 continue;
45 c1 = tolower(c1);
46 c2 = tolower(c2);
47 if (c1 != c2)
48 break;
49 } while (--len);
50 }
51 return (int)c1 - (int)c2;
52}
53#endif
54
55char * ___strtok;
56
57#ifndef __HAVE_ARCH_STRCPY
58
59
60
61
62
63char * strcpy(char * dest,const char *src)
64{
65 char *tmp = dest;
66
67 while ((*dest++ = *src++) != '\0')
68 ;
69 return tmp;
70}
71#endif
72
73#ifndef __HAVE_ARCH_STRNCPY
74
75
76
77
78
79
80
81
82
83
84char * strncpy(char * dest,const char *src,size_t count)
85{
86 char *tmp = dest;
87
88 while (count-- && (*dest++ = *src++) != '\0')
89 ;
90
91 return tmp;
92}
93#endif
94
95#ifndef __HAVE_ARCH_STRCAT
96
97
98
99
100
101char * strcat(char * dest, const char * src)
102{
103 char *tmp = dest;
104
105 while (*dest)
106 dest++;
107 while ((*dest++ = *src++) != '\0')
108 ;
109
110 return tmp;
111}
112#endif
113
114#ifndef __HAVE_ARCH_STRNCAT
115
116
117
118
119
120
121
122
123
124char * strncat(char *dest, const char *src, size_t count)
125{
126 char *tmp = dest;
127
128 if (count) {
129 while (*dest)
130 dest++;
131 while ((*dest++ = *src++)) {
132 if (--count == 0) {
133 *dest = '\0';
134 break;
135 }
136 }
137 }
138
139 return tmp;
140}
141#endif
142
143#ifndef __HAVE_ARCH_STRCMP
144
145
146
147
148
149int strcmp(const char * cs,const char * ct)
150{
151 register signed char __res;
152
153 while (1) {
154 if ((__res = *cs - *ct++) != 0 || !*cs++)
155 break;
156 }
157
158 return __res;
159}
160#endif
161
162#ifndef __HAVE_ARCH_STRNCMP
163
164
165
166
167
168
169int strncmp(const char * cs,const char * ct,size_t count)
170{
171 register signed char __res = 0;
172
173 while (count) {
174 if ((__res = *cs - *ct++) != 0 || !*cs++)
175 break;
176 count--;
177 }
178
179 return __res;
180}
181#endif
182
183#ifndef __HAVE_ARCH_STRCHR
184
185
186
187
188
189char * strchr(const char * s, int c)
190{
191 for(; *s != (char) c; ++s)
192 if (*s == '\0')
193 return NULL;
194 return (char *) s;
195}
196#endif
197
198#ifndef __HAVE_ARCH_STRRCHR
199
200
201
202
203
204char * strrchr(const char * s, int c)
205{
206 const char *p = s + strlen(s);
207 do {
208 if (*p == (char)c)
209 return (char *)p;
210 } while (--p >= s);
211 return NULL;
212}
213#endif
214
215#ifndef __HAVE_ARCH_STRLEN
216
217
218
219
220size_t strlen(const char * s)
221{
222 const char *sc;
223
224 for (sc = s; *sc != '\0'; ++sc)
225 ;
226 return sc - s;
227}
228#endif
229
230#ifndef __HAVE_ARCH_STRNLEN
231
232
233
234
235
236size_t strnlen(const char * s, size_t count)
237{
238 const char *sc;
239
240 for (sc = s; count-- && *sc != '\0'; ++sc)
241 ;
242 return sc - s;
243}
244#endif
245
246#ifndef __HAVE_ARCH_STRSPN
247
248
249
250
251
252
253size_t strspn(const char *s, const char *accept)
254{
255 const char *p;
256 const char *a;
257 size_t count = 0;
258
259 for (p = s; *p != '\0'; ++p) {
260 for (a = accept; *a != '\0'; ++a) {
261 if (*p == *a)
262 break;
263 }
264 if (*a == '\0')
265 return count;
266 ++count;
267 }
268
269 return count;
270}
271#endif
272
273#ifndef __HAVE_ARCH_STRPBRK
274
275
276
277
278
279char * strpbrk(const char * cs,const char * ct)
280{
281 const char *sc1,*sc2;
282
283 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
284 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
285 if (*sc1 == *sc2)
286 return (char *) sc1;
287 }
288 }
289 return NULL;
290}
291#endif
292
293#ifndef __HAVE_ARCH_STRTOK
294
295
296
297
298
299
300
301char * strtok(char * s,const char * ct)
302{
303 char *sbegin, *send;
304
305 sbegin = s ? s : ___strtok;
306 if (!sbegin) {
307 return NULL;
308 }
309 sbegin += strspn(sbegin,ct);
310 if (*sbegin == '\0') {
311 ___strtok = NULL;
312 return( NULL );
313 }
314 send = strpbrk( sbegin, ct);
315 if (send && *send != '\0')
316 *send++ = '\0';
317 ___strtok = send;
318 return (sbegin);
319}
320#endif
321
322#ifndef __HAVE_ARCH_STRSEP
323
324
325
326
327
328
329
330
331
332
333
334char * strsep(char **s, const char *ct)
335{
336 char *sbegin = *s, *end;
337
338 if (sbegin == NULL)
339 return NULL;
340
341 end = strpbrk(sbegin, ct);
342 if (end)
343 *end++ = '\0';
344 *s = end;
345
346 return sbegin;
347}
348#endif
349
350#ifndef __HAVE_ARCH_MEMSET
351
352
353
354
355
356
357
358
359void * memset(void * s,int c,size_t count)
360{
361 char *xs = (char *) s;
362
363 while (count--)
364 *xs++ = c;
365
366 return s;
367}
368#endif
369
370#ifndef __HAVE_ARCH_BCOPY
371
372
373
374
375
376
377
378
379
380
381
382
383char * bcopy(const char * src, char * dest, int count)
384{
385 char *tmp = dest;
386
387 while (count--)
388 *tmp++ = *src++;
389
390 return dest;
391}
392#endif
393
394#ifndef __HAVE_ARCH_MEMCPY
395
396
397
398
399
400
401
402
403
404void * memcpy(void * dest,const void *src,size_t count)
405{
406 char *tmp = (char *) dest, *s = (char *) src;
407
408 while (count--)
409 *tmp++ = *s++;
410
411 return dest;
412}
413#endif
414
415#ifndef __HAVE_ARCH_MEMMOVE
416
417
418
419
420
421
422
423
424void * memmove(void * dest,const void *src,size_t count)
425{
426 char *tmp, *s;
427
428 if (dest <= src) {
429 tmp = (char *) dest;
430 s = (char *) src;
431 while (count--)
432 *tmp++ = *s++;
433 }
434 else {
435 tmp = (char *) dest + count;
436 s = (char *) src + count;
437 while (count--)
438 *--tmp = *--s;
439 }
440
441 return dest;
442}
443#endif
444
445#ifndef __HAVE_ARCH_MEMCMP
446
447
448
449
450
451
452int memcmp(const void * cs,const void * ct,size_t count)
453{
454 const unsigned char *su1, *su2;
455 signed char res = 0;
456
457 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
458 if ((res = *su1 - *su2) != 0)
459 break;
460 return res;
461}
462#endif
463
464#ifndef __HAVE_ARCH_MEMSCAN
465
466
467
468
469
470
471
472
473
474void * memscan(void * addr, int c, size_t size)
475{
476 unsigned char * p = (unsigned char *) addr;
477
478 while (size) {
479 if (*p == c)
480 return (void *) p;
481 p++;
482 size--;
483 }
484 return (void *) p;
485}
486#endif
487
488#ifndef __HAVE_ARCH_STRSTR
489
490
491
492
493
494char * strstr(const char * s1,const char * s2)
495{
496 int l1, l2;
497
498 l2 = strlen(s2);
499 if (!l2)
500 return (char *) s1;
501 l1 = strlen(s1);
502 while (l1 >= l2) {
503 l1--;
504 if (!memcmp(s1,s2,l2))
505 return (char *) s1;
506 s1++;
507 }
508 return NULL;
509}
510#endif
511
512#ifndef __HAVE_ARCH_MEMCHR
513
514
515
516
517
518
519
520
521
522void *memchr(const void *s, int c, size_t n)
523{
524 const unsigned char *p = s;
525 while (n-- != 0) {
526 if ((unsigned char)c == *p++) {
527 return (void *)(p-1);
528 }
529 }
530 return NULL;
531}
532
533#endif
534