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 @s.
  313. * @s: The string to be stripped.
  314. *
  315. * Returns a pointer to the first non-whitespace character in @s.
  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. * strstrip - 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 *strstrip(char *s)
  333. {
  334. size_t size;
  335. char *end;
  336. size = strlen(s);
  337. if (!size)
  338. return s;
  339. end = s + size - 1;
  340. while (end >= s && isspace(*end))
  341. end--;
  342. *(end + 1) = '\0';
  343. return skip_spaces(s);
  344. }
  345. EXPORT_SYMBOL(strstrip);
  346. #ifndef __HAVE_ARCH_STRLEN
  347. /**
  348. * strlen - Find the length of a string
  349. * @s: The string to be sized
  350. */
  351. size_t strlen(const char *s)
  352. {
  353. const char *sc;
  354. for (sc = s; *sc != '\0'; ++sc)
  355. /* nothing */;
  356. return sc - s;
  357. }
  358. EXPORT_SYMBOL(strlen);
  359. #endif
  360. #ifndef __HAVE_ARCH_STRNLEN
  361. /**
  362. * strnlen - Find the length of a length-limited string
  363. * @s: The string to be sized
  364. * @count: The maximum number of bytes to search
  365. */
  366. size_t strnlen(const char *s, size_t count)
  367. {
  368. const char *sc;
  369. for (sc = s; count-- && *sc != '\0'; ++sc)
  370. /* nothing */;
  371. return sc - s;
  372. }
  373. EXPORT_SYMBOL(strnlen);
  374. #endif
  375. #ifndef __HAVE_ARCH_STRSPN
  376. /**
  377. * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
  378. * @s: The string to be searched
  379. * @accept: The string to search for
  380. */
  381. size_t strspn(const char *s, const char *accept)
  382. {
  383. const char *p;
  384. const char *a;
  385. size_t count = 0;
  386. for (p = s; *p != '\0'; ++p) {
  387. for (a = accept; *a != '\0'; ++a) {
  388. if (*p == *a)
  389. break;
  390. }
  391. if (*a == '\0')
  392. return count;
  393. ++count;
  394. }
  395. return count;
  396. }
  397. EXPORT_SYMBOL(strspn);
  398. #endif
  399. #ifndef __HAVE_ARCH_STRCSPN
  400. /**
  401. * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
  402. * @s: The string to be searched
  403. * @reject: The string to avoid
  404. */
  405. size_t strcspn(const char *s, const char *reject)
  406. {
  407. const char *p;
  408. const char *r;
  409. size_t count = 0;
  410. for (p = s; *p != '\0'; ++p) {
  411. for (r = reject; *r != '\0'; ++r) {
  412. if (*p == *r)
  413. return count;
  414. }
  415. ++count;
  416. }
  417. return count;
  418. }
  419. EXPORT_SYMBOL(strcspn);
  420. #endif
  421. #ifndef __HAVE_ARCH_STRPBRK
  422. /**
  423. * strpbrk - Find the first occurrence of a set of characters
  424. * @cs: The string to be searched
  425. * @ct: The characters to search for
  426. */
  427. char *strpbrk(const char *cs, const char *ct)
  428. {
  429. const char *sc1, *sc2;
  430. for (sc1 = cs; *sc1 != '\0'; ++sc1) {
  431. for (sc2 = ct; *sc2 != '\0'; ++sc2) {
  432. if (*sc1 == *sc2)
  433. return (char *)sc1;
  434. }
  435. }
  436. return NULL;
  437. }
  438. EXPORT_SYMBOL(strpbrk);
  439. #endif
  440. #ifndef __HAVE_ARCH_STRSEP
  441. /**
  442. * strsep - Split a string into tokens
  443. * @s: The string to be searched
  444. * @ct: The characters to search for
  445. *
  446. * strsep() updates @s to point after the token, ready for the next call.
  447. *
  448. * It returns empty tokens, too, behaving exactly like the libc function
  449. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  450. * Same semantics, slimmer shape. ;)
  451. */
  452. char *strsep(char **s, const char *ct)
  453. {
  454. char *sbegin = *s;
  455. char *end;
  456. if (sbegin == NULL)
  457. return NULL;
  458. end = strpbrk(sbegin, ct);
  459. if (end)
  460. *end++ = '\0';
  461. *s = end;
  462. return sbegin;
  463. }
  464. EXPORT_SYMBOL(strsep);
  465. #endif
  466. /**
  467. * sysfs_streq - return true if strings are equal, modulo trailing newline
  468. * @s1: one string
  469. * @s2: another string
  470. *
  471. * This routine returns true iff two strings are equal, treating both
  472. * NUL and newline-then-NUL as equivalent string terminations. It's
  473. * geared for use with sysfs input strings, which generally terminate
  474. * with newlines but are compared against values without newlines.
  475. */
  476. bool sysfs_streq(const char *s1, const char *s2)
  477. {
  478. while (*s1 && *s1 == *s2) {
  479. s1++;
  480. s2++;
  481. }
  482. if (*s1 == *s2)
  483. return true;
  484. if (!*s1 && *s2 == '\n' && !s2[1])
  485. return true;
  486. if (*s1 == '\n' && !s1[1] && !*s2)
  487. return true;
  488. return false;
  489. }
  490. EXPORT_SYMBOL(sysfs_streq);
  491. #ifndef __HAVE_ARCH_MEMSET
  492. /**
  493. * memset - Fill a region of memory with the given value
  494. * @s: Pointer to the start of the area.
  495. * @c: The byte to fill the area with
  496. * @count: The size of the area.
  497. *
  498. * Do not use memset() to access IO space, use memset_io() instead.
  499. */
  500. void *memset(void *s, int c, size_t count)
  501. {
  502. char *xs = s;
  503. while (count--)
  504. *xs++ = c;
  505. return s;
  506. }
  507. EXPORT_SYMBOL(memset);
  508. #endif
  509. #ifndef __HAVE_ARCH_MEMCPY
  510. /**
  511. * memcpy - Copy one area of memory to another
  512. * @dest: Where to copy to
  513. * @src: Where to copy from
  514. * @count: The size of the area.
  515. *
  516. * You should not use this function to access IO space, use memcpy_toio()
  517. * or memcpy_fromio() instead.
  518. */
  519. void *memcpy(void *dest, const void *src, size_t count)
  520. {
  521. char *tmp = dest;
  522. const char *s = src;
  523. while (count--)
  524. *tmp++ = *s++;
  525. return dest;
  526. }
  527. EXPORT_SYMBOL(memcpy);
  528. #endif
  529. #ifndef __HAVE_ARCH_MEMMOVE
  530. /**
  531. * memmove - Copy one area of memory to another
  532. * @dest: Where to copy to
  533. * @src: Where to copy from
  534. * @count: The size of the area.
  535. *
  536. * Unlike memcpy(), memmove() copes with overlapping areas.
  537. */
  538. void *memmove(void *dest, const void *src, size_t count)
  539. {
  540. char *tmp;
  541. const char *s;
  542. if (dest <= src) {
  543. tmp = dest;
  544. s = src;
  545. while (count--)
  546. *tmp++ = *s++;
  547. } else {
  548. tmp = dest;
  549. tmp += count;
  550. s = src;
  551. s += count;
  552. while (count--)
  553. *--tmp = *--s;
  554. }
  555. return dest;
  556. }
  557. EXPORT_SYMBOL(memmove);
  558. #endif
  559. #ifndef __HAVE_ARCH_MEMCMP
  560. /**
  561. * memcmp - Compare two areas of memory
  562. * @cs: One area of memory
  563. * @ct: Another area of memory
  564. * @count: The size of the area.
  565. */
  566. #undef memcmp
  567. int memcmp(const void *cs, const void *ct, size_t count)
  568. {
  569. const unsigned char *su1, *su2;
  570. int res = 0;
  571. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  572. if ((res = *su1 - *su2) != 0)
  573. break;
  574. return res;
  575. }
  576. EXPORT_SYMBOL(memcmp);
  577. #endif
  578. #ifndef __HAVE_ARCH_MEMSCAN
  579. /**
  580. * memscan - Find a character in an area of memory.
  581. * @addr: The memory area
  582. * @c: The byte to search for
  583. * @size: The size of the area.
  584. *
  585. * returns the address of the first occurrence of @c, or 1 byte past
  586. * the area if @c is not found
  587. */
  588. void *memscan(void *addr, int c, size_t size)
  589. {
  590. unsigned char *p = addr;
  591. while (size) {
  592. if (*p == c)
  593. return (void *)p;
  594. p++;
  595. size--;
  596. }
  597. return (void *)p;
  598. }
  599. EXPORT_SYMBOL(memscan);
  600. #endif
  601. #ifndef __HAVE_ARCH_STRSTR
  602. /**
  603. * strstr - Find the first substring in a %NUL terminated string
  604. * @s1: The string to be searched
  605. * @s2: The string to search for
  606. */
  607. char *strstr(const char *s1, const char *s2)
  608. {
  609. int l1, l2;
  610. l2 = strlen(s2);
  611. if (!l2)
  612. return (char *)s1;
  613. l1 = strlen(s1);
  614. while (l1 >= l2) {
  615. l1--;
  616. if (!memcmp(s1, s2, l2))
  617. return (char *)s1;
  618. s1++;
  619. }
  620. return NULL;
  621. }
  622. EXPORT_SYMBOL(strstr);
  623. #endif
  624. #ifndef __HAVE_ARCH_MEMCHR
  625. /**
  626. * memchr - Find a character in an area of memory.
  627. * @s: The memory area
  628. * @c: The byte to search for
  629. * @n: The size of the area.
  630. *
  631. * returns the address of the first occurrence of @c, or %NULL
  632. * if @c is not found
  633. */
  634. void *memchr(const void *s, int c, size_t n)
  635. {
  636. const unsigned char *p = s;
  637. while (n-- != 0) {
  638. if ((unsigned char)c == *p++) {
  639. return (void *)(p - 1);
  640. }
  641. }
  642. return NULL;
  643. }
  644. EXPORT_SYMBOL(memchr);
  645. #endif