wstring.c 61 KB

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