You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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