string.c 12 KB

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