wstring.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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. /* extern unsigned char *_ctype_collate; */
  270. /* int strcoll(register const char *s1, const char *s2) */
  271. /* { */
  272. /* int r; */
  273. /* while (!(r = (_ctype_collate[(int)(*s1++)]-_ctype_collate[(int)(*s2++)]))); */
  274. /* return r; */
  275. /* } */
  276. #endif
  277. /**********************************************************************/
  278. #ifdef L_wcsncmp
  279. #define L_strncmp
  280. #define Wstrncmp wcsncmp
  281. #else
  282. #define Wstrncmp strncmp
  283. #endif
  284. #ifdef L_strncmp
  285. int Wstrncmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  286. {
  287. #ifdef WANT_WIDE
  288. while (n && (*((Wuchar *)s1) == *((Wuchar *)s2))) {
  289. if (!*s1++) {
  290. return 0;
  291. }
  292. ++s2;
  293. --n;
  294. }
  295. return (n == 0) ? 0 : ((*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1);
  296. #else
  297. int r = 0;
  298. while (n--
  299. && ((r = ((int)(*((unsigned char *)s1))) - *((unsigned char *)s2++))
  300. == 0)
  301. && *s1++);
  302. return r;
  303. #endif
  304. }
  305. #endif
  306. /**********************************************************************/
  307. #ifdef L_strxfrm
  308. #error implement strxfrm
  309. /* size_t strxfrm(char * __restrict s1, const char * __restrict s2, size_t n) */
  310. #endif
  311. /**********************************************************************/
  312. #ifdef L_wmemchr
  313. #define L_memchr
  314. #define Wmemchr wmemchr
  315. #else
  316. #define Wmemchr memchr
  317. #endif
  318. #ifdef L_memchr
  319. Wvoid *Wmemchr(const Wvoid *s, Wint c, size_t n)
  320. {
  321. register const Wuchar *r = (const Wuchar *) s;
  322. #ifdef __BCC__
  323. /* bcc can optimize the counter if it thinks it is a pointer... */
  324. register const char *np = (const char *) n;
  325. #else
  326. #define np n
  327. #endif
  328. while (np) {
  329. if (*r == ((Wuchar)c)) {
  330. return (Wvoid *) r; /* silence the warning */
  331. }
  332. ++r;
  333. --np;
  334. }
  335. return NULL;
  336. }
  337. #undef np
  338. #endif
  339. /**********************************************************************/
  340. #ifdef L_wcschr
  341. #define L_strchr
  342. #define Wstrchr wcschr
  343. #else
  344. #define Wstrchr strchr
  345. #endif
  346. #ifdef L_strchr
  347. #ifndef L_wcschr
  348. weak_alias(strchr,index)
  349. #endif
  350. Wchar *Wstrchr(register const Wchar *s, Wint c)
  351. {
  352. do {
  353. if (*s == ((Wchar)c)) {
  354. return (Wchar *) s; /* silence the warning */
  355. }
  356. } while (*s++);
  357. return NULL;
  358. }
  359. #endif
  360. /**********************************************************************/
  361. #ifdef L_wcscspn
  362. #define L_strcspn
  363. #define Wstrcspn wcscspn
  364. #else
  365. #define Wstrcspn strcspn
  366. #endif
  367. #ifdef L_strcspn
  368. size_t Wstrcspn(const Wchar *s1, const Wchar *s2)
  369. {
  370. register const Wchar *s;
  371. register const Wchar *p;
  372. for ( s=s1 ; *s ; s++ ) {
  373. for ( p=s2 ; *p ; p++ ) {
  374. if (*p == *s) goto done;
  375. }
  376. }
  377. done:
  378. return s - s1;
  379. }
  380. #endif
  381. /**********************************************************************/
  382. #ifdef L_wcspbrk
  383. #define L_strpbrk
  384. #define Wstrpbrk wcspbrk
  385. #else
  386. #define Wstrpbrk strpbrk
  387. #endif
  388. #ifdef L_strpbrk
  389. Wchar *Wstrpbrk(const Wchar *s1, const Wchar *s2)
  390. {
  391. register const Wchar *s;
  392. register const Wchar *p;
  393. for ( s=s1 ; *s ; s++ ) {
  394. for ( p=s2 ; *p ; p++ ) {
  395. if (*p == *s) return (Wchar *) s; /* silence the warning */
  396. }
  397. }
  398. return NULL;
  399. }
  400. #endif
  401. /**********************************************************************/
  402. #ifdef L_wcsrchr
  403. #define L_strrchr
  404. #define Wstrrchr wcsrchr
  405. #else
  406. #define Wstrrchr strrchr
  407. #endif
  408. #ifdef L_strrchr
  409. #ifndef L_wcsrchr
  410. weak_alias(strrchr,rindex)
  411. #endif
  412. Wchar *Wstrrchr(register const Wchar *s, Wint c)
  413. {
  414. register const Wchar *p;
  415. p = NULL;
  416. do {
  417. if (*s == (Wchar) c) {
  418. p = s;
  419. }
  420. } while (*s++);
  421. return (Wchar *) p; /* silence the warning */
  422. }
  423. #endif
  424. /**********************************************************************/
  425. #ifdef L_wcsspn
  426. #define L_strspn
  427. #define Wstrspn wcsspn
  428. #else
  429. #define Wstrspn strspn
  430. #endif
  431. #ifdef L_strspn
  432. size_t Wstrspn(const Wchar *s1, const Wchar *s2)
  433. {
  434. register const Wchar *s = s1;
  435. register const Wchar *p = s2;
  436. while (*p) {
  437. if (*p++ == *s) {
  438. ++s;
  439. p = s2;
  440. }
  441. }
  442. return s - s1;
  443. }
  444. #endif
  445. /**********************************************************************/
  446. #ifdef L_wcsstr
  447. #define L_strstr
  448. #define Wstrstr wcsstr
  449. #else
  450. #define Wstrstr strstr
  451. #endif
  452. #ifdef L_strstr
  453. /* NOTE: This is the simple-minded O(len(s1) * len(s2)) worst-case approach. */
  454. #ifdef L_wcsstr
  455. weak_alias(wcsstr,wcswcs)
  456. #endif
  457. Wchar *Wstrstr(const Wchar *s1, const Wchar *s2)
  458. {
  459. register const Wchar *s = s1;
  460. register const Wchar *p = s2;
  461. do {
  462. if (!*p) {
  463. return (Wchar *) s1;;
  464. }
  465. if (*p == *s) {
  466. ++p;
  467. ++s;
  468. } else {
  469. p = s2;
  470. if (!*s) {
  471. return NULL;
  472. }
  473. s = ++s1;
  474. }
  475. } while (1);
  476. }
  477. #endif
  478. /**********************************************************************/
  479. #ifdef L_wcstok
  480. #define L_strtok_r
  481. #define Wstrtok_r wcstok
  482. #define Wstrspn wcsspn
  483. #define Wstrpbrk wcspbrk
  484. #else
  485. #define Wstrtok_r strtok_r
  486. #define Wstrspn strspn
  487. #define Wstrpbrk strpbrk
  488. #endif
  489. #ifdef L_strtok_r
  490. Wchar *Wstrtok_r(Wchar * __restrict s1, const Wchar * __restrict s2,
  491. Wchar ** __restrict next_start)
  492. {
  493. register Wchar *s;
  494. register Wchar *p;
  495. #if 1
  496. if (((s = s1) != NULL) || ((s = *next_start) != NULL)) {
  497. if (*(s += Wstrspn(s, s2))) {
  498. if ((p = Wstrpbrk(s, s2)) != NULL) {
  499. *p++ = 0;
  500. }
  501. } else {
  502. p = s = NULL;
  503. }
  504. *next_start = p;
  505. }
  506. return s;
  507. #else
  508. if (!(s = s1)) {
  509. s = *next_start;
  510. }
  511. if (s && *(s += Wstrspn(s, s2))) {
  512. if (*(p = s + Wstrcspn(s, s2))) {
  513. *p++ = 0;
  514. }
  515. *next_start = p;
  516. return s;
  517. }
  518. return NULL; /* TODO: set *next_start = NULL for safety? */
  519. #endif
  520. }
  521. #endif
  522. /**********************************************************************/
  523. /* #ifdef L_wcstok */
  524. /* #define L_strtok */
  525. /* #define Wstrtok wcstok */
  526. /* #define Wstrtok_r wcstok_r */
  527. /* #else */
  528. /* #define Wstrtok strtok */
  529. /* #define Wstrtok_r strtok_r */
  530. /* #endif */
  531. #ifdef L_strtok
  532. #define Wstrtok strtok
  533. #define Wstrtok_r strtok_r
  534. Wchar *Wstrtok(Wchar * __restrict s1, const Wchar * __restrict s2)
  535. {
  536. static Wchar *next_start; /* Initialized to 0 since in bss. */
  537. return Wstrtok_r(s1, s2, &next_start);
  538. }
  539. #endif
  540. /**********************************************************************/
  541. #ifdef L_wmemset
  542. #define L_memset
  543. #define Wmemset wmemset
  544. #else
  545. #define Wmemset memset
  546. #endif
  547. #ifdef L_memset
  548. Wvoid *Wmemset(Wvoid *s, Wint c, size_t n)
  549. {
  550. register Wuchar *p = (Wuchar *) s;
  551. #ifdef __BCC__
  552. /* bcc can optimize the counter if it thinks it is a pointer... */
  553. register const char *np = (const char *) n;
  554. #else
  555. #define np n
  556. #endif
  557. while (np) {
  558. *p++ = (Wuchar) c;
  559. --np;
  560. }
  561. return s;
  562. }
  563. #undef np
  564. #endif
  565. /**********************************************************************/
  566. #ifdef L_strerror
  567. #error implement strerror
  568. /* char *strerror(int errnum); */
  569. #endif
  570. /**********************************************************************/
  571. #ifdef L_wcslen
  572. #define L_strlen
  573. #define Wstrlen wcslen
  574. #else
  575. #define Wstrlen strlen
  576. #endif
  577. #ifdef L_strlen
  578. size_t Wstrlen(const Wchar *s)
  579. {
  580. register const Wchar *p;
  581. for (p=s ; *p ; p++);
  582. return p - s;
  583. }
  584. #endif
  585. /**********************************************************************/
  586. /* ANSI/ISO end here */
  587. /**********************************************************************/
  588. #ifdef L_ffs
  589. int ffs(int i)
  590. {
  591. #if 1
  592. /* inlined binary search method */
  593. char n = 1;
  594. #if UINT_MAX == 0xffffU
  595. /* nothing to do here -- just trying to avoiding possible problems */
  596. #elif UINT_MAX == 0xffffffffU
  597. if (!(i & 0xffff)) {
  598. n += 16;
  599. i >>= 16;
  600. }
  601. #else
  602. #error ffs needs rewriting!
  603. #endif
  604. if (!(i & 0xff)) {
  605. n += 8;
  606. i >>= 8;
  607. }
  608. if (!(i & 0x0f)) {
  609. n += 4;
  610. i >>= 4;
  611. }
  612. if (!(i & 0x03)) {
  613. n += 2;
  614. i >>= 2;
  615. }
  616. return (i) ? (n + ((i+1) & 0x01)) : 0;
  617. #else
  618. /* linear search -- slow, but small */
  619. int n;
  620. for (n = 0 ; i ; ++n) {
  621. i >>= 1;
  622. }
  623. return n;
  624. #endif
  625. }
  626. #endif
  627. /**********************************************************************/
  628. #ifdef L_wcscasecmp
  629. #define L_strcasecmp
  630. #define Wstrcasecmp wcscasecmp
  631. #else
  632. #define Wstrcasecmp strcasecmp
  633. #endif
  634. #ifdef L_strcasecmp
  635. int Wstrcasecmp(register const Wchar *s1, register const Wchar *s2)
  636. {
  637. #ifdef WANT_WIDE
  638. while ((*s1 == *s2) || (towlower(*s1) == towlower(*s2))) {
  639. if (!*s1++) {
  640. return 0;
  641. }
  642. ++s2;
  643. }
  644. return (((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1;
  645. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  646. #else
  647. int r = 0;
  648. while ( ((s1 == s2) ||
  649. !(r = ((int)( tolower(*((Wuchar *)s1))))
  650. - tolower(*((Wuchar *)s2))))
  651. && (++s2, *s1++));
  652. return r;
  653. #endif
  654. }
  655. #endif
  656. /**********************************************************************/
  657. #ifdef L_wcsncasecmp
  658. #define L_strncasecmp
  659. #define Wstrncasecmp wcsncasecmp
  660. #else
  661. #define Wstrncasecmp strncasecmp
  662. #endif
  663. #ifdef L_strncasecmp
  664. int Wstrncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  665. {
  666. #ifdef WANT_WIDE
  667. while (n && ((*s1 == *s2) || (towlower(*s1) == towlower(*s2)))) {
  668. if (!*s1++) {
  669. return 0;
  670. }
  671. ++s2;
  672. --n;
  673. }
  674. return (n == 0)
  675. ? 0
  676. : ((((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1);
  677. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  678. #else
  679. int r = 0;
  680. while ( n
  681. && ((s1 == s2) ||
  682. !(r = ((int)( tolower(*((unsigned char *)s1))))
  683. - tolower(*((unsigned char *)s2))))
  684. && (--n, ++s2, *s1++));
  685. return r;
  686. #endif
  687. }
  688. #endif
  689. /**********************************************************************/
  690. #ifdef L_wcsnlen
  691. #define L_strnlen
  692. #define Wstrnlen wcsnlen
  693. #else
  694. #define Wstrnlen strnlen
  695. #endif
  696. #ifdef L_strnlen
  697. size_t Wstrnlen(const Wchar *s, size_t max)
  698. {
  699. register const Wchar *p = s;
  700. #ifdef __BCC__
  701. /* bcc can optimize the counter if it thinks it is a pointer... */
  702. register const char *maxp = (const char *) max;
  703. #else
  704. #define maxp max
  705. #endif
  706. while (maxp && *p) {
  707. ++p;
  708. --maxp;
  709. }
  710. return p - s;
  711. }
  712. #undef maxp
  713. #endif
  714. /**********************************************************************/
  715. /* No wide analog. */
  716. #ifdef L_memccpy
  717. void *memccpy(void * __restrict s1, const void * __restrict s2, int c, size_t n)
  718. {
  719. register char *r1 = s1;
  720. register const char *r2 = s2;
  721. while (n-- && (((unsigned char)(*r1++ = *r2++)) != ((unsigned char) c)));
  722. return (n == (size_t) -1) ? NULL : r1;
  723. }
  724. #endif
  725. /**********************************************************************/
  726. #ifdef L_wcsdup
  727. #define L_strdup
  728. #define Wstrdup wcsdup
  729. #define Wstrlen wcslen
  730. #define Wstrcpy wcscpy
  731. #else
  732. #define Wstrdup strdup
  733. #define Wstrlen strlen
  734. #define Wstrcpy strcpy
  735. #endif
  736. #ifdef L_strdup
  737. Wchar *Wstrdup(register const Wchar *s1)
  738. {
  739. register Wchar *s;
  740. if ((s = malloc((Wstrlen(s1) + 1) * sizeof(Wchar))) != NULL) {
  741. Wstrcpy(s, s1);
  742. }
  743. return s;
  744. }
  745. #endif
  746. /**********************************************************************/
  747. /* GNU extension functions. */
  748. /**********************************************************************/
  749. #ifdef L_wmempcpy
  750. #define L_mempcpy
  751. #define Wmempcpy wmempcpy
  752. #else
  753. #define Wmempcpy mempcpy
  754. #endif
  755. #ifdef L_mempcpy
  756. #ifndef L_wmempcpy
  757. /* uClibc's old string implementation did this to cater to some app. */
  758. weak_alias(mempcpy,__mempcpy)
  759. #endif
  760. Wvoid *Wmempcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  761. {
  762. register Wchar *r1 = s1;
  763. register const Wchar *r2 = s2;
  764. #ifdef __BCC__
  765. while (n--) {
  766. *r1++ = *r2++;
  767. }
  768. #else
  769. while (n) {
  770. *r1++ = *r2++;
  771. --n;
  772. }
  773. #endif
  774. return r1;
  775. }
  776. #endif
  777. /**********************************************************************/
  778. #ifdef L_memrchr
  779. void *memrchr(const void *s, int c, size_t n)
  780. {
  781. register const unsigned char *r;
  782. #ifdef __BCC__
  783. /* bcc can optimize the counter if it thinks it is a pointer... */
  784. register const char *np = (const char *) n;
  785. #else
  786. #define np n
  787. #endif
  788. r = ((unsigned char *)s) + ((size_t) np);
  789. while (np) {
  790. if (*--r == ((unsigned char)c)) {
  791. return (void *) r; /* silence the warning */
  792. }
  793. --np;
  794. }
  795. return NULL;
  796. }
  797. #undef np
  798. #endif
  799. /**********************************************************************/
  800. #ifdef L_stpcpy
  801. char *stpcpy(register char * __restrict s1, const char * __restrict s2)
  802. {
  803. #ifdef __BCC__
  804. do {
  805. *s1 = *s2++;
  806. } while (*s1++ != 0);
  807. #else
  808. while ( (*s1++ = *s2++) != 0 );
  809. #endif
  810. return s1 - 1;
  811. }
  812. #endif
  813. /**********************************************************************/
  814. #ifdef L_stpncpy
  815. char *stpncpy(register char * __restrict s1,
  816. register const char * __restrict s2,
  817. size_t n)
  818. {
  819. char *s = s1;
  820. const char *p = s2;
  821. #ifdef __BCC__
  822. while (n--) {
  823. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  824. ++s;
  825. }
  826. return s1 + (s2 - p);
  827. #else
  828. while (n) {
  829. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  830. ++s;
  831. --n;
  832. }
  833. return s1 + (s2 - p);
  834. #endif
  835. }
  836. #endif
  837. /**********************************************************************/
  838. #ifdef L_bzero
  839. void bzero(void *s, size_t n)
  840. {
  841. register unsigned char *p = s;
  842. #ifdef __BCC__
  843. /* bcc can optimize the counter if it thinks it is a pointer... */
  844. register const char *np = (const char *) n;
  845. #else
  846. #define np n
  847. #endif
  848. while (np) {
  849. *p++ = 0;
  850. --np;
  851. }
  852. }
  853. #undef np
  854. #endif
  855. /**********************************************************************/
  856. #ifdef L_bcopy
  857. void bcopy(const void *s2, void *s1, size_t n)
  858. {
  859. #if 1
  860. memmove(s1, s2, n);
  861. #else
  862. #ifdef __BCC__
  863. register char *s;
  864. register const char *p;
  865. s = s1;
  866. p = s2;
  867. if (p >= s) {
  868. while (n--) {
  869. *s++ = *p++;
  870. }
  871. } else {
  872. s += n;
  873. p += n;
  874. while (n--) {
  875. *--s = *--p;
  876. }
  877. }
  878. #else
  879. register char *s;
  880. register const char *p;
  881. s = s1;
  882. p = s2;
  883. if (p >= s) {
  884. while (n) {
  885. *s++ = *p++;
  886. --n;
  887. }
  888. } else {
  889. while (n) {
  890. --n;
  891. s[n] = p[n];
  892. }
  893. }
  894. #endif
  895. #endif
  896. }
  897. #endif
  898. /**********************************************************************/
  899. #ifdef L_strcasestr
  900. char *strcasestr(const char *s1, const char *s2)
  901. {
  902. register const char *s = s1;
  903. register const char *p = s2;
  904. #if 1
  905. do {
  906. if (!*p) {
  907. return (char *) s1;;
  908. }
  909. if ((*p == *s)
  910. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  911. ) {
  912. ++p;
  913. ++s;
  914. } else {
  915. p = s2;
  916. if (!*s) {
  917. return NULL;
  918. }
  919. s = ++s1;
  920. }
  921. } while (1);
  922. #else
  923. while (*p && *s) {
  924. if ((*p == *s)
  925. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  926. ) {
  927. ++p;
  928. ++s;
  929. } else {
  930. p = s2;
  931. s = ++s1;
  932. }
  933. }
  934. return (*p) ? NULL : (char *) s1;
  935. #endif
  936. }
  937. #endif
  938. /**********************************************************************/
  939. #ifdef L_strndup
  940. char *strndup(register const char *s1, size_t n)
  941. {
  942. register char *s;
  943. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  944. if ((s = malloc(n + 1)) != NULL) {
  945. memcpy(s, s1, n);
  946. s[n] = 0;
  947. }
  948. return s;
  949. }
  950. #endif
  951. /**********************************************************************/
  952. #ifdef L_strsep
  953. char *strsep(char ** __restrict s1, const char * __restrict s2)
  954. {
  955. register char *s = *s1;
  956. register char *p;
  957. #if 1
  958. p = NULL;
  959. if (s && *s && (p = strpbrk(s, s2))) {
  960. *p++ = 0;
  961. }
  962. #else
  963. if (s && *s && *(p = s + strcspn(s, s2))) {
  964. *p++ = 0;
  965. } else {
  966. p = NULL;
  967. }
  968. #endif
  969. *s1 = p;
  970. return s;
  971. }
  972. #endif
  973. /**********************************************************************/
  974. #ifdef L_wcschrnul
  975. #define L_strchrnul
  976. #define Wstrchrnul wcschrnul
  977. #else
  978. #define Wstrchrnul strchrnul
  979. #endif
  980. #ifdef L_strchrnul
  981. Wchar *Wstrchrnul(register const Wchar *s, Wint c)
  982. {
  983. --s;
  984. while (*++s && (*s != ((Wchar)c)));
  985. return (Wchar *) s;
  986. }
  987. #endif
  988. /**********************************************************************/
  989. #ifdef L_rawmemchr
  990. void *rawmemchr(const void *s, int c)
  991. {
  992. register const unsigned char *r = s;
  993. while (*r != ((unsigned char)c)) ++r;
  994. return (void *) r; /* silence the warning */
  995. }
  996. #endif
  997. /**********************************************************************/
  998. #ifdef L_basename
  999. char *basename(const char *path)
  1000. {
  1001. register const char *s;
  1002. register const char *p;
  1003. p = s = path;
  1004. while (*s) {
  1005. if (*s++ == '/') {
  1006. p = s;
  1007. }
  1008. }
  1009. return (char *) p;
  1010. }
  1011. #endif
  1012. /**********************************************************************/
  1013. #ifdef L___xpg_basename
  1014. char *__xpg_basename(register char *path)
  1015. {
  1016. static const char null_or_empty[] = ".";
  1017. char *first;
  1018. register char *last;
  1019. first = (char *) null_or_empty;
  1020. if (path && *path) {
  1021. first = path;
  1022. last = path - 1;
  1023. do {
  1024. if ((*path != '/') && (path > ++last)) {
  1025. last = first = path;
  1026. }
  1027. } while (*++path);
  1028. if (*first == '/') {
  1029. last = first;
  1030. }
  1031. last[1] = 0;
  1032. }
  1033. return first;
  1034. }
  1035. #endif
  1036. /**********************************************************************/
  1037. #ifdef L_dirname
  1038. char *dirname(char *path)
  1039. {
  1040. static const char null_or_empty_or_noslash[] = ".";
  1041. register char *s;
  1042. register char *last;
  1043. char *first;
  1044. last = s = path;
  1045. if (s != NULL) {
  1046. LOOP:
  1047. while (*s && (*s != '/')) ++s;
  1048. first = s;
  1049. while (*s == '/') ++s;
  1050. if (*s) {
  1051. last = first;
  1052. goto LOOP;
  1053. }
  1054. if (last == path) {
  1055. if (*last != '/') {
  1056. goto DOT;
  1057. }
  1058. if ((*++last == '/') && (last[1] == 0)) {
  1059. ++last;
  1060. }
  1061. }
  1062. *last = 0;
  1063. return path;
  1064. }
  1065. DOT:
  1066. return (char *) null_or_empty_or_noslash;
  1067. }
  1068. #endif
  1069. /**********************************************************************/