string.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 strndup ************************************/
  219. #ifdef L_strndup
  220. char *strndup(const char *str, size_t len)
  221. {
  222. register size_t n;
  223. register char *dst;
  224. n = strlen(str);
  225. if (len < n)
  226. n = len;
  227. dst = (char *) malloc(n+1);
  228. if (dst) {
  229. memcpy(dst, str, n);
  230. dst[n] = '\0';
  231. }
  232. return dst;
  233. }
  234. #endif
  235. /********************** Function memcpy ************************************/
  236. #ifdef L_memcpy
  237. void *memcpy(void *dst, const void *src, size_t len)
  238. {
  239. register char *a = dst;
  240. register const char *b = src;
  241. while (len--)
  242. *a++ = *b++;
  243. return dst;
  244. }
  245. #endif
  246. /********************** Function memccpy ************************************/
  247. #ifdef L_memccpy
  248. void *memccpy(void *dst, const void *src, int c, size_t len)
  249. {
  250. register char *a = dst;
  251. register const char *b = src;
  252. while (len--) {
  253. if ((*a++ = *b++) == c)
  254. return a;
  255. }
  256. return 0;
  257. }
  258. #endif
  259. /********************** Function memset ************************************/
  260. #ifdef L_memset
  261. void *memset(void *str, int c, size_t len)
  262. {
  263. register char *a = str;
  264. while (len--)
  265. *a++ = c;
  266. return str;
  267. }
  268. #endif
  269. /********************** Function memmove ************************************/
  270. #ifdef L_memmove
  271. void *memmove(void *dst, const void *src, size_t len)
  272. {
  273. register char *s1 = dst, *s2 = (char *) src;
  274. /* This bit of sneakyness c/o Glibc, it assumes the test is unsigned */
  275. if (s1 - s2 >= len)
  276. return memcpy(dst, src, len);
  277. /* This reverse copy only used if we absolutly have to */
  278. s1 += len;
  279. s2 += len;
  280. while (len-- > 0)
  281. *(--s1) = *(--s2);
  282. return dst;
  283. }
  284. #endif
  285. /********************** Function memchr ************************************/
  286. #ifdef L_memchr
  287. void *memchr(const void *str, int c, size_t len)
  288. {
  289. register unsigned char *ptr = (unsigned char *) str;
  290. while (len--) {
  291. if (*ptr == (unsigned char) c)
  292. return ptr;
  293. ptr++;
  294. }
  295. return 0;
  296. }
  297. #endif
  298. /********************** Function memcmp ************************************/
  299. #ifdef L_memcmp
  300. int memcmp(const void *s1, const void *s2, size_t len)
  301. {
  302. unsigned char *c1 = (unsigned char *)s1;
  303. unsigned char *c2 = (unsigned char *)s2;
  304. while (len--) {
  305. if (*c1 != *c2)
  306. return *c1 - *c2;
  307. c1++;
  308. c2++;
  309. }
  310. return 0;
  311. }
  312. #endif
  313. /********************** Function memcmp ************************************/
  314. #ifdef L_ffs
  315. int ffs(int x)
  316. {
  317. int r = 1;
  318. if (!x)
  319. return 0;
  320. if (!(x & 0xffff)) {
  321. x >>= 16;
  322. r += 16;
  323. }
  324. if (!(x & 0xff)) {
  325. x >>= 8;
  326. r += 8;
  327. }
  328. if (!(x & 0xf)) {
  329. x >>= 4;
  330. r += 4;
  331. }
  332. if (!(x & 3)) {
  333. x >>= 2;
  334. r += 2;
  335. }
  336. if (!(x & 1)) {
  337. x >>= 1;
  338. r += 1;
  339. }
  340. return r;
  341. }
  342. #endif
  343. /********************** THE END ********************************************/