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