string.c 14 KB

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