string.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. #if 0 /* not used - was: #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, *q;
  334. if ((NULL == s) || ('\0' == *s)) {
  335. return (NULL);
  336. }
  337. for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
  338. char tmp;
  339. tmp = *p;
  340. *p = *q;
  341. *q = 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. unsigned long *sl = (unsigned long *) s;
  358. unsigned long cl = 0;
  359. char *s8;
  360. int i;
  361. /* do it one word at a time (32 bits or 64 bits) while possible */
  362. if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
  363. for (i = 0; i < sizeof(*sl); i++) {
  364. cl <<= 8;
  365. cl |= c & 0xff;
  366. }
  367. while (count >= sizeof(*sl)) {
  368. *sl++ = cl;
  369. count -= sizeof(*sl);
  370. }
  371. }
  372. /* fill 8 bits at a time */
  373. s8 = (char *)sl;
  374. while (count--)
  375. *s8++ = c;
  376. return s;
  377. }
  378. #endif
  379. #ifndef __HAVE_ARCH_BCOPY
  380. /**
  381. * bcopy - Copy one area of memory to another
  382. * @src: Where to copy from
  383. * @dest: Where to copy to
  384. * @count: The size of the area.
  385. *
  386. * Note that this is the same as memcpy(), with the arguments reversed.
  387. * memcpy() is the standard, bcopy() is a legacy BSD function.
  388. *
  389. * You should not use this function to access IO space, use memcpy_toio()
  390. * or memcpy_fromio() instead.
  391. */
  392. char * bcopy(const char * src, char * dest, int count)
  393. {
  394. char *tmp = dest;
  395. while (count--)
  396. *tmp++ = *src++;
  397. return dest;
  398. }
  399. #endif
  400. #ifndef __HAVE_ARCH_MEMCPY
  401. /**
  402. * memcpy - Copy one area of memory to another
  403. * @dest: Where to copy to
  404. * @src: Where to copy from
  405. * @count: The size of the area.
  406. *
  407. * You should not use this function to access IO space, use memcpy_toio()
  408. * or memcpy_fromio() instead.
  409. */
  410. void * memcpy(void *dest, const void *src, size_t count)
  411. {
  412. unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
  413. char *d8, *s8;
  414. /* while all data is aligned (common case), copy a word at a time */
  415. if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
  416. while (count >= sizeof(*dl)) {
  417. *dl++ = *sl++;
  418. count -= sizeof(*dl);
  419. }
  420. }
  421. /* copy the reset one byte at a time */
  422. d8 = (char *)dl;
  423. s8 = (char *)sl;
  424. while (count--)
  425. *d8++ = *s8++;
  426. return dest;
  427. }
  428. #endif
  429. #ifndef __HAVE_ARCH_MEMMOVE
  430. /**
  431. * memmove - Copy one area of memory to another
  432. * @dest: Where to copy to
  433. * @src: Where to copy from
  434. * @count: The size of the area.
  435. *
  436. * Unlike memcpy(), memmove() copes with overlapping areas.
  437. */
  438. void * memmove(void * dest,const void *src,size_t count)
  439. {
  440. char *tmp, *s;
  441. if (dest <= src) {
  442. tmp = (char *) dest;
  443. s = (char *) src;
  444. while (count--)
  445. *tmp++ = *s++;
  446. }
  447. else {
  448. tmp = (char *) dest + count;
  449. s = (char *) src + count;
  450. while (count--)
  451. *--tmp = *--s;
  452. }
  453. return dest;
  454. }
  455. #endif
  456. #ifndef __HAVE_ARCH_MEMCMP
  457. /**
  458. * memcmp - Compare two areas of memory
  459. * @cs: One area of memory
  460. * @ct: Another area of memory
  461. * @count: The size of the area.
  462. */
  463. int memcmp(const void * cs,const void * ct,size_t count)
  464. {
  465. const unsigned char *su1, *su2;
  466. int res = 0;
  467. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  468. if ((res = *su1 - *su2) != 0)
  469. break;
  470. return res;
  471. }
  472. #endif
  473. #ifndef __HAVE_ARCH_MEMSCAN
  474. /**
  475. * memscan - Find a character in an area of memory.
  476. * @addr: The memory area
  477. * @c: The byte to search for
  478. * @size: The size of the area.
  479. *
  480. * returns the address of the first occurrence of @c, or 1 byte past
  481. * the area if @c is not found
  482. */
  483. void * memscan(void * addr, int c, size_t size)
  484. {
  485. unsigned char * p = (unsigned char *) addr;
  486. while (size) {
  487. if (*p == c)
  488. return (void *) p;
  489. p++;
  490. size--;
  491. }
  492. return (void *) p;
  493. }
  494. #endif
  495. #ifndef __HAVE_ARCH_STRSTR
  496. /**
  497. * strstr - Find the first substring in a %NUL terminated string
  498. * @s1: The string to be searched
  499. * @s2: The string to search for
  500. */
  501. char * strstr(const char * s1,const char * s2)
  502. {
  503. int l1, l2;
  504. l2 = strlen(s2);
  505. if (!l2)
  506. return (char *) s1;
  507. l1 = strlen(s1);
  508. while (l1 >= l2) {
  509. l1--;
  510. if (!memcmp(s1,s2,l2))
  511. return (char *) s1;
  512. s1++;
  513. }
  514. return NULL;
  515. }
  516. #endif
  517. #ifndef __HAVE_ARCH_MEMCHR
  518. /**
  519. * memchr - Find a character in an area of memory.
  520. * @s: The memory area
  521. * @c: The byte to search for
  522. * @n: The size of the area.
  523. *
  524. * returns the address of the first occurrence of @c, or %NULL
  525. * if @c is not found
  526. */
  527. void *memchr(const void *s, int c, size_t n)
  528. {
  529. const unsigned char *p = s;
  530. while (n-- != 0) {
  531. if ((unsigned char)c == *p++) {
  532. return (void *)(p-1);
  533. }
  534. }
  535. return NULL;
  536. }
  537. #endif