Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

stringextra.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. FILE_LICENCE ( GPL2_ONLY );
  13. /*
  14. * stupid library routines.. The optimized versions should generally be found
  15. * as inline code in <asm-xx/string.h>
  16. *
  17. * These are buggy as well..
  18. *
  19. * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
  20. * - Added strsep() which will replace strtok() soon (because strsep() is
  21. * reentrant and should be faster). Use only strsep() in new code, please.
  22. */
  23. /*
  24. * these are the standard string functions that are currently not used by
  25. * any code in etherboot. put into a separate file to avoid linking them in
  26. * with the rest of string.o
  27. * if anything ever does want to use a function of these, consider moving
  28. * the function in question back into string.c
  29. */
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. /* *** FROM string.c *** */
  35. #ifndef __HAVE_ARCH_STRNICMP
  36. /**
  37. * strnicmp - Case insensitive, length-limited string comparison
  38. * @s1: One string
  39. * @s2: The other string
  40. * @len: the maximum number of characters to compare
  41. */
  42. int strnicmp(const char *s1, const char *s2, size_t len)
  43. {
  44. /* Yes, Virginia, it had better be unsigned */
  45. unsigned char c1, c2;
  46. c1 = 0; c2 = 0;
  47. if (len) {
  48. do {
  49. c1 = *s1; c2 = *s2;
  50. s1++; s2++;
  51. if (!c1)
  52. break;
  53. if (!c2)
  54. break;
  55. if (c1 == c2)
  56. continue;
  57. c1 = tolower(c1);
  58. c2 = tolower(c2);
  59. if (c1 != c2)
  60. break;
  61. } while (--len);
  62. }
  63. return (int)c1 - (int)c2;
  64. }
  65. #endif
  66. char * ___strtok;
  67. #ifndef __HAVE_ARCH_STRNCAT
  68. /**
  69. * strncat - Append a length-limited, %NUL-terminated string to another
  70. * @dest: The string to be appended to
  71. * @src: The string to append to it
  72. * @count: The maximum numbers of bytes to copy
  73. *
  74. * Note that in contrast to strncpy, strncat ensures the result is
  75. * terminated.
  76. */
  77. char * strncat(char *dest, const char *src, size_t count)
  78. {
  79. char *tmp = dest;
  80. if (count) {
  81. while (*dest)
  82. dest++;
  83. while ((*dest++ = *src++)) {
  84. if (--count == 0) {
  85. *dest = '\0';
  86. break;
  87. }
  88. }
  89. }
  90. return tmp;
  91. }
  92. #endif
  93. #ifndef __HAVE_ARCH_STRSPN
  94. /**
  95. * strspn - Calculate the length of the initial substring of @s which only
  96. * contain letters in @accept
  97. * @s: The string to be searched
  98. * @accept: The string to search for
  99. */
  100. size_t strspn(const char *s, const char *accept)
  101. {
  102. const char *p;
  103. const char *a;
  104. size_t count = 0;
  105. for (p = s; *p != '\0'; ++p) {
  106. for (a = accept; *a != '\0'; ++a) {
  107. if (*p == *a)
  108. break;
  109. }
  110. if (*a == '\0')
  111. return count;
  112. ++count;
  113. }
  114. return count;
  115. }
  116. #endif
  117. #ifndef __HAVE_ARCH_STRCSPN
  118. /**
  119. * strcspn - Calculate the length of the initial substring of @s which only
  120. * contain letters not in @reject
  121. * @s: The string to be searched
  122. * @accept: The string to search for
  123. */
  124. size_t strcspn(const char *s, const char *reject)
  125. {
  126. const char *p;
  127. const char *r;
  128. size_t count = 0;
  129. for (p = s; *p != '\0'; ++p) {
  130. for (r = reject; *r != '\0'; ++r) {
  131. if (*p == *r)
  132. return count;
  133. }
  134. ++count;
  135. }
  136. return count;
  137. }
  138. #endif
  139. #ifndef __HAVE_ARCH_STRPBRK
  140. /**
  141. * strpbrk - Find the first occurrence of a set of characters
  142. * @cs: The string to be searched
  143. * @ct: The characters to search for
  144. */
  145. char * strpbrk(const char * cs,const char * ct)
  146. {
  147. const char *sc1,*sc2;
  148. for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  149. for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  150. if (*sc1 == *sc2)
  151. return (char *) sc1;
  152. }
  153. }
  154. return NULL;
  155. }
  156. #endif
  157. #ifndef __HAVE_ARCH_STRTOK
  158. /**
  159. * strtok - Split a string into tokens
  160. * @s: The string to be searched
  161. * @ct: The characters to search for
  162. *
  163. * WARNING: strtok is deprecated, use strsep instead.
  164. */
  165. char * strtok(char * s,const char * ct)
  166. {
  167. char *sbegin, *send;
  168. sbegin = s ? s : ___strtok;
  169. if (!sbegin) {
  170. return NULL;
  171. }
  172. sbegin += strspn(sbegin,ct);
  173. if (*sbegin == '\0') {
  174. ___strtok = NULL;
  175. return( NULL );
  176. }
  177. send = strpbrk( sbegin, ct);
  178. if (send && *send != '\0')
  179. *send++ = '\0';
  180. ___strtok = send;
  181. return (sbegin);
  182. }
  183. #endif
  184. #ifndef __HAVE_ARCH_STRSEP
  185. /**
  186. * strsep - Split a string into tokens
  187. * @s: The string to be searched
  188. * @ct: The characters to search for
  189. *
  190. * strsep() updates @s to point after the token, ready for the next call.
  191. *
  192. * It returns empty tokens, too, behaving exactly like the libc function
  193. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  194. * Same semantics, slimmer shape. ;)
  195. */
  196. char * strsep(char **s, const char *ct)
  197. {
  198. char *sbegin = *s, *end;
  199. if (sbegin == NULL)
  200. return NULL;
  201. end = strpbrk(sbegin, ct);
  202. if (end)
  203. *end++ = '\0';
  204. *s = end;
  205. return sbegin;
  206. }
  207. #endif
  208. #ifndef __HAVE_ARCH_BCOPY
  209. /**
  210. * bcopy - Copy one area of memory to another
  211. * @src: Where to copy from
  212. * @dest: Where to copy to
  213. * @count: The size of the area.
  214. *
  215. * Note that this is the same as memcpy(), with the arguments reversed.
  216. * memcpy() is the standard, bcopy() is a legacy BSD function.
  217. *
  218. * You should not use this function to access IO space, use memcpy_toio()
  219. * or memcpy_fromio() instead.
  220. */
  221. char * bcopy(const char * src, char * dest, int count)
  222. {
  223. return memmove(dest,src,count);
  224. }
  225. #endif
  226. #ifndef __HAVE_ARCH_MEMSCAN
  227. /**
  228. * memscan - Find a character in an area of memory.
  229. * @addr: The memory area
  230. * @c: The byte to search for
  231. * @size: The size of the area.
  232. *
  233. * returns the address of the first occurrence of @c, or 1 byte past
  234. * the area if @c is not found
  235. */
  236. void * memscan(const void * addr, int c, size_t size)
  237. {
  238. unsigned char * p = (unsigned char *) addr;
  239. while (size) {
  240. if (*p == c)
  241. return (void *) p;
  242. p++;
  243. size--;
  244. }
  245. return (void *) p;
  246. }
  247. #endif