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 10KB

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