您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

string.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2004 Tobias Lorenz
  4. *
  5. * string handling functions
  6. * based on linux/lib/string.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * stupid library routines.. The optimized versions should generally be found
  14. * as inline code in <asm-xx/string.h>
  15. *
  16. * These are buggy as well..
  17. *
  18. * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
  19. * - Added strsep() which will replace strtok() soon (because strsep() is
  20. * reentrant and should be faster). Use only strsep() in new code, please.
  21. */
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. /* *** FROM string.c *** */
  27. #ifndef __HAVE_ARCH_STRNICMP
  28. /**
  29. * strnicmp - Case insensitive, length-limited string comparison
  30. * @s1: One string
  31. * @s2: The other string
  32. * @len: the maximum number of characters to compare
  33. */
  34. int strnicmp(const char *s1, const char *s2, size_t len)
  35. {
  36. /* Yes, Virginia, it had better be unsigned */
  37. unsigned char c1, c2;
  38. c1 = 0; c2 = 0;
  39. if (len) {
  40. do {
  41. c1 = *s1; c2 = *s2;
  42. s1++; s2++;
  43. if (!c1)
  44. break;
  45. if (!c2)
  46. break;
  47. if (c1 == c2)
  48. continue;
  49. c1 = tolower(c1);
  50. c2 = tolower(c2);
  51. if (c1 != c2)
  52. break;
  53. } while (--len);
  54. }
  55. return (int)c1 - (int)c2;
  56. }
  57. #endif
  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_STRSPN
  226. /**
  227. * strspn - Calculate the length of the initial substring of @s which only
  228. * contain letters in @accept
  229. * @s: The string to be searched
  230. * @accept: The string to search for
  231. */
  232. size_t strspn(const char *s, const char *accept)
  233. {
  234. const char *p;
  235. const char *a;
  236. size_t count = 0;
  237. for (p = s; *p != '\0'; ++p) {
  238. for (a = accept; *a != '\0'; ++a) {
  239. if (*p == *a)
  240. break;
  241. }
  242. if (*a == '\0')
  243. return count;
  244. ++count;
  245. }
  246. return count;
  247. }
  248. #endif
  249. #ifndef __HAVE_ARCH_STRCSPN
  250. /**
  251. * strcspn - Calculate the length of the initial substring of @s which only
  252. * contain letters not in @reject
  253. * @s: The string to be searched
  254. * @accept: The string to search for
  255. */
  256. size_t strcspn(const char *s, const char *reject)
  257. {
  258. const char *p;
  259. const char *r;
  260. size_t count = 0;
  261. for (p = s; *p != '\0'; ++p) {
  262. for (r = reject; *r != '\0'; ++r) {
  263. if (*p == *r)
  264. return count;
  265. }
  266. ++count;
  267. }
  268. return count;
  269. }
  270. #endif
  271. #ifndef __HAVE_ARCH_STRPBRK
  272. /**
  273. * strpbrk - Find the first occurrence of a set of characters
  274. * @cs: The string to be searched
  275. * @ct: The characters to search for
  276. */
  277. char * strpbrk(const char * cs,const char * ct)
  278. {
  279. const char *sc1,*sc2;
  280. for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  281. for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  282. if (*sc1 == *sc2)
  283. return (char *) sc1;
  284. }
  285. }
  286. return NULL;
  287. }
  288. #endif
  289. #ifndef __HAVE_ARCH_STRTOK
  290. /**
  291. * strtok - Split a string into tokens
  292. * @s: The string to be searched
  293. * @ct: The characters to search for
  294. *
  295. * WARNING: strtok is deprecated, use strsep instead.
  296. */
  297. char * strtok(char * s,const char * ct)
  298. {
  299. char *sbegin, *send;
  300. sbegin = s ? s : ___strtok;
  301. if (!sbegin) {
  302. return NULL;
  303. }
  304. sbegin += strspn(sbegin,ct);
  305. if (*sbegin == '\0') {
  306. ___strtok = NULL;
  307. return( NULL );
  308. }
  309. send = strpbrk( sbegin, ct);
  310. if (send && *send != '\0')
  311. *send++ = '\0';
  312. ___strtok = send;
  313. return (sbegin);
  314. }
  315. #endif
  316. #ifndef __HAVE_ARCH_STRSEP
  317. /**
  318. * strsep - Split a string into tokens
  319. * @s: The string to be searched
  320. * @ct: The characters to search for
  321. *
  322. * strsep() updates @s to point after the token, ready for the next call.
  323. *
  324. * It returns empty tokens, too, behaving exactly like the libc function
  325. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  326. * Same semantics, slimmer shape. ;)
  327. */
  328. char * strsep(char **s, const char *ct)
  329. {
  330. char *sbegin = *s, *end;
  331. if (sbegin == NULL)
  332. return NULL;
  333. end = strpbrk(sbegin, ct);
  334. if (end)
  335. *end++ = '\0';
  336. *s = end;
  337. return sbegin;
  338. }
  339. #endif
  340. #ifndef __HAVE_ARCH_MEMSET
  341. /**
  342. * memset - Fill a region of memory with the given value
  343. * @s: Pointer to the start of the area.
  344. * @c: The byte to fill the area with
  345. * @count: The size of the area.
  346. *
  347. * Do not use memset() to access IO space, use memset_io() instead.
  348. */
  349. void * memset(void * s,int c,size_t count)
  350. {
  351. char *xs = (char *) s;
  352. while (count--)
  353. *xs++ = c;
  354. return s;
  355. }
  356. #endif
  357. #ifndef __HAVE_ARCH_BCOPY
  358. /**
  359. * bcopy - Copy one area of memory to another
  360. * @src: Where to copy from
  361. * @dest: Where to copy to
  362. * @count: The size of the area.
  363. *
  364. * Note that this is the same as memcpy(), with the arguments reversed.
  365. * memcpy() is the standard, bcopy() is a legacy BSD function.
  366. *
  367. * You should not use this function to access IO space, use memcpy_toio()
  368. * or memcpy_fromio() instead.
  369. */
  370. char * bcopy(const char * src, char * dest, int count)
  371. {
  372. char *tmp = dest;
  373. while (count--)
  374. *tmp++ = *src++;
  375. return dest;
  376. }
  377. #endif
  378. #ifndef __HAVE_ARCH_MEMCPY
  379. /**
  380. * memcpy - Copy one area of memory to another
  381. * @dest: Where to copy to
  382. * @src: Where to copy from
  383. * @count: The size of the area.
  384. *
  385. * You should not use this function to access IO space, use memcpy_toio()
  386. * or memcpy_fromio() instead.
  387. */
  388. void * memcpy(void * dest,const void *src,size_t count)
  389. {
  390. char *tmp = (char *) dest, *s = (char *) src;
  391. while (count--)
  392. *tmp++ = *s++;
  393. return dest;
  394. }
  395. #endif
  396. #ifndef __HAVE_ARCH_MEMMOVE
  397. /**
  398. * memmove - Copy one area of memory to another
  399. * @dest: Where to copy to
  400. * @src: Where to copy from
  401. * @count: The size of the area.
  402. *
  403. * Unlike memcpy(), memmove() copes with overlapping areas.
  404. */
  405. void * memmove(void * dest,const void *src,size_t count)
  406. {
  407. char *tmp, *s;
  408. if (dest <= src) {
  409. tmp = (char *) dest;
  410. s = (char *) src;
  411. while (count--)
  412. *tmp++ = *s++;
  413. }
  414. else {
  415. tmp = (char *) dest + count;
  416. s = (char *) src + count;
  417. while (count--)
  418. *--tmp = *--s;
  419. }
  420. return dest;
  421. }
  422. #endif
  423. #ifndef __HAVE_ARCH_MEMCMP
  424. /**
  425. * memcmp - Compare two areas of memory
  426. * @cs: One area of memory
  427. * @ct: Another area of memory
  428. * @count: The size of the area.
  429. */
  430. int memcmp(const void * cs,const void * ct,size_t count)
  431. {
  432. const unsigned char *su1, *su2;
  433. int res = 0;
  434. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  435. if ((res = *su1 - *su2) != 0)
  436. break;
  437. return res;
  438. }
  439. #endif
  440. #ifndef __HAVE_ARCH_MEMSCAN
  441. /**
  442. * memscan - Find a character in an area of memory.
  443. * @addr: The memory area
  444. * @c: The byte to search for
  445. * @size: The size of the area.
  446. *
  447. * returns the address of the first occurrence of @c, or 1 byte past
  448. * the area if @c is not found
  449. */
  450. void * memscan(void * addr, int c, size_t size)
  451. {
  452. unsigned char * p = (unsigned char *) addr;
  453. while (size) {
  454. if (*p == c)
  455. return (void *) p;
  456. p++;
  457. size--;
  458. }
  459. return (void *) p;
  460. }
  461. #endif
  462. #ifndef __HAVE_ARCH_STRSTR
  463. /**
  464. * strstr - Find the first substring in a %NUL terminated string
  465. * @s1: The string to be searched
  466. * @s2: The string to search for
  467. */
  468. char * strstr(const char * s1,const char * s2)
  469. {
  470. int l1, l2;
  471. l2 = strlen(s2);
  472. if (!l2)
  473. return (char *) s1;
  474. l1 = strlen(s1);
  475. while (l1 >= l2) {
  476. l1--;
  477. if (!memcmp(s1,s2,l2))
  478. return (char *) s1;
  479. s1++;
  480. }
  481. return NULL;
  482. }
  483. #endif
  484. #ifndef __HAVE_ARCH_MEMCHR
  485. /**
  486. * memchr - Find a character in an area of memory.
  487. * @s: The memory area
  488. * @c: The byte to search for
  489. * @n: The size of the area.
  490. *
  491. * returns the address of the first occurrence of @c, or %NULL
  492. * if @c is not found
  493. */
  494. void * memchr(const void *s, int c, size_t n)
  495. {
  496. const unsigned char *p = s;
  497. while (n-- != 0) {
  498. if ((unsigned char)c == *p++) {
  499. return (void *)(p-1);
  500. }
  501. }
  502. return NULL;
  503. }
  504. #endif
  505. char * strndup(const char *s, size_t n)
  506. {
  507. size_t len = strlen(s);
  508. char *new;
  509. if (len>n)
  510. len = n;
  511. new = malloc(len+1);
  512. if (new) {
  513. new[len] = '\0';
  514. memcpy(new,s,len);
  515. }
  516. return new;
  517. }
  518. char * strdup(const char *s) {
  519. return strndup(s, ~((size_t)0));
  520. }