string.c 14 KB

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