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.

string.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <ctype.h>
  26. /* *** FROM string.c *** */
  27. #ifndef __HAVE_ARCH_STRCPY
  28. /**
  29. * strcpy - Copy a %NUL terminated string
  30. * @dest: Where to copy the string to
  31. * @src: Where to copy the string from
  32. */
  33. char * strcpy(char * dest,const char *src)
  34. {
  35. char *tmp = dest;
  36. while ((*dest++ = *src++) != '\0')
  37. /* nothing */;
  38. return tmp;
  39. }
  40. #endif
  41. #ifndef __HAVE_ARCH_STRNCPY
  42. /**
  43. * strncpy - Copy a length-limited, %NUL-terminated string
  44. * @dest: Where to copy the string to
  45. * @src: Where to copy the string from
  46. * @count: The maximum number of bytes to copy
  47. *
  48. * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
  49. * However, the result is not %NUL-terminated if the source exceeds
  50. * @count bytes.
  51. */
  52. char * strncpy(char * dest,const char *src,size_t count)
  53. {
  54. char *tmp = dest;
  55. while (count-- && (*dest++ = *src++) != '\0')
  56. /* nothing */;
  57. return tmp;
  58. }
  59. #endif
  60. #ifndef __HAVE_ARCH_STRCAT
  61. /**
  62. * strcat - Append one %NUL-terminated string to another
  63. * @dest: The string to be appended to
  64. * @src: The string to append to it
  65. */
  66. char * strcat(char * dest, const char * src)
  67. {
  68. char *tmp = dest;
  69. while (*dest)
  70. dest++;
  71. while ((*dest++ = *src++) != '\0')
  72. ;
  73. return tmp;
  74. }
  75. #endif
  76. #ifndef __HAVE_ARCH_STRCMP
  77. /**
  78. * strcmp - Compare two strings
  79. * @cs: One string
  80. * @ct: Another string
  81. */
  82. int strcmp(const char * cs,const char * ct)
  83. {
  84. register signed char __res;
  85. while (1) {
  86. if ((__res = *cs - *ct++) != 0 || !*cs++)
  87. break;
  88. }
  89. return __res;
  90. }
  91. #endif
  92. #ifndef __HAVE_ARCH_STRNCMP
  93. /**
  94. * strncmp - Compare two length-limited strings
  95. * @cs: One string
  96. * @ct: Another string
  97. * @count: The maximum number of bytes to compare
  98. */
  99. int strncmp(const char * cs,const char * ct,size_t count)
  100. {
  101. register signed char __res = 0;
  102. while (count) {
  103. if ((__res = *cs - *ct++) != 0 || !*cs++)
  104. break;
  105. count--;
  106. }
  107. return __res;
  108. }
  109. #endif
  110. #ifndef __HAVE_ARCH_STRCASECMP
  111. int strcasecmp(const char *a, const char *b)
  112. {
  113. while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
  114. return((*a & ~0x20) - (*b & ~0x20));
  115. }
  116. #endif
  117. #ifndef __HAVE_ARCH_STRCHR
  118. /**
  119. * strchr - Find the first occurrence of a character in a string
  120. * @s: The string to be searched
  121. * @c: The character to search for
  122. */
  123. char * strchr(const char * s, int c)
  124. {
  125. for(; *s != (char) c; ++s)
  126. if (*s == '\0')
  127. return NULL;
  128. return (char *) s;
  129. }
  130. #endif
  131. #ifndef __HAVE_ARCH_STRRCHR
  132. /**
  133. * strrchr - Find the last occurrence of a character in a string
  134. * @s: The string to be searched
  135. * @c: The character to search for
  136. */
  137. char * strrchr(const char * s, int c)
  138. {
  139. const char *p = s + strlen(s);
  140. do {
  141. if (*p == (char)c)
  142. return (char *)p;
  143. } while (--p >= s);
  144. return NULL;
  145. }
  146. #endif
  147. #ifndef __HAVE_ARCH_STRLEN
  148. /**
  149. * strlen - Find the length of a string
  150. * @s: The string to be sized
  151. */
  152. size_t strlen(const char * s)
  153. {
  154. const char *sc;
  155. for (sc = s; *sc != '\0'; ++sc)
  156. /* nothing */;
  157. return sc - s;
  158. }
  159. #endif
  160. #ifndef __HAVE_ARCH_STRNLEN
  161. /**
  162. * strnlen - Find the length of a length-limited string
  163. * @s: The string to be sized
  164. * @count: The maximum number of bytes to search
  165. */
  166. size_t strnlen(const char * s, size_t count)
  167. {
  168. const char *sc;
  169. for (sc = s; count-- && *sc != '\0'; ++sc)
  170. /* nothing */;
  171. return sc - s;
  172. }
  173. #endif
  174. #ifndef __HAVE_ARCH_MEMSET
  175. /**
  176. * memset - Fill a region of memory with the given value
  177. * @s: Pointer to the start of the area.
  178. * @c: The byte to fill the area with
  179. * @count: The size of the area.
  180. *
  181. * Do not use memset() to access IO space, use memset_io() instead.
  182. */
  183. void * memset(void * s,int c,size_t count)
  184. {
  185. char *xs = (char *) s;
  186. while (count--)
  187. *xs++ = c;
  188. return s;
  189. }
  190. #endif
  191. #ifndef __HAVE_ARCH_MEMCPY
  192. /**
  193. * memcpy - Copy one area of memory to another
  194. * @dest: Where to copy to
  195. * @src: Where to copy from
  196. * @count: The size of the area.
  197. *
  198. * You should not use this function to access IO space, use memcpy_toio()
  199. * or memcpy_fromio() instead.
  200. */
  201. void * memcpy(void * dest,const void *src,size_t count)
  202. {
  203. char *tmp = (char *) dest, *s = (char *) src;
  204. while (count--)
  205. *tmp++ = *s++;
  206. return dest;
  207. }
  208. #endif
  209. #ifndef __HAVE_ARCH_MEMMOVE
  210. /**
  211. * memmove - Copy one area of memory to another
  212. * @dest: Where to copy to
  213. * @src: Where to copy from
  214. * @count: The size of the area.
  215. *
  216. * Unlike memcpy(), memmove() copes with overlapping areas.
  217. */
  218. void * memmove(void * dest,const void *src,size_t count)
  219. {
  220. char *tmp, *s;
  221. if (dest <= src) {
  222. tmp = (char *) dest;
  223. s = (char *) src;
  224. while (count--)
  225. *tmp++ = *s++;
  226. }
  227. else {
  228. tmp = (char *) dest + count;
  229. s = (char *) src + count;
  230. while (count--)
  231. *--tmp = *--s;
  232. }
  233. return dest;
  234. }
  235. #endif
  236. #ifndef __HAVE_ARCH_MEMCMP
  237. /**
  238. * memcmp - Compare two areas of memory
  239. * @cs: One area of memory
  240. * @ct: Another area of memory
  241. * @count: The size of the area.
  242. */
  243. int memcmp(const void * cs,const void * ct,size_t count)
  244. {
  245. const unsigned char *su1, *su2;
  246. int res = 0;
  247. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  248. if ((res = *su1 - *su2) != 0)
  249. break;
  250. return res;
  251. }
  252. #endif
  253. #ifndef __HAVE_ARCH_STRSTR
  254. /**
  255. * strstr - Find the first substring in a %NUL terminated string
  256. * @s1: The string to be searched
  257. * @s2: The string to search for
  258. */
  259. char * strstr(const char * s1,const char * s2)
  260. {
  261. int l1, l2;
  262. l2 = strlen(s2);
  263. if (!l2)
  264. return (char *) s1;
  265. l1 = strlen(s1);
  266. while (l1 >= l2) {
  267. l1--;
  268. if (!memcmp(s1,s2,l2))
  269. return (char *) s1;
  270. s1++;
  271. }
  272. return NULL;
  273. }
  274. #endif
  275. #ifndef __HAVE_ARCH_MEMCHR
  276. /**
  277. * memchr - Find a character in an area of memory.
  278. * @s: The memory area
  279. * @c: The byte to search for
  280. * @n: The size of the area.
  281. *
  282. * returns the address of the first occurrence of @c, or %NULL
  283. * if @c is not found
  284. */
  285. void * memchr(const void *s, int c, size_t n)
  286. {
  287. const unsigned char *p = s;
  288. while (n-- != 0) {
  289. if ((unsigned char)c == *p++) {
  290. return (void *)(p-1);
  291. }
  292. }
  293. return NULL;
  294. }
  295. #endif
  296. char * strndup(const char *s, size_t n)
  297. {
  298. size_t len = strlen(s);
  299. char *new;
  300. if (len>n)
  301. len = n;
  302. new = malloc(len+1);
  303. if (new) {
  304. new[len] = '\0';
  305. memcpy(new,s,len);
  306. }
  307. return new;
  308. }
  309. char * strdup(const char *s) {
  310. return strndup(s, ~((size_t)0));
  311. }