wstring.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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_wcpcpy
  803. #define L_stpcpy
  804. #define Wstpcpy wcpcpy
  805. #else
  806. #define Wstpcpy stpcpy
  807. #endif
  808. #ifdef L_stpcpy
  809. Wchar *Wstpcpy(register Wchar * __restrict s1, const Wchar * __restrict s2)
  810. {
  811. #ifdef __BCC__
  812. do {
  813. *s1 = *s2++;
  814. } while (*s1++ != 0);
  815. #else
  816. while ( (*s1++ = *s2++) != 0 );
  817. #endif
  818. return s1 - 1;
  819. }
  820. #endif
  821. /**********************************************************************/
  822. #ifdef L_wcpncpy
  823. #define L_stpncpy
  824. #define Wstpncpy wcpncpy
  825. #else
  826. #define Wstpncpy stpncpy
  827. #endif
  828. #ifdef L_stpncpy
  829. Wchar *Wstpncpy(register Wchar * __restrict s1,
  830. register const Wchar * __restrict s2,
  831. size_t n)
  832. {
  833. Wchar *s = s1;
  834. const Wchar *p = s2;
  835. #ifdef __BCC__
  836. while (n--) {
  837. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  838. ++s;
  839. }
  840. return s1 + (s2 - p);
  841. #else
  842. while (n) {
  843. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  844. ++s;
  845. --n;
  846. }
  847. return s1 + (s2 - p);
  848. #endif
  849. }
  850. #endif
  851. /**********************************************************************/
  852. #ifdef L_bzero
  853. void bzero(void *s, size_t n)
  854. {
  855. register unsigned char *p = s;
  856. #ifdef __BCC__
  857. /* bcc can optimize the counter if it thinks it is a pointer... */
  858. register const char *np = (const char *) n;
  859. #else
  860. #define np n
  861. #endif
  862. while (np) {
  863. *p++ = 0;
  864. --np;
  865. }
  866. }
  867. #undef np
  868. #endif
  869. /**********************************************************************/
  870. #ifdef L_bcopy
  871. void bcopy(const void *s2, void *s1, size_t n)
  872. {
  873. #if 1
  874. memmove(s1, s2, n);
  875. #else
  876. #ifdef __BCC__
  877. register char *s;
  878. register const char *p;
  879. s = s1;
  880. p = s2;
  881. if (p >= s) {
  882. while (n--) {
  883. *s++ = *p++;
  884. }
  885. } else {
  886. s += n;
  887. p += n;
  888. while (n--) {
  889. *--s = *--p;
  890. }
  891. }
  892. #else
  893. register char *s;
  894. register const char *p;
  895. s = s1;
  896. p = s2;
  897. if (p >= s) {
  898. while (n) {
  899. *s++ = *p++;
  900. --n;
  901. }
  902. } else {
  903. while (n) {
  904. --n;
  905. s[n] = p[n];
  906. }
  907. }
  908. #endif
  909. #endif
  910. }
  911. #endif
  912. /**********************************************************************/
  913. #ifdef L_strcasestr
  914. char *strcasestr(const char *s1, const char *s2)
  915. {
  916. register const char *s = s1;
  917. register const char *p = s2;
  918. #if 1
  919. do {
  920. if (!*p) {
  921. return (char *) s1;;
  922. }
  923. if ((*p == *s)
  924. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  925. ) {
  926. ++p;
  927. ++s;
  928. } else {
  929. p = s2;
  930. if (!*s) {
  931. return NULL;
  932. }
  933. s = ++s1;
  934. }
  935. } while (1);
  936. #else
  937. while (*p && *s) {
  938. if ((*p == *s)
  939. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  940. ) {
  941. ++p;
  942. ++s;
  943. } else {
  944. p = s2;
  945. s = ++s1;
  946. }
  947. }
  948. return (*p) ? NULL : (char *) s1;
  949. #endif
  950. }
  951. #endif
  952. /**********************************************************************/
  953. #ifdef L_strndup
  954. char *strndup(register const char *s1, size_t n)
  955. {
  956. register char *s;
  957. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  958. if ((s = malloc(n + 1)) != NULL) {
  959. memcpy(s, s1, n);
  960. s[n] = 0;
  961. }
  962. return s;
  963. }
  964. #endif
  965. /**********************************************************************/
  966. #ifdef L_strsep
  967. char *strsep(char ** __restrict s1, const char * __restrict s2)
  968. {
  969. register char *s = *s1;
  970. register char *p;
  971. #if 1
  972. p = NULL;
  973. if (s && *s && (p = strpbrk(s, s2))) {
  974. *p++ = 0;
  975. }
  976. #else
  977. if (s && *s && *(p = s + strcspn(s, s2))) {
  978. *p++ = 0;
  979. } else {
  980. p = NULL;
  981. }
  982. #endif
  983. *s1 = p;
  984. return s;
  985. }
  986. #endif
  987. /**********************************************************************/
  988. #ifdef L_wcschrnul
  989. #define L_strchrnul
  990. #define Wstrchrnul wcschrnul
  991. #else
  992. #define Wstrchrnul strchrnul
  993. #endif
  994. #ifdef L_strchrnul
  995. Wchar *Wstrchrnul(register const Wchar *s, Wint c)
  996. {
  997. --s;
  998. while (*++s && (*s != ((Wchar)c)));
  999. return (Wchar *) s;
  1000. }
  1001. #endif
  1002. /**********************************************************************/
  1003. #ifdef L_rawmemchr
  1004. void *rawmemchr(const void *s, int c)
  1005. {
  1006. register const unsigned char *r = s;
  1007. while (*r != ((unsigned char)c)) ++r;
  1008. return (void *) r; /* silence the warning */
  1009. }
  1010. #endif
  1011. /**********************************************************************/
  1012. #ifdef L_basename
  1013. char *basename(const char *path)
  1014. {
  1015. register const char *s;
  1016. register const char *p;
  1017. p = s = path;
  1018. while (*s) {
  1019. if (*s++ == '/') {
  1020. p = s;
  1021. }
  1022. }
  1023. return (char *) p;
  1024. }
  1025. #endif
  1026. /**********************************************************************/
  1027. #ifdef L___xpg_basename
  1028. char *__xpg_basename(register char *path)
  1029. {
  1030. static const char null_or_empty[] = ".";
  1031. char *first;
  1032. register char *last;
  1033. first = (char *) null_or_empty;
  1034. if (path && *path) {
  1035. first = path;
  1036. last = path - 1;
  1037. do {
  1038. if ((*path != '/') && (path > ++last)) {
  1039. last = first = path;
  1040. }
  1041. } while (*++path);
  1042. if (*first == '/') {
  1043. last = first;
  1044. }
  1045. last[1] = 0;
  1046. }
  1047. return first;
  1048. }
  1049. #endif
  1050. /**********************************************************************/
  1051. #ifdef L_dirname
  1052. char *dirname(char *path)
  1053. {
  1054. static const char null_or_empty_or_noslash[] = ".";
  1055. register char *s;
  1056. register char *last;
  1057. char *first;
  1058. last = s = path;
  1059. if (s != NULL) {
  1060. LOOP:
  1061. while (*s && (*s != '/')) ++s;
  1062. first = s;
  1063. while (*s == '/') ++s;
  1064. if (*s) {
  1065. last = first;
  1066. goto LOOP;
  1067. }
  1068. if (last == path) {
  1069. if (*last != '/') {
  1070. goto DOT;
  1071. }
  1072. if ((*++last == '/') && (last[1] == 0)) {
  1073. ++last;
  1074. }
  1075. }
  1076. *last = 0;
  1077. return path;
  1078. }
  1079. DOT:
  1080. return (char *) null_or_empty_or_noslash;
  1081. }
  1082. #endif
  1083. /**********************************************************************/
  1084. #ifdef L_strlcat
  1085. /* OpenBSD function:
  1086. * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst.
  1087. * Returns strlen(src) + strlen({original} dst), so truncation occurred if the
  1088. * return val is >= n.
  1089. * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is
  1090. * taken as n. */
  1091. size_t strlcat(register char *__restrict dst,
  1092. register const char *__restrict src,
  1093. size_t n)
  1094. {
  1095. size_t len;
  1096. char dummy[1];
  1097. len = 0;
  1098. while (1) {
  1099. if (len >= n) {
  1100. dst = dummy;
  1101. break;
  1102. }
  1103. if (!*dst) {
  1104. break;
  1105. }
  1106. ++dst;
  1107. ++len;
  1108. }
  1109. while ((*dst = *src) != 0) {
  1110. if (++len < n) {
  1111. ++dst;
  1112. }
  1113. ++src;
  1114. }
  1115. return len;
  1116. }
  1117. #endif
  1118. /**********************************************************************/
  1119. #ifdef L_strlcpy
  1120. /* OpenBSD function:
  1121. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  1122. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  1123. size_t strlcpy(register char *__restrict dst,
  1124. register const char *__restrict src,
  1125. size_t n)
  1126. {
  1127. const char *src0 = src;
  1128. char dummy[1];
  1129. if (!n) {
  1130. dst = dummy;
  1131. } else {
  1132. --n;
  1133. }
  1134. while ((*dst = *src) != 0) {
  1135. if (n) {
  1136. --n;
  1137. ++dst;
  1138. }
  1139. ++src;
  1140. }
  1141. return src - src0;
  1142. }
  1143. #endif
  1144. /**********************************************************************/