PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/string.c

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