string.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* vi: set sw=4 ts=4: */
  2. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  3. * This file is part of the Linux-8086 C library and is distributed
  4. * under the GNU Library General Public License.
  5. *
  6. * Many of the functions in this file have been rewritten for correctness
  7. * (but not necessarily speed) by Erik Andersen <andersee@debian.org>
  8. */
  9. #include <string.h>
  10. #include <malloc.h>
  11. /********************** Function strlen ************************************/
  12. #ifdef L_strlen
  13. size_t strlen(const char *str)
  14. {
  15. register char *ptr = (char *) str;
  16. while (*ptr)
  17. ptr++;
  18. return (ptr - str);
  19. }
  20. #endif
  21. /********************** Function strnlen ************************************/
  22. #ifdef L_strnlen
  23. size_t strnlen (const char *string, size_t maxlen)
  24. {
  25. const char *end = memchr (string, '\0', maxlen);
  26. return end ? end - string : maxlen;
  27. }
  28. #endif
  29. /********************** Function strcat ************************************/
  30. #ifdef L_strcat
  31. char *strcat(char *dst, const char *src)
  32. {
  33. strcpy(dst + strlen(dst), src);
  34. return dst;
  35. }
  36. #endif
  37. /********************** Function strcpy ************************************/
  38. #ifdef L_strcpy
  39. char *strcpy(char *dst, const char *src)
  40. {
  41. register char *ptr = dst;
  42. while (*src)
  43. *dst++ = *src++;
  44. *dst = '\0';
  45. return ptr;
  46. }
  47. #endif
  48. /********************** Function stpcpy ************************************/
  49. #ifdef L_stpcpy
  50. char *stpcpy(char *dst, const char *src)
  51. {
  52. while (*src)
  53. *dst++ = *src++;
  54. *dst = '\0';
  55. return dst;
  56. }
  57. #endif
  58. /********************** Function strcmp ************************************/
  59. #ifdef L_strcmp
  60. int strcmp(const char *s1, const char *s2)
  61. {
  62. unsigned register char c1, c2;
  63. do {
  64. c1 = (unsigned char) *s1++;
  65. c2 = (unsigned char) *s2++;
  66. if (c1 == '\0')
  67. return c1 - c2;
  68. }
  69. while (c1 == c2);
  70. return c1 - c2;
  71. }
  72. #ifndef __UCLIBC_HAS_LOCALE__
  73. weak_alias(strcmp, strcoll);
  74. #endif /* __UCLIBC_HAS_LOCALE__ */
  75. #endif
  76. /***** Function strcoll (locale only, as non-locale is alias of strcmp *****/
  77. #ifdef L_strcoll
  78. #ifdef __UCLIBC_HAS_LOCALE__
  79. #include "../misc/locale/_locale.h"
  80. const unsigned char *_uc_collate_b; /* NULL for no collate, strcoll->strcmp */
  81. int strcoll(const char *s1, const char *s2)
  82. {
  83. unsigned char c1, c2;
  84. while(1) {
  85. c1 = (unsigned char) *s1;
  86. c2 = (unsigned char) *s2;
  87. if(_uc_collate_b) { /* setuped non-C locale? */
  88. c1 = _uc_collate_b[c1];
  89. c2 = _uc_collate_b[c2];
  90. }
  91. if (*s1 == '\0' || c1 != c2)
  92. return c1 - c2;
  93. s1++;
  94. s2++;
  95. }
  96. }
  97. #endif /* __UCLIBC_HAS_LOCALE__ */
  98. #endif
  99. /********************** Function strncat ************************************/
  100. #ifdef L_strncat
  101. char *strncat(char *dst, const char *src, size_t len)
  102. {
  103. register char *s1 = dst + strlen(dst), *s2;
  104. s2 = memchr(src, 0, len);
  105. if (s2) {
  106. memcpy(s1, src, s2 - src + 1);
  107. } else {
  108. memcpy(s1, src, len);
  109. s1[len] = '\0';
  110. }
  111. return dst;
  112. }
  113. #endif
  114. /********************** Function strncpy ************************************/
  115. #ifdef L_strncpy
  116. char *strncpy(char *dst, const char *src, size_t len)
  117. {
  118. char *ptr = dst;
  119. while (len--) {
  120. if (*src)
  121. *dst++ = *src++;
  122. else
  123. *dst++ = '\0';
  124. }
  125. return ptr;
  126. }
  127. #endif
  128. #ifdef L_strxfrm
  129. size_t strxfrm(char *dst, const char *src, size_t len)
  130. {
  131. size_t length;
  132. strncpy( dst, src, len);
  133. length = strlen(src);
  134. if (length<len)
  135. return(length);
  136. return( len);
  137. }
  138. #endif
  139. /********************** Function strncmp ************************************/
  140. #ifdef L_strncmp
  141. int strncmp(const char *s1, const char *s2, size_t len)
  142. {
  143. unsigned register char c1 = '\0';
  144. unsigned register char c2 = '\0';
  145. while (len > 0) {
  146. c1 = (unsigned char) *s1++;
  147. c2 = (unsigned char) *s2++;
  148. if (c1 == '\0' || c1 != c2)
  149. return c1 - c2;
  150. len--;
  151. }
  152. return c1 - c2;
  153. }
  154. #endif
  155. /********************** Function strchr ************************************/
  156. #ifdef L_strchr
  157. char *strchr(const char *str, int c)
  158. {
  159. register char ch;
  160. do {
  161. if ((ch = *str) == c)
  162. return (char *) str;
  163. str++;
  164. }
  165. while (ch);
  166. return 0;
  167. }
  168. weak_alias(strchr, index);
  169. #endif
  170. /********************** Function strrchr ************************************/
  171. #ifdef L_strrchr
  172. char *strrchr(const char *str, int c)
  173. {
  174. register char *prev = 0;
  175. register char *ptr = (char *) str;
  176. while (*ptr != '\0') {
  177. if (*ptr == c)
  178. prev = ptr;
  179. ptr++;
  180. }
  181. if (c == '\0')
  182. return(ptr);
  183. return(prev);
  184. }
  185. weak_alias(strrchr, rindex);
  186. #endif
  187. /********************** Function strdup ************************************/
  188. #ifdef L_strdup
  189. char *strdup(const char *str)
  190. {
  191. register size_t len;
  192. register char *dst;
  193. len = strlen(str) + 1;
  194. dst = (char *) malloc(len);
  195. if (dst)
  196. memcpy(dst, str, len);
  197. return dst;
  198. }
  199. #endif
  200. /********************** Function memcpy ************************************/
  201. #ifdef L_memcpy
  202. void *memcpy(void *dst, const void *src, size_t len)
  203. {
  204. register char *a = dst;
  205. register const char *b = src;
  206. while (len--)
  207. *a++ = *b++;
  208. return dst;
  209. }
  210. #endif
  211. /********************** Function memccpy ************************************/
  212. #ifdef L_memccpy
  213. void *memccpy(void *dst, const void *src, int c, size_t len)
  214. {
  215. register char *a = dst;
  216. register const char *b = src;
  217. while (len--) {
  218. if ((*a++ = *b++) == c)
  219. return a;
  220. }
  221. return 0;
  222. }
  223. #endif
  224. /********************** Function memset ************************************/
  225. #ifdef L_memset
  226. void *memset(void *str, int c, size_t len)
  227. {
  228. register char *a = str;
  229. while (len--)
  230. *a++ = c;
  231. return str;
  232. }
  233. #endif
  234. /********************** Function memmove ************************************/
  235. #ifdef L_memmove
  236. void *memmove(void *dst, const void *src, size_t len)
  237. {
  238. register char *s1 = dst, *s2 = (char *) src;
  239. /* This bit of sneakyness c/o Glibc, it assumes the test is unsigned */
  240. if (s1 - s2 >= len)
  241. return memcpy(dst, src, len);
  242. /* This reverse copy only used if we absolutly have to */
  243. s1 += len;
  244. s2 += len;
  245. while (len-- > 0)
  246. *(--s1) = *(--s2);
  247. return dst;
  248. }
  249. #endif
  250. /********************** Function memchr ************************************/
  251. #ifdef L_memchr
  252. void *memchr(const void *str, int c, size_t len)
  253. {
  254. register unsigned char *ptr = (unsigned char *) str;
  255. while (len--) {
  256. if (*ptr == (unsigned char) c)
  257. return ptr;
  258. ptr++;
  259. }
  260. return 0;
  261. }
  262. #endif
  263. /********************** Function memcmp ************************************/
  264. #ifdef L_memcmp
  265. int memcmp(const void *s1, const void *s2, size_t len)
  266. {
  267. unsigned char *c1 = (unsigned char *)s1;
  268. unsigned char *c2 = (unsigned char *)s2;
  269. while (len--) {
  270. if (*c1 != *c2)
  271. return *c1 - *c2;
  272. c1++;
  273. c2++;
  274. }
  275. return 0;
  276. }
  277. #endif
  278. /********************** Function memcmp ************************************/
  279. #ifdef L_ffs
  280. int ffs(int x)
  281. {
  282. int r = 1;
  283. if (!x)
  284. return 0;
  285. if (!(x & 0xffff)) {
  286. x >>= 16;
  287. r += 16;
  288. }
  289. if (!(x & 0xff)) {
  290. x >>= 8;
  291. r += 8;
  292. }
  293. if (!(x & 0xf)) {
  294. x >>= 4;
  295. r += 4;
  296. }
  297. if (!(x & 3)) {
  298. x >>= 2;
  299. r += 2;
  300. }
  301. if (!(x & 1)) {
  302. x >>= 1;
  303. r += 1;
  304. }
  305. return r;
  306. }
  307. #endif
  308. /********************** THE END ********************************************/