string.c 12 KB

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