string.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 strcmp ************************************/
  49. #ifdef L_strcmp
  50. int strcmp(const char *s1, const char *s2)
  51. {
  52. unsigned register char c1, c2;
  53. do {
  54. c1 = (unsigned char) *s1++;
  55. c2 = (unsigned char) *s2++;
  56. if (c1 == '\0')
  57. return c1 - c2;
  58. }
  59. while (c1 == c2);
  60. return c1 - c2;
  61. }
  62. __asm__(".weak strcoll; strcoll = strcmp");
  63. #endif
  64. /********************** Function strncat ************************************/
  65. #ifdef L_strncat
  66. char *strncat(char *dst, const char *src, size_t len)
  67. {
  68. register char *s1 = dst + strlen(dst), *s2;
  69. s2 = memchr(src, 0, len);
  70. if (s2) {
  71. memcpy(s1, src, s2 - src + 1);
  72. } else {
  73. memcpy(s1, src, len);
  74. s1[len] = '\0';
  75. }
  76. return dst;
  77. }
  78. #endif
  79. /********************** Function strncpy ************************************/
  80. #ifdef L_strncpy
  81. char *strncpy(char *dst, const char *src, size_t len)
  82. {
  83. char *ptr = dst;
  84. while (len--) {
  85. if (*src)
  86. *dst++ = *src++;
  87. else
  88. *dst++ = '\0';
  89. }
  90. return ptr;
  91. }
  92. #endif
  93. #ifdef L_strxfrm
  94. size_t strxfrm(char *dst, const char *src, size_t len)
  95. {
  96. strncpy( dst, src, len);
  97. return( len);
  98. }
  99. #endif
  100. /********************** Function strncmp ************************************/
  101. #ifdef L_strncmp
  102. int strncmp(const char *s1, const char *s2, size_t len)
  103. {
  104. unsigned register char c1 = '\0';
  105. unsigned register char c2 = '\0';
  106. while (len > 0) {
  107. c1 = (unsigned char) *s1++;
  108. c2 = (unsigned char) *s2++;
  109. if (c1 == '\0' || c1 != c2)
  110. return c1 - c2;
  111. len--;
  112. }
  113. return c1 - c2;
  114. }
  115. #endif
  116. /********************** Function strchr ************************************/
  117. #ifdef L_strchr
  118. char *strchr(const char *str, int c)
  119. {
  120. register char ch;
  121. do {
  122. if ((ch = *str) == c)
  123. return (char *) str;
  124. str++;
  125. }
  126. while (ch);
  127. return 0;
  128. }
  129. __asm__(".weak index; index = strchr");
  130. #endif
  131. /********************** Function strrchr ************************************/
  132. #ifdef L_strrchr
  133. char *strrchr(const char *str, int c)
  134. {
  135. register char *prev = 0;
  136. register char *ptr = (char *) str;
  137. /* For null it's just like strlen */
  138. if (c == '\0')
  139. return ptr + strlen(ptr);
  140. /* everything else just step along the string. */
  141. while ((ptr = strchr(ptr, c)) != 0) {
  142. prev = ptr;
  143. ptr++;
  144. }
  145. return prev;
  146. }
  147. __asm__(".weak rindex; rindex = strrchr");
  148. #endif
  149. /********************** Function strdup ************************************/
  150. #ifdef L_strdup
  151. char *strdup(const char *str)
  152. {
  153. register size_t len;
  154. register char *dst;
  155. len = strlen(str) + 1;
  156. dst = (char *) malloc(len);
  157. if (dst)
  158. memcpy(dst, str, len);
  159. return dst;
  160. }
  161. #endif
  162. /********************** Function memcpy ************************************/
  163. #ifdef L_memcpy
  164. void *memcpy(void *dst, const void *src, size_t len)
  165. {
  166. register char *a = dst;
  167. register const char *b = src;
  168. while (len--)
  169. *a++ = *b++;
  170. return dst;
  171. }
  172. #endif
  173. /********************** Function memccpy ************************************/
  174. #ifdef L_memccpy
  175. void *memccpy(void *dst, const void *src, int c, size_t len)
  176. {
  177. register char *a = dst;
  178. register const char *b = src;
  179. while (len--) {
  180. if ((*a++ = *b++) == c)
  181. return a;
  182. }
  183. return 0;
  184. }
  185. #endif
  186. /********************** Function memset ************************************/
  187. #ifdef L_memset
  188. void *memset(void *str, int c, size_t len)
  189. {
  190. register char *a = str;
  191. while (len--)
  192. *a++ = c;
  193. return str;
  194. }
  195. #endif
  196. /********************** Function memmove ************************************/
  197. #ifdef L_memmove
  198. void *memmove(void *dst, const void *src, size_t len)
  199. {
  200. register char *s1 = dst, *s2 = (char *) src;
  201. /* This bit of sneakyness c/o Glibc, it assumes the test is unsigned */
  202. if (s1 - s2 >= len)
  203. return memcpy(dst, src, len);
  204. /* This reverse copy only used if we absolutly have to */
  205. s1 += len;
  206. s2 += len;
  207. while (len-- > 0)
  208. *(--s1) = *(--s2);
  209. return dst;
  210. }
  211. #endif
  212. /********************** Function memchr ************************************/
  213. #ifdef L_memchr
  214. void *memchr(const void *str, int c, size_t len)
  215. {
  216. register unsigned char *ptr = (unsigned char *) str;
  217. while (len--) {
  218. if (*ptr == (unsigned char) c)
  219. return ptr;
  220. ptr++;
  221. }
  222. return 0;
  223. }
  224. #endif
  225. /********************** Function memcmp ************************************/
  226. #ifdef L_memcmp
  227. int memcmp(const void *s1, const void *s2, size_t len)
  228. {
  229. unsigned char *c1 = (unsigned char *)s1;
  230. unsigned char *c2 = (unsigned char *)s2;
  231. while (len--) {
  232. if (*c1 != *c2)
  233. return *c1 - *c2;
  234. c1++;
  235. c2++;
  236. }
  237. return 0;
  238. }
  239. #endif
  240. /********************** Function memcmp ************************************/
  241. #ifdef L_ffs
  242. int ffs(int x)
  243. {
  244. int r = 1;
  245. if (!x)
  246. return 0;
  247. if (!(x & 0xffff)) {
  248. x >>= 16;
  249. r += 16;
  250. }
  251. if (!(x & 0xff)) {
  252. x >>= 8;
  253. r += 8;
  254. }
  255. if (!(x & 0xf)) {
  256. x >>= 4;
  257. r += 4;
  258. }
  259. if (!(x & 3)) {
  260. x >>= 2;
  261. r += 2;
  262. }
  263. if (!(x & 1)) {
  264. x >>= 1;
  265. r += 1;
  266. }
  267. return r;
  268. }
  269. #endif
  270. /********************** THE END ********************************************/