string.c 12 KB

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