string.c 6.2 KB

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