You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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_STRCASECMP
  169. int strcasecmp(const char *a, const char *b)
  170. {
  171. while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
  172. return((*a & ~0x20) - (*b & ~0x20));
  173. }
  174. #endif
  175. #ifndef __HAVE_ARCH_STRCHR
  176. /**
  177. * strchr - Find the first occurrence of a character in a string
  178. * @s: The string to be searched
  179. * @c: The character to search for
  180. */
  181. char * strchr(const char * s, int c)
  182. {
  183. for(; *s != (char) c; ++s)
  184. if (*s == '\0')
  185. return NULL;
  186. return (char *) s;
  187. }
  188. #endif
  189. #ifndef __HAVE_ARCH_STRRCHR
  190. /**
  191. * strrchr - Find the last occurrence of a character in a string
  192. * @s: The string to be searched
  193. * @c: The character to search for
  194. */
  195. char * strrchr(const char * s, int c)
  196. {
  197. const char *p = s + strlen(s);
  198. do {
  199. if (*p == (char)c)
  200. return (char *)p;
  201. } while (--p >= s);
  202. return NULL;
  203. }
  204. #endif
  205. #ifndef __HAVE_ARCH_STRLEN
  206. /**
  207. * strlen - Find the length of a string
  208. * @s: The string to be sized
  209. */
  210. size_t strlen(const char * s)
  211. {
  212. const char *sc;
  213. for (sc = s; *sc != '\0'; ++sc)
  214. /* nothing */;
  215. return sc - s;
  216. }
  217. #endif
  218. #ifndef __HAVE_ARCH_STRNLEN
  219. /**
  220. * strnlen - Find the length of a length-limited string
  221. * @s: The string to be sized
  222. * @count: The maximum number of bytes to search
  223. */
  224. size_t strnlen(const char * s, size_t count)
  225. {
  226. const char *sc;
  227. for (sc = s; count-- && *sc != '\0'; ++sc)
  228. /* nothing */;
  229. return sc - s;
  230. }
  231. #endif
  232. #ifndef __HAVE_ARCH_STRSPN
  233. /**
  234. * strspn - Calculate the length of the initial substring of @s which only
  235. * contain letters in @accept
  236. * @s: The string to be searched
  237. * @accept: The string to search for
  238. */
  239. size_t strspn(const char *s, const char *accept)
  240. {
  241. const char *p;
  242. const char *a;
  243. size_t count = 0;
  244. for (p = s; *p != '\0'; ++p) {
  245. for (a = accept; *a != '\0'; ++a) {
  246. if (*p == *a)
  247. break;
  248. }
  249. if (*a == '\0')
  250. return count;
  251. ++count;
  252. }
  253. return count;
  254. }
  255. #endif
  256. #ifndef __HAVE_ARCH_STRCSPN
  257. /**
  258. * strcspn - Calculate the length of the initial substring of @s which only
  259. * contain letters not in @reject
  260. * @s: The string to be searched
  261. * @accept: The string to search for
  262. */
  263. size_t strcspn(const char *s, const char *reject)
  264. {
  265. const char *p;
  266. const char *r;
  267. size_t count = 0;
  268. for (p = s; *p != '\0'; ++p) {
  269. for (r = reject; *r != '\0'; ++r) {
  270. if (*p == *r)
  271. return count;
  272. }
  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
  512. char * strndup(const char *s, size_t n)
  513. {
  514. size_t len = strlen(s);
  515. char *new;
  516. if (len>n)
  517. len = n;
  518. new = malloc(len+1);
  519. if (new) {
  520. new[len] = '\0';
  521. memcpy(new,s,len);
  522. }
  523. return new;
  524. }
  525. char * strdup(const char *s) {
  526. return strndup(s, ~((size_t)0));
  527. }