string.c 12 KB

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