Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

string.c 11KB

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