string.c 5.2 KB

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