wstring.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936
  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. /* Dec 20, 2002
  28. *
  29. * Initial test implementation of strcoll, strxfrm, wcscoll, and wcsxfrm.
  30. * The code needs to be cleaned up a good bit, but I'd like to see people
  31. * test it out.
  32. */
  33. #define _STDIO_UTILITY
  34. #define _GNU_SOURCE
  35. #include <string.h>
  36. #include <strings.h>
  37. #include <stdio.h>
  38. #include <limits.h>
  39. #include <ctype.h>
  40. #include <stdlib.h>
  41. #include <errno.h>
  42. #include <signal.h>
  43. #include <assert.h>
  44. #include <locale.h>
  45. #ifdef WANT_WIDE
  46. #include <wchar.h>
  47. #include <wctype.h>
  48. #include <bits/uClibc_uwchar.h>
  49. #define Wvoid wchar_t
  50. #define Wchar wchar_t
  51. #define Wuchar __uwchar_t
  52. #define Wint wchar_t
  53. #else
  54. #define Wvoid void
  55. #define Wchar char
  56. typedef unsigned char __string_uchar_t;
  57. #define Wuchar __string_uchar_t
  58. #define Wint int
  59. #endif
  60. /**********************************************************************/
  61. /* NOTE: If we ever do internationalized syserr messages, this will
  62. * have to be changed! */
  63. #define _SYS_NERR 125
  64. #if defined(__mips__) || defined(__sparc__)
  65. /* sparce and mips have an extra error entry, as EDEADLK and EDEADLOCK have
  66. * different meanings on those platforms. */
  67. #undef _SYS_NERR
  68. #define _SYS_NERR 126
  69. #endif
  70. #ifdef __UCLIBC_HAS_ERRNO_MESSAGES__
  71. #define _SYS_ERRMSG_MAXLEN 50
  72. #else /* __UCLIBC_HAS_ERRNO_MESSAGES__ */
  73. #define _SYS_ERRMSG_MAXLEN 0
  74. #endif /* __UCLIBC_HAS_ERRNO_MESSAGES__ */
  75. extern const char _string_syserrmsgs[];
  76. #define _SYS_NSIG 32
  77. #ifdef __UCLIBC_HAS_SIGNUM_MESSAGES__
  78. #define _SYS_SIGMSG_MAXLEN 25
  79. #else /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  80. #define _SYS_SIGMSG_MAXLEN 0
  81. #endif /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  82. extern const char _string_syssigmsgs[];
  83. #if _SYS_ERRMSG_MAXLEN < __UIM_BUFLEN_INT + 14
  84. #define _STRERROR_BUFSIZE (__UIM_BUFLEN_INT + 14)
  85. #else
  86. #define _STRERROR_BUFSIZE _SYS_ERRMSG_MAXLEN
  87. #endif
  88. #if _SYS_SIGMSG_MAXLEN < __UIM_BUFLEN_INT + 15
  89. #define _STRSIGNAL_BUFSIZE (__UIM_BUFLEN_INT + 15)
  90. #else
  91. #define _STRSIGNAL_BUFSIZE _SYS_SIGMSG_MAXLEN
  92. #endif
  93. /**********************************************************************/
  94. #if defined(L__string_syserrmsgs) && defined(__UCLIBC_HAS_ERRNO_MESSAGES__)
  95. const char _string_syserrmsgs[] = {
  96. /* 0: 0, 8 */ "Success\0"
  97. /* 1: 8, 24 */ "Operation not permitted\0"
  98. /* 2: 32, 26 */ "No such file or directory\0"
  99. /* 3: 58, 16 */ "No such process\0"
  100. /* 4: 74, 24 */ "Interrupted system call\0"
  101. /* 5: 98, 19 */ "Input/output error\0"
  102. /* 6: 117, 26 */ "No such device or address\0"
  103. /* 7: 143, 23 */ "Argument list too long\0"
  104. /* 8: 166, 18 */ "Exec format error\0"
  105. /* 9: 184, 20 */ "Bad file descriptor\0"
  106. /* 10: 204, 19 */ "No child processes\0"
  107. /* 11: 223, 33 */ "Resource temporarily unavailable\0"
  108. /* 12: 256, 23 */ "Cannot allocate memory\0"
  109. /* 13: 279, 18 */ "Permission denied\0"
  110. /* 14: 297, 12 */ "Bad address\0"
  111. /* 15: 309, 22 */ "Block device required\0"
  112. /* 16: 331, 24 */ "Device or resource busy\0"
  113. /* 17: 355, 12 */ "File exists\0"
  114. /* 18: 367, 26 */ "Invalid cross-device link\0"
  115. /* 19: 393, 15 */ "No such device\0"
  116. /* 20: 408, 16 */ "Not a directory\0"
  117. /* 21: 424, 15 */ "Is a directory\0"
  118. /* 22: 439, 17 */ "Invalid argument\0"
  119. /* 23: 456, 30 */ "Too many open files in system\0"
  120. /* 24: 486, 20 */ "Too many open files\0"
  121. /* 25: 506, 31 */ "Inappropriate ioctl for device\0"
  122. /* 26: 537, 15 */ "Text file busy\0"
  123. /* 27: 552, 15 */ "File too large\0"
  124. /* 28: 567, 24 */ "No space left on device\0"
  125. /* 29: 591, 13 */ "Illegal seek\0"
  126. /* 30: 604, 22 */ "Read-only file system\0"
  127. /* 31: 626, 15 */ "Too many links\0"
  128. /* 32: 641, 12 */ "Broken pipe\0"
  129. /* 33: 653, 33 */ "Numerical argument out of domain\0"
  130. /* 34: 686, 30 */ "Numerical result out of range\0"
  131. /* 35: 716, 26 */ "Resource deadlock avoided\0"
  132. /* 36: 742, 19 */ "File name too long\0"
  133. /* 37: 761, 19 */ "No locks available\0"
  134. /* 38: 780, 25 */ "Function not implemented\0"
  135. /* 39: 805, 20 */ "Directory not empty\0"
  136. /* 40: 825, 34 */ "Too many levels of symbolic links\0"
  137. /* 41: 859, 1 */ "\0"
  138. /* 42: 860, 27 */ "No message of desired type\0"
  139. /* 43: 887, 19 */ "Identifier removed\0"
  140. /* 44: 906, 28 */ "Channel number out of range\0"
  141. /* 45: 934, 25 */ "Level 2 not synchronized\0"
  142. /* 46: 959, 15 */ "Level 3 halted\0"
  143. /* 47: 974, 14 */ "Level 3 reset\0"
  144. /* 48: 988, 25 */ "Link number out of range\0"
  145. /* 49: 1013, 29 */ "Protocol driver not attached\0"
  146. /* 50: 1042, 27 */ "No CSI structure available\0"
  147. /* 51: 1069, 15 */ "Level 2 halted\0"
  148. /* 52: 1084, 17 */ "Invalid exchange\0"
  149. /* 53: 1101, 27 */ "Invalid request descriptor\0"
  150. /* 54: 1128, 14 */ "Exchange full\0"
  151. /* 55: 1142, 9 */ "No anode\0"
  152. /* 56: 1151, 21 */ "Invalid request code\0"
  153. /* 57: 1172, 13 */ "Invalid slot\0"
  154. /* 58: 1185, 1 */ "\0"
  155. /* 59: 1186, 21 */ "Bad font file format\0"
  156. /* 60: 1207, 20 */ "Device not a stream\0"
  157. /* 61: 1227, 18 */ "No data available\0"
  158. /* 62: 1245, 14 */ "Timer expired\0"
  159. /* 63: 1259, 25 */ "Out of streams resources\0"
  160. /* 64: 1284, 30 */ "Machine is not on the network\0"
  161. /* 65: 1314, 22 */ "Package not installed\0"
  162. /* 66: 1336, 17 */ "Object is remote\0"
  163. /* 67: 1353, 22 */ "Link has been severed\0"
  164. /* 68: 1375, 16 */ "Advertise error\0"
  165. /* 69: 1391, 14 */ "Srmount error\0"
  166. /* 70: 1405, 28 */ "Communication error on send\0"
  167. /* 71: 1433, 15 */ "Protocol error\0"
  168. /* 72: 1448, 19 */ "Multihop attempted\0"
  169. /* 73: 1467, 19 */ "RFS specific error\0"
  170. /* 74: 1486, 12 */ "Bad message\0"
  171. /* 75: 1498, 38 */ "Value too large for defined data type\0"
  172. /* 76: 1536, 27 */ "Name not unique on network\0"
  173. /* 77: 1563, 29 */ "File descriptor in bad state\0"
  174. /* 78: 1592, 23 */ "Remote address changed\0"
  175. /* 79: 1615, 39 */ "Can not access a needed shared library\0"
  176. /* 80: 1654, 37 */ "Accessing a corrupted shared library\0"
  177. /* 81: 1691, 32 */ ".lib section in a.out corrupted\0"
  178. /* 82: 1723, 48 */ "Attempting to link in too many shared libraries\0"
  179. /* 83: 1771, 38 */ "Cannot exec a shared library directly\0"
  180. /* 84: 1809, 50 */ "Invalid or incomplete multibyte or wide character\0"
  181. /* 85: 1859, 44 */ "Interrupted system call should be restarted\0"
  182. /* 86: 1903, 19 */ "Streams pipe error\0"
  183. /* 87: 1922, 15 */ "Too many users\0"
  184. /* 88: 1937, 31 */ "Socket operation on non-socket\0"
  185. /* 89: 1968, 29 */ "Destination address required\0"
  186. /* 90: 1997, 17 */ "Message too long\0"
  187. /* 91: 2014, 31 */ "Protocol wrong type for socket\0"
  188. /* 92: 2045, 23 */ "Protocol not available\0"
  189. /* 93: 2068, 23 */ "Protocol not supported\0"
  190. /* 94: 2091, 26 */ "Socket type not supported\0"
  191. /* 95: 2117, 24 */ "Operation not supported\0"
  192. /* 96: 2141, 30 */ "Protocol family not supported\0"
  193. /* 97: 2171, 41 */ "Address family not supported by protocol\0"
  194. /* 98: 2212, 23 */ "Address already in use\0"
  195. /* 99: 2235, 32 */ "Cannot assign requested address\0"
  196. /* 100: 2267, 16 */ "Network is down\0"
  197. /* 101: 2283, 23 */ "Network is unreachable\0"
  198. /* 102: 2306, 36 */ "Network dropped connection on reset\0"
  199. /* 103: 2342, 33 */ "Software caused connection abort\0"
  200. /* 104: 2375, 25 */ "Connection reset by peer\0"
  201. /* 105: 2400, 26 */ "No buffer space available\0"
  202. /* 106: 2426, 40 */ "Transport endpoint is already connected\0"
  203. /* 107: 2466, 36 */ "Transport endpoint is not connected\0"
  204. /* 108: 2502, 46 */ "Cannot send after transport endpoint shutdown\0"
  205. /* 109: 2548, 35 */ "Too many references: cannot splice\0"
  206. /* 110: 2583, 21 */ "Connection timed out\0"
  207. /* 111: 2604, 19 */ "Connection refused\0"
  208. /* 112: 2623, 13 */ "Host is down\0"
  209. /* 113: 2636, 17 */ "No route to host\0"
  210. /* 114: 2653, 30 */ "Operation already in progress\0"
  211. /* 115: 2683, 26 */ "Operation now in progress\0"
  212. /* 116: 2709, 22 */ "Stale NFS file handle\0"
  213. /* 117: 2731, 25 */ "Structure needs cleaning\0"
  214. /* 118: 2756, 28 */ "Not a XENIX named type file\0"
  215. /* 119: 2784, 30 */ "No XENIX semaphores available\0"
  216. /* 120: 2814, 21 */ "Is a named type file\0"
  217. /* 121: 2835, 17 */ "Remote I/O error\0"
  218. /* 122: 2852, 20 */ "Disk quota exceeded\0"
  219. /* 123: 2872, 16 */ "No medium found\0"
  220. /* 124: 2888, 18 */ "Wrong medium type"
  221. #if defined(__mips__) || defined(__sparc__)
  222. "\0"
  223. /* 125: 2906, 28 */ "File locking deadlock error"
  224. #endif
  225. /* Note: for mips we are ignoring ECANCELED since glibc doesn't have a
  226. * corresponsding message.*/
  227. };
  228. #endif
  229. /**********************************************************************/
  230. #if defined(L_sys_errlist) && defined(__UCLIBC_HAS_SYS_ERRLIST__)
  231. link_warning(_sys_errlist, "sys_nerr and sys_errlist are obsolete and uClibc support for them (in at least some configurations) will probably be unavailable in the near future.")
  232. const char *const sys_errlist[] = {
  233. [0] = _string_syserrmsgs + 0,
  234. [EPERM] = _string_syserrmsgs + 8,
  235. [ENOENT] = _string_syserrmsgs + 32,
  236. [ESRCH] = _string_syserrmsgs + 58,
  237. [EINTR] = _string_syserrmsgs + 74,
  238. [EIO] = _string_syserrmsgs + 98,
  239. [ENXIO] = _string_syserrmsgs + 117,
  240. [E2BIG] = _string_syserrmsgs + 143,
  241. [ENOEXEC] = _string_syserrmsgs + 166,
  242. [EBADF] = _string_syserrmsgs + 184,
  243. [ECHILD] = _string_syserrmsgs + 204,
  244. [EAGAIN] = _string_syserrmsgs + 223,
  245. [ENOMEM] = _string_syserrmsgs + 256,
  246. [EACCES] = _string_syserrmsgs + 279,
  247. [EFAULT] = _string_syserrmsgs + 297,
  248. [ENOTBLK] = _string_syserrmsgs + 309,
  249. [EBUSY] = _string_syserrmsgs + 331,
  250. [EEXIST] = _string_syserrmsgs + 355,
  251. [EXDEV] = _string_syserrmsgs + 367,
  252. [ENODEV] = _string_syserrmsgs + 393,
  253. [ENOTDIR] = _string_syserrmsgs + 408,
  254. [EISDIR] = _string_syserrmsgs + 424,
  255. [EINVAL] = _string_syserrmsgs + 439,
  256. [ENFILE] = _string_syserrmsgs + 456,
  257. [EMFILE] = _string_syserrmsgs + 486,
  258. [ENOTTY] = _string_syserrmsgs + 506,
  259. [ETXTBSY] = _string_syserrmsgs + 537,
  260. [EFBIG] = _string_syserrmsgs + 552,
  261. [ENOSPC] = _string_syserrmsgs + 567,
  262. [ESPIPE] = _string_syserrmsgs + 591,
  263. [EROFS] = _string_syserrmsgs + 604,
  264. [EMLINK] = _string_syserrmsgs + 626,
  265. [EPIPE] = _string_syserrmsgs + 641,
  266. [EDOM] = _string_syserrmsgs + 653,
  267. [ERANGE] = _string_syserrmsgs + 686,
  268. [EDEADLK] = _string_syserrmsgs + 716,
  269. [ENAMETOOLONG] = _string_syserrmsgs + 742,
  270. [ENOLCK] = _string_syserrmsgs + 761,
  271. [ENOSYS] = _string_syserrmsgs + 780,
  272. [ENOTEMPTY] = _string_syserrmsgs + 805,
  273. [ELOOP] = _string_syserrmsgs + 825,
  274. /* _string_syserrmsgs + 859, */
  275. [ENOMSG] = _string_syserrmsgs + 860,
  276. [EIDRM] = _string_syserrmsgs + 887,
  277. [ECHRNG] = _string_syserrmsgs + 906,
  278. [EL2NSYNC] = _string_syserrmsgs + 934,
  279. [EL3HLT] = _string_syserrmsgs + 959,
  280. [EL3RST] = _string_syserrmsgs + 974,
  281. [ELNRNG] = _string_syserrmsgs + 988,
  282. [EUNATCH] = _string_syserrmsgs + 1013,
  283. [ENOCSI] = _string_syserrmsgs + 1042,
  284. [EL2HLT] = _string_syserrmsgs + 1069,
  285. [EBADE] = _string_syserrmsgs + 1084,
  286. [EBADR] = _string_syserrmsgs + 1101,
  287. [EXFULL] = _string_syserrmsgs + 1128,
  288. [ENOANO] = _string_syserrmsgs + 1142,
  289. [EBADRQC] = _string_syserrmsgs + 1151,
  290. [EBADSLT] = _string_syserrmsgs + 1172,
  291. /* _string_syserrmsgs + 1185, */
  292. [EBFONT] = _string_syserrmsgs + 1186,
  293. [ENOSTR] = _string_syserrmsgs + 1207,
  294. [ENODATA] = _string_syserrmsgs + 1227,
  295. [ETIME] = _string_syserrmsgs + 1245,
  296. [ENOSR] = _string_syserrmsgs + 1259,
  297. [ENONET] = _string_syserrmsgs + 1284,
  298. [ENOPKG] = _string_syserrmsgs + 1314,
  299. [EREMOTE] = _string_syserrmsgs + 1336,
  300. [ENOLINK] = _string_syserrmsgs + 1353,
  301. [EADV] = _string_syserrmsgs + 1375,
  302. [ESRMNT] = _string_syserrmsgs + 1391,
  303. [ECOMM] = _string_syserrmsgs + 1405,
  304. [EPROTO] = _string_syserrmsgs + 1433,
  305. [EMULTIHOP] = _string_syserrmsgs + 1448,
  306. [EDOTDOT] = _string_syserrmsgs + 1467,
  307. [EBADMSG] = _string_syserrmsgs + 1486,
  308. [EOVERFLOW] = _string_syserrmsgs + 1498,
  309. [ENOTUNIQ] = _string_syserrmsgs + 1536,
  310. [EBADFD] = _string_syserrmsgs + 1563,
  311. [EREMCHG] = _string_syserrmsgs + 1592,
  312. [ELIBACC] = _string_syserrmsgs + 1615,
  313. [ELIBBAD] = _string_syserrmsgs + 1654,
  314. [ELIBSCN] = _string_syserrmsgs + 1691,
  315. [ELIBMAX] = _string_syserrmsgs + 1723,
  316. [ELIBEXEC] = _string_syserrmsgs + 1771,
  317. [EILSEQ] = _string_syserrmsgs + 1809,
  318. [ERESTART] = _string_syserrmsgs + 1859,
  319. [ESTRPIPE] = _string_syserrmsgs + 1903,
  320. [EUSERS] = _string_syserrmsgs + 1922,
  321. [ENOTSOCK] = _string_syserrmsgs + 1937,
  322. [EDESTADDRREQ] = _string_syserrmsgs + 1968,
  323. [EMSGSIZE] = _string_syserrmsgs + 1997,
  324. [EPROTOTYPE] = _string_syserrmsgs + 2014,
  325. [ENOPROTOOPT] = _string_syserrmsgs + 2045,
  326. [EPROTONOSUPPORT] = _string_syserrmsgs + 2068,
  327. [ESOCKTNOSUPPORT] = _string_syserrmsgs + 2091,
  328. [EOPNOTSUPP] = _string_syserrmsgs + 2117,
  329. [EPFNOSUPPORT] = _string_syserrmsgs + 2141,
  330. [EAFNOSUPPORT] = _string_syserrmsgs + 2171,
  331. [EADDRINUSE] = _string_syserrmsgs + 2212,
  332. [EADDRNOTAVAIL] = _string_syserrmsgs + 2235,
  333. [ENETDOWN] = _string_syserrmsgs + 2267,
  334. [ENETUNREACH] = _string_syserrmsgs + 2283,
  335. [ENETRESET] = _string_syserrmsgs + 2306,
  336. [ECONNABORTED] = _string_syserrmsgs + 2342,
  337. [ECONNRESET] = _string_syserrmsgs + 2375,
  338. [ENOBUFS] = _string_syserrmsgs + 2400,
  339. [EISCONN] = _string_syserrmsgs + 2426,
  340. [ENOTCONN] = _string_syserrmsgs + 2466,
  341. [ESHUTDOWN] = _string_syserrmsgs + 2502,
  342. [ETOOMANYREFS] = _string_syserrmsgs + 2548,
  343. [ETIMEDOUT] = _string_syserrmsgs + 2583,
  344. [ECONNREFUSED] = _string_syserrmsgs + 2604,
  345. [EHOSTDOWN] = _string_syserrmsgs + 2623,
  346. [EHOSTUNREACH] = _string_syserrmsgs + 2636,
  347. [EALREADY] = _string_syserrmsgs + 2653,
  348. [EINPROGRESS] = _string_syserrmsgs + 2683,
  349. [ESTALE] = _string_syserrmsgs + 2709,
  350. [EUCLEAN] = _string_syserrmsgs + 2731,
  351. [ENOTNAM] = _string_syserrmsgs + 2756,
  352. [ENAVAIL] = _string_syserrmsgs + 2784,
  353. [EISNAM] = _string_syserrmsgs + 2814,
  354. [EREMOTEIO] = _string_syserrmsgs + 2835,
  355. [EDQUOT] = _string_syserrmsgs + 2852,
  356. [ENOMEDIUM] = _string_syserrmsgs + 2872,
  357. [EMEDIUMTYPE] = _string_syserrmsgs + 2888,
  358. #if EDEADLOCK != EDEADLK
  359. [EDEADLOCK] = _string_syserrmsgs + 2906,
  360. #endif
  361. #if EWOULDBLOCK != EAGAIN
  362. #error EWOULDBLOCK does not equal EAGAIN
  363. #endif
  364. /* For now, ignore the other arch-specific errors. glibc only maps EPROCLIM. */
  365. /* some other mips errors */
  366. #ifdef ECANCELED
  367. #endif
  368. #ifdef EINIT
  369. #endif
  370. #ifdef EREMDEV
  371. #endif
  372. /* some other sparc errors */
  373. #ifdef EPROCLIM
  374. #endif
  375. #ifdef ERREMOTE
  376. #endif
  377. };
  378. int sys_nerr = sizeof(sys_errlist)/sizeof(sys_errlist[0]);
  379. #endif
  380. /**********************************************************************/
  381. #ifdef L_wmemcpy
  382. #define L_memcpy
  383. #define Wmemcpy wmemcpy
  384. #else
  385. #define Wmemcpy memcpy
  386. #endif
  387. #ifdef L_memcpy
  388. Wvoid *Wmemcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  389. {
  390. register Wchar *r1 = s1;
  391. register const Wchar *r2 = s2;
  392. #ifdef __BCC__
  393. while (n--) {
  394. *r1++ = *r2++;
  395. }
  396. #else
  397. while (n) {
  398. *r1++ = *r2++;
  399. --n;
  400. }
  401. #endif
  402. return s1;
  403. }
  404. #endif
  405. /**********************************************************************/
  406. #ifdef L_wmemmove
  407. #define L_memmove
  408. #define Wmemmove wmemmove
  409. #else
  410. #define Wmemmove memmove
  411. #endif
  412. #ifdef L_memmove
  413. Wvoid *Wmemmove(Wvoid *s1, const Wvoid *s2, size_t n)
  414. {
  415. #ifdef __BCC__
  416. register Wchar *s = (Wchar *) s1;
  417. register const Wchar *p = (const Wchar *) s2;
  418. if (p >= s) {
  419. while (n--) {
  420. *s++ = *p++;
  421. }
  422. } else {
  423. s += n;
  424. p += n;
  425. while (n--) {
  426. *--s = *--p;
  427. }
  428. }
  429. return s1;
  430. #else
  431. register Wchar *s = (Wchar *) s1;
  432. register const Wchar *p = (const Wchar *) s2;
  433. if (p >= s) {
  434. while (n) {
  435. *s++ = *p++;
  436. --n;
  437. }
  438. } else {
  439. while (n) {
  440. --n;
  441. s[n] = p[n];
  442. }
  443. }
  444. return s1;
  445. #endif
  446. }
  447. #endif
  448. /**********************************************************************/
  449. #ifdef L_wcscpy
  450. #define L_strcpy
  451. #define Wstrcpy wcscpy
  452. #else
  453. #define Wstrcpy strcpy
  454. #endif
  455. #ifdef L_strcpy
  456. Wchar *Wstrcpy(Wchar * __restrict s1, const Wchar * __restrict s2)
  457. {
  458. register Wchar *s = s1;
  459. #ifdef __BCC__
  460. do {
  461. *s = *s2++;
  462. } while (*s++ != 0);
  463. #else
  464. while ( (*s++ = *s2++) != 0 );
  465. #endif
  466. return s1;
  467. }
  468. #endif
  469. /**********************************************************************/
  470. #ifdef L_wcsncpy
  471. #define L_strncpy
  472. #define Wstrncpy wcsncpy
  473. #else
  474. #define Wstrncpy strncpy
  475. #endif
  476. #ifdef L_strncpy
  477. Wchar *Wstrncpy(Wchar * __restrict s1, register const Wchar * __restrict s2,
  478. size_t n)
  479. {
  480. register Wchar *s = s1;
  481. #ifdef __BCC__
  482. while (n--) {
  483. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  484. ++s;
  485. }
  486. #else
  487. while (n) {
  488. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  489. ++s;
  490. --n;
  491. }
  492. #endif
  493. return s1;
  494. }
  495. #endif
  496. /**********************************************************************/
  497. #ifdef L_wcscat
  498. #define L_strcat
  499. #define Wstrcat wcscat
  500. #else
  501. #define Wstrcat strcat
  502. #endif
  503. #ifdef L_strcat
  504. Wchar *Wstrcat(Wchar * __restrict s1, register const Wchar * __restrict s2)
  505. {
  506. register Wchar *s = s1;
  507. while (*s++);
  508. --s;
  509. while ((*s++ = *s2++) != 0);
  510. return s1;
  511. }
  512. #endif
  513. /**********************************************************************/
  514. #ifdef L_wcsncat
  515. #define L_strncat
  516. #define Wstrncat wcsncat
  517. #else
  518. #define Wstrncat strncat
  519. #endif
  520. #ifdef L_strncat
  521. Wchar *Wstrncat(Wchar * __restrict s1, register const Wchar * __restrict s2,
  522. size_t n)
  523. {
  524. register Wchar *s = s1;
  525. while (*s++);
  526. --s;
  527. #if __BCC__
  528. while (n-- && ((*s = *s2++) != 0)) ++s;
  529. #else
  530. while (n && ((*s = *s2++) != 0)) {
  531. --n;
  532. ++s;
  533. }
  534. #endif
  535. *s = 0;
  536. return s1;
  537. }
  538. #endif
  539. /**********************************************************************/
  540. #ifdef L_wmemcmp
  541. #define L_memcmp
  542. #define Wmemcmp wmemcmp
  543. #else
  544. #define Wmemcmp memcmp
  545. #endif
  546. #ifdef L_memcmp
  547. #ifndef L_wmemcmp
  548. weak_alias(memcmp,bcmp);
  549. #endif
  550. int Wmemcmp(const Wvoid *s1, const Wvoid *s2, size_t n)
  551. {
  552. register const Wuchar *r1 = (const Wuchar *) s1;
  553. register const Wuchar *r2 = (const Wuchar *) s2;
  554. #ifdef WANT_WIDE
  555. while (n && (*r1 == *r2)) {
  556. ++r1;
  557. ++r2;
  558. --n;
  559. }
  560. return (n == 0) ? 0 : ((*r1 < *r2) ? -1 : 1);
  561. #else
  562. int r = 0;
  563. while (n-- && ((r = ((int)(*r1++)) - *r2++) == 0));
  564. return r;
  565. #endif
  566. }
  567. #endif
  568. /**********************************************************************/
  569. #ifdef L_wcscmp
  570. #define L_strcmp
  571. #define Wstrcmp wcscmp
  572. #else
  573. #define Wstrcmp strcmp
  574. #endif
  575. #ifdef L_strcmp
  576. #ifdef __LOCALE_C_ONLY
  577. #ifdef L_wcscmp
  578. weak_alias(wcscmp,wcscoll);
  579. #else /* L_wcscmp */
  580. weak_alias(strcmp,strcoll);
  581. #endif /* L_wcscmp */
  582. #endif /* __LOCALE_C_ONLY */
  583. int Wstrcmp(register const Wchar *s1, register const Wchar *s2)
  584. {
  585. #ifdef WANT_WIDE
  586. while (*((Wuchar *)s1) == *((Wuchar *)s2)) {
  587. if (!*s1++) {
  588. return 0;
  589. }
  590. ++s2;
  591. }
  592. return (*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1;
  593. #else
  594. int r;
  595. while (((r = ((int)(*((Wuchar *)s1))) - *((Wuchar *)s2++))
  596. == 0) && *s1++);
  597. return r;
  598. #endif
  599. }
  600. #endif
  601. /**********************************************************************/
  602. #ifdef L_wcsncmp
  603. #define L_strncmp
  604. #define Wstrncmp wcsncmp
  605. #else
  606. #define Wstrncmp strncmp
  607. #endif
  608. #ifdef L_strncmp
  609. int Wstrncmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  610. {
  611. #ifdef WANT_WIDE
  612. while (n && (*((Wuchar *)s1) == *((Wuchar *)s2))) {
  613. if (!*s1++) {
  614. return 0;
  615. }
  616. ++s2;
  617. --n;
  618. }
  619. return (n == 0) ? 0 : ((*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1);
  620. #else
  621. int r = 0;
  622. while (n--
  623. && ((r = ((int)(*((unsigned char *)s1))) - *((unsigned char *)s2++))
  624. == 0)
  625. && *s1++);
  626. return r;
  627. #endif
  628. }
  629. #endif
  630. /**********************************************************************/
  631. #ifdef L_wmemchr
  632. #define L_memchr
  633. #define Wmemchr wmemchr
  634. #else
  635. #define Wmemchr memchr
  636. #endif
  637. #ifdef L_memchr
  638. Wvoid *Wmemchr(const Wvoid *s, Wint c, size_t n)
  639. {
  640. register const Wuchar *r = (const Wuchar *) s;
  641. #ifdef __BCC__
  642. /* bcc can optimize the counter if it thinks it is a pointer... */
  643. register const char *np = (const char *) n;
  644. #else
  645. #define np n
  646. #endif
  647. while (np) {
  648. if (*r == ((Wuchar)c)) {
  649. return (Wvoid *) r; /* silence the warning */
  650. }
  651. ++r;
  652. --np;
  653. }
  654. return NULL;
  655. }
  656. #undef np
  657. #endif
  658. /**********************************************************************/
  659. #ifdef L_wcschr
  660. #define L_strchr
  661. #define Wstrchr wcschr
  662. #else
  663. #define Wstrchr strchr
  664. #endif
  665. #ifdef L_strchr
  666. #ifndef L_wcschr
  667. weak_alias(strchr,index);
  668. #endif
  669. Wchar *Wstrchr(register const Wchar *s, Wint c)
  670. {
  671. do {
  672. if (*s == ((Wchar)c)) {
  673. return (Wchar *) s; /* silence the warning */
  674. }
  675. } while (*s++);
  676. return NULL;
  677. }
  678. #endif
  679. /**********************************************************************/
  680. #ifdef L_wcscspn
  681. #define L_strcspn
  682. #define Wstrcspn wcscspn
  683. #else
  684. #define Wstrcspn strcspn
  685. #endif
  686. #ifdef L_strcspn
  687. size_t Wstrcspn(const Wchar *s1, const Wchar *s2)
  688. {
  689. register const Wchar *s;
  690. register const Wchar *p;
  691. for ( s=s1 ; *s ; s++ ) {
  692. for ( p=s2 ; *p ; p++ ) {
  693. if (*p == *s) goto done;
  694. }
  695. }
  696. done:
  697. return s - s1;
  698. }
  699. #endif
  700. /**********************************************************************/
  701. #ifdef L_wcspbrk
  702. #define L_strpbrk
  703. #define Wstrpbrk wcspbrk
  704. #else
  705. #define Wstrpbrk strpbrk
  706. #endif
  707. #ifdef L_strpbrk
  708. Wchar *Wstrpbrk(const Wchar *s1, const Wchar *s2)
  709. {
  710. register const Wchar *s;
  711. register const Wchar *p;
  712. for ( s=s1 ; *s ; s++ ) {
  713. for ( p=s2 ; *p ; p++ ) {
  714. if (*p == *s) return (Wchar *) s; /* silence the warning */
  715. }
  716. }
  717. return NULL;
  718. }
  719. #endif
  720. /**********************************************************************/
  721. #ifdef L_wcsrchr
  722. #define L_strrchr
  723. #define Wstrrchr wcsrchr
  724. #else
  725. #define Wstrrchr strrchr
  726. #endif
  727. #ifdef L_strrchr
  728. #ifndef L_wcsrchr
  729. weak_alias(strrchr,rindex);
  730. #endif
  731. Wchar *Wstrrchr(register const Wchar *s, Wint c)
  732. {
  733. register const Wchar *p;
  734. p = NULL;
  735. do {
  736. if (*s == (Wchar) c) {
  737. p = s;
  738. }
  739. } while (*s++);
  740. return (Wchar *) p; /* silence the warning */
  741. }
  742. #endif
  743. /**********************************************************************/
  744. #ifdef L_wcsspn
  745. #define L_strspn
  746. #define Wstrspn wcsspn
  747. #else
  748. #define Wstrspn strspn
  749. #endif
  750. #ifdef L_strspn
  751. size_t Wstrspn(const Wchar *s1, const Wchar *s2)
  752. {
  753. register const Wchar *s = s1;
  754. register const Wchar *p = s2;
  755. while (*p) {
  756. if (*p++ == *s) {
  757. ++s;
  758. p = s2;
  759. }
  760. }
  761. return s - s1;
  762. }
  763. #endif
  764. /**********************************************************************/
  765. #ifdef L_wcsstr
  766. #define L_strstr
  767. #define Wstrstr wcsstr
  768. #else
  769. #define Wstrstr strstr
  770. #endif
  771. #ifdef L_strstr
  772. /* NOTE: This is the simple-minded O(len(s1) * len(s2)) worst-case approach. */
  773. #ifdef L_wcsstr
  774. weak_alias(wcsstr,wcswcs);
  775. #endif
  776. Wchar *Wstrstr(const Wchar *s1, const Wchar *s2)
  777. {
  778. register const Wchar *s = s1;
  779. register const Wchar *p = s2;
  780. do {
  781. if (!*p) {
  782. return (Wchar *) s1;;
  783. }
  784. if (*p == *s) {
  785. ++p;
  786. ++s;
  787. } else {
  788. p = s2;
  789. if (!*s) {
  790. return NULL;
  791. }
  792. s = ++s1;
  793. }
  794. } while (1);
  795. }
  796. #endif
  797. /**********************************************************************/
  798. #undef Wstrspn
  799. #undef Wstrpbrk
  800. #ifdef L_wcstok
  801. #define L_strtok_r
  802. #define Wstrtok_r wcstok
  803. #define Wstrspn wcsspn
  804. #define Wstrpbrk wcspbrk
  805. #else
  806. #define Wstrtok_r strtok_r
  807. #define Wstrspn strspn
  808. #define Wstrpbrk strpbrk
  809. #endif
  810. #ifdef L_strtok_r
  811. Wchar *Wstrtok_r(Wchar * __restrict s1, const Wchar * __restrict s2,
  812. Wchar ** __restrict next_start)
  813. {
  814. register Wchar *s;
  815. register Wchar *p;
  816. #if 1
  817. if (((s = s1) != NULL) || ((s = *next_start) != NULL)) {
  818. if (*(s += Wstrspn(s, s2))) {
  819. if ((p = Wstrpbrk(s, s2)) != NULL) {
  820. *p++ = 0;
  821. }
  822. } else {
  823. p = s = NULL;
  824. }
  825. *next_start = p;
  826. }
  827. return s;
  828. #else
  829. if (!(s = s1)) {
  830. s = *next_start;
  831. }
  832. if (s && *(s += Wstrspn(s, s2))) {
  833. if (*(p = s + Wstrcspn(s, s2))) {
  834. *p++ = 0;
  835. }
  836. *next_start = p;
  837. return s;
  838. }
  839. return NULL; /* TODO: set *next_start = NULL for safety? */
  840. #endif
  841. }
  842. #endif
  843. /**********************************************************************/
  844. /* #ifdef L_wcstok */
  845. /* #define L_strtok */
  846. /* #define Wstrtok wcstok */
  847. /* #define Wstrtok_r wcstok_r */
  848. /* #else */
  849. /* #define Wstrtok strtok */
  850. /* #define Wstrtok_r strtok_r */
  851. /* #endif */
  852. #ifdef L_strtok
  853. #define Wstrtok strtok
  854. #define Wstrtok_r strtok_r
  855. Wchar *Wstrtok(Wchar * __restrict s1, const Wchar * __restrict s2)
  856. {
  857. static Wchar *next_start; /* Initialized to 0 since in bss. */
  858. return Wstrtok_r(s1, s2, &next_start);
  859. }
  860. #endif
  861. /**********************************************************************/
  862. #ifdef L_wmemset
  863. #define L_memset
  864. #define Wmemset wmemset
  865. #else
  866. #define Wmemset memset
  867. #endif
  868. #ifdef L_memset
  869. Wvoid *Wmemset(Wvoid *s, Wint c, size_t n)
  870. {
  871. register Wuchar *p = (Wuchar *) s;
  872. #ifdef __BCC__
  873. /* bcc can optimize the counter if it thinks it is a pointer... */
  874. register const char *np = (const char *) n;
  875. #else
  876. #define np n
  877. #endif
  878. while (np) {
  879. *p++ = (Wuchar) c;
  880. --np;
  881. }
  882. return s;
  883. }
  884. #undef np
  885. #endif
  886. /**********************************************************************/
  887. #ifdef L_wcslen
  888. #define L_strlen
  889. #define Wstrlen wcslen
  890. #else
  891. #define Wstrlen strlen
  892. #endif
  893. #ifdef L_strlen
  894. size_t Wstrlen(const Wchar *s)
  895. {
  896. register const Wchar *p;
  897. for (p=s ; *p ; p++);
  898. return p - s;
  899. }
  900. #endif
  901. /**********************************************************************/
  902. /* ANSI/ISO end here */
  903. /**********************************************************************/
  904. #ifdef L_ffs
  905. int ffs(int i)
  906. {
  907. #if 1
  908. /* inlined binary search method */
  909. char n = 1;
  910. #if UINT_MAX == 0xffffU
  911. /* nothing to do here -- just trying to avoiding possible problems */
  912. #elif UINT_MAX == 0xffffffffU
  913. if (!(i & 0xffff)) {
  914. n += 16;
  915. i >>= 16;
  916. }
  917. #else
  918. #error ffs needs rewriting!
  919. #endif
  920. if (!(i & 0xff)) {
  921. n += 8;
  922. i >>= 8;
  923. }
  924. if (!(i & 0x0f)) {
  925. n += 4;
  926. i >>= 4;
  927. }
  928. if (!(i & 0x03)) {
  929. n += 2;
  930. i >>= 2;
  931. }
  932. return (i) ? (n + ((i+1) & 0x01)) : 0;
  933. #else
  934. /* linear search -- slow, but small */
  935. int n;
  936. for (n = 0 ; i ; ++n) {
  937. i >>= 1;
  938. }
  939. return n;
  940. #endif
  941. }
  942. #endif
  943. /**********************************************************************/
  944. #if defined(L_strcasecmp) || defined(L_strcasecmp_l) || defined(L_wcscasecmp) || defined(L_wcscasecmp_l)
  945. #if defined(L_wcscasecmp) || defined(L_wcscasecmp_l)
  946. #define strcasecmp wcscasecmp
  947. #define strcasecmp_l wcscasecmp_l
  948. #ifdef __UCLIBC_DO_XLOCALE
  949. #define TOLOWER(C) towlower_l((C), locale_arg)
  950. #else
  951. #define TOLOWER(C) towlower((C))
  952. #endif
  953. #else /* defined(L_wcscasecmp) || defined(L_wcscasecmp_l) */
  954. #ifdef __UCLIBC_DO_XLOCALE
  955. #define TOLOWER(C) tolower_l((C), locale_arg)
  956. #else
  957. #define TOLOWER(C) tolower((C))
  958. #endif
  959. #endif /* defined(L_wcscasecmp) || defined(L_wcscasecmp_l) */
  960. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  961. int strcasecmp(register const Wchar *s1, register const Wchar *s2)
  962. {
  963. return strcasecmp_l(s1, s2, __UCLIBC_CURLOCALE);
  964. }
  965. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  966. int __XL(strcasecmp)(register const Wchar *s1, register const Wchar *s2
  967. __LOCALE_PARAM )
  968. {
  969. #ifdef WANT_WIDE
  970. while ((*s1 == *s2) || (TOLOWER(*s1) == TOLOWER(*s2))) {
  971. if (!*s1++) {
  972. return 0;
  973. }
  974. ++s2;
  975. }
  976. return (((Wuchar)TOLOWER(*s1)) < ((Wuchar)TOLOWER(*s2))) ? -1 : 1;
  977. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  978. #else
  979. int r = 0;
  980. while ( ((s1 == s2) ||
  981. !(r = ((int)( TOLOWER(*((Wuchar *)s1))))
  982. - TOLOWER(*((Wuchar *)s2))))
  983. && (++s2, *s1++));
  984. return r;
  985. #endif
  986. }
  987. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  988. #endif
  989. /**********************************************************************/
  990. #if defined(L_strncasecmp) || defined(L_strncasecmp_l) || defined(L_wcsncasecmp) || defined(L_wcsncasecmp_l)
  991. #if defined(L_wcsncasecmp) || defined(L_wcsncasecmp_l)
  992. #define strncasecmp wcsncasecmp
  993. #define strncasecmp_l wcsncasecmp_l
  994. #ifdef __UCLIBC_DO_XLOCALE
  995. #define TOLOWER(C) towlower_l((C), locale_arg)
  996. #else
  997. #define TOLOWER(C) towlower((C))
  998. #endif
  999. #else /* defined(L_wcsncasecmp) || defined(L_wcsncasecmp_l) */
  1000. #ifdef __UCLIBC_DO_XLOCALE
  1001. #define TOLOWER(C) tolower_l((C), locale_arg)
  1002. #else
  1003. #define TOLOWER(C) tolower((C))
  1004. #endif
  1005. #endif /* defined(L_wcsncasecmp) || defined(L_wcsncasecmp_l) */
  1006. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  1007. int strncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  1008. {
  1009. return strncasecmp_l(s1, s2, n, __UCLIBC_CURLOCALE);
  1010. }
  1011. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1012. int __XL(strncasecmp)(register const Wchar *s1, register const Wchar *s2,
  1013. size_t n __LOCALE_PARAM )
  1014. {
  1015. #ifdef WANT_WIDE
  1016. while (n && ((*s1 == *s2) || (TOLOWER(*s1) == TOLOWER(*s2)))) {
  1017. if (!*s1++) {
  1018. return 0;
  1019. }
  1020. ++s2;
  1021. --n;
  1022. }
  1023. return (n == 0)
  1024. ? 0
  1025. : ((((Wuchar)TOLOWER(*s1)) < ((Wuchar)TOLOWER(*s2))) ? -1 : 1);
  1026. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  1027. #else
  1028. int r = 0;
  1029. while ( n
  1030. && ((s1 == s2) ||
  1031. !(r = ((int)( TOLOWER(*((unsigned char *)s1))))
  1032. - TOLOWER(*((unsigned char *)s2))))
  1033. && (--n, ++s2, *s1++));
  1034. return r;
  1035. #endif
  1036. }
  1037. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1038. #endif
  1039. /**********************************************************************/
  1040. #ifdef L_wcsnlen
  1041. #define L_strnlen
  1042. #define Wstrnlen wcsnlen
  1043. #else
  1044. #define Wstrnlen strnlen
  1045. #endif
  1046. #ifdef L_strnlen
  1047. size_t Wstrnlen(const Wchar *s, size_t max)
  1048. {
  1049. register const Wchar *p = s;
  1050. #ifdef __BCC__
  1051. /* bcc can optimize the counter if it thinks it is a pointer... */
  1052. register const char *maxp = (const char *) max;
  1053. #else
  1054. #define maxp max
  1055. #endif
  1056. while (maxp && *p) {
  1057. ++p;
  1058. --maxp;
  1059. }
  1060. return p - s;
  1061. }
  1062. #undef maxp
  1063. #endif
  1064. /**********************************************************************/
  1065. /* No wide analog. */
  1066. #ifdef L_memccpy
  1067. void *memccpy(void * __restrict s1, const void * __restrict s2, int c, size_t n)
  1068. {
  1069. register char *r1 = s1;
  1070. register const char *r2 = s2;
  1071. while (n-- && (((unsigned char)(*r1++ = *r2++)) != ((unsigned char) c)));
  1072. return (n == (size_t) -1) ? NULL : r1;
  1073. }
  1074. #endif
  1075. /**********************************************************************/
  1076. #undef Wstrlen
  1077. #undef Wstrcpy
  1078. #ifdef L_wcsdup
  1079. #define L_strdup
  1080. #define Wstrdup wcsdup
  1081. #define Wstrlen wcslen
  1082. #define Wstrcpy wcscpy
  1083. #else
  1084. #define Wstrdup strdup
  1085. #define Wstrlen strlen
  1086. #define Wstrcpy strcpy
  1087. #endif
  1088. #ifdef L_strdup
  1089. Wchar *Wstrdup(register const Wchar *s1)
  1090. {
  1091. register Wchar *s;
  1092. if ((s = malloc((Wstrlen(s1) + 1) * sizeof(Wchar))) != NULL) {
  1093. Wstrcpy(s, s1);
  1094. }
  1095. return s;
  1096. }
  1097. #endif
  1098. /**********************************************************************/
  1099. #ifdef L_strerror
  1100. char *strerror(int errnum)
  1101. {
  1102. static char buf[_STRERROR_BUFSIZE];
  1103. _susv3_strerror_r(errnum, buf, sizeof(buf));
  1104. return buf;
  1105. }
  1106. #endif
  1107. /**********************************************************************/
  1108. /* SUSv3 functions. */
  1109. /**********************************************************************/
  1110. #ifdef L__susv3_strerror_r
  1111. #ifdef __UCLIBC_HAS_ERRNO_MESSAGES__
  1112. #if defined(__alpha__) || defined(__mips__) || defined(__sparc__)
  1113. static const unsigned char estridx[] = {
  1114. 0, /* success is always 0 */
  1115. EPERM,
  1116. ENOENT,
  1117. ESRCH,
  1118. EINTR,
  1119. EIO,
  1120. ENXIO,
  1121. E2BIG,
  1122. ENOEXEC,
  1123. EBADF,
  1124. ECHILD,
  1125. EAGAIN,
  1126. ENOMEM,
  1127. EACCES,
  1128. EFAULT,
  1129. ENOTBLK,
  1130. EBUSY,
  1131. EEXIST,
  1132. EXDEV,
  1133. ENODEV,
  1134. ENOTDIR,
  1135. EISDIR,
  1136. EINVAL,
  1137. ENFILE,
  1138. EMFILE,
  1139. ENOTTY,
  1140. ETXTBSY,
  1141. EFBIG,
  1142. ENOSPC,
  1143. ESPIPE,
  1144. EROFS,
  1145. EMLINK,
  1146. EPIPE,
  1147. EDOM,
  1148. ERANGE,
  1149. EDEADLK,
  1150. ENAMETOOLONG,
  1151. ENOLCK,
  1152. ENOSYS,
  1153. ENOTEMPTY,
  1154. ELOOP,
  1155. 0,
  1156. ENOMSG,
  1157. EIDRM,
  1158. ECHRNG,
  1159. EL2NSYNC,
  1160. EL3HLT,
  1161. EL3RST,
  1162. ELNRNG,
  1163. EUNATCH,
  1164. ENOCSI,
  1165. EL2HLT,
  1166. EBADE,
  1167. EBADR,
  1168. EXFULL,
  1169. ENOANO,
  1170. EBADRQC,
  1171. EBADSLT,
  1172. 0,
  1173. EBFONT,
  1174. ENOSTR,
  1175. ENODATA,
  1176. ETIME,
  1177. ENOSR,
  1178. ENONET,
  1179. ENOPKG,
  1180. EREMOTE,
  1181. ENOLINK,
  1182. EADV,
  1183. ESRMNT,
  1184. ECOMM,
  1185. EPROTO,
  1186. EMULTIHOP,
  1187. EDOTDOT,
  1188. EBADMSG,
  1189. EOVERFLOW,
  1190. ENOTUNIQ,
  1191. EBADFD,
  1192. EREMCHG,
  1193. ELIBACC,
  1194. ELIBBAD,
  1195. ELIBSCN,
  1196. ELIBMAX,
  1197. ELIBEXEC,
  1198. EILSEQ,
  1199. ERESTART,
  1200. ESTRPIPE,
  1201. EUSERS,
  1202. ENOTSOCK,
  1203. EDESTADDRREQ,
  1204. EMSGSIZE,
  1205. EPROTOTYPE,
  1206. ENOPROTOOPT,
  1207. EPROTONOSUPPORT,
  1208. ESOCKTNOSUPPORT,
  1209. EOPNOTSUPP,
  1210. EPFNOSUPPORT,
  1211. EAFNOSUPPORT,
  1212. EADDRINUSE,
  1213. EADDRNOTAVAIL,
  1214. ENETDOWN,
  1215. ENETUNREACH,
  1216. ENETRESET,
  1217. ECONNABORTED,
  1218. ECONNRESET,
  1219. ENOBUFS,
  1220. EISCONN,
  1221. ENOTCONN,
  1222. ESHUTDOWN,
  1223. ETOOMANYREFS,
  1224. ETIMEDOUT,
  1225. ECONNREFUSED,
  1226. EHOSTDOWN,
  1227. EHOSTUNREACH,
  1228. EALREADY,
  1229. EINPROGRESS,
  1230. ESTALE,
  1231. EUCLEAN,
  1232. ENOTNAM,
  1233. ENAVAIL,
  1234. EISNAM,
  1235. EREMOTEIO,
  1236. #ifdef __mips__
  1237. 0, /* mips has an outrageous value for this... */
  1238. #else
  1239. EDQUOT,
  1240. #endif
  1241. ENOMEDIUM,
  1242. EMEDIUMTYPE,
  1243. #if defined(__mips__) || defined(__sparc__)
  1244. EDEADLOCK,
  1245. #endif
  1246. };
  1247. #endif
  1248. int _susv3_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1249. {
  1250. register char *s;
  1251. int i, retval;
  1252. char buf[_STRERROR_BUFSIZE];
  1253. static const char unknown[] = {
  1254. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'e', 'r', 'r', 'o', 'r', ' '
  1255. };
  1256. retval = EINVAL;
  1257. #ifdef __UCLIBC_HAS_ERRNO_MESSAGES__
  1258. #if defined(__alpha__) || defined(__mips__) || defined(__sparc__)
  1259. /* Need to translate errno to string index. */
  1260. for (i = 0 ; i < sizeof(estridx)/sizeof(estridx[0]) ; i++) {
  1261. if (estridx[i] == errnum) {
  1262. goto GOT_ESTRIDX;
  1263. }
  1264. }
  1265. i = INT_MAX; /* Failed, but may need to check mips special case. */
  1266. #ifdef __mips__
  1267. if (errnum == EDQUOT) { /* Deal with large EDQUOT value on mips */
  1268. i = 122;
  1269. }
  1270. #endif /* __mips__ */
  1271. GOT_ESTRIDX:
  1272. #else
  1273. /* No errno to string index translation needed. */
  1274. i = errnum;
  1275. #endif
  1276. if (((unsigned int) i) < _SYS_NERR) {
  1277. /* Trade time for space. This function should rarely be called
  1278. * so rather than keeping an array of pointers for the different
  1279. * messages, just run through the buffer until we find the
  1280. * correct string. */
  1281. for (s = (char *) _string_syserrmsgs ; i ; ++s) {
  1282. if (!*s) {
  1283. --i;
  1284. }
  1285. }
  1286. if (*s) { /* Make sure we have an actual message. */
  1287. retval = 0;
  1288. goto GOT_MESG;
  1289. }
  1290. }
  1291. #endif /* __UCLIBC_HAS_ERRNO_MESSAGES__ */
  1292. s = _int10tostr(buf+sizeof(buf)-1, errnum) - sizeof(unknown);
  1293. memcpy(s, unknown, sizeof(unknown));
  1294. GOT_MESG:
  1295. if (!strerrbuf) { /* SUSv3 */
  1296. buflen = 0;
  1297. }
  1298. i = strlen(s) + 1;
  1299. if (i > buflen) {
  1300. i = buflen;
  1301. retval = ERANGE;
  1302. }
  1303. if (i) {
  1304. memcpy(strerrbuf, s, i);
  1305. strerrbuf[i-1] = 0; /* In case buf was too small. */
  1306. }
  1307. if (retval) {
  1308. __set_errno(retval);
  1309. }
  1310. return retval;
  1311. }
  1312. #else /* __UCLIBC_HAS_ERRNO_MESSAGES__ */
  1313. int _susv3_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1314. {
  1315. register char *s;
  1316. int i, retval;
  1317. char buf[_STRERROR_BUFSIZE];
  1318. static const char unknown[] = {
  1319. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'e', 'r', 'r', 'o', 'r', ' '
  1320. };
  1321. s = _int10tostr(buf+sizeof(buf)-1, errnum) - sizeof(unknown);
  1322. memcpy(s, unknown, sizeof(unknown));
  1323. if (!strerrbuf) { /* SUSv3 */
  1324. buflen = 0;
  1325. }
  1326. retval = EINVAL;
  1327. i = buf + sizeof(buf) - s;
  1328. if (i > buflen) {
  1329. i = buflen;
  1330. retval = ERANGE;
  1331. }
  1332. if (i) {
  1333. memcpy(strerrbuf, s, i);
  1334. strerrbuf[i-1] = 0; /* In case buf was too small. */
  1335. }
  1336. __set_errno(retval);
  1337. return retval;
  1338. }
  1339. #endif /* __UCLIBC_HAS_ERRNO_MESSAGES__ */
  1340. #endif
  1341. /**********************************************************************/
  1342. /* GNU extension functions. */
  1343. /**********************************************************************/
  1344. #ifdef L__glibc_strerror_r
  1345. weak_alias(_glibc_strerror_r,__strerror_r);
  1346. char *_glibc_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1347. {
  1348. _susv3_strerror_r(errnum, strerrbuf, buflen);
  1349. return strerrbuf;
  1350. }
  1351. #endif
  1352. /**********************************************************************/
  1353. #ifdef L_memmem
  1354. void *memmem(const void *haystack, size_t haystacklen,
  1355. const void *needle, size_t needlelen)
  1356. {
  1357. register const char *ph;
  1358. register const char *pn;
  1359. const char *plast;
  1360. size_t n;
  1361. if (needlelen == 0) {
  1362. return (void *) haystack;
  1363. }
  1364. if (haystacklen >= needlelen) {
  1365. ph = (const char *) haystack;
  1366. pn = (const char *) needle;
  1367. plast = ph + (haystacklen - needlelen);
  1368. do {
  1369. n = 0;
  1370. while (ph[n] == pn[n]) {
  1371. if (++n == needlelen) {
  1372. return (void *) ph;
  1373. }
  1374. }
  1375. } while (++ph <= plast);
  1376. }
  1377. return NULL;
  1378. }
  1379. #endif
  1380. /**********************************************************************/
  1381. #ifdef L_wmempcpy
  1382. #define L_mempcpy
  1383. #define Wmempcpy wmempcpy
  1384. #else
  1385. #define Wmempcpy mempcpy
  1386. #endif
  1387. #ifdef L_mempcpy
  1388. #ifndef L_wmempcpy
  1389. /* uClibc's old string implementation did this to cater to some app. */
  1390. weak_alias(mempcpy,__mempcpy);
  1391. #endif
  1392. Wvoid *Wmempcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  1393. {
  1394. register Wchar *r1 = s1;
  1395. register const Wchar *r2 = s2;
  1396. #ifdef __BCC__
  1397. while (n--) {
  1398. *r1++ = *r2++;
  1399. }
  1400. #else
  1401. while (n) {
  1402. *r1++ = *r2++;
  1403. --n;
  1404. }
  1405. #endif
  1406. return r1;
  1407. }
  1408. #endif
  1409. /**********************************************************************/
  1410. #ifdef L_memrchr
  1411. void *memrchr(const void *s, int c, size_t n)
  1412. {
  1413. register const unsigned char *r;
  1414. #ifdef __BCC__
  1415. /* bcc can optimize the counter if it thinks it is a pointer... */
  1416. register const char *np = (const char *) n;
  1417. #else
  1418. #define np n
  1419. #endif
  1420. r = ((unsigned char *)s) + ((size_t) np);
  1421. while (np) {
  1422. if (*--r == ((unsigned char)c)) {
  1423. return (void *) r; /* silence the warning */
  1424. }
  1425. --np;
  1426. }
  1427. return NULL;
  1428. }
  1429. #undef np
  1430. #endif
  1431. /**********************************************************************/
  1432. #ifdef L_wcpcpy
  1433. #define L_stpcpy
  1434. #define Wstpcpy wcpcpy
  1435. #else
  1436. #define Wstpcpy stpcpy
  1437. #endif
  1438. #ifdef L_stpcpy
  1439. Wchar *Wstpcpy(register Wchar * __restrict s1, const Wchar * __restrict s2)
  1440. {
  1441. #ifdef __BCC__
  1442. do {
  1443. *s1 = *s2++;
  1444. } while (*s1++ != 0);
  1445. #else
  1446. while ( (*s1++ = *s2++) != 0 );
  1447. #endif
  1448. return s1 - 1;
  1449. }
  1450. #endif
  1451. /**********************************************************************/
  1452. #ifdef L_wcpncpy
  1453. #define L_stpncpy
  1454. #define Wstpncpy wcpncpy
  1455. #else
  1456. #define Wstpncpy stpncpy
  1457. #endif
  1458. #ifdef L_stpncpy
  1459. Wchar *Wstpncpy(register Wchar * __restrict s1,
  1460. register const Wchar * __restrict s2,
  1461. size_t n)
  1462. {
  1463. Wchar *s = s1;
  1464. const Wchar *p = s2;
  1465. #ifdef __BCC__
  1466. while (n--) {
  1467. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1468. ++s;
  1469. }
  1470. return s1 + (s2 - p);
  1471. #else
  1472. while (n) {
  1473. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1474. ++s;
  1475. --n;
  1476. }
  1477. return s1 + (s2 - p);
  1478. #endif
  1479. }
  1480. #endif
  1481. /**********************************************************************/
  1482. #ifdef L_bzero
  1483. void bzero(void *s, size_t n)
  1484. {
  1485. register unsigned char *p = s;
  1486. #ifdef __BCC__
  1487. /* bcc can optimize the counter if it thinks it is a pointer... */
  1488. register const char *np = (const char *) n;
  1489. #else
  1490. #define np n
  1491. #endif
  1492. while (np) {
  1493. *p++ = 0;
  1494. --np;
  1495. }
  1496. }
  1497. #undef np
  1498. #endif
  1499. /**********************************************************************/
  1500. #ifdef L_bcopy
  1501. void bcopy(const void *s2, void *s1, size_t n)
  1502. {
  1503. #if 1
  1504. memmove(s1, s2, n);
  1505. #else
  1506. #ifdef __BCC__
  1507. register char *s;
  1508. register const char *p;
  1509. s = s1;
  1510. p = s2;
  1511. if (p >= s) {
  1512. while (n--) {
  1513. *s++ = *p++;
  1514. }
  1515. } else {
  1516. s += n;
  1517. p += n;
  1518. while (n--) {
  1519. *--s = *--p;
  1520. }
  1521. }
  1522. #else
  1523. register char *s;
  1524. register const char *p;
  1525. s = s1;
  1526. p = s2;
  1527. if (p >= s) {
  1528. while (n) {
  1529. *s++ = *p++;
  1530. --n;
  1531. }
  1532. } else {
  1533. while (n) {
  1534. --n;
  1535. s[n] = p[n];
  1536. }
  1537. }
  1538. #endif
  1539. #endif
  1540. }
  1541. #endif
  1542. /**********************************************************************/
  1543. #ifdef L_strcasestr
  1544. char *strcasestr(const char *s1, const char *s2)
  1545. {
  1546. register const char *s = s1;
  1547. register const char *p = s2;
  1548. #if 1
  1549. do {
  1550. if (!*p) {
  1551. return (char *) s1;;
  1552. }
  1553. if ((*p == *s)
  1554. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1555. ) {
  1556. ++p;
  1557. ++s;
  1558. } else {
  1559. p = s2;
  1560. if (!*s) {
  1561. return NULL;
  1562. }
  1563. s = ++s1;
  1564. }
  1565. } while (1);
  1566. #else
  1567. while (*p && *s) {
  1568. if ((*p == *s)
  1569. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1570. ) {
  1571. ++p;
  1572. ++s;
  1573. } else {
  1574. p = s2;
  1575. s = ++s1;
  1576. }
  1577. }
  1578. return (*p) ? NULL : (char *) s1;
  1579. #endif
  1580. }
  1581. #endif
  1582. /**********************************************************************/
  1583. #ifdef L_strndup
  1584. char *strndup(register const char *s1, size_t n)
  1585. {
  1586. register char *s;
  1587. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  1588. if ((s = malloc(n + 1)) != NULL) {
  1589. memcpy(s, s1, n);
  1590. s[n] = 0;
  1591. }
  1592. return s;
  1593. }
  1594. #endif
  1595. /**********************************************************************/
  1596. #ifdef L_strsep
  1597. char *strsep(char ** __restrict s1, const char * __restrict s2)
  1598. {
  1599. register char *s = *s1;
  1600. register char *p;
  1601. #if 1
  1602. p = NULL;
  1603. if (s && *s && (p = strpbrk(s, s2))) {
  1604. *p++ = 0;
  1605. }
  1606. #else
  1607. if (s && *s && *(p = s + strcspn(s, s2))) {
  1608. *p++ = 0;
  1609. } else {
  1610. p = NULL;
  1611. }
  1612. #endif
  1613. *s1 = p;
  1614. return s;
  1615. }
  1616. #endif
  1617. /**********************************************************************/
  1618. #ifdef L_wcschrnul
  1619. #define L_strchrnul
  1620. #define __Wstrchrnul __wcschrnul
  1621. #define Wstrchrnul wcschrnul
  1622. #else
  1623. #define __Wstrchrnul __strchrnul
  1624. #define Wstrchrnul strchrnul
  1625. #endif
  1626. #ifdef L_strchrnul
  1627. extern Wchar *__Wstrchrnul(register const Wchar *s, Wint c);
  1628. weak_alias(__Wstrchrnul, Wstrchrnul);
  1629. Wchar *__Wstrchrnul(register const Wchar *s, Wint c)
  1630. {
  1631. --s;
  1632. while (*++s && (*s != ((Wchar)c)));
  1633. return (Wchar *) s;
  1634. }
  1635. #endif
  1636. /**********************************************************************/
  1637. #ifdef L_rawmemchr
  1638. void *rawmemchr(const void *s, int c)
  1639. {
  1640. register const unsigned char *r = s;
  1641. while (*r != ((unsigned char)c)) ++r;
  1642. return (void *) r; /* silence the warning */
  1643. }
  1644. #endif
  1645. /**********************************************************************/
  1646. #ifdef L_basename
  1647. char *basename(const char *path)
  1648. {
  1649. register const char *s;
  1650. register const char *p;
  1651. p = s = path;
  1652. while (*s) {
  1653. if (*s++ == '/') {
  1654. p = s;
  1655. }
  1656. }
  1657. return (char *) p;
  1658. }
  1659. #endif
  1660. /**********************************************************************/
  1661. #ifdef L___xpg_basename
  1662. char *__xpg_basename(register char *path)
  1663. {
  1664. static const char null_or_empty[] = ".";
  1665. char *first;
  1666. register char *last;
  1667. first = (char *) null_or_empty;
  1668. if (path && *path) {
  1669. first = path;
  1670. last = path - 1;
  1671. do {
  1672. if ((*path != '/') && (path > ++last)) {
  1673. last = first = path;
  1674. }
  1675. } while (*++path);
  1676. if (*first == '/') {
  1677. last = first;
  1678. }
  1679. last[1] = 0;
  1680. }
  1681. return first;
  1682. }
  1683. #endif
  1684. /**********************************************************************/
  1685. #ifdef L_dirname
  1686. char *dirname(char *path)
  1687. {
  1688. static const char null_or_empty_or_noslash[] = ".";
  1689. register char *s;
  1690. register char *last;
  1691. char *first;
  1692. last = s = path;
  1693. if (s != NULL) {
  1694. LOOP:
  1695. while (*s && (*s != '/')) ++s;
  1696. first = s;
  1697. while (*s == '/') ++s;
  1698. if (*s) {
  1699. last = first;
  1700. goto LOOP;
  1701. }
  1702. if (last == path) {
  1703. if (*last != '/') {
  1704. goto DOT;
  1705. }
  1706. if ((*++last == '/') && (last[1] == 0)) {
  1707. ++last;
  1708. }
  1709. }
  1710. *last = 0;
  1711. return path;
  1712. }
  1713. DOT:
  1714. return (char *) null_or_empty_or_noslash;
  1715. }
  1716. #endif
  1717. /**********************************************************************/
  1718. #ifdef L_strlcat
  1719. /* OpenBSD function:
  1720. * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst.
  1721. * Returns strlen(src) + strlen({original} dst), so truncation occurred if the
  1722. * return val is >= n.
  1723. * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is
  1724. * taken as n. */
  1725. size_t strlcat(register char *__restrict dst,
  1726. register const char *__restrict src,
  1727. size_t n)
  1728. {
  1729. size_t len;
  1730. char dummy[1];
  1731. len = 0;
  1732. while (1) {
  1733. if (len >= n) {
  1734. dst = dummy;
  1735. break;
  1736. }
  1737. if (!*dst) {
  1738. break;
  1739. }
  1740. ++dst;
  1741. ++len;
  1742. }
  1743. while ((*dst = *src) != 0) {
  1744. if (++len < n) {
  1745. ++dst;
  1746. }
  1747. ++src;
  1748. }
  1749. return len;
  1750. }
  1751. #endif
  1752. /**********************************************************************/
  1753. #ifdef WANT_WIDE
  1754. extern size_t __wcslcpy(wchar_t *__restrict dst,
  1755. const wchar_t *__restrict src,
  1756. size_t n);
  1757. #endif
  1758. #ifdef L___wcslcpy
  1759. #define L_strlcpy
  1760. #define Wstrlcpy __wcslcpy
  1761. #ifdef __LOCALE_C_ONLY
  1762. weak_alias(__wcslcpy,wcsxfrm);
  1763. #endif
  1764. #endif
  1765. #ifdef L_strlcpy
  1766. #ifndef L___wcslcpy
  1767. #define Wstrlcpy strlcpy
  1768. #ifdef __LOCALE_C_ONLY
  1769. weak_alias(strlcpy,strxfrm);
  1770. #endif
  1771. #endif
  1772. /* OpenBSD function:
  1773. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  1774. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  1775. size_t Wstrlcpy(register Wchar *__restrict dst,
  1776. register const Wchar *__restrict src,
  1777. size_t n)
  1778. {
  1779. const Wchar *src0 = src;
  1780. Wchar dummy[1];
  1781. if (!n) {
  1782. dst = dummy;
  1783. } else {
  1784. --n;
  1785. }
  1786. while ((*dst = *src) != 0) {
  1787. if (n) {
  1788. --n;
  1789. ++dst;
  1790. }
  1791. ++src;
  1792. }
  1793. return src - src0;
  1794. }
  1795. #endif
  1796. /**********************************************************************/
  1797. #if defined(L__string_syssigmsgs) && defined(__UCLIBC_HAS_SIGNUM_MESSAGES__)
  1798. const char _string_syssigmsgs[] = {
  1799. /* 0: 0, 1 */ "\0"
  1800. /* 1: 1, 7 */ "Hangup\0"
  1801. /* 2: 8, 10 */ "Interrupt\0"
  1802. /* 3: 18, 5 */ "Quit\0"
  1803. /* 4: 23, 20 */ "Illegal instruction\0"
  1804. /* 5: 43, 22 */ "Trace/breakpoint trap\0"
  1805. /* 6: 65, 8 */ "Aborted\0"
  1806. /* 7: 73, 10 */ "Bus error\0"
  1807. /* 8: 83, 25 */ "Floating point exception\0"
  1808. /* 9: 108, 7 */ "Killed\0"
  1809. /* 10: 115, 22 */ "User defined signal 1\0"
  1810. /* 11: 137, 19 */ "Segmentation fault\0"
  1811. /* 12: 156, 22 */ "User defined signal 2\0"
  1812. /* 13: 178, 12 */ "Broken pipe\0"
  1813. /* 14: 190, 12 */ "Alarm clock\0"
  1814. /* 15: 202, 11 */ "Terminated\0"
  1815. /* 16: 213, 12 */ "Stack fault\0"
  1816. /* 17: 225, 13 */ "Child exited\0"
  1817. /* 18: 238, 10 */ "Continued\0"
  1818. /* 19: 248, 17 */ "Stopped (signal)\0"
  1819. /* 20: 265, 8 */ "Stopped\0"
  1820. /* 21: 273, 20 */ "Stopped (tty input)\0"
  1821. /* 22: 293, 21 */ "Stopped (tty output)\0"
  1822. /* 23: 314, 21 */ "Urgent I/O condition\0"
  1823. /* 24: 335, 24 */ "CPU time limit exceeded\0"
  1824. /* 25: 359, 25 */ "File size limit exceeded\0"
  1825. /* 26: 384, 22 */ "Virtual timer expired\0"
  1826. /* 27: 406, 24 */ "Profiling timer expired\0"
  1827. /* 28: 430, 15 */ "Window changed\0"
  1828. /* 29: 445, 13 */ "I/O possible\0"
  1829. /* 30: 458, 14 */ "Power failure\0"
  1830. /* 31: 472, 16 */ "Bad system call"
  1831. };
  1832. #endif
  1833. /**********************************************************************/
  1834. #if defined(L_sys_siglist) && defined(__UCLIBC_HAS_SYS_SIGLIST__)
  1835. const char *const sys_siglist[_NSIG] = {
  1836. NULL,
  1837. _string_syssigmsgs + 1,
  1838. _string_syssigmsgs + 8,
  1839. _string_syssigmsgs + 18,
  1840. _string_syssigmsgs + 23,
  1841. _string_syssigmsgs + 43,
  1842. _string_syssigmsgs + 65,
  1843. _string_syssigmsgs + 73,
  1844. _string_syssigmsgs + 83,
  1845. _string_syssigmsgs + 108,
  1846. _string_syssigmsgs + 115,
  1847. _string_syssigmsgs + 137,
  1848. _string_syssigmsgs + 156,
  1849. _string_syssigmsgs + 178,
  1850. _string_syssigmsgs + 190,
  1851. _string_syssigmsgs + 202,
  1852. _string_syssigmsgs + 213,
  1853. _string_syssigmsgs + 225,
  1854. _string_syssigmsgs + 238,
  1855. _string_syssigmsgs + 248,
  1856. _string_syssigmsgs + 265,
  1857. _string_syssigmsgs + 273,
  1858. _string_syssigmsgs + 293,
  1859. _string_syssigmsgs + 314,
  1860. _string_syssigmsgs + 335,
  1861. _string_syssigmsgs + 359,
  1862. _string_syssigmsgs + 384,
  1863. _string_syssigmsgs + 406,
  1864. _string_syssigmsgs + 430,
  1865. _string_syssigmsgs + 445,
  1866. _string_syssigmsgs + 458,
  1867. _string_syssigmsgs + 472,
  1868. NULL,
  1869. NULL,
  1870. NULL,
  1871. NULL,
  1872. NULL,
  1873. NULL,
  1874. NULL,
  1875. NULL,
  1876. NULL,
  1877. NULL,
  1878. NULL,
  1879. NULL,
  1880. NULL,
  1881. NULL,
  1882. NULL,
  1883. NULL,
  1884. NULL,
  1885. NULL,
  1886. NULL,
  1887. NULL,
  1888. NULL,
  1889. NULL,
  1890. NULL,
  1891. NULL,
  1892. NULL,
  1893. NULL,
  1894. NULL,
  1895. NULL,
  1896. NULL,
  1897. NULL,
  1898. NULL,
  1899. NULL,
  1900. };
  1901. #endif
  1902. /**********************************************************************/
  1903. #ifdef L_strsignal
  1904. /* TODO: make a threadsafe version? */
  1905. #ifdef __UCLIBC_HAS_SIGNUM_MESSAGES__
  1906. char *strsignal(int signum)
  1907. {
  1908. register char *s;
  1909. int i;
  1910. static char buf[_STRSIGNAL_BUFSIZE];
  1911. static const char unknown[] = {
  1912. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  1913. };
  1914. if (((unsigned int) signum) < _SYS_NSIG) {
  1915. /* Trade time for space. This function should rarely be called
  1916. * so rather than keeping an array of pointers for the different
  1917. * messages, just run through the buffer until we find the
  1918. * correct string. */
  1919. for (s = (char *) _string_syssigmsgs, i = signum ; i ; ++s) {
  1920. if (!*s) {
  1921. --i;
  1922. }
  1923. }
  1924. if (*s) { /* Make sure we have an actual message. */
  1925. goto DONE;
  1926. }
  1927. }
  1928. s = _int10tostr(buf+sizeof(buf)-1, signum) - sizeof(unknown);
  1929. memcpy(s, unknown, sizeof(unknown));
  1930. DONE:
  1931. return s;
  1932. }
  1933. #else /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  1934. char *strsignal(int signum)
  1935. {
  1936. static char buf[_STRSIGNAL_BUFSIZE];
  1937. static const char unknown[] = {
  1938. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  1939. };
  1940. return (char *) memcpy(_int10tostr(buf+sizeof(buf)-1, signum)
  1941. - sizeof(unknown),
  1942. unknown, sizeof(unknown));
  1943. }
  1944. #endif /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  1945. #endif
  1946. /**********************************************************************/
  1947. #ifdef L_psignal
  1948. /* TODO: make this threadsafe with a reentrant version of strsignal? */
  1949. void psignal(int signum, register const char *message)
  1950. {
  1951. /* If the program is calling psignal, it's a safe bet that printf and
  1952. * friends are used as well. It is also possible that the calling
  1953. * program could buffer stderr, or reassign it. */
  1954. register const char *sep;
  1955. sep = ": ";
  1956. if (!(message && *message)) { /* Caller did not supply a prefix message */
  1957. message = (sep += 2); /* or passed an empty string. */
  1958. }
  1959. #if 1
  1960. fprintf(stderr, "%s%s%s\n", message, sep, strsignal(signum));
  1961. #else
  1962. /* Note: Assumes stderr not closed or buffered. */
  1963. __STDIO_THREADLOCK(stderr);
  1964. _stdio_fdout(STDERR_FILENO, message, sep, strsignal(signum));
  1965. __STDIO_THREADUNLOCK(stderr);
  1966. #endif
  1967. }
  1968. #endif
  1969. /**********************************************************************/
  1970. #ifndef __LOCALE_C_ONLY
  1971. #if defined(L_strxfrm) || defined(L_strxfrm_l) || defined(L_wcsxfrm) || defined(L_wcsxfrm_l)
  1972. #ifdef L_strxfrm
  1973. #ifndef WANT_WIDE
  1974. #error WANT_WIDE should be defined for L_strxfrm
  1975. #endif
  1976. #ifdef L_wcsxfrm
  1977. #error L_wcsxfrm already defined for L_strxfrm
  1978. #endif
  1979. #endif /* L_strxfrm */
  1980. #if defined(L_strxfrm) || defined(L_strxfrm_l)
  1981. #define wcscoll strcoll
  1982. #define wcscoll_l strcoll_l
  1983. #define wcsxfrm strxfrm
  1984. #define wcsxfrm_l strxfrm_l
  1985. #undef WANT_WIDE
  1986. #undef Wvoid
  1987. #undef Wchar
  1988. #undef Wuchar
  1989. #undef Wint
  1990. #define Wchar char
  1991. #endif /* defined(L_strxfrm) || defined(L_strxfrm_l) */
  1992. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  1993. int wcscoll (const Wchar *s0, const Wchar *s1)
  1994. {
  1995. return wcscoll_l(s0, s1, __UCLIBC_CURLOCALE );
  1996. }
  1997. size_t wcsxfrm(Wchar *__restrict ws1, const Wchar *__restrict ws2, size_t n)
  1998. {
  1999. return wcsxfrm_l(ws1, ws2, n, __UCLIBC_CURLOCALE );
  2000. }
  2001. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  2002. #if 0
  2003. #define CUR_COLLATE (&__UCLIBC_CURLOCALE_DATA.collate)
  2004. #else
  2005. #define CUR_COLLATE (& __LOCALE_PTR->collate)
  2006. #endif
  2007. #define MAX_PENDING 8
  2008. typedef struct {
  2009. const Wchar *s;
  2010. const Wchar *eob; /* end of backward */
  2011. __uwchar_t weight;
  2012. __uwchar_t ui_weight; /* undefined or invalid */
  2013. int colitem;
  2014. int weightidx;
  2015. int rule;
  2016. size_t position;
  2017. /* should be wchar_t. if wchar < 0 do EILSEQ? */
  2018. __uwchar_t *cip;
  2019. __uwchar_t ci_pending[MAX_PENDING]; /* nul-terminated */
  2020. char *back_buf;
  2021. char *bbe; /* end of back_buf (actual last... not 1 past end) */
  2022. char *bp; /* ptr into backbuf, NULL if not in backward mode */
  2023. char ibb[128];
  2024. size_t bb_size;
  2025. int ru_pushed;
  2026. } col_state_t;
  2027. #define WEIGHT_MASK 0x3fffU
  2028. #define RULE_MASK 0xc000U
  2029. #define RULE_FORWARD (1 << 14)
  2030. #define RULE_POSITION (1 << 15)
  2031. #define UI_IDX (WEIGHT_MASK-6)
  2032. #define POSIT_IDX (WEIGHT_MASK-5)
  2033. #define RANGE_IDX (WEIGHT_MASK-4)
  2034. #define UNDEF_IDX (WEIGHT_MASK-3)
  2035. #define INVAL_IDX (WEIGHT_MASK-2)
  2036. #define DITTO_IDX (WEIGHT_MASK-1)
  2037. #undef TRACE
  2038. #if 0
  2039. #define TRACE(X) printf##X
  2040. #else
  2041. #define TRACE(X) ((void)0)
  2042. #endif
  2043. static int lookup(wchar_t wc __LOCALE_PARAM )
  2044. {
  2045. unsigned int sc, n, i0, i1;
  2046. if (((__uwchar_t) wc) > 0xffffU) {
  2047. return 0;
  2048. }
  2049. sc = wc & CUR_COLLATE->ti_mask;
  2050. wc >>= CUR_COLLATE->ti_shift;
  2051. n = wc & CUR_COLLATE->ii_mask;
  2052. wc >>= CUR_COLLATE->ii_shift;
  2053. i0 = CUR_COLLATE->wcs2colidt_tbl[wc];
  2054. i0 <<= CUR_COLLATE->ii_shift;
  2055. i1 = CUR_COLLATE->wcs2colidt_tbl[CUR_COLLATE->ii_len + i0 + n];
  2056. i1 <<= CUR_COLLATE->ti_shift;
  2057. return CUR_COLLATE->wcs2colidt_tbl[CUR_COLLATE->ii_len + CUR_COLLATE->ti_len + i1 + sc];
  2058. }
  2059. static void init_col_state(col_state_t *cs, const Wchar *wcs)
  2060. {
  2061. memset(cs, 0, sizeof(col_state_t));
  2062. cs->s = wcs;
  2063. cs->bp = cs->back_buf = cs->ibb;
  2064. cs->bb_size = 128;
  2065. cs->bbe = cs->back_buf + (cs->bb_size -1);
  2066. }
  2067. static void next_weight(col_state_t *cs, int pass __LOCALE_PARAM )
  2068. {
  2069. int r, w, ru, ri, popping_backup_stack;
  2070. ssize_t n;
  2071. const uint16_t *p;
  2072. #ifdef WANT_WIDE
  2073. #define WC (*cs->s)
  2074. #define N (1)
  2075. #else /* WANT_WIDE */
  2076. wchar_t WC;
  2077. size_t n0, nx;
  2078. #define N n0
  2079. #endif /* WANT_WIDE */
  2080. do {
  2081. if (cs->ru_pushed) {
  2082. ru = cs->ru_pushed;
  2083. TRACE(("ru_pushed = %d\n", ru));
  2084. cs->ru_pushed = 0;
  2085. goto POSITION_SKIP;
  2086. }
  2087. #ifdef __UCLIBC_MJN3_ONLY__
  2088. #warning should we walk pendings backwards?
  2089. #endif
  2090. if (cs->cip) { /* possible pending weight */
  2091. if ((r = *(cs->cip++)) == 0) {
  2092. cs->cip = NULL;
  2093. continue;
  2094. }
  2095. cs->weightidx = r & WEIGHT_MASK;
  2096. assert(cs->weightidx);
  2097. /* assert(cs->weightidx != WEIGHT_MASK); */
  2098. } else { /* get the next collation item from the string */
  2099. TRACE(("clearing popping flag\n"));
  2100. popping_backup_stack = 0;
  2101. IGNORE_LOOP:
  2102. /* keep first pos as 0 for a sentinal */
  2103. if (*cs->bp) { /* pending backward chars */
  2104. POP_BACKUP:
  2105. popping_backup_stack = 1;
  2106. TRACE(("setting popping flag\n"));
  2107. n = 0;
  2108. if (*cs->bp > 0) { /* singles pending */
  2109. cs->s -= 1;
  2110. if ((*cs->bp -= 1) == 0) {
  2111. cs->bp -= 1;
  2112. }
  2113. } else { /* last was a multi */
  2114. cs->s += *cs->bp;
  2115. cs->bp -= 1;
  2116. }
  2117. } else if (!*cs->s) { /* not in backward mode and end of string */
  2118. cs->weight = 0;
  2119. return;
  2120. } else {
  2121. cs->position += 1;
  2122. }
  2123. BACK_LOOP:
  2124. #ifdef WANT_WIDE
  2125. n = 1;
  2126. cs->colitem = r = lookup(*cs->s __LOCALE_ARG );
  2127. #else /* WANT_WIDE */
  2128. n = n0 = __locale_mbrtowc_l(&WC, cs->s, __LOCALE_PTR);
  2129. if (n < 0) {
  2130. __set_errno(EILSEQ);
  2131. cs->weight = 0;
  2132. return;
  2133. }
  2134. cs->colitem = r = lookup(WC __LOCALE_ARG );
  2135. #endif /* WANT_WIDE */
  2136. TRACE((" r=%d WC=%#lx\n", r, (unsigned long)(WC)));
  2137. if (r > CUR_COLLATE->max_col_index) { /* starting char for one or more sequences */
  2138. p = CUR_COLLATE->multistart_tbl;
  2139. p += p[r-CUR_COLLATE->max_col_index -1];
  2140. do {
  2141. n = N;
  2142. r = *p++;
  2143. do {
  2144. if (!*p) { /* found it */
  2145. cs->colitem = r;
  2146. TRACE((" found multi %d\n", n));
  2147. goto FOUND;
  2148. }
  2149. #ifdef WANT_WIDE
  2150. /* the lookup check here is safe since we're assured that *p is a valid colidx */
  2151. if (!cs->s[n] || (lookup(cs->s[n] __LOCALE_ARG ) != *p)) {
  2152. do {} while (*p++);
  2153. break;
  2154. }
  2155. ++p;
  2156. ++n;
  2157. #else /* WANT_WIDE */
  2158. if (cs->s[n]) {
  2159. nx = __locale_mbrtowc_l(&WC, cs->s + n, __LOCALE_PTR);
  2160. if (nx < 0) {
  2161. __set_errno(EILSEQ);
  2162. cs->weight = 0;
  2163. return;
  2164. }
  2165. }
  2166. if (!cs->s[n] || (lookup(WC __LOCALE_ARG ) != *p)) {
  2167. do {} while (*p++);
  2168. break;
  2169. }
  2170. ++p;
  2171. n += nx; /* Only gets here if cs->s[n] != 0, so nx is set. */
  2172. #endif /* WANT_WIDE */
  2173. } while (1);
  2174. } while (1);
  2175. } else if (r == 0) { /* illegal, undefined, or part of a range */
  2176. if ((CUR_COLLATE->range_count)
  2177. #ifdef __UCLIBC_MJN3_ONLY__
  2178. #warning .. need to introduce range as a collating item?
  2179. #endif
  2180. && (((__uwchar_t)(WC - CUR_COLLATE->range_low)) <= CUR_COLLATE->range_count)
  2181. ) { /* part of a range */
  2182. /* Note: cs->colitem = 0 already. */
  2183. TRACE((" found range\n"));
  2184. ru = CUR_COLLATE->ruletable[CUR_COLLATE->range_rule_offset*CUR_COLLATE->MAX_WEIGHTS + pass];
  2185. assert((ru & WEIGHT_MASK) != DITTO_IDX);
  2186. if ((ru & WEIGHT_MASK) == WEIGHT_MASK) {
  2187. ru = (ru & RULE_MASK) | RANGE_IDX;
  2188. cs->weight = CUR_COLLATE->range_base_weight + (WC - CUR_COLLATE->range_low);
  2189. }
  2190. goto RANGE_SKIP_TO;
  2191. } else if (((__uwchar_t)(WC)) <= 0x7fffffffUL) { /* legal but undefined */
  2192. UNDEFINED:
  2193. /* Note: cs->colitem = 0 already. */
  2194. ri = CUR_COLLATE->undefined_idx;
  2195. assert(ri != 0); /* implicit undefined isn't supported */
  2196. TRACE((" found explicit UNDEFINED\n"));
  2197. #ifdef __UCLIBC_MJN3_ONLY__
  2198. #warning right now single weight locales do not support ..
  2199. #endif
  2200. if (CUR_COLLATE->num_weights == 1) {
  2201. TRACE((" single weight UNDEFINED\n"));
  2202. cs->weightidx = RANGE_IDX;
  2203. cs->weight = ri;
  2204. cs->s += n;
  2205. goto PROCESS_WEIGHT;
  2206. }
  2207. ri = CUR_COLLATE->index2ruleidx[ri - 1];
  2208. ru = CUR_COLLATE->ruletable[ri * CUR_COLLATE->MAX_WEIGHTS + pass];
  2209. assert((ru & WEIGHT_MASK) != WEIGHT_MASK); /* TODO: handle ".." */
  2210. if ((ru & WEIGHT_MASK) == DITTO_IDX) {
  2211. cs->colitem = CUR_COLLATE->undefined_idx;
  2212. }
  2213. goto RANGE_SKIP_TO;
  2214. } else { /* illegal */
  2215. TRACE((" found illegal\n"));
  2216. __set_errno(EINVAL);
  2217. /* We put all illegals in the same equiv class with maximal weight,
  2218. * and ignore them after the first pass. */
  2219. if (pass > 0) {
  2220. cs->s += n;
  2221. goto IGNORE_LOOP;
  2222. }
  2223. ru = (RULE_FORWARD | RANGE_IDX);
  2224. cs->weight = 0xffffU;
  2225. goto RANGE_SKIP_TO;
  2226. }
  2227. } else if (CUR_COLLATE->num_weights == 1) {
  2228. TRACE((" single weight\n"));
  2229. cs->weightidx = RANGE_IDX;
  2230. cs->weight = cs->colitem;
  2231. cs->s += n;
  2232. goto PROCESS_WEIGHT;
  2233. } else {
  2234. TRACE((" normal\n"));
  2235. }
  2236. /* if we get here, it is a normal char either singlely weighted, undefined, or in a range */
  2237. FOUND:
  2238. ri = CUR_COLLATE->index2ruleidx[cs->colitem - 1];
  2239. TRACE((" ri=%d ", ri));
  2240. #ifdef __UCLIBC_MJN3_ONLY__
  2241. #warning make sure this is correct
  2242. #endif
  2243. if (!ri) {
  2244. TRACE(("NOT IN THIS LOCALE\n"));
  2245. goto UNDEFINED;
  2246. }
  2247. ru = CUR_COLLATE->ruletable[ri * CUR_COLLATE->MAX_WEIGHTS + pass];
  2248. RANGE_SKIP_TO:
  2249. #ifdef __UCLIBC_MJN3_ONLY__
  2250. #warning ignoreables probably should not interrupt backwards processing, but this is wrong
  2251. #endif
  2252. /* if (!(ru & WEIGHT_MASK)) { */
  2253. /* TRACE(("IGNORE\n")); */
  2254. /* cs->s += n; */
  2255. /* continue; */
  2256. /* } */
  2257. TRACE((" rule = %#x weight = %#x popping = %d s = %p eob = %p\n",
  2258. ru & RULE_MASK, ru & WEIGHT_MASK, popping_backup_stack,
  2259. cs->s, cs->eob));
  2260. /* now we need to check if we're going backwards... */
  2261. if (!popping_backup_stack) {
  2262. if (!(ru & RULE_MASK)) { /* backward */
  2263. TRACE(("backwards\n"));
  2264. assert(cs->bp <= cs->bbe);
  2265. if (cs->bp == cs->bbe) {
  2266. if (cs->back_buf == cs->ibb) { /* was using internal buffer */
  2267. cs->bp = malloc(cs->bb_size + 128);
  2268. if (!cs->bp) {
  2269. __set_errno(ENOMEM);
  2270. #ifdef __UCLIBC_MJN3_ONLY__
  2271. #warning what to do here?
  2272. #endif
  2273. cs->weight = 0;
  2274. return;
  2275. }
  2276. memcpy(cs->bp, cs->back_buf, cs->bb_size);
  2277. } else {
  2278. cs->bp = realloc(cs->back_buf, cs->bb_size + 128);
  2279. if (!cs->bp) {
  2280. __set_errno(ENOMEM);
  2281. #ifdef __UCLIBC_MJN3_ONLY__
  2282. #warning what to do here?
  2283. #endif
  2284. cs->weight = 0;
  2285. return;
  2286. }
  2287. }
  2288. cs->bb_size += 128;
  2289. cs->bbe = cs->bp + (cs->bbe - cs->back_buf);
  2290. cs->back_buf = cs->bp;
  2291. cs->bp = cs->bbe;
  2292. }
  2293. if (n==1) { /* single char */
  2294. if (*cs->bp && (((unsigned char)(*cs->bp)) < CHAR_MAX)) {
  2295. *cs->bp += 1; /* increment last single's count */
  2296. } else { /* last was a multi, or just starting */
  2297. if (!cs->bp) {
  2298. cs->bp = cs->back_buf;
  2299. } else {
  2300. assert(cs->bp < cs->bbe);
  2301. ++cs->bp;
  2302. }
  2303. *cs->bp = 1;
  2304. }
  2305. } else { /* multichar */
  2306. assert(n>1);
  2307. assert(cs->bp < cs->bbe);
  2308. *++cs->bp = -n;
  2309. }
  2310. cs->s += n;
  2311. if (*cs->s) {
  2312. goto BACK_LOOP;
  2313. }
  2314. /* end-of-string so start popping */
  2315. cs->eob = cs->s;
  2316. TRACE(("popping\n"));
  2317. goto POP_BACKUP;
  2318. } else if (*cs->bp) { /* was going backward but this element isn't */
  2319. /* discard current and use previous backward element */
  2320. assert(!cs->cip);
  2321. cs->eob = cs->s;
  2322. TRACE(("popping\n"));
  2323. goto POP_BACKUP;
  2324. } else { /* was and still going forward */
  2325. TRACE(("forwards\n"));
  2326. if ((ru & (RULE_POSITION|WEIGHT_MASK)) > RULE_POSITION) {
  2327. assert(ru & WEIGHT_MASK);
  2328. cs->ru_pushed = ru;
  2329. cs->weight = cs->position;
  2330. #ifdef __UCLIBC_MJN3_ONLY__
  2331. #warning devel code
  2332. #endif
  2333. cs->position = 0; /* reset to reduce size for strcoll? */
  2334. cs->s += n;
  2335. cs->weightidx = RANGE_IDX;
  2336. goto PROCESS_WEIGHT;
  2337. }
  2338. }
  2339. } else { /* popping backwards stack */
  2340. TRACE(("popping (continued)\n"));
  2341. if (!*cs->bp) {
  2342. cs->s = cs->eob;
  2343. }
  2344. cs->s -= n;
  2345. }
  2346. cs->s += n;
  2347. POSITION_SKIP:
  2348. cs->weightidx = ru & WEIGHT_MASK;
  2349. cs->rule = ru & RULE_MASK;
  2350. }
  2351. #ifdef __UCLIBC_MJN3_ONLY__
  2352. #warning for pending we only want the weight... _not_ the rule
  2353. #endif
  2354. if (!cs->weightidx) { /* ignore */
  2355. continue;
  2356. }
  2357. PROCESS_WEIGHT:
  2358. assert(cs->weightidx);
  2359. if (((unsigned int)(cs->weightidx - UI_IDX)) <= (INVAL_IDX-UI_IDX)) {
  2360. if (cs->weightidx == UI_IDX) {
  2361. cs->weight = cs->ui_weight;
  2362. }
  2363. return;
  2364. }
  2365. assert(cs->weightidx != WEIGHT_MASK);
  2366. if (cs->weightidx == DITTO_IDX) { /* want the weight of the current collating item */
  2367. TRACE(("doing ditto\n"));
  2368. w = CUR_COLLATE->index2weight[cs->colitem -1];
  2369. } else if (cs->weightidx <= CUR_COLLATE->max_col_index) { /* normal */
  2370. TRACE(("doing normal\n"));
  2371. w = CUR_COLLATE->index2weight[cs->weightidx -1];
  2372. } else { /* a string */
  2373. TRACE(("doing string\n"));
  2374. assert(!(cs->weightidx & RULE_MASK));
  2375. /* note: iso14561 allows null string here */
  2376. p = CUR_COLLATE->weightstr + (cs->weightidx - (CUR_COLLATE->max_col_index + 2));
  2377. if (*p & WEIGHT_MASK) {
  2378. r = 0;
  2379. do {
  2380. assert(r < MAX_PENDING);
  2381. cs->ci_pending[r++] = *p++;
  2382. } while (*p & WEIGHT_MASK);
  2383. cs->cip = cs->ci_pending;
  2384. }
  2385. continue;
  2386. }
  2387. cs->weight = w;
  2388. return;
  2389. } while (1);
  2390. }
  2391. int __XL(wcscoll) (const Wchar *s0, const Wchar *s1 __LOCALE_PARAM )
  2392. {
  2393. col_state_t ws[2];
  2394. int pass;
  2395. if (!CUR_COLLATE->num_weights) { /* C locale */
  2396. #ifdef WANT_WIDE
  2397. return wcscmp(s0, s1);
  2398. #else /* WANT_WIDE */
  2399. return strcmp(s0, s1);
  2400. #endif /* WANT_WIDE */
  2401. }
  2402. pass = 0;
  2403. do { /* loop through the weights levels */
  2404. init_col_state(ws, s0);
  2405. init_col_state(ws+1, s1);
  2406. do { /* loop through the strings */
  2407. /* for each string, get the next weight */
  2408. next_weight(ws, pass __LOCALE_ARG );
  2409. next_weight(ws+1, pass __LOCALE_ARG );
  2410. TRACE(("w0=%lu w1=%lu\n",
  2411. (unsigned long) ws[0].weight,
  2412. (unsigned long) ws[1].weight));
  2413. if (ws[0].weight != ws[1].weight) {
  2414. return ws[0].weight - ws[1].weight;
  2415. }
  2416. } while (ws[0].weight);
  2417. } while (++pass < CUR_COLLATE->num_weights);
  2418. return 0;
  2419. }
  2420. #ifdef WANT_WIDE
  2421. size_t __XL(wcsxfrm)(wchar_t *__restrict ws1, const wchar_t *__restrict ws2,
  2422. size_t n __LOCALE_PARAM )
  2423. {
  2424. col_state_t cs;
  2425. size_t count;
  2426. int pass;
  2427. if (!CUR_COLLATE->num_weights) { /* C locale */
  2428. return __wcslcpy(ws1, ws2, n);
  2429. }
  2430. #ifdef __UCLIBC_MJN3_ONLY__
  2431. #warning handle empty string as a special case
  2432. #endif
  2433. count = pass = 0;
  2434. do { /* loop through the weights levels */
  2435. init_col_state(&cs, ws2);
  2436. do { /* loop through the string */
  2437. next_weight(&cs, pass __LOCALE_ARG );
  2438. TRACE(("weight=%lu (%#lx)\n", (unsigned long) cs.weight, (unsigned long) cs.weight));
  2439. if (count < n) {
  2440. ws1[count] = cs.weight +1;
  2441. }
  2442. ++count;
  2443. TRACE(("--------------------------------------------\n"));
  2444. } while (cs.weight);
  2445. if (count <= n) { /* overwrite the trailing 0 end-of-pass marker */
  2446. ws1[count-1] = 1;
  2447. }
  2448. TRACE(("-------------------- pass %d --------------------\n", pass));
  2449. } while (++pass < CUR_COLLATE->num_weights);
  2450. if (count <= n) { /* oops... change it back */
  2451. ws1[count-1] = 0;
  2452. }
  2453. return count-1;
  2454. }
  2455. #else /* WANT_WIDE */
  2456. static const unsigned long bound[] = {
  2457. 1UL << 7,
  2458. 1UL << 11,
  2459. 1UL << 16,
  2460. 1UL << 21,
  2461. 1UL << 26,
  2462. };
  2463. static unsigned char first[] = {
  2464. 0x0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
  2465. };
  2466. /* Use an extension of UTF-8 to store a 32 bit val in max 6 bytes. */
  2467. static size_t store(unsigned char *s, size_t count, size_t n, __uwchar_t weight)
  2468. {
  2469. int i, r;
  2470. i = 0;
  2471. do {
  2472. if (weight < bound[i]) {
  2473. break;
  2474. }
  2475. } while (++i < sizeof(bound)/sizeof(bound[0]));
  2476. r = i+1;
  2477. if (i + count < n) {
  2478. s += count;
  2479. s[0] = first[i];
  2480. while (i) {
  2481. s[i] = 0x80 | (weight & 0x3f);
  2482. weight >>= 6;
  2483. --i;
  2484. }
  2485. s[0] |= weight;
  2486. }
  2487. return r;
  2488. }
  2489. size_t __XL(strxfrm)(char *__restrict ws1, const char *__restrict ws2, size_t n
  2490. __LOCALE_PARAM )
  2491. {
  2492. col_state_t cs;
  2493. size_t count, inc;
  2494. int pass;
  2495. if (!CUR_COLLATE->num_weights) { /* C locale */
  2496. return strlcpy(ws1, ws2, n);
  2497. }
  2498. #ifdef __UCLIBC_MJN3_ONLY__
  2499. #warning handle empty string as a special case
  2500. #endif
  2501. inc = count = pass = 0;
  2502. do { /* loop through the weights levels */
  2503. init_col_state(&cs, ws2);
  2504. do { /* loop through the string */
  2505. next_weight(&cs, pass __LOCALE_ARG );
  2506. TRACE(("weight=%lu (%#lx)\n", (unsigned long) cs.weight, (unsigned long) cs.weight));
  2507. inc = store((unsigned char *)ws1, count, n, cs.weight + 1);
  2508. count += inc;
  2509. TRACE(("--------------------------------------------\n"));
  2510. } while (cs.weight);
  2511. /* overwrite the trailing 0 end-of-pass marker */
  2512. assert(inc == 1);
  2513. if (count <= n) {
  2514. ws1[count-1] = 1;
  2515. }
  2516. TRACE(("-------------------- pass %d --------------------\n", pass));
  2517. } while (++pass < CUR_COLLATE->num_weights);
  2518. if (count <= n) { /* oops... change it back */
  2519. ws1[count-1] = 0;
  2520. }
  2521. return count-1;
  2522. }
  2523. #endif /* WANT_WIDE */
  2524. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  2525. #endif /* defined(L_strxfrm) || defined(L_strxfrm_l) || defined(L_wcsxfrm) || defined(L_wcsxfrm_l) */
  2526. #endif /* __LOCALE_C_ONLY */
  2527. /**********************************************************************/