wstring.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. #define _GNU_SOURCE
  28. #include <string.h>
  29. #include <strings.h>
  30. #include <limits.h>
  31. #include <ctype.h>
  32. #include <stdlib.h>
  33. #ifdef WANT_WIDE
  34. #include <wchar.h>
  35. #include <wctype.h>
  36. #include <locale.h>
  37. #define Wvoid wchar_t
  38. #define Wchar wchar_t
  39. #define Wuchar __uwchar_t
  40. #define Wint wchar_t
  41. #else
  42. #define Wvoid void
  43. #define Wchar char
  44. typedef unsigned char __string_uchar_t;
  45. #define Wuchar __string_uchar_t
  46. #define Wint int
  47. #endif
  48. /**********************************************************************/
  49. #ifdef L_wmemcpy
  50. #define L_memcpy
  51. #define Wmemcpy wmemcpy
  52. #else
  53. #define Wmemcpy memcpy
  54. #endif
  55. #ifdef L_memcpy
  56. Wvoid *Wmemcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  57. {
  58. register Wchar *r1 = s1;
  59. register const Wchar *r2 = s2;
  60. #ifdef __BCC__
  61. while (n--) {
  62. *r1++ = *r2++;
  63. }
  64. #else
  65. while (n) {
  66. *r1++ = *r2++;
  67. --n;
  68. }
  69. #endif
  70. return s1;
  71. }
  72. #endif
  73. /**********************************************************************/
  74. #ifdef L_wmemmove
  75. #define L_memmove
  76. #define Wmemmove wmemmove
  77. #else
  78. #define Wmemmove memmove
  79. #endif
  80. #ifdef L_memmove
  81. Wvoid *Wmemmove(Wvoid *s1, const Wvoid *s2, size_t n)
  82. {
  83. #ifdef __BCC__
  84. register Wchar *s = (Wchar *) s1;
  85. register const Wchar *p = (const Wchar *) s2;
  86. if (p >= s) {
  87. while (n--) {
  88. *s++ = *p++;
  89. }
  90. } else {
  91. s += n;
  92. p += n;
  93. while (n--) {
  94. *--s = *--p;
  95. }
  96. }
  97. return s1;
  98. #else
  99. register Wchar *s = (Wchar *) s1;
  100. register const Wchar *p = (const Wchar *) s2;
  101. if (p >= s) {
  102. while (n) {
  103. *s++ = *p++;
  104. --n;
  105. }
  106. } else {
  107. while (n) {
  108. --n;
  109. s[n] = p[n];
  110. }
  111. }
  112. return s1;
  113. #endif
  114. }
  115. #endif
  116. /**********************************************************************/
  117. #ifdef L_wcscpy
  118. #define L_strcpy
  119. #define Wstrcpy wcscpy
  120. #else
  121. #define Wstrcpy strcpy
  122. #endif
  123. #ifdef L_strcpy
  124. Wchar *Wstrcpy(Wchar * __restrict s1, const Wchar * __restrict s2)
  125. {
  126. register Wchar *s = s1;
  127. #ifdef __BCC__
  128. do {
  129. *s = *s2++;
  130. } while (*s++ != 0);
  131. #else
  132. while ( (*s++ = *s2++) != 0 );
  133. #endif
  134. return s1;
  135. }
  136. #endif
  137. /**********************************************************************/
  138. #ifdef L_wcsncpy
  139. #define L_strncpy
  140. #define Wstrncpy wcsncpy
  141. #else
  142. #define Wstrncpy strncpy
  143. #endif
  144. #ifdef L_strncpy
  145. Wchar *Wstrncpy(Wchar * __restrict s1, register const Wchar * __restrict s2,
  146. size_t n)
  147. {
  148. register Wchar *s = s1;
  149. #ifdef __BCC__
  150. while (n--) {
  151. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  152. ++s;
  153. }
  154. #else
  155. while (n) {
  156. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  157. ++s;
  158. --n;
  159. }
  160. #endif
  161. return s1;
  162. }
  163. #endif
  164. /**********************************************************************/
  165. #ifdef L_wcscat
  166. #define L_strcat
  167. #define Wstrcat wcscat
  168. #else
  169. #define Wstrcat strcat
  170. #endif
  171. #ifdef L_strcat
  172. Wchar *Wstrcat(Wchar * __restrict s1, register const Wchar * __restrict s2)
  173. {
  174. register Wchar *s = s1;
  175. while (*s++);
  176. --s;
  177. while ((*s++ = *s2++) != 0);
  178. return s1;
  179. }
  180. #endif
  181. /**********************************************************************/
  182. #ifdef L_wcsncat
  183. #define L_strncat
  184. #define Wstrncat wcsncat
  185. #else
  186. #define Wstrncat strncat
  187. #endif
  188. #ifdef L_strncat
  189. Wchar *Wstrncat(Wchar * __restrict s1, register const Wchar * __restrict s2,
  190. size_t n)
  191. {
  192. register Wchar *s = s1;
  193. while (*s++);
  194. --s;
  195. #if __BCC__
  196. while (n-- && ((*s = *s2++) != 0)) ++s;
  197. #else
  198. while (n && ((*s = *s2++) != 0)) {
  199. --n;
  200. ++s;
  201. }
  202. #endif
  203. *s = 0;
  204. return s1;
  205. }
  206. #endif
  207. /**********************************************************************/
  208. #ifdef L_wmemcmp
  209. #define L_memcmp
  210. #define Wmemcmp wmemcmp
  211. #else
  212. #define Wmemcmp memcmp
  213. #endif
  214. #ifdef L_memcmp
  215. #ifndef L_wmemcmp
  216. weak_alias(memcmp,bcmp)
  217. #endif
  218. int Wmemcmp(const Wvoid *s1, const Wvoid *s2, size_t n)
  219. {
  220. register const Wuchar *r1 = (const Wuchar *) s1;
  221. register const Wuchar *r2 = (const Wuchar *) s2;
  222. #ifdef WANT_WIDE
  223. while (n && (*r1 == *r2)) {
  224. ++r1;
  225. ++r2;
  226. --n;
  227. }
  228. return (n == 0) ? 0 : ((*r1 < *r2) ? -1 : 1);
  229. #else
  230. int r = 0;
  231. while (n-- && ((r = ((int)(*r1++)) - *r2++) == 0));
  232. return r;
  233. #endif
  234. }
  235. #endif
  236. /**********************************************************************/
  237. #ifdef L_wcscmp
  238. #define L_strcmp
  239. #define Wstrcmp wcscmp
  240. #else
  241. #define Wstrcmp strcmp
  242. #endif
  243. #ifdef L_strcmp
  244. #ifndef L_wcscmp
  245. #warning implement strcoll and remove weak alias (or enable for C locale only)
  246. weak_alias(strcmp, strcoll);
  247. #endif
  248. int Wstrcmp(register const Wchar *s1, register const Wchar *s2)
  249. {
  250. #ifdef WANT_WIDE
  251. while (*((Wuchar *)s1) == *((Wuchar *)s2)) {
  252. if (!*s1++) {
  253. return 0;
  254. }
  255. ++s2;
  256. }
  257. return (*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1;
  258. #else
  259. int r;
  260. while (((r = ((int)(*((Wuchar *)s1))) - *((Wuchar *)s2++))
  261. == 0) && *s1++);
  262. return r;
  263. #endif
  264. }
  265. #endif
  266. /**********************************************************************/
  267. #ifdef L_strcoll
  268. #error implement strcoll and remove weak_alias!!
  269. #if 0
  270. extern unsigned char *_ctype_collate;
  271. int strcoll(register const char *s1, const char *s2)
  272. {
  273. int r;
  274. while (!(r = (_ctype_collate[(int)(*s1++)]-_ctype_collate[(int)(*s2++)])));
  275. return r;
  276. }
  277. #endif
  278. #endif
  279. /**********************************************************************/
  280. #ifdef L_wcsncmp
  281. #define L_strncmp
  282. #define Wstrncmp wcsncmp
  283. #else
  284. #define Wstrncmp strncmp
  285. #endif
  286. #ifdef L_strncmp
  287. int Wstrncmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  288. {
  289. #ifdef WANT_WIDE
  290. while (n && (*((Wuchar *)s1) == *((Wuchar *)s2))) {
  291. if (!*s1++) {
  292. return 0;
  293. }
  294. ++s2;
  295. --n;
  296. }
  297. return (n == 0) ? 0 : ((*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1);
  298. #else
  299. int r = 0;
  300. while (n--
  301. && ((r = ((int)(*((unsigned char *)s1))) - *((unsigned char *)s2++))
  302. == 0)
  303. && *s1++);
  304. return r;
  305. #endif
  306. }
  307. #endif
  308. /**********************************************************************/
  309. #ifdef L_strxfrm
  310. #error implement strxfrm
  311. /* size_t strxfrm(char *dst, const char *src, size_t len); */
  312. #endif
  313. /**********************************************************************/
  314. #ifdef L_wmemchr
  315. #define L_memchr
  316. #define Wmemchr wmemchr
  317. #else
  318. #define Wmemchr memchr
  319. #endif
  320. #ifdef L_memchr
  321. Wvoid *Wmemchr(const Wvoid *s, Wint c, size_t n)
  322. {
  323. register const Wuchar *r = (const Wuchar *) s;
  324. #ifdef __BCC__
  325. /* bcc can optimize the counter if it thinks it is a pointer... */
  326. register const char *np = (const char *) n;
  327. #else
  328. #define np n
  329. #endif
  330. while (np) {
  331. if (*r == ((Wuchar)c)) {
  332. return (Wvoid *) r; /* silence the warning */
  333. }
  334. ++r;
  335. --np;
  336. }
  337. return NULL;
  338. }
  339. #undef np
  340. #endif
  341. /**********************************************************************/
  342. #ifdef L_wcschr
  343. #define L_strchr
  344. #define Wstrchr wcschr
  345. #else
  346. #define Wstrchr strchr
  347. #endif
  348. #ifdef L_strchr
  349. #ifndef L_wcschr
  350. weak_alias(strchr,index)
  351. #endif
  352. Wchar *Wstrchr(register const Wchar *s, Wint c)
  353. {
  354. do {
  355. if (*s == ((Wchar)c)) {
  356. return (Wchar *) s; /* silence the warning */
  357. }
  358. } while (*s++);
  359. return NULL;
  360. }
  361. #endif
  362. /**********************************************************************/
  363. #ifdef L_wcscspn
  364. #define L_strcspn
  365. #define Wstrcspn wcscspn
  366. #else
  367. #define Wstrcspn strcspn
  368. #endif
  369. #ifdef L_strcspn
  370. size_t Wstrcspn(const Wchar *s1, const Wchar *s2)
  371. {
  372. register const Wchar *s;
  373. register const Wchar *p;
  374. for ( s=s1 ; *s ; s++ ) {
  375. for ( p=s2 ; *p ; p++ ) {
  376. if (*p == *s) goto done;
  377. }
  378. }
  379. done:
  380. return s - s1;
  381. }
  382. #endif
  383. /**********************************************************************/
  384. #ifdef L_wcspbrk
  385. #define L_strpbrk
  386. #define Wstrpbrk wcspbrk
  387. #else
  388. #define Wstrpbrk strpbrk
  389. #endif
  390. #ifdef L_strpbrk
  391. Wchar *Wstrpbrk(const Wchar *s1, const Wchar *s2)
  392. {
  393. register const Wchar *s;
  394. register const Wchar *p;
  395. for ( s=s1 ; *s ; s++ ) {
  396. for ( p=s2 ; *p ; p++ ) {
  397. if (*p == *s) return (Wchar *) s; /* silence the warning */
  398. }
  399. }
  400. return NULL;
  401. }
  402. #endif
  403. /**********************************************************************/
  404. #ifdef L_wcsrchr
  405. #define L_strrchr
  406. #define Wstrrchr wcsrchr
  407. #else
  408. #define Wstrrchr strrchr
  409. #endif
  410. #ifdef L_strrchr
  411. #ifndef L_wcsrchr
  412. weak_alias(strrchr,rindex)
  413. #endif
  414. Wchar *Wstrrchr(register const Wchar *s, Wint c)
  415. {
  416. register const Wchar *p;
  417. p = NULL;
  418. do {
  419. if (*s == (Wchar) c) {
  420. p = s;
  421. }
  422. } while (*s++);
  423. return (Wchar *) p; /* silence the warning */
  424. }
  425. #endif
  426. /**********************************************************************/
  427. #ifdef L_wcsspn
  428. #define L_strspn
  429. #define Wstrspn wcsspn
  430. #else
  431. #define Wstrspn strspn
  432. #endif
  433. #ifdef L_strspn
  434. size_t Wstrspn(const Wchar *s1, const Wchar *s2)
  435. {
  436. register const Wchar *s = s1;
  437. register const Wchar *p = s2;
  438. while (*p) {
  439. if (*p++ == *s) {
  440. ++s;
  441. p = s2;
  442. }
  443. }
  444. return s - s1;
  445. }
  446. #endif
  447. /**********************************************************************/
  448. #ifdef L_wcsstr
  449. #define L_strstr
  450. #define Wstrstr wcsstr
  451. #else
  452. #define Wstrstr strstr
  453. #endif
  454. #ifdef L_strstr
  455. /* NOTE: This is the simple-minded O(len(s1) * len(s2)) worst-case approach. */
  456. #ifdef L_wcsstr
  457. weak_alias(wcsstr,wcswcs)
  458. #endif
  459. Wchar *Wstrstr(const Wchar *s1, const Wchar *s2)
  460. {
  461. register const Wchar *s = s1;
  462. register const Wchar *p = s2;
  463. do {
  464. if (!*p) {
  465. return (Wchar *) s1;;
  466. }
  467. if (*p == *s) {
  468. ++p;
  469. ++s;
  470. } else {
  471. p = s2;
  472. if (!*s) {
  473. return NULL;
  474. }
  475. s = ++s1;
  476. }
  477. } while (1);
  478. }
  479. #endif
  480. /**********************************************************************/
  481. #ifdef L_wcstok
  482. #define L_strtok_r
  483. #define Wstrtok_r wcstok
  484. #define Wstrspn wcsspn
  485. #define Wstrpbrk wcspbrk
  486. #else
  487. #define Wstrtok_r strtok_r
  488. #define Wstrspn strspn
  489. #define Wstrpbrk strpbrk
  490. #endif
  491. #ifdef L_strtok_r
  492. Wchar *Wstrtok_r(Wchar * __restrict s1, const Wchar * __restrict s2,
  493. Wchar ** __restrict next_start)
  494. {
  495. register Wchar *s;
  496. register Wchar *p;
  497. #if 1
  498. if (((s = s1) != NULL) || ((s = *next_start) != NULL)) {
  499. if (*(s += Wstrspn(s, s2))) {
  500. if ((p = Wstrpbrk(s, s2)) != NULL) {
  501. *p++ = 0;
  502. }
  503. } else {
  504. p = s = NULL;
  505. }
  506. *next_start = p;
  507. }
  508. return s;
  509. #else
  510. if (!(s = s1)) {
  511. s = *next_start;
  512. }
  513. if (s && *(s += Wstrspn(s, s2))) {
  514. if (*(p = s + Wstrcspn(s, s2))) {
  515. *p++ = 0;
  516. }
  517. *next_start = p;
  518. return s;
  519. }
  520. return NULL; /* TODO: set *next_start = NULL for safety? */
  521. #endif
  522. }
  523. #endif
  524. /**********************************************************************/
  525. /* #ifdef L_wcstok */
  526. /* #define L_strtok */
  527. /* #define Wstrtok wcstok */
  528. /* #define Wstrtok_r wcstok_r */
  529. /* #else */
  530. /* #define Wstrtok strtok */
  531. /* #define Wstrtok_r strtok_r */
  532. /* #endif */
  533. #ifdef L_strtok
  534. #define Wstrtok strtok
  535. #define Wstrtok_r strtok_r
  536. Wchar *Wstrtok(Wchar * __restrict s1, const Wchar * __restrict s2)
  537. {
  538. static Wchar *next_start; /* Initialized to 0 since in bss. */
  539. return Wstrtok_r(s1, s2, &next_start);
  540. }
  541. #endif
  542. /**********************************************************************/
  543. #ifdef L_wmemset
  544. #define L_memset
  545. #define Wmemset wmemset
  546. #else
  547. #define Wmemset memset
  548. #endif
  549. #ifdef L_memset
  550. Wvoid *Wmemset(Wvoid *s, Wint c, size_t n)
  551. {
  552. register Wuchar *p = (Wuchar *) s;
  553. #ifdef __BCC__
  554. /* bcc can optimize the counter if it thinks it is a pointer... */
  555. register const char *np = (const char *) n;
  556. #else
  557. #define np n
  558. #endif
  559. while (np) {
  560. *p++ = (Wuchar) c;
  561. --np;
  562. }
  563. return s;
  564. }
  565. #undef np
  566. #endif
  567. /**********************************************************************/
  568. #ifdef L_strerror
  569. #error implement strerror
  570. /* char *strerror(int errnum); */
  571. #endif
  572. /**********************************************************************/
  573. #ifdef L_wcslen
  574. #define L_strlen
  575. #define Wstrlen wcslen
  576. #else
  577. #define Wstrlen strlen
  578. #endif
  579. #ifdef L_strlen
  580. size_t Wstrlen(const Wchar *s)
  581. {
  582. register const Wchar *p;
  583. for (p=s ; *p ; p++);
  584. return p - s;
  585. }
  586. #endif
  587. /**********************************************************************/
  588. /* ANSI/ISO end here */
  589. /**********************************************************************/
  590. #ifdef L_ffs
  591. int ffs(int i)
  592. {
  593. #if 1
  594. /* inlined binary search method */
  595. char n = 1;
  596. #if UINT_MAX == 0xffffU
  597. /* nothing to do here -- just trying to avoiding possible problems */
  598. #elif UINT_MAX == 0xffffffffU
  599. if (!(i & 0xffff)) {
  600. n += 16;
  601. i >>= 16;
  602. }
  603. #else
  604. #error ffs needs rewriting!
  605. #endif
  606. if (!(i & 0xff)) {
  607. n += 8;
  608. i >>= 8;
  609. }
  610. if (!(i & 0x0f)) {
  611. n += 4;
  612. i >>= 4;
  613. }
  614. if (!(i & 0x03)) {
  615. n += 2;
  616. i >>= 2;
  617. }
  618. return (i) ? (n + ((i+1) & 0x01)) : 0;
  619. #else
  620. /* linear search -- slow, but small */
  621. int n;
  622. for (n = 0 ; i ; ++n) {
  623. i >>= 1;
  624. }
  625. return n;
  626. #endif
  627. }
  628. #endif
  629. /**********************************************************************/
  630. #ifdef L_wcscasecmp
  631. #define L_strcasecmp
  632. #define Wstrcasecmp wcscasecmp
  633. #else
  634. #define Wstrcasecmp strcasecmp
  635. #endif
  636. #ifdef L_strcasecmp
  637. int Wstrcasecmp(register const Wchar *s1, register const Wchar *s2)
  638. {
  639. #ifdef WANT_WIDE
  640. while ((*s1 == *s2) || (towlower(*s1) == towlower(*s2))) {
  641. if (!*s1++) {
  642. return 0;
  643. }
  644. ++s2;
  645. }
  646. return (((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1;
  647. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  648. #else
  649. int r = 0;
  650. while ( ((s1 == s2) ||
  651. !(r = ((int)( tolower(*((Wuchar *)s1))))
  652. - tolower(*((Wuchar *)s2))))
  653. && (++s2, *s1++));
  654. return r;
  655. #endif
  656. }
  657. #endif
  658. /**********************************************************************/
  659. #ifdef L_wcsncasecmp
  660. #define L_strncasecmp
  661. #define Wstrncasecmp wcsncasecmp
  662. #else
  663. #define Wstrncasecmp strncasecmp
  664. #endif
  665. #ifdef L_strncasecmp
  666. int Wstrncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  667. {
  668. #ifdef WANT_WIDE
  669. while (n && ((*s1 == *s2) || (towlower(*s1) == towlower(*s2)))) {
  670. if (!*s1++) {
  671. return 0;
  672. }
  673. ++s2;
  674. --n;
  675. }
  676. return (n == 0)
  677. ? 0
  678. : ((((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1);
  679. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  680. #else
  681. int r = 0;
  682. while ( n
  683. && ((s1 == s2) ||
  684. !(r = ((int)( tolower(*((unsigned char *)s1))))
  685. - tolower(*((unsigned char *)s2))))
  686. && (--n, ++s2, *s1++));
  687. return r;
  688. #endif
  689. }
  690. #endif
  691. /**********************************************************************/
  692. #ifdef L_wcsnlen
  693. #define L_strnlen
  694. #define Wstrnlen wcsnlen
  695. #else
  696. #define Wstrnlen strnlen
  697. #endif
  698. #ifdef L_strnlen
  699. size_t Wstrnlen(const Wchar *s, size_t max)
  700. {
  701. register const Wchar *p = s;
  702. #ifdef __BCC__
  703. /* bcc can optimize the counter if it thinks it is a pointer... */
  704. register const char *maxp = (const char *) max;
  705. #else
  706. #define maxp max
  707. #endif
  708. while (maxp && *p) {
  709. ++p;
  710. --maxp;
  711. }
  712. return p - s;
  713. }
  714. #undef maxp
  715. #endif
  716. /**********************************************************************/
  717. /* No wide analog. */
  718. #ifdef L_memccpy
  719. void *memccpy(void * __restrict s1, const void * __restrict s2, int c, size_t n)
  720. {
  721. register char *r1 = s1;
  722. register const char *r2 = s2;
  723. while (n-- && (((unsigned char)(*r1++ = *r2++)) != ((unsigned char) c)));
  724. return (n == (size_t) -1) ? NULL : r1;
  725. }
  726. #endif
  727. /**********************************************************************/
  728. #ifdef L_wcsdup
  729. #define L_strdup
  730. #define Wstrdup wcsdup
  731. #define Wstrlen wcslen
  732. #define Wstrcpy wcscpy
  733. #else
  734. #define Wstrdup strdup
  735. #define Wstrlen strlen
  736. #define Wstrcpy strcpy
  737. #endif
  738. #ifdef L_strdup
  739. Wchar *Wstrdup(register const Wchar *s1)
  740. {
  741. register Wchar *s;
  742. if ((s = malloc((Wstrlen(s1) + 1) * sizeof(Wchar))) != NULL) {
  743. Wstrcpy(s, s1);
  744. }
  745. return s;
  746. }
  747. #endif
  748. /**********************************************************************/
  749. /* GNU extension functions. */
  750. /**********************************************************************/
  751. #ifdef L_wmempcpy
  752. #define L_mempcpy
  753. #define Wmempcpy wmempcpy
  754. #else
  755. #define Wmempcpy mempcpy
  756. #endif
  757. #ifdef L_mempcpy
  758. #ifndef L_wmempcpy
  759. /* uClibc's old string implementation did this to cater to some app. */
  760. weak_alias(mempcpy,__mempcpy)
  761. #endif
  762. Wvoid *Wmempcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  763. {
  764. register Wchar *r1 = s1;
  765. register const Wchar *r2 = s2;
  766. #ifdef __BCC__
  767. while (n--) {
  768. *r1++ = *r2++;
  769. }
  770. #else
  771. while (n) {
  772. *r1++ = *r2++;
  773. --n;
  774. }
  775. #endif
  776. return r1;
  777. }
  778. #endif
  779. /**********************************************************************/
  780. #ifdef L_memrchr
  781. void *memrchr(const void *s, int c, size_t n)
  782. {
  783. register const unsigned char *r;
  784. #ifdef __BCC__
  785. /* bcc can optimize the counter if it thinks it is a pointer... */
  786. register const char *np = (const char *) n;
  787. #else
  788. #define np n
  789. #endif
  790. r = ((unsigned char *)s) + ((size_t) np);
  791. while (np) {
  792. if (*--r == ((unsigned char)c)) {
  793. return (void *) r; /* silence the warning */
  794. }
  795. --np;
  796. }
  797. return NULL;
  798. }
  799. #undef np
  800. #endif
  801. /**********************************************************************/
  802. #ifdef L_stpcpy
  803. char *stpcpy(register char * __restrict s1, const char * __restrict s2)
  804. {
  805. #ifdef __BCC__
  806. do {
  807. *s1 = *s2++;
  808. } while (*s1++ != 0);
  809. #else
  810. while ( (*s1++ = *s2++) != 0 );
  811. #endif
  812. return s1 - 1;
  813. }
  814. #endif
  815. /**********************************************************************/
  816. #ifdef L_stpncpy
  817. char *stpncpy(register char * __restrict s1,
  818. register const char * __restrict s2,
  819. size_t n)
  820. {
  821. char *s = s1;
  822. const char *p = s2;
  823. #ifdef __BCC__
  824. while (n--) {
  825. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  826. ++s;
  827. }
  828. return s1 + (s2 - p);
  829. #else
  830. while (n) {
  831. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  832. ++s;
  833. --n;
  834. }
  835. return s1 + (s2 - p);
  836. #endif
  837. }
  838. #endif
  839. /**********************************************************************/
  840. #ifdef L_bzero
  841. void bzero(void *s, size_t n)
  842. {
  843. register unsigned char *p = s;
  844. #ifdef __BCC__
  845. /* bcc can optimize the counter if it thinks it is a pointer... */
  846. register const char *np = (const char *) n;
  847. #else
  848. #define np n
  849. #endif
  850. while (np) {
  851. *p++ = 0;
  852. --np;
  853. }
  854. }
  855. #undef np
  856. #endif
  857. /**********************************************************************/
  858. #ifdef L_bcopy
  859. void bcopy(const void *s2, void *s1, size_t n)
  860. {
  861. #if 1
  862. memmove(s1, s2, n);
  863. #else
  864. #ifdef __BCC__
  865. register char *s;
  866. register const char *p;
  867. s = s1;
  868. p = s2;
  869. if (p >= s) {
  870. while (n--) {
  871. *s++ = *p++;
  872. }
  873. } else {
  874. s += n;
  875. p += n;
  876. while (n--) {
  877. *--s = *--p;
  878. }
  879. }
  880. #else
  881. register char *s;
  882. register const char *p;
  883. s = s1;
  884. p = s2;
  885. if (p >= s) {
  886. while (n) {
  887. *s++ = *p++;
  888. --n;
  889. }
  890. } else {
  891. while (n) {
  892. --n;
  893. s[n] = p[n];
  894. }
  895. }
  896. #endif
  897. #endif
  898. }
  899. #endif
  900. /**********************************************************************/
  901. #ifdef L_strcasestr
  902. char *strcasestr(const char *s1, const char *s2)
  903. {
  904. register const char *s = s1;
  905. register const char *p = s2;
  906. #if 1
  907. do {
  908. if (!*p) {
  909. return (char *) s1;;
  910. }
  911. if ((*p == *s)
  912. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  913. ) {
  914. ++p;
  915. ++s;
  916. } else {
  917. p = s2;
  918. if (!*s) {
  919. return NULL;
  920. }
  921. s = ++s1;
  922. }
  923. } while (1);
  924. #else
  925. while (*p && *s) {
  926. if ((*p == *s)
  927. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  928. ) {
  929. ++p;
  930. ++s;
  931. } else {
  932. p = s2;
  933. s = ++s1;
  934. }
  935. }
  936. return (*p) ? NULL : (char *) s1;
  937. #endif
  938. }
  939. #endif
  940. /**********************************************************************/
  941. #ifdef L_strndup
  942. char *strndup(register const char *s1, size_t n)
  943. {
  944. register char *s;
  945. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  946. if ((s = malloc(n + 1)) != NULL) {
  947. memcpy(s, s1, n);
  948. s[n] = 0;
  949. }
  950. return s;
  951. }
  952. #endif
  953. /**********************************************************************/
  954. #ifdef L_strsep
  955. char *strsep(char ** __restrict s1, const char * __restrict s2)
  956. {
  957. register char *s = *s1;
  958. register char *p;
  959. #if 1
  960. p = NULL;
  961. if (s && *s && (p = strpbrk(s, s2))) {
  962. *p++ = 0;
  963. }
  964. #else
  965. if (s && *s && *(p = s + strcspn(s, s2))) {
  966. *p++ = 0;
  967. } else {
  968. p = NULL;
  969. }
  970. #endif
  971. *s1 = p;
  972. return s;
  973. }
  974. #endif
  975. /**********************************************************************/
  976. #ifdef L_wcschrnul
  977. #define L_strchrnul
  978. #define Wstrchrnul wcschrnul
  979. #else
  980. #define Wstrchrnul strchrnul
  981. #endif
  982. #ifdef L_strchrnul
  983. Wchar *Wstrchrnul(register const Wchar *s, Wint c)
  984. {
  985. --s;
  986. while (*++s && (*s != ((Wchar)c)));
  987. return (Wchar *) s;
  988. }
  989. #endif
  990. /**********************************************************************/
  991. #ifdef L_rawmemchr
  992. void *rawmemchr(const void *s, int c)
  993. {
  994. register const unsigned char *r = s;
  995. while (*r != ((unsigned char)c)) ++r;
  996. return (void *) r; /* silence the warning */
  997. }
  998. #endif
  999. /**********************************************************************/
  1000. #ifdef L_basename
  1001. char *basename(const char *path)
  1002. {
  1003. register const char *s;
  1004. register const char *p;
  1005. p = s = path;
  1006. while (*s) {
  1007. if (*s++ == '/') {
  1008. p = s;
  1009. }
  1010. }
  1011. return (char *) p;
  1012. }
  1013. #endif
  1014. /**********************************************************************/
  1015. #ifdef L___xpg_basename
  1016. char *__xpg_basename(register char *path)
  1017. {
  1018. static const char null_or_empty[] = ".";
  1019. char *first;
  1020. register char *last;
  1021. first = (char *) null_or_empty;
  1022. if (path && *path) {
  1023. first = path;
  1024. last = path - 1;
  1025. do {
  1026. if ((*path != '/') && (path > ++last)) {
  1027. last = first = path;
  1028. }
  1029. } while (*++path);
  1030. if (*first == '/') {
  1031. last = first;
  1032. }
  1033. last[1] = 0;
  1034. }
  1035. return first;
  1036. }
  1037. #endif
  1038. /**********************************************************************/
  1039. #ifdef L_dirname
  1040. char *dirname(char *path)
  1041. {
  1042. static const char null_or_empty_or_noslash[] = ".";
  1043. register char *s;
  1044. register char *last;
  1045. char *first;
  1046. last = s = path;
  1047. if (s != NULL) {
  1048. LOOP:
  1049. while (*s && (*s != '/')) ++s;
  1050. first = s;
  1051. while (*s == '/') ++s;
  1052. if (*s) {
  1053. last = first;
  1054. goto LOOP;
  1055. }
  1056. if (last == path) {
  1057. if (*last != '/') {
  1058. goto DOT;
  1059. }
  1060. if ((*++last == '/') && (last[1] == 0)) {
  1061. ++last;
  1062. }
  1063. }
  1064. *last = 0;
  1065. return path;
  1066. }
  1067. DOT:
  1068. return (char *) null_or_empty_or_noslash;
  1069. }
  1070. #endif
  1071. /**********************************************************************/