PageRenderTime 28ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/string.c

https://github.com/xoox/linux-2.6.18_pro500
C | 636 lines | 377 code | 57 blank | 202 comment | 97 complexity | 7f8a8a7814707260ffea0d14523ce338 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. 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
  335. * contain letters in @accept
  336. * @s: The string to be searched
  337. * @accept: The string to search for
  338. */
  339. size_t strspn(const char *s, const char *accept)
  340. {
  341. const char *p;
  342. const char *a;
  343. size_t count = 0;
  344. for (p = s; *p != '\0'; ++p) {
  345. for (a = accept; *a != '\0'; ++a) {
  346. if (*p == *a)
  347. break;
  348. }
  349. if (*a == '\0')
  350. return count;
  351. ++count;
  352. }
  353. return count;
  354. }
  355. EXPORT_SYMBOL(strspn);
  356. #endif
  357. #ifndef __HAVE_ARCH_STRCSPN
  358. /**
  359. * strcspn - Calculate the length of the initial substring of @s which does
  360. * not contain letters in @reject
  361. * @s: The string to be searched
  362. * @reject: The string to avoid
  363. */
  364. size_t strcspn(const char *s, const char *reject)
  365. {
  366. const char *p;
  367. const char *r;
  368. size_t count = 0;
  369. for (p = s; *p != '\0'; ++p) {
  370. for (r = reject; *r != '\0'; ++r) {
  371. if (*p == *r)
  372. return count;
  373. }
  374. ++count;
  375. }
  376. return count;
  377. }
  378. EXPORT_SYMBOL(strcspn);
  379. #endif
  380. #ifndef __HAVE_ARCH_STRPBRK
  381. /**
  382. * strpbrk - Find the first occurrence of a set of characters
  383. * @cs: The string to be searched
  384. * @ct: The characters to search for
  385. */
  386. char *strpbrk(const char *cs, const char *ct)
  387. {
  388. const char *sc1, *sc2;
  389. for (sc1 = cs; *sc1 != '\0'; ++sc1) {
  390. for (sc2 = ct; *sc2 != '\0'; ++sc2) {
  391. if (*sc1 == *sc2)
  392. return (char *)sc1;
  393. }
  394. }
  395. return NULL;
  396. }
  397. EXPORT_SYMBOL(strpbrk);
  398. #endif
  399. #ifndef __HAVE_ARCH_STRSEP
  400. /**
  401. * strsep - Split a string into tokens
  402. * @s: The string to be searched
  403. * @ct: The characters to search for
  404. *
  405. * strsep() updates @s to point after the token, ready for the next call.
  406. *
  407. * It returns empty tokens, too, behaving exactly like the libc function
  408. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  409. * Same semantics, slimmer shape. ;)
  410. */
  411. char *strsep(char **s, const char *ct)
  412. {
  413. char *sbegin = *s;
  414. char *end;
  415. if (sbegin == NULL)
  416. return NULL;
  417. end = strpbrk(sbegin, ct);
  418. if (end)
  419. *end++ = '\0';
  420. *s = end;
  421. return sbegin;
  422. }
  423. EXPORT_SYMBOL(strsep);
  424. #endif
  425. #ifndef __HAVE_ARCH_MEMSET
  426. /**
  427. * memset - Fill a region of memory with the given value
  428. * @s: Pointer to the start of the area.
  429. * @c: The byte to fill the area with
  430. * @count: The size of the area.
  431. *
  432. * Do not use memset() to access IO space, use memset_io() instead.
  433. */
  434. void *memset(void *s, int c, size_t count)
  435. {
  436. char *xs = s;
  437. while (count--)
  438. *xs++ = c;
  439. return s;
  440. }
  441. EXPORT_SYMBOL(memset);
  442. #endif
  443. #ifndef __HAVE_ARCH_MEMCPY
  444. /**
  445. * memcpy - Copy one area of memory to another
  446. * @dest: Where to copy to
  447. * @src: Where to copy from
  448. * @count: The size of the area.
  449. *
  450. * You should not use this function to access IO space, use memcpy_toio()
  451. * or memcpy_fromio() instead.
  452. */
  453. void *memcpy(void *dest, const void *src, size_t count)
  454. {
  455. char *tmp = dest;
  456. const char *s = src;
  457. while (count--)
  458. *tmp++ = *s++;
  459. return dest;
  460. }
  461. EXPORT_SYMBOL(memcpy);
  462. #endif
  463. #ifndef __HAVE_ARCH_MEMMOVE
  464. /**
  465. * memmove - Copy one area of memory to another
  466. * @dest: Where to copy to
  467. * @src: Where to copy from
  468. * @count: The size of the area.
  469. *
  470. * Unlike memcpy(), memmove() copes with overlapping areas.
  471. */
  472. void *memmove(void *dest, const void *src, size_t count)
  473. {
  474. char *tmp;
  475. const char *s;
  476. if (dest <= src) {
  477. tmp = dest;
  478. s = src;
  479. while (count--)
  480. *tmp++ = *s++;
  481. } else {
  482. tmp = dest;
  483. tmp += count;
  484. s = src;
  485. s += count;
  486. while (count--)
  487. *--tmp = *--s;
  488. }
  489. return dest;
  490. }
  491. EXPORT_SYMBOL(memmove);
  492. #endif
  493. #ifndef __HAVE_ARCH_MEMCMP
  494. /**
  495. * memcmp - Compare two areas of memory
  496. * @cs: One area of memory
  497. * @ct: Another area of memory
  498. * @count: The size of the area.
  499. */
  500. #undef memcmp
  501. int memcmp(const void *cs, const void *ct, size_t count)
  502. {
  503. const unsigned char *su1, *su2;
  504. int res = 0;
  505. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  506. if ((res = *su1 - *su2) != 0)
  507. break;
  508. return res;
  509. }
  510. EXPORT_SYMBOL(memcmp);
  511. #endif
  512. #ifndef __HAVE_ARCH_MEMSCAN
  513. /**
  514. * memscan - Find a character in an area of memory.
  515. * @addr: The memory area
  516. * @c: The byte to search for
  517. * @size: The size of the area.
  518. *
  519. * returns the address of the first occurrence of @c, or 1 byte past
  520. * the area if @c is not found
  521. */
  522. void *memscan(void *addr, int c, size_t size)
  523. {
  524. unsigned char *p = addr;
  525. while (size) {
  526. if (*p == c)
  527. return (void *)p;
  528. p++;
  529. size--;
  530. }
  531. return (void *)p;
  532. }
  533. EXPORT_SYMBOL(memscan);
  534. #endif
  535. #ifndef __HAVE_ARCH_STRSTR
  536. /**
  537. * strstr - Find the first substring in a %NUL terminated string
  538. * @s1: The string to be searched
  539. * @s2: The string to search for
  540. */
  541. char *strstr(const char *s1, const char *s2)
  542. {
  543. int l1, l2;
  544. l2 = strlen(s2);
  545. if (!l2)
  546. return (char *)s1;
  547. l1 = strlen(s1);
  548. while (l1 >= l2) {
  549. l1--;
  550. if (!memcmp(s1, s2, l2))
  551. return (char *)s1;
  552. s1++;
  553. }
  554. return NULL;
  555. }
  556. EXPORT_SYMBOL(strstr);
  557. #endif
  558. #ifndef __HAVE_ARCH_MEMCHR
  559. /**
  560. * memchr - Find a character in an area of memory.
  561. * @s: The memory area
  562. * @c: The byte to search for
  563. * @n: The size of the area.
  564. *
  565. * returns the address of the first occurrence of @c, or %NULL
  566. * if @c is not found
  567. */
  568. void *memchr(const void *s, int c, size_t n)
  569. {
  570. const unsigned char *p = s;
  571. while (n-- != 0) {
  572. if ((unsigned char)c == *p++) {
  573. return (void *)(p - 1);
  574. }
  575. }
  576. return NULL;
  577. }
  578. EXPORT_SYMBOL(memchr);
  579. #endif