string.c 11 KB

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