string.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 strcat ************************************/
  22. #ifdef L_strcat
  23. char *strcat(char *dst, const char *src)
  24. {
  25. strcpy(dst + strlen(dst), src);
  26. return dst;
  27. }
  28. #endif
  29. /********************** Function strcpy ************************************/
  30. #ifdef L_strcpy
  31. char *strcpy(char *dst, const char *src)
  32. {
  33. register char *ptr = dst;
  34. while (*src)
  35. *dst++ = *src++;
  36. *dst = '\0';
  37. return ptr;
  38. }
  39. #endif
  40. /********************** Function strcmp ************************************/
  41. #ifdef L_strcmp
  42. int strcmp(const char *s1, const char *s2)
  43. {
  44. unsigned register char c1, c2;
  45. do {
  46. c1 = (unsigned char) *s1++;
  47. c2 = (unsigned char) *s2++;
  48. if (c1 == '\0')
  49. return c1 - c2;
  50. }
  51. while (c1 == c2);
  52. return c1 - c2;
  53. }
  54. #endif
  55. /********************** Function strncat ************************************/
  56. #ifdef L_strncat
  57. char *strncat(char *dst, const char *src, size_t len)
  58. {
  59. register char *s1 = dst + strlen(dst), *s2;
  60. s2 = memchr(src, 0, len);
  61. if (s2) {
  62. memcpy(s1, src, s2 - src + 1);
  63. } else {
  64. memcpy(s1, src, len);
  65. s1[len] = '\0';
  66. }
  67. return dst;
  68. }
  69. #endif
  70. /********************** Function strncpy ************************************/
  71. #ifdef L_strncpy
  72. char *strncpy(char *dst, const char *src, size_t len)
  73. {
  74. char *ptr = dst;
  75. while (len--) {
  76. if (*src)
  77. *dst++ = *src++;
  78. else
  79. *dst++ = '\0';
  80. }
  81. return ptr;
  82. }
  83. #endif
  84. /********************** Function strncmp ************************************/
  85. #ifdef L_strncmp
  86. int strncmp(const char *s1, const char *s2, size_t len)
  87. {
  88. unsigned register char c1 = '\0';
  89. unsigned register char c2 = '\0';
  90. while (len > 0) {
  91. c1 = (unsigned char) *s1++;
  92. c2 = (unsigned char) *s2++;
  93. if (c1 == '\0' || c1 != c2)
  94. return c1 - c2;
  95. len--;
  96. }
  97. return c1 - c2;
  98. }
  99. #endif
  100. /********************** Function strchr ************************************/
  101. #ifdef L_strchr
  102. char *strchr(const char *str, int c)
  103. {
  104. register char ch;
  105. do {
  106. if ((ch = *str) == c)
  107. return (char *) str;
  108. str++;
  109. }
  110. while (ch);
  111. return 0;
  112. }
  113. #endif
  114. /********************** Function strrchr ************************************/
  115. #ifdef L_strrchr
  116. char *strrchr(const char *str, int c)
  117. {
  118. register char *prev = 0;
  119. register char *ptr = (char *) str;
  120. /* For null it's just like strlen */
  121. if (c == '\0')
  122. return ptr + strlen(ptr);
  123. /* everything else just step along the string. */
  124. while ((ptr = strchr(ptr, c)) != 0) {
  125. prev = ptr;
  126. ptr++;
  127. }
  128. return prev;
  129. }
  130. #endif
  131. /********************** Function strdup ************************************/
  132. #ifdef L_strdup
  133. char *strdup(const char *str)
  134. {
  135. register size_t len;
  136. register char *dst;
  137. len = strlen(str) + 1;
  138. dst = (char *) malloc(len);
  139. if (dst)
  140. memcpy(dst, str, len);
  141. return dst;
  142. }
  143. #endif
  144. /********************** Function memcpy ************************************/
  145. #ifdef L_memcpy
  146. void *memcpy(void *dst, const void *src, size_t len)
  147. {
  148. register char *a = dst;
  149. register const char *b = src;
  150. while (len--)
  151. *a++ = *b++;
  152. return dst;
  153. }
  154. #endif
  155. /********************** Function memccpy ************************************/
  156. #ifdef L_memccpy
  157. void *memccpy(void *dst, const void *src, int c, size_t len)
  158. {
  159. register char *a = dst;
  160. register const char *b = src;
  161. while (len--) {
  162. if ((*a++ = *b++) == c)
  163. return a;
  164. }
  165. return 0;
  166. }
  167. #endif
  168. /********************** Function memset ************************************/
  169. #ifdef L_memset
  170. void *memset(void *str, int c, size_t len)
  171. {
  172. register char *a = str;
  173. while (len--)
  174. *a++ = c;
  175. return str;
  176. }
  177. #endif
  178. /********************** Function memmove ************************************/
  179. #ifdef L_memmove
  180. void *memmove(void *dst, const void *src, size_t len)
  181. {
  182. register char *s1 = dst, *s2 = (char *) src;
  183. /* This bit of sneakyness c/o Glibc, it assumes the test is unsigned */
  184. if (s1 - s2 >= len)
  185. return memcpy(dst, src, len);
  186. /* This reverse copy only used if we absolutly have to */
  187. s1 += len;
  188. s2 += len;
  189. while (len-- > 0)
  190. *(--s1) = *(--s2);
  191. return dst;
  192. }
  193. #endif
  194. /********************** Function memchr ************************************/
  195. #ifdef L_memchr
  196. void *memchr(const void *str, int c, size_t len)
  197. {
  198. register unsigned char *ptr = (unsigned char *) str;
  199. while (len--) {
  200. if (*ptr == (unsigned char) c)
  201. return ptr;
  202. ptr++;
  203. }
  204. return 0;
  205. }
  206. #endif
  207. /********************** Function memcmp ************************************/
  208. #ifdef L_memcmp
  209. int memcmp(const void *s1, const void *s2, size_t len)
  210. {
  211. unsigned char *c1 = (unsigned char *)s1;
  212. unsigned char *c2 = (unsigned char *)s2;
  213. while (len--) {
  214. if (*c1 != *c2)
  215. return *c1 - *c2;
  216. c1++;
  217. c2++;
  218. }
  219. return 0;
  220. }
  221. #endif
  222. /********************** THE END ********************************************/