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

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