string.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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_MEMSET
  324. /**
  325. * memset - Fill a region of memory with the given value
  326. * @s: Pointer to the start of the area.
  327. * @c: The byte to fill the area with
  328. * @count: The size of the area.
  329. *
  330. * Do not use memset() to access IO space, use memset_io() instead.
  331. */
  332. void * memset(void * s,int c,size_t count)
  333. {
  334. char *xs = (char *) s;
  335. while (count--)
  336. *xs++ = c;
  337. return s;
  338. }
  339. #endif
  340. #ifndef __HAVE_ARCH_BCOPY
  341. /**
  342. * bcopy - Copy one area of memory to another
  343. * @src: Where to copy from
  344. * @dest: Where to copy to
  345. * @count: The size of the area.
  346. *
  347. * Note that this is the same as memcpy(), with the arguments reversed.
  348. * memcpy() is the standard, bcopy() is a legacy BSD function.
  349. *
  350. * You should not use this function to access IO space, use memcpy_toio()
  351. * or memcpy_fromio() instead.
  352. */
  353. char * bcopy(const char * src, char * dest, int count)
  354. {
  355. char *tmp = dest;
  356. while (count--)
  357. *tmp++ = *src++;
  358. return dest;
  359. }
  360. #endif
  361. #ifndef __HAVE_ARCH_MEMCPY
  362. /**
  363. * memcpy - Copy one area of memory to another
  364. * @dest: Where to copy to
  365. * @src: Where to copy from
  366. * @count: The size of the area.
  367. *
  368. * You should not use this function to access IO space, use memcpy_toio()
  369. * or memcpy_fromio() instead.
  370. */
  371. void * memcpy(void * dest,const void *src,size_t count)
  372. {
  373. char *tmp = (char *) dest, *s = (char *) src;
  374. while (count--)
  375. *tmp++ = *s++;
  376. return dest;
  377. }
  378. #endif
  379. #ifndef __HAVE_ARCH_MEMMOVE
  380. /**
  381. * memmove - Copy one area of memory to another
  382. * @dest: Where to copy to
  383. * @src: Where to copy from
  384. * @count: The size of the area.
  385. *
  386. * Unlike memcpy(), memmove() copes with overlapping areas.
  387. */
  388. void * memmove(void * dest,const void *src,size_t count)
  389. {
  390. char *tmp, *s;
  391. if (dest <= src) {
  392. tmp = (char *) dest;
  393. s = (char *) src;
  394. while (count--)
  395. *tmp++ = *s++;
  396. }
  397. else {
  398. tmp = (char *) dest + count;
  399. s = (char *) src + count;
  400. while (count--)
  401. *--tmp = *--s;
  402. }
  403. return dest;
  404. }
  405. #endif
  406. #ifndef __HAVE_ARCH_MEMCMP
  407. /**
  408. * memcmp - Compare two areas of memory
  409. * @cs: One area of memory
  410. * @ct: Another area of memory
  411. * @count: The size of the area.
  412. */
  413. int memcmp(const void * cs,const void * ct,size_t count)
  414. {
  415. const unsigned char *su1, *su2;
  416. int res = 0;
  417. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  418. if ((res = *su1 - *su2) != 0)
  419. break;
  420. return res;
  421. }
  422. #endif
  423. #ifndef __HAVE_ARCH_MEMSCAN
  424. /**
  425. * memscan - Find a character in an area of memory.
  426. * @addr: The memory area
  427. * @c: The byte to search for
  428. * @size: The size of the area.
  429. *
  430. * returns the address of the first occurrence of @c, or 1 byte past
  431. * the area if @c is not found
  432. */
  433. void * memscan(void * addr, int c, size_t size)
  434. {
  435. unsigned char * p = (unsigned char *) addr;
  436. while (size) {
  437. if (*p == c)
  438. return (void *) p;
  439. p++;
  440. size--;
  441. }
  442. return (void *) p;
  443. }
  444. #endif
  445. #ifndef __HAVE_ARCH_STRSTR
  446. /**
  447. * strstr - Find the first substring in a %NUL terminated string
  448. * @s1: The string to be searched
  449. * @s2: The string to search for
  450. */
  451. char * strstr(const char * s1,const char * s2)
  452. {
  453. int l1, l2;
  454. l2 = strlen(s2);
  455. if (!l2)
  456. return (char *) s1;
  457. l1 = strlen(s1);
  458. while (l1 >= l2) {
  459. l1--;
  460. if (!memcmp(s1,s2,l2))
  461. return (char *) s1;
  462. s1++;
  463. }
  464. return NULL;
  465. }
  466. #endif
  467. #ifndef __HAVE_ARCH_MEMCHR
  468. /**
  469. * memchr - Find a character in an area of memory.
  470. * @s: The memory area
  471. * @c: The byte to search for
  472. * @n: The size of the area.
  473. *
  474. * returns the address of the first occurrence of @c, or %NULL
  475. * if @c is not found
  476. */
  477. void *memchr(const void *s, int c, size_t n)
  478. {
  479. const unsigned char *p = s;
  480. while (n-- != 0) {
  481. if ((unsigned char)c == *p++) {
  482. return (void *)(p-1);
  483. }
  484. }
  485. return NULL;
  486. }
  487. #endif