string.c 14 KB

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