string.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * linux/lib/string.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * stupid library routines.. The optimized versions should generally be found
  8. * as inline code in <asm-xx/string.h>
  9. *
  10. * These are buggy as well..
  11. */
  12. #include <linux/types.h>
  13. #include <linux/string.h>
  14. #include <malloc.h>
  15. #define __HAVE_ARCH_BCOPY
  16. #define __HAVE_ARCH_MEMCMP
  17. #define __HAVE_ARCH_MEMCPY
  18. #define __HAVE_ARCH_MEMMOVE
  19. #define __HAVE_ARCH_MEMSET
  20. #define __HAVE_ARCH_STRCAT
  21. #define __HAVE_ARCH_STRCMP
  22. #define __HAVE_ARCH_STRCPY
  23. #define __HAVE_ARCH_STRLEN
  24. #define __HAVE_ARCH_STRNCPY
  25. char * ___strtok = NULL;
  26. #ifndef __HAVE_ARCH_STRCPY
  27. char * strcpy(char * dest,const char *src)
  28. {
  29. char *tmp = dest;
  30. while ((*dest++ = *src++) != '\0')
  31. /* nothing */;
  32. return tmp;
  33. }
  34. #endif
  35. #ifndef __HAVE_ARCH_STRNCPY
  36. char * strncpy(char * dest,const char *src,size_t count)
  37. {
  38. char *tmp = dest;
  39. while (count-- && (*dest++ = *src++) != '\0')
  40. /* nothing */;
  41. return tmp;
  42. }
  43. #endif
  44. #ifndef __HAVE_ARCH_STRCAT
  45. char * strcat(char * dest, const char * src)
  46. {
  47. char *tmp = dest;
  48. while (*dest)
  49. dest++;
  50. while ((*dest++ = *src++) != '\0')
  51. ;
  52. return tmp;
  53. }
  54. #endif
  55. #ifndef __HAVE_ARCH_STRNCAT
  56. char * strncat(char *dest, const char *src, size_t count)
  57. {
  58. char *tmp = dest;
  59. if (count) {
  60. while (*dest)
  61. dest++;
  62. while ((*dest++ = *src++)) {
  63. if (--count == 0) {
  64. *dest = '\0';
  65. break;
  66. }
  67. }
  68. }
  69. return tmp;
  70. }
  71. #endif
  72. #ifndef __HAVE_ARCH_STRCMP
  73. int strcmp(const char * cs,const char * ct)
  74. {
  75. register signed char __res;
  76. while (1) {
  77. if ((__res = *cs - *ct++) != 0 || !*cs++)
  78. break;
  79. }
  80. return __res;
  81. }
  82. #endif
  83. #ifndef __HAVE_ARCH_STRNCMP
  84. int strncmp(const char * cs,const char * ct,size_t count)
  85. {
  86. register signed char __res = 0;
  87. while (count) {
  88. if ((__res = *cs - *ct++) != 0 || !*cs++)
  89. break;
  90. count--;
  91. }
  92. return __res;
  93. }
  94. #endif
  95. #ifndef __HAVE_ARCH_STRCHR
  96. char * strchr(const char * s, int c)
  97. {
  98. for(; *s != (char) c; ++s)
  99. if (*s == '\0')
  100. return NULL;
  101. return (char *) s;
  102. }
  103. #endif
  104. #ifndef __HAVE_ARCH_STRRCHR
  105. char * strrchr(const char * s, int c)
  106. {
  107. const char *p = s + strlen(s);
  108. do {
  109. if (*p == (char)c)
  110. return (char *)p;
  111. } while (--p >= s);
  112. return NULL;
  113. }
  114. #endif
  115. #ifndef __HAVE_ARCH_STRLEN
  116. size_t strlen(const char * s)
  117. {
  118. const char *sc;
  119. for (sc = s; *sc != '\0'; ++sc)
  120. /* nothing */;
  121. return sc - s;
  122. }
  123. #endif
  124. #ifndef __HAVE_ARCH_STRNLEN
  125. size_t strnlen(const char * s, size_t count)
  126. {
  127. const char *sc;
  128. for (sc = s; count-- && *sc != '\0'; ++sc)
  129. /* nothing */;
  130. return sc - s;
  131. }
  132. #endif
  133. #ifndef __HAVE_ARCH_STRDUP
  134. char * strdup(const char *s)
  135. {
  136. char *new;
  137. if ((s == NULL) ||
  138. ((new = malloc (strlen(s) + 1)) == NULL) ) {
  139. return NULL;
  140. }
  141. strcpy (new, s);
  142. return new;
  143. }
  144. #endif
  145. #ifndef __HAVE_ARCH_STRSPN
  146. size_t strspn(const char *s, const char *accept)
  147. {
  148. const char *p;
  149. const char *a;
  150. size_t count = 0;
  151. for (p = s; *p != '\0'; ++p) {
  152. for (a = accept; *a != '\0'; ++a) {
  153. if (*p == *a)
  154. break;
  155. }
  156. if (*a == '\0')
  157. return count;
  158. ++count;
  159. }
  160. return count;
  161. }
  162. #endif
  163. #ifndef __HAVE_ARCH_STRPBRK
  164. char * strpbrk(const char * cs,const char * ct)
  165. {
  166. const char *sc1,*sc2;
  167. for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  168. for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  169. if (*sc1 == *sc2)
  170. return (char *) sc1;
  171. }
  172. }
  173. return NULL;
  174. }
  175. #endif
  176. #ifndef __HAVE_ARCH_STRTOK
  177. char * strtok(char * s,const char * ct)
  178. {
  179. char *sbegin, *send;
  180. sbegin = s ? s : ___strtok;
  181. if (!sbegin) {
  182. return NULL;
  183. }
  184. sbegin += strspn(sbegin,ct);
  185. if (*sbegin == '\0') {
  186. ___strtok = NULL;
  187. return( NULL );
  188. }
  189. send = strpbrk( sbegin, ct);
  190. if (send && *send != '\0')
  191. *send++ = '\0';
  192. ___strtok = send;
  193. return (sbegin);
  194. }
  195. #endif
  196. #ifndef __HAVE_ARCH_MEMSET
  197. void * memset(void * s,char c,size_t count)
  198. {
  199. char *xs = (char *) s;
  200. while (count--)
  201. *xs++ = c;
  202. return s;
  203. }
  204. #endif
  205. #ifndef __HAVE_ARCH_BCOPY
  206. char * bcopy(const char * src, char * dest, int count)
  207. {
  208. char *tmp = dest;
  209. while (count--)
  210. *tmp++ = *src++;
  211. return dest;
  212. }
  213. #endif
  214. #ifndef __HAVE_ARCH_MEMCPY
  215. void * memcpy(void * dest,const void *src,size_t count)
  216. {
  217. char *tmp = (char *) dest, *s = (char *) src;
  218. while (count--)
  219. *tmp++ = *s++;
  220. return dest;
  221. }
  222. #endif
  223. #ifndef __HAVE_ARCH_MEMMOVE
  224. void * memmove(void * dest,const void *src,size_t count)
  225. {
  226. char *tmp, *s;
  227. if (dest <= src) {
  228. tmp = (char *) dest;
  229. s = (char *) src;
  230. while (count--)
  231. *tmp++ = *s++;
  232. }
  233. else {
  234. tmp = (char *) dest + count;
  235. s = (char *) src + count;
  236. while (count--)
  237. *--tmp = *--s;
  238. }
  239. return dest;
  240. }
  241. #endif
  242. #ifndef __HAVE_ARCH_MEMCMP
  243. int memcmp(const void * cs,const void * ct,size_t count)
  244. {
  245. const unsigned char *su1, *su2;
  246. signed char 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. /*
  254. * find the first occurrence of byte 'c', or 1 past the area if none
  255. */
  256. #ifndef __HAVE_ARCH_MEMSCAN
  257. void * memscan(void * addr, int c, size_t size)
  258. {
  259. unsigned char * p = (unsigned char *) addr;
  260. while (size) {
  261. if (*p == c)
  262. return (void *) p;
  263. p++;
  264. size--;
  265. }
  266. return (void *) p;
  267. }
  268. #endif
  269. #ifndef __HAVE_ARCH_STRSTR
  270. char * strstr(const char * s1,const char * s2)
  271. {
  272. int l1, l2;
  273. l2 = strlen(s2);
  274. if (!l2)
  275. return (char *) s1;
  276. l1 = strlen(s1);
  277. while (l1 >= l2) {
  278. l1--;
  279. if (!memcmp(s1,s2,l2))
  280. return (char *) s1;
  281. s1++;
  282. }
  283. return NULL;
  284. }
  285. #endif