string.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. register char *ptr = dst;
  34. while (*ptr)
  35. ptr++;
  36. while (*src)
  37. *ptr++ = *src++;
  38. *ptr = '\0';
  39. return dst;
  40. }
  41. #endif
  42. /********************** Function strcpy ************************************/
  43. #ifdef L_strcpy
  44. char *strcpy(char *dst, const char *src)
  45. {
  46. register char *ptr = dst;
  47. while (*src)
  48. *dst++ = *src++;
  49. *dst = '\0';
  50. return ptr;
  51. }
  52. #endif
  53. /********************** Function stpcpy ************************************/
  54. #ifdef L_stpcpy
  55. char *stpcpy(char *dst, const char *src)
  56. {
  57. while (*src)
  58. *dst++ = *src++;
  59. *dst = '\0';
  60. return dst;
  61. }
  62. #endif
  63. /********************** Function stpncpy ************************************/
  64. #ifdef L_stpncpy
  65. char *stpncpy(char *dst, const char *src, size_t len)
  66. {
  67. while (len--) {
  68. if (*src)
  69. *dst++ = *src++;
  70. else
  71. *dst++ = '\0';
  72. }
  73. return dst;
  74. }
  75. #endif
  76. /********************** Function strcmp ************************************/
  77. #ifdef L_strcmp
  78. int strcmp(const char *s1, const char *s2)
  79. {
  80. unsigned register char c1, c2;
  81. do {
  82. c1 = (unsigned char) *s1++;
  83. c2 = (unsigned char) *s2++;
  84. if (c1 == '\0')
  85. return c1 - c2;
  86. }
  87. while (c1 == c2);
  88. return c1 - c2;
  89. }
  90. #ifndef __UCLIBC_HAS_LOCALE__
  91. weak_alias(strcmp, strcoll);
  92. #endif /* __UCLIBC_HAS_LOCALE__ */
  93. #endif
  94. /***** Function strcoll (locale only, as non-locale is alias of strcmp *****/
  95. #ifdef L_strcoll
  96. #ifdef __UCLIBC_HAS_LOCALE__
  97. #include "../misc/locale/_locale.h"
  98. const unsigned char *_uc_collate_b; /* NULL for no collate, strcoll->strcmp */
  99. int strcoll(const char *s1, const char *s2)
  100. {
  101. unsigned char c1, c2;
  102. while(1) {
  103. c1 = (unsigned char) *s1;
  104. c2 = (unsigned char) *s2;
  105. if(_uc_collate_b) { /* setuped non-C locale? */
  106. c1 = _uc_collate_b[c1];
  107. c2 = _uc_collate_b[c2];
  108. }
  109. if (*s1 == '\0' || c1 != c2)
  110. return c1 - c2;
  111. s1++;
  112. s2++;
  113. }
  114. }
  115. #endif /* __UCLIBC_HAS_LOCALE__ */
  116. #endif
  117. /********************** Function strncat ************************************/
  118. #ifdef L_strncat
  119. char *strncat(char *dst, const char *src, size_t len)
  120. {
  121. register char *s1 = dst + strlen(dst), *s2;
  122. s2 = memchr(src, 0, len);
  123. if (s2) {
  124. memcpy(s1, src, s2 - src + 1);
  125. } else {
  126. memcpy(s1, src, len);
  127. s1[len] = '\0';
  128. }
  129. return dst;
  130. }
  131. #endif
  132. /********************** Function strncpy ************************************/
  133. #ifdef L_strncpy
  134. char *strncpy(char *dst, const char *src, size_t len)
  135. {
  136. char *ptr = dst;
  137. while (len--) {
  138. if (*src)
  139. *dst++ = *src++;
  140. else
  141. *dst++ = '\0';
  142. }
  143. return ptr;
  144. }
  145. #endif
  146. #ifdef L_strxfrm
  147. size_t strxfrm(char *dst, const char *src, size_t len)
  148. {
  149. size_t length;
  150. strncpy( dst, src, len);
  151. length = strlen(src);
  152. if (length<len)
  153. return(length);
  154. return( len);
  155. }
  156. #endif
  157. /********************** Function strncmp ************************************/
  158. #ifdef L_strncmp
  159. int strncmp(const char *s1, const char *s2, size_t len)
  160. {
  161. unsigned register char c1 = '\0';
  162. unsigned register char c2 = '\0';
  163. while (len > 0) {
  164. c1 = (unsigned char) *s1++;
  165. c2 = (unsigned char) *s2++;
  166. if (c1 == '\0' || c1 != c2)
  167. return c1 - c2;
  168. len--;
  169. }
  170. return c1 - c2;
  171. }
  172. #endif
  173. /********************** Function strchr ************************************/
  174. #ifdef L_strchr
  175. char *strchr(const char *str, int c)
  176. {
  177. register char ch;
  178. do {
  179. if ((ch = *str) == c)
  180. return (char *) str;
  181. str++;
  182. }
  183. while (ch);
  184. return 0;
  185. }
  186. weak_alias(strchr, index);
  187. #endif
  188. /********************** Function strrchr ************************************/
  189. #ifdef L_strrchr
  190. char *strrchr(const char *str, int c)
  191. {
  192. register char *prev = 0;
  193. register char *ptr = (char *) str;
  194. while (*ptr != '\0') {
  195. if (*ptr == c)
  196. prev = ptr;
  197. ptr++;
  198. }
  199. if (c == '\0')
  200. return(ptr);
  201. return(prev);
  202. }
  203. weak_alias(strrchr, rindex);
  204. #endif
  205. /********************** Function strdup ************************************/
  206. #ifdef L_strdup
  207. char *strdup(const char *str)
  208. {
  209. register size_t len;
  210. register char *dst;
  211. len = strlen(str) + 1;
  212. dst = (char *) malloc(len);
  213. if (dst)
  214. memcpy(dst, str, len);
  215. return dst;
  216. }
  217. #endif
  218. /********************** Function memcpy ************************************/
  219. #ifdef L_memcpy
  220. void *memcpy(void *dst, const void *src, size_t len)
  221. {
  222. register char *a = dst;
  223. register const char *b = src;
  224. while (len--)
  225. *a++ = *b++;
  226. return dst;
  227. }
  228. #endif
  229. /********************** Function memccpy ************************************/
  230. #ifdef L_memccpy
  231. void *memccpy(void *dst, const void *src, int c, size_t len)
  232. {
  233. register char *a = dst;
  234. register const char *b = src;
  235. while (len--) {
  236. if ((*a++ = *b++) == c)
  237. return a;
  238. }
  239. return 0;
  240. }
  241. #endif
  242. /********************** Function memset ************************************/
  243. #ifdef L_memset
  244. void *memset(void *str, int c, size_t len)
  245. {
  246. register char *a = str;
  247. while (len--)
  248. *a++ = c;
  249. return str;
  250. }
  251. #endif
  252. /********************** Function memmove ************************************/
  253. #ifdef L_memmove
  254. void *memmove(void *dst, const void *src, size_t len)
  255. {
  256. register char *s1 = dst, *s2 = (char *) src;
  257. /* This bit of sneakyness c/o Glibc, it assumes the test is unsigned */
  258. if (s1 - s2 >= len)
  259. return memcpy(dst, src, len);
  260. /* This reverse copy only used if we absolutly have to */
  261. s1 += len;
  262. s2 += len;
  263. while (len-- > 0)
  264. *(--s1) = *(--s2);
  265. return dst;
  266. }
  267. #endif
  268. /********************** Function memchr ************************************/
  269. #ifdef L_memchr
  270. void *memchr(const void *str, int c, size_t len)
  271. {
  272. register unsigned char *ptr = (unsigned char *) str;
  273. while (len--) {
  274. if (*ptr == (unsigned char) c)
  275. return ptr;
  276. ptr++;
  277. }
  278. return 0;
  279. }
  280. #endif
  281. /********************** Function memcmp ************************************/
  282. #ifdef L_memcmp
  283. int memcmp(const void *s1, const void *s2, size_t len)
  284. {
  285. unsigned char *c1 = (unsigned char *)s1;
  286. unsigned char *c2 = (unsigned char *)s2;
  287. while (len--) {
  288. if (*c1 != *c2)
  289. return *c1 - *c2;
  290. c1++;
  291. c2++;
  292. }
  293. return 0;
  294. }
  295. #endif
  296. /********************** Function memcmp ************************************/
  297. #ifdef L_ffs
  298. int ffs(int x)
  299. {
  300. int r = 1;
  301. if (!x)
  302. return 0;
  303. if (!(x & 0xffff)) {
  304. x >>= 16;
  305. r += 16;
  306. }
  307. if (!(x & 0xff)) {
  308. x >>= 8;
  309. r += 8;
  310. }
  311. if (!(x & 0xf)) {
  312. x >>= 4;
  313. r += 4;
  314. }
  315. if (!(x & 3)) {
  316. x >>= 2;
  317. r += 2;
  318. }
  319. if (!(x & 1)) {
  320. x >>= 1;
  321. r += 1;
  322. }
  323. return r;
  324. }
  325. #endif
  326. /********************** THE END ********************************************/