string.c 14 KB

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