string.c 11 KB

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