string.c 12 KB

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