string.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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(register char * __restrict s1,
  66. register const char * __restrict s2, size_t n)
  67. {
  68. char *s = s1;
  69. const char *p = s2;
  70. while (n) {
  71. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  72. ++s;
  73. --n;
  74. }
  75. return s1 + (s2 - p);
  76. }
  77. #endif
  78. /********************** Function strcmp ************************************/
  79. #ifdef L_strcmp
  80. int strcmp(const char *s1, const char *s2)
  81. {
  82. unsigned register char c1, c2;
  83. do {
  84. c1 = (unsigned char) *s1++;
  85. c2 = (unsigned char) *s2++;
  86. if (c1 == '\0')
  87. return c1 - c2;
  88. }
  89. while (c1 == c2);
  90. return c1 - c2;
  91. }
  92. #ifndef __UCLIBC_HAS_LOCALE__
  93. weak_alias(strcmp, strcoll);
  94. #endif /* __UCLIBC_HAS_LOCALE__ */
  95. #endif
  96. /***** Function strcoll (locale only, as non-locale is alias of strcmp *****/
  97. #ifdef L_strcoll
  98. #ifdef __UCLIBC_HAS_LOCALE__
  99. #include "../misc/locale/_locale.h"
  100. const unsigned char *_uc_collate_b; /* NULL for no collate, strcoll->strcmp */
  101. int strcoll(const char *s1, const char *s2)
  102. {
  103. unsigned char c1, c2;
  104. while(1) {
  105. c1 = (unsigned char) *s1;
  106. c2 = (unsigned char) *s2;
  107. if(_uc_collate_b) { /* setuped non-C locale? */
  108. c1 = _uc_collate_b[c1];
  109. c2 = _uc_collate_b[c2];
  110. }
  111. if (*s1 == '\0' || c1 != c2)
  112. return c1 - c2;
  113. s1++;
  114. s2++;
  115. }
  116. }
  117. #endif /* __UCLIBC_HAS_LOCALE__ */
  118. #endif
  119. /********************** Function strncat ************************************/
  120. #ifdef L_strncat
  121. char *strncat(char *dst, const char *src, size_t len)
  122. {
  123. register char *s1 = dst + strlen(dst), *s2;
  124. s2 = memchr(src, 0, len);
  125. if (s2) {
  126. memcpy(s1, src, s2 - src + 1);
  127. } else {
  128. memcpy(s1, src, len);
  129. s1[len] = '\0';
  130. }
  131. return dst;
  132. }
  133. #endif
  134. /********************** Function strncpy ************************************/
  135. #ifdef L_strncpy
  136. char *strncpy(char *dst, const char *src, size_t len)
  137. {
  138. char *ptr = dst;
  139. while (len--) {
  140. if (*src)
  141. *dst++ = *src++;
  142. else
  143. *dst++ = '\0';
  144. }
  145. return ptr;
  146. }
  147. #endif
  148. #ifdef L_strxfrm
  149. size_t strxfrm(char *dst, const char *src, size_t len)
  150. {
  151. size_t length;
  152. strncpy( dst, src, len);
  153. length = strlen(src);
  154. if (length<len)
  155. return(length);
  156. return( len);
  157. }
  158. #endif
  159. /********************** Function strncmp ************************************/
  160. #ifdef L_strncmp
  161. int strncmp(const char *s1, const char *s2, size_t len)
  162. {
  163. unsigned register char c1 = '\0';
  164. unsigned register char c2 = '\0';
  165. while (len > 0) {
  166. c1 = (unsigned char) *s1++;
  167. c2 = (unsigned char) *s2++;
  168. if (c1 == '\0' || c1 != c2)
  169. return c1 - c2;
  170. len--;
  171. }
  172. return c1 - c2;
  173. }
  174. #endif
  175. /********************** Function strchr ************************************/
  176. #ifdef L_strchr
  177. char *strchr(const char *str, int c)
  178. {
  179. register char ch;
  180. do {
  181. if ((ch = *str) == c)
  182. return (char *) str;
  183. str++;
  184. }
  185. while (ch);
  186. return 0;
  187. }
  188. weak_alias(strchr, index);
  189. #endif
  190. /********************** Function strrchr ************************************/
  191. #ifdef L_strrchr
  192. char *strrchr(const char *str, int c)
  193. {
  194. register char *prev = 0;
  195. register char *ptr = (char *) str;
  196. while (*ptr != '\0') {
  197. if (*ptr == c)
  198. prev = ptr;
  199. ptr++;
  200. }
  201. if (c == '\0')
  202. return(ptr);
  203. return(prev);
  204. }
  205. weak_alias(strrchr, rindex);
  206. #endif
  207. /********************** Function strdup ************************************/
  208. #ifdef L_strdup
  209. char *strdup(const char *str)
  210. {
  211. register size_t len;
  212. register char *dst;
  213. len = strlen(str) + 1;
  214. dst = (char *) malloc(len);
  215. if (dst)
  216. memcpy(dst, str, len);
  217. return dst;
  218. }
  219. #endif
  220. /********************** Function strndup ************************************/
  221. #ifdef L_strndup
  222. char *strndup(const char *str, size_t len)
  223. {
  224. register size_t n;
  225. register char *dst;
  226. n = strlen(str);
  227. if (len < n)
  228. n = len;
  229. dst = (char *) malloc(n+1);
  230. if (dst) {
  231. memcpy(dst, str, n);
  232. dst[n] = '\0';
  233. }
  234. return dst;
  235. }
  236. #endif
  237. /********************** Function memcpy ************************************/
  238. #ifdef L_memcpy
  239. void *memcpy(void *dst, const void *src, size_t len)
  240. {
  241. register char *a = dst;
  242. register const char *b = src;
  243. while (len--)
  244. *a++ = *b++;
  245. return dst;
  246. }
  247. #endif
  248. /********************** Function mempcpy ************************************/
  249. #ifdef L_mempcpy
  250. void *mempcpy(void *dst, const void *src, size_t len)
  251. {
  252. register char *a = dst;
  253. register const char *b = src;
  254. while (len--)
  255. *a++ = *b++;
  256. return (void *) a;
  257. }
  258. weak_alias(mempcpy, __mempcpy);
  259. #endif
  260. /********************** Function memccpy ************************************/
  261. #ifdef L_memccpy
  262. void *memccpy(void *dst, const void *src, int c, size_t len)
  263. {
  264. register char *a = dst;
  265. register const char *b = src;
  266. while (len--) {
  267. if ((*a++ = *b++) == c)
  268. return a;
  269. }
  270. return 0;
  271. }
  272. #endif
  273. /********************** Function memset ************************************/
  274. #ifdef L_memset
  275. void *memset(void *str, int c, size_t len)
  276. {
  277. register char *a = str;
  278. while (len--)
  279. *a++ = c;
  280. return str;
  281. }
  282. #endif
  283. /********************** Function memmove ************************************/
  284. #ifdef L_memmove
  285. void *memmove(void *dst, const void *src, size_t len)
  286. {
  287. register char *s1 = dst, *s2 = (char *) src;
  288. /* This bit of sneakyness c/o Glibc, it assumes the test is unsigned */
  289. if (s1 - s2 >= len)
  290. return memcpy(dst, src, len);
  291. /* This reverse copy only used if we absolutly have to */
  292. s1 += len;
  293. s2 += len;
  294. while (len-- > 0)
  295. *(--s1) = *(--s2);
  296. return dst;
  297. }
  298. #endif
  299. /********************** Function memchr ************************************/
  300. #ifdef L_memchr
  301. void *memchr(const void *str, int c, size_t len)
  302. {
  303. register unsigned char *ptr = (unsigned char *) str;
  304. while (len--) {
  305. if (*ptr == (unsigned char) c)
  306. return ptr;
  307. ptr++;
  308. }
  309. return 0;
  310. }
  311. #endif
  312. /********************** Function memrchr ************************************/
  313. #ifdef L_memrchr
  314. void *memrchr(const void *str, int c, size_t len)
  315. {
  316. register unsigned char *ptr;
  317. for (ptr = (unsigned char *) str + len; len > 0; len--) {
  318. if (*--ptr == (unsigned char) c)
  319. return ptr;
  320. }
  321. return 0;
  322. }
  323. #endif
  324. /********************** Function memcmp ************************************/
  325. #ifdef L_memcmp
  326. int memcmp(const void *s1, const void *s2, size_t len)
  327. {
  328. unsigned char *c1 = (unsigned char *)s1;
  329. unsigned char *c2 = (unsigned char *)s2;
  330. while (len--) {
  331. if (*c1 != *c2)
  332. return *c1 - *c2;
  333. c1++;
  334. c2++;
  335. }
  336. return 0;
  337. }
  338. #endif
  339. /********************** Function memcmp ************************************/
  340. #ifdef L_ffs
  341. int ffs(int x)
  342. {
  343. int r = 1;
  344. if (!x)
  345. return 0;
  346. if (!(x & 0xffff)) {
  347. x >>= 16;
  348. r += 16;
  349. }
  350. if (!(x & 0xff)) {
  351. x >>= 8;
  352. r += 8;
  353. }
  354. if (!(x & 0xf)) {
  355. x >>= 4;
  356. r += 4;
  357. }
  358. if (!(x & 3)) {
  359. x >>= 2;
  360. r += 2;
  361. }
  362. if (!(x & 1)) {
  363. x >>= 1;
  364. r += 1;
  365. }
  366. return r;
  367. }
  368. #endif
  369. /********************** THE END ********************************************/