string.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. /**
  192. * skip_spaces - Removes leading whitespace from @str.
  193. * @str: The string to be stripped.
  194. *
  195. * Returns a pointer to the first non-whitespace character in @str.
  196. */
  197. char *skip_spaces(const char *str)
  198. {
  199. while (isspace(*str))
  200. ++str;
  201. return (char *)str;
  202. }
  203. /**
  204. * strim - Removes leading and trailing whitespace from @s.
  205. * @s: The string to be stripped.
  206. *
  207. * Note that the first trailing whitespace is replaced with a %NUL-terminator
  208. * in the given string @s. Returns a pointer to the first non-whitespace
  209. * character in @s.
  210. */
  211. char *strim(char *s)
  212. {
  213. size_t size;
  214. char *end;
  215. s = skip_spaces(s);
  216. size = strlen(s);
  217. if (!size)
  218. return s;
  219. end = s + size - 1;
  220. while (end >= s && isspace(*end))
  221. end--;
  222. *(end + 1) = '\0';
  223. return s;
  224. }
  225. #ifndef __HAVE_ARCH_STRLEN
  226. /**
  227. * strlen - Find the length of a string
  228. * @s: The string to be sized
  229. */
  230. size_t strlen(const char * s)
  231. {
  232. const char *sc;
  233. for (sc = s; *sc != '\0'; ++sc)
  234. /* nothing */;
  235. return sc - s;
  236. }
  237. #endif
  238. #ifndef __HAVE_ARCH_STRNLEN
  239. /**
  240. * strnlen - Find the length of a length-limited string
  241. * @s: The string to be sized
  242. * @count: The maximum number of bytes to search
  243. */
  244. size_t strnlen(const char * s, size_t count)
  245. {
  246. const char *sc;
  247. for (sc = s; count-- && *sc != '\0'; ++sc)
  248. /* nothing */;
  249. return sc - s;
  250. }
  251. #endif
  252. #ifndef __HAVE_ARCH_STRDUP
  253. char * strdup(const char *s)
  254. {
  255. char *new;
  256. if ((s == NULL) ||
  257. ((new = malloc (strlen(s) + 1)) == NULL) ) {
  258. return NULL;
  259. }
  260. strcpy (new, s);
  261. return new;
  262. }
  263. #endif
  264. #ifndef __HAVE_ARCH_STRSPN
  265. /**
  266. * strspn - Calculate the length of the initial substring of @s which only
  267. * contain letters in @accept
  268. * @s: The string to be searched
  269. * @accept: The string to search for
  270. */
  271. size_t strspn(const char *s, const char *accept)
  272. {
  273. const char *p;
  274. const char *a;
  275. size_t count = 0;
  276. for (p = s; *p != '\0'; ++p) {
  277. for (a = accept; *a != '\0'; ++a) {
  278. if (*p == *a)
  279. break;
  280. }
  281. if (*a == '\0')
  282. return count;
  283. ++count;
  284. }
  285. return count;
  286. }
  287. #endif
  288. #ifndef __HAVE_ARCH_STRPBRK
  289. /**
  290. * strpbrk - Find the first occurrence of a set of characters
  291. * @cs: The string to be searched
  292. * @ct: The characters to search for
  293. */
  294. char * strpbrk(const char * cs,const char * ct)
  295. {
  296. const char *sc1,*sc2;
  297. for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  298. for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  299. if (*sc1 == *sc2)
  300. return (char *) sc1;
  301. }
  302. }
  303. return NULL;
  304. }
  305. #endif
  306. #ifndef __HAVE_ARCH_STRTOK
  307. /**
  308. * strtok - Split a string into tokens
  309. * @s: The string to be searched
  310. * @ct: The characters to search for
  311. *
  312. * WARNING: strtok is deprecated, use strsep instead.
  313. */
  314. char * strtok(char * s,const char * ct)
  315. {
  316. char *sbegin, *send;
  317. sbegin = s ? s : ___strtok;
  318. if (!sbegin) {
  319. return NULL;
  320. }
  321. sbegin += strspn(sbegin,ct);
  322. if (*sbegin == '\0') {
  323. ___strtok = NULL;
  324. return( NULL );
  325. }
  326. send = strpbrk( sbegin, ct);
  327. if (send && *send != '\0')
  328. *send++ = '\0';
  329. ___strtok = send;
  330. return (sbegin);
  331. }
  332. #endif
  333. #ifndef __HAVE_ARCH_STRSEP
  334. /**
  335. * strsep - Split a string into tokens
  336. * @s: The string to be searched
  337. * @ct: The characters to search for
  338. *
  339. * strsep() updates @s to point after the token, ready for the next call.
  340. *
  341. * It returns empty tokens, too, behaving exactly like the libc function
  342. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  343. * Same semantics, slimmer shape. ;)
  344. */
  345. char * strsep(char **s, const char *ct)
  346. {
  347. char *sbegin = *s, *end;
  348. if (sbegin == NULL)
  349. return NULL;
  350. end = strpbrk(sbegin, ct);
  351. if (end)
  352. *end++ = '\0';
  353. *s = end;
  354. return sbegin;
  355. }
  356. #endif
  357. #ifndef __HAVE_ARCH_STRSWAB
  358. /**
  359. * strswab - swap adjacent even and odd bytes in %NUL-terminated string
  360. * s: address of the string
  361. *
  362. * returns the address of the swapped string or NULL on error. If
  363. * string length is odd, last byte is untouched.
  364. */
  365. char *strswab(const char *s)
  366. {
  367. char *p, *q;
  368. if ((NULL == s) || ('\0' == *s)) {
  369. return (NULL);
  370. }
  371. for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
  372. char tmp;
  373. tmp = *p;
  374. *p = *q;
  375. *q = tmp;
  376. }
  377. return (char *) s;
  378. }
  379. #endif
  380. #ifndef __HAVE_ARCH_MEMSET
  381. /**
  382. * memset - Fill a region of memory with the given value
  383. * @s: Pointer to the start of the area.
  384. * @c: The byte to fill the area with
  385. * @count: The size of the area.
  386. *
  387. * Do not use memset() to access IO space, use memset_io() instead.
  388. */
  389. void * memset(void * s,int c,size_t count)
  390. {
  391. unsigned long *sl = (unsigned long *) s;
  392. unsigned long cl = 0;
  393. char *s8;
  394. int i;
  395. /* do it one word at a time (32 bits or 64 bits) while possible */
  396. if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
  397. for (i = 0; i < sizeof(*sl); i++) {
  398. cl <<= 8;
  399. cl |= c & 0xff;
  400. }
  401. while (count >= sizeof(*sl)) {
  402. *sl++ = cl;
  403. count -= sizeof(*sl);
  404. }
  405. }
  406. /* fill 8 bits at a time */
  407. s8 = (char *)sl;
  408. while (count--)
  409. *s8++ = c;
  410. return s;
  411. }
  412. #endif
  413. #ifndef __HAVE_ARCH_BCOPY
  414. /**
  415. * bcopy - Copy one area of memory to another
  416. * @src: Where to copy from
  417. * @dest: Where to copy to
  418. * @count: The size of the area.
  419. *
  420. * Note that this is the same as memcpy(), with the arguments reversed.
  421. * memcpy() is the standard, bcopy() is a legacy BSD function.
  422. *
  423. * You should not use this function to access IO space, use memcpy_toio()
  424. * or memcpy_fromio() instead.
  425. */
  426. char * bcopy(const char * src, char * dest, int count)
  427. {
  428. char *tmp = dest;
  429. while (count--)
  430. *tmp++ = *src++;
  431. return dest;
  432. }
  433. #endif
  434. #ifndef __HAVE_ARCH_MEMCPY
  435. /**
  436. * memcpy - Copy one area of memory to another
  437. * @dest: Where to copy to
  438. * @src: Where to copy from
  439. * @count: The size of the area.
  440. *
  441. * You should not use this function to access IO space, use memcpy_toio()
  442. * or memcpy_fromio() instead.
  443. */
  444. void * memcpy(void *dest, const void *src, size_t count)
  445. {
  446. unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
  447. char *d8, *s8;
  448. if (src == dest)
  449. return dest;
  450. /* while all data is aligned (common case), copy a word at a time */
  451. if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
  452. while (count >= sizeof(*dl)) {
  453. *dl++ = *sl++;
  454. count -= sizeof(*dl);
  455. }
  456. }
  457. /* copy the reset one byte at a time */
  458. d8 = (char *)dl;
  459. s8 = (char *)sl;
  460. while (count--)
  461. *d8++ = *s8++;
  462. return dest;
  463. }
  464. #endif
  465. #ifndef __HAVE_ARCH_MEMMOVE
  466. /**
  467. * memmove - Copy one area of memory to another
  468. * @dest: Where to copy to
  469. * @src: Where to copy from
  470. * @count: The size of the area.
  471. *
  472. * Unlike memcpy(), memmove() copes with overlapping areas.
  473. */
  474. void * memmove(void * dest,const void *src,size_t count)
  475. {
  476. char *tmp, *s;
  477. if (src == dest)
  478. return dest;
  479. if (dest <= src) {
  480. tmp = (char *) dest;
  481. s = (char *) src;
  482. while (count--)
  483. *tmp++ = *s++;
  484. }
  485. else {
  486. tmp = (char *) dest + count;
  487. s = (char *) src + count;
  488. while (count--)
  489. *--tmp = *--s;
  490. }
  491. return dest;
  492. }
  493. #endif
  494. #ifndef __HAVE_ARCH_MEMCMP
  495. /**
  496. * memcmp - Compare two areas of memory
  497. * @cs: One area of memory
  498. * @ct: Another area of memory
  499. * @count: The size of the area.
  500. */
  501. int memcmp(const void * cs,const void * ct,size_t count)
  502. {
  503. const unsigned char *su1, *su2;
  504. int res = 0;
  505. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  506. if ((res = *su1 - *su2) != 0)
  507. break;
  508. return res;
  509. }
  510. #endif
  511. #ifndef __HAVE_ARCH_MEMSCAN
  512. /**
  513. * memscan - Find a character in an area of memory.
  514. * @addr: The memory area
  515. * @c: The byte to search for
  516. * @size: The size of the area.
  517. *
  518. * returns the address of the first occurrence of @c, or 1 byte past
  519. * the area if @c is not found
  520. */
  521. void * memscan(void * addr, int c, size_t size)
  522. {
  523. unsigned char * p = (unsigned char *) addr;
  524. while (size) {
  525. if (*p == c)
  526. return (void *) p;
  527. p++;
  528. size--;
  529. }
  530. return (void *) p;
  531. }
  532. #endif
  533. #ifndef __HAVE_ARCH_STRSTR
  534. /**
  535. * strstr - Find the first substring in a %NUL terminated string
  536. * @s1: The string to be searched
  537. * @s2: The string to search for
  538. */
  539. char * strstr(const char * s1,const char * s2)
  540. {
  541. int l1, l2;
  542. l2 = strlen(s2);
  543. if (!l2)
  544. return (char *) s1;
  545. l1 = strlen(s1);
  546. while (l1 >= l2) {
  547. l1--;
  548. if (!memcmp(s1,s2,l2))
  549. return (char *) s1;
  550. s1++;
  551. }
  552. return NULL;
  553. }
  554. #endif
  555. #ifndef __HAVE_ARCH_MEMCHR
  556. /**
  557. * memchr - Find a character in an area of memory.
  558. * @s: The memory area
  559. * @c: The byte to search for
  560. * @n: The size of the area.
  561. *
  562. * returns the address of the first occurrence of @c, or %NULL
  563. * if @c is not found
  564. */
  565. void *memchr(const void *s, int c, size_t n)
  566. {
  567. const unsigned char *p = s;
  568. while (n-- != 0) {
  569. if ((unsigned char)c == *p++) {
  570. return (void *)(p-1);
  571. }
  572. }
  573. return NULL;
  574. }
  575. #endif