wstring.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  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. #warning c only
  569. #ifdef L_wcscmp
  570. weak_alias(wcscmp,wcscoll);
  571. #else /* L_wcscmp */
  572. weak_alias(strcmp,strcoll);
  573. #endif /* L_wcscmp */
  574. #endif /* __LOCALE_C_ONLY */
  575. int Wstrcmp(register const Wchar *s1, register const Wchar *s2)
  576. {
  577. #ifdef WANT_WIDE
  578. while (*((Wuchar *)s1) == *((Wuchar *)s2)) {
  579. if (!*s1++) {
  580. return 0;
  581. }
  582. ++s2;
  583. }
  584. return (*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1;
  585. #else
  586. int r;
  587. while (((r = ((int)(*((Wuchar *)s1))) - *((Wuchar *)s2++))
  588. == 0) && *s1++);
  589. return r;
  590. #endif
  591. }
  592. #endif
  593. /**********************************************************************/
  594. #ifdef L_wcsncmp
  595. #define L_strncmp
  596. #define Wstrncmp wcsncmp
  597. #else
  598. #define Wstrncmp strncmp
  599. #endif
  600. #ifdef L_strncmp
  601. int Wstrncmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  602. {
  603. #ifdef WANT_WIDE
  604. while (n && (*((Wuchar *)s1) == *((Wuchar *)s2))) {
  605. if (!*s1++) {
  606. return 0;
  607. }
  608. ++s2;
  609. --n;
  610. }
  611. return (n == 0) ? 0 : ((*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1);
  612. #else
  613. int r = 0;
  614. while (n--
  615. && ((r = ((int)(*((unsigned char *)s1))) - *((unsigned char *)s2++))
  616. == 0)
  617. && *s1++);
  618. return r;
  619. #endif
  620. }
  621. #endif
  622. /**********************************************************************/
  623. #ifdef L_wmemchr
  624. #define L_memchr
  625. #define Wmemchr wmemchr
  626. #else
  627. #define Wmemchr memchr
  628. #endif
  629. #ifdef L_memchr
  630. Wvoid *Wmemchr(const Wvoid *s, Wint c, size_t n)
  631. {
  632. register const Wuchar *r = (const Wuchar *) s;
  633. #ifdef __BCC__
  634. /* bcc can optimize the counter if it thinks it is a pointer... */
  635. register const char *np = (const char *) n;
  636. #else
  637. #define np n
  638. #endif
  639. while (np) {
  640. if (*r == ((Wuchar)c)) {
  641. return (Wvoid *) r; /* silence the warning */
  642. }
  643. ++r;
  644. --np;
  645. }
  646. return NULL;
  647. }
  648. #undef np
  649. #endif
  650. /**********************************************************************/
  651. #ifdef L_wcschr
  652. #define L_strchr
  653. #define Wstrchr wcschr
  654. #else
  655. #define Wstrchr strchr
  656. #endif
  657. #ifdef L_strchr
  658. #ifndef L_wcschr
  659. weak_alias(strchr,index);
  660. #endif
  661. Wchar *Wstrchr(register const Wchar *s, Wint c)
  662. {
  663. do {
  664. if (*s == ((Wchar)c)) {
  665. return (Wchar *) s; /* silence the warning */
  666. }
  667. } while (*s++);
  668. return NULL;
  669. }
  670. #endif
  671. /**********************************************************************/
  672. #ifdef L_wcscspn
  673. #define L_strcspn
  674. #define Wstrcspn wcscspn
  675. #else
  676. #define Wstrcspn strcspn
  677. #endif
  678. #ifdef L_strcspn
  679. size_t Wstrcspn(const Wchar *s1, const Wchar *s2)
  680. {
  681. register const Wchar *s;
  682. register const Wchar *p;
  683. for ( s=s1 ; *s ; s++ ) {
  684. for ( p=s2 ; *p ; p++ ) {
  685. if (*p == *s) goto done;
  686. }
  687. }
  688. done:
  689. return s - s1;
  690. }
  691. #endif
  692. /**********************************************************************/
  693. #ifdef L_wcspbrk
  694. #define L_strpbrk
  695. #define Wstrpbrk wcspbrk
  696. #else
  697. #define Wstrpbrk strpbrk
  698. #endif
  699. #ifdef L_strpbrk
  700. Wchar *Wstrpbrk(const Wchar *s1, const Wchar *s2)
  701. {
  702. register const Wchar *s;
  703. register const Wchar *p;
  704. for ( s=s1 ; *s ; s++ ) {
  705. for ( p=s2 ; *p ; p++ ) {
  706. if (*p == *s) return (Wchar *) s; /* silence the warning */
  707. }
  708. }
  709. return NULL;
  710. }
  711. #endif
  712. /**********************************************************************/
  713. #ifdef L_wcsrchr
  714. #define L_strrchr
  715. #define Wstrrchr wcsrchr
  716. #else
  717. #define Wstrrchr strrchr
  718. #endif
  719. #ifdef L_strrchr
  720. #ifndef L_wcsrchr
  721. weak_alias(strrchr,rindex);
  722. #endif
  723. Wchar *Wstrrchr(register const Wchar *s, Wint c)
  724. {
  725. register const Wchar *p;
  726. p = NULL;
  727. do {
  728. if (*s == (Wchar) c) {
  729. p = s;
  730. }
  731. } while (*s++);
  732. return (Wchar *) p; /* silence the warning */
  733. }
  734. #endif
  735. /**********************************************************************/
  736. #ifdef L_wcsspn
  737. #define L_strspn
  738. #define Wstrspn wcsspn
  739. #else
  740. #define Wstrspn strspn
  741. #endif
  742. #ifdef L_strspn
  743. size_t Wstrspn(const Wchar *s1, const Wchar *s2)
  744. {
  745. register const Wchar *s = s1;
  746. register const Wchar *p = s2;
  747. while (*p) {
  748. if (*p++ == *s) {
  749. ++s;
  750. p = s2;
  751. }
  752. }
  753. return s - s1;
  754. }
  755. #endif
  756. /**********************************************************************/
  757. #ifdef L_wcsstr
  758. #define L_strstr
  759. #define Wstrstr wcsstr
  760. #else
  761. #define Wstrstr strstr
  762. #endif
  763. #ifdef L_strstr
  764. /* NOTE: This is the simple-minded O(len(s1) * len(s2)) worst-case approach. */
  765. #ifdef L_wcsstr
  766. weak_alias(wcsstr,wcswcs);
  767. #endif
  768. Wchar *Wstrstr(const Wchar *s1, const Wchar *s2)
  769. {
  770. register const Wchar *s = s1;
  771. register const Wchar *p = s2;
  772. do {
  773. if (!*p) {
  774. return (Wchar *) s1;;
  775. }
  776. if (*p == *s) {
  777. ++p;
  778. ++s;
  779. } else {
  780. p = s2;
  781. if (!*s) {
  782. return NULL;
  783. }
  784. s = ++s1;
  785. }
  786. } while (1);
  787. }
  788. #endif
  789. /**********************************************************************/
  790. #undef Wstrspn
  791. #undef Wstrpbrk
  792. #ifdef L_wcstok
  793. #define L_strtok_r
  794. #define Wstrtok_r wcstok
  795. #define Wstrspn wcsspn
  796. #define Wstrpbrk wcspbrk
  797. #else
  798. #define Wstrtok_r strtok_r
  799. #define Wstrspn strspn
  800. #define Wstrpbrk strpbrk
  801. #endif
  802. #ifdef L_strtok_r
  803. Wchar *Wstrtok_r(Wchar * __restrict s1, const Wchar * __restrict s2,
  804. Wchar ** __restrict next_start)
  805. {
  806. register Wchar *s;
  807. register Wchar *p;
  808. #if 1
  809. if (((s = s1) != NULL) || ((s = *next_start) != NULL)) {
  810. if (*(s += Wstrspn(s, s2))) {
  811. if ((p = Wstrpbrk(s, s2)) != NULL) {
  812. *p++ = 0;
  813. }
  814. } else {
  815. p = s = NULL;
  816. }
  817. *next_start = p;
  818. }
  819. return s;
  820. #else
  821. if (!(s = s1)) {
  822. s = *next_start;
  823. }
  824. if (s && *(s += Wstrspn(s, s2))) {
  825. if (*(p = s + Wstrcspn(s, s2))) {
  826. *p++ = 0;
  827. }
  828. *next_start = p;
  829. return s;
  830. }
  831. return NULL; /* TODO: set *next_start = NULL for safety? */
  832. #endif
  833. }
  834. #endif
  835. /**********************************************************************/
  836. /* #ifdef L_wcstok */
  837. /* #define L_strtok */
  838. /* #define Wstrtok wcstok */
  839. /* #define Wstrtok_r wcstok_r */
  840. /* #else */
  841. /* #define Wstrtok strtok */
  842. /* #define Wstrtok_r strtok_r */
  843. /* #endif */
  844. #ifdef L_strtok
  845. #define Wstrtok strtok
  846. #define Wstrtok_r strtok_r
  847. Wchar *Wstrtok(Wchar * __restrict s1, const Wchar * __restrict s2)
  848. {
  849. static Wchar *next_start; /* Initialized to 0 since in bss. */
  850. return Wstrtok_r(s1, s2, &next_start);
  851. }
  852. #endif
  853. /**********************************************************************/
  854. #ifdef L_wmemset
  855. #define L_memset
  856. #define Wmemset wmemset
  857. #else
  858. #define Wmemset memset
  859. #endif
  860. #ifdef L_memset
  861. Wvoid *Wmemset(Wvoid *s, Wint c, size_t n)
  862. {
  863. register Wuchar *p = (Wuchar *) s;
  864. #ifdef __BCC__
  865. /* bcc can optimize the counter if it thinks it is a pointer... */
  866. register const char *np = (const char *) n;
  867. #else
  868. #define np n
  869. #endif
  870. while (np) {
  871. *p++ = (Wuchar) c;
  872. --np;
  873. }
  874. return s;
  875. }
  876. #undef np
  877. #endif
  878. /**********************************************************************/
  879. #ifdef L_wcslen
  880. #define L_strlen
  881. #define Wstrlen wcslen
  882. #else
  883. #define Wstrlen strlen
  884. #endif
  885. #ifdef L_strlen
  886. size_t Wstrlen(const Wchar *s)
  887. {
  888. register const Wchar *p;
  889. for (p=s ; *p ; p++);
  890. return p - s;
  891. }
  892. #endif
  893. /**********************************************************************/
  894. /* ANSI/ISO end here */
  895. /**********************************************************************/
  896. #ifdef L_ffs
  897. int ffs(int i)
  898. {
  899. #if 1
  900. /* inlined binary search method */
  901. char n = 1;
  902. #if UINT_MAX == 0xffffU
  903. /* nothing to do here -- just trying to avoiding possible problems */
  904. #elif UINT_MAX == 0xffffffffU
  905. if (!(i & 0xffff)) {
  906. n += 16;
  907. i >>= 16;
  908. }
  909. #else
  910. #error ffs needs rewriting!
  911. #endif
  912. if (!(i & 0xff)) {
  913. n += 8;
  914. i >>= 8;
  915. }
  916. if (!(i & 0x0f)) {
  917. n += 4;
  918. i >>= 4;
  919. }
  920. if (!(i & 0x03)) {
  921. n += 2;
  922. i >>= 2;
  923. }
  924. return (i) ? (n + ((i+1) & 0x01)) : 0;
  925. #else
  926. /* linear search -- slow, but small */
  927. int n;
  928. for (n = 0 ; i ; ++n) {
  929. i >>= 1;
  930. }
  931. return n;
  932. #endif
  933. }
  934. #endif
  935. /**********************************************************************/
  936. #ifdef L_wcscasecmp
  937. #define L_strcasecmp
  938. #define Wstrcasecmp wcscasecmp
  939. #else
  940. #define Wstrcasecmp strcasecmp
  941. #endif
  942. #ifdef L_strcasecmp
  943. int Wstrcasecmp(register const Wchar *s1, register const Wchar *s2)
  944. {
  945. #ifdef WANT_WIDE
  946. while ((*s1 == *s2) || (towlower(*s1) == towlower(*s2))) {
  947. if (!*s1++) {
  948. return 0;
  949. }
  950. ++s2;
  951. }
  952. return (((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1;
  953. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  954. #else
  955. int r = 0;
  956. while ( ((s1 == s2) ||
  957. !(r = ((int)( tolower(*((Wuchar *)s1))))
  958. - tolower(*((Wuchar *)s2))))
  959. && (++s2, *s1++));
  960. return r;
  961. #endif
  962. }
  963. #endif
  964. /**********************************************************************/
  965. #ifdef L_wcsncasecmp
  966. #define L_strncasecmp
  967. #define Wstrncasecmp wcsncasecmp
  968. #else
  969. #define Wstrncasecmp strncasecmp
  970. #endif
  971. #ifdef L_strncasecmp
  972. int Wstrncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  973. {
  974. #ifdef WANT_WIDE
  975. while (n && ((*s1 == *s2) || (towlower(*s1) == towlower(*s2)))) {
  976. if (!*s1++) {
  977. return 0;
  978. }
  979. ++s2;
  980. --n;
  981. }
  982. return (n == 0)
  983. ? 0
  984. : ((((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1);
  985. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  986. #else
  987. int r = 0;
  988. while ( n
  989. && ((s1 == s2) ||
  990. !(r = ((int)( tolower(*((unsigned char *)s1))))
  991. - tolower(*((unsigned char *)s2))))
  992. && (--n, ++s2, *s1++));
  993. return r;
  994. #endif
  995. }
  996. #endif
  997. /**********************************************************************/
  998. #ifdef L_wcsnlen
  999. #define L_strnlen
  1000. #define Wstrnlen wcsnlen
  1001. #else
  1002. #define Wstrnlen strnlen
  1003. #endif
  1004. #ifdef L_strnlen
  1005. size_t Wstrnlen(const Wchar *s, size_t max)
  1006. {
  1007. register const Wchar *p = s;
  1008. #ifdef __BCC__
  1009. /* bcc can optimize the counter if it thinks it is a pointer... */
  1010. register const char *maxp = (const char *) max;
  1011. #else
  1012. #define maxp max
  1013. #endif
  1014. while (maxp && *p) {
  1015. ++p;
  1016. --maxp;
  1017. }
  1018. return p - s;
  1019. }
  1020. #undef maxp
  1021. #endif
  1022. /**********************************************************************/
  1023. /* No wide analog. */
  1024. #ifdef L_memccpy
  1025. void *memccpy(void * __restrict s1, const void * __restrict s2, int c, size_t n)
  1026. {
  1027. register char *r1 = s1;
  1028. register const char *r2 = s2;
  1029. while (n-- && (((unsigned char)(*r1++ = *r2++)) != ((unsigned char) c)));
  1030. return (n == (size_t) -1) ? NULL : r1;
  1031. }
  1032. #endif
  1033. /**********************************************************************/
  1034. #undef Wstrlen
  1035. #undef Wstrcpy
  1036. #ifdef L_wcsdup
  1037. #define L_strdup
  1038. #define Wstrdup wcsdup
  1039. #define Wstrlen wcslen
  1040. #define Wstrcpy wcscpy
  1041. #else
  1042. #define Wstrdup strdup
  1043. #define Wstrlen strlen
  1044. #define Wstrcpy strcpy
  1045. #endif
  1046. #ifdef L_strdup
  1047. Wchar *Wstrdup(register const Wchar *s1)
  1048. {
  1049. register Wchar *s;
  1050. if ((s = malloc((Wstrlen(s1) + 1) * sizeof(Wchar))) != NULL) {
  1051. Wstrcpy(s, s1);
  1052. }
  1053. return s;
  1054. }
  1055. #endif
  1056. /**********************************************************************/
  1057. #ifdef L_strerror
  1058. char *strerror(int errnum)
  1059. {
  1060. static char buf[_SYS_ERRMSG_MAXLEN];
  1061. _susv3_strerror_r(errnum, buf, sizeof(buf));
  1062. return buf;
  1063. }
  1064. #endif
  1065. /**********************************************************************/
  1066. /* SUSv3 functions. */
  1067. /**********************************************************************/
  1068. #ifdef L__susv3_strerror_r
  1069. #if defined(__alpha__) || defined(__mips__) || defined(__sparc__)
  1070. static const unsigned char estridx[] = {
  1071. 0, /* success is always 0 */
  1072. EPERM,
  1073. ENOENT,
  1074. ESRCH,
  1075. EINTR,
  1076. EIO,
  1077. ENXIO,
  1078. E2BIG,
  1079. ENOEXEC,
  1080. EBADF,
  1081. ECHILD,
  1082. EAGAIN,
  1083. ENOMEM,
  1084. EACCES,
  1085. EFAULT,
  1086. ENOTBLK,
  1087. EBUSY,
  1088. EEXIST,
  1089. EXDEV,
  1090. ENODEV,
  1091. ENOTDIR,
  1092. EISDIR,
  1093. EINVAL,
  1094. ENFILE,
  1095. EMFILE,
  1096. ENOTTY,
  1097. ETXTBSY,
  1098. EFBIG,
  1099. ENOSPC,
  1100. ESPIPE,
  1101. EROFS,
  1102. EMLINK,
  1103. EPIPE,
  1104. EDOM,
  1105. ERANGE,
  1106. EDEADLK,
  1107. ENAMETOOLONG,
  1108. ENOLCK,
  1109. ENOSYS,
  1110. ENOTEMPTY,
  1111. ELOOP,
  1112. 0,
  1113. ENOMSG,
  1114. EIDRM,
  1115. ECHRNG,
  1116. EL2NSYNC,
  1117. EL3HLT,
  1118. EL3RST,
  1119. ELNRNG,
  1120. EUNATCH,
  1121. ENOCSI,
  1122. EL2HLT,
  1123. EBADE,
  1124. EBADR,
  1125. EXFULL,
  1126. ENOANO,
  1127. EBADRQC,
  1128. EBADSLT,
  1129. 0,
  1130. EBFONT,
  1131. ENOSTR,
  1132. ENODATA,
  1133. ETIME,
  1134. ENOSR,
  1135. ENONET,
  1136. ENOPKG,
  1137. EREMOTE,
  1138. ENOLINK,
  1139. EADV,
  1140. ESRMNT,
  1141. ECOMM,
  1142. EPROTO,
  1143. EMULTIHOP,
  1144. EDOTDOT,
  1145. EBADMSG,
  1146. EOVERFLOW,
  1147. ENOTUNIQ,
  1148. EBADFD,
  1149. EREMCHG,
  1150. ELIBACC,
  1151. ELIBBAD,
  1152. ELIBSCN,
  1153. ELIBMAX,
  1154. ELIBEXEC,
  1155. EILSEQ,
  1156. ERESTART,
  1157. ESTRPIPE,
  1158. EUSERS,
  1159. ENOTSOCK,
  1160. EDESTADDRREQ,
  1161. EMSGSIZE,
  1162. EPROTOTYPE,
  1163. ENOPROTOOPT,
  1164. EPROTONOSUPPORT,
  1165. ESOCKTNOSUPPORT,
  1166. EOPNOTSUPP,
  1167. EPFNOSUPPORT,
  1168. EAFNOSUPPORT,
  1169. EADDRINUSE,
  1170. EADDRNOTAVAIL,
  1171. ENETDOWN,
  1172. ENETUNREACH,
  1173. ENETRESET,
  1174. ECONNABORTED,
  1175. ECONNRESET,
  1176. ENOBUFS,
  1177. EISCONN,
  1178. ENOTCONN,
  1179. ESHUTDOWN,
  1180. ETOOMANYREFS,
  1181. ETIMEDOUT,
  1182. ECONNREFUSED,
  1183. EHOSTDOWN,
  1184. EHOSTUNREACH,
  1185. EALREADY,
  1186. EINPROGRESS,
  1187. ESTALE,
  1188. EUCLEAN,
  1189. ENOTNAM,
  1190. ENAVAIL,
  1191. EISNAM,
  1192. EREMOTEIO,
  1193. #ifdef __mips__
  1194. 0, /* mips has an outrageous value for this... */
  1195. #else
  1196. EDQUOT,
  1197. #endif
  1198. ENOMEDIUM,
  1199. EMEDIUMTYPE,
  1200. #if defined(__mips__) || defined(__sparc__)
  1201. EDEADLOCK,
  1202. #endif
  1203. };
  1204. #endif
  1205. int _susv3_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1206. {
  1207. register char *s;
  1208. int i, retval;
  1209. char buf[_SYS_ERRMSG_MAXLEN];
  1210. static const char unknown[14] = {
  1211. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'e', 'r', 'r', 'o', 'r', ' '
  1212. };
  1213. retval = EINVAL;
  1214. #if defined(__alpha__) || defined(__mips__) || defined(__sparc__)
  1215. /* Need to translate errno to string index. */
  1216. for (i = 0 ; i < sizeof(estridx)/sizeof(estridx[0]) ; i++) {
  1217. if (estridx[i] == errnum) {
  1218. goto GOT_ESTRIDX;
  1219. }
  1220. }
  1221. i = INT_MAX; /* Failed, but may need to check mips special case. */
  1222. #ifdef __mips__
  1223. if (errnum == EDQUOT) { /* Deal with large EDQUOT value on mips */
  1224. i = 122;
  1225. }
  1226. #endif /* __mips__ */
  1227. GOT_ESTRIDX:
  1228. #else
  1229. /* No errno to string index translation needed. */
  1230. i = errnum;
  1231. #endif
  1232. if (((unsigned int) i) < _SYS_NERR) {
  1233. /* Trade time for space. This function should rarely be called
  1234. * so rather than keeping an array of pointers for the different
  1235. * messages, just run through the buffer until we find the
  1236. * correct string. */
  1237. for (s = (char *) _string_syserrmsgs ; i ; ++s) {
  1238. if (!*s) {
  1239. --i;
  1240. }
  1241. }
  1242. if (*s) { /* Make sure we have an actual message. */
  1243. retval = 0;
  1244. goto GOT_MESG;
  1245. }
  1246. }
  1247. s = _int10tostr(buf+sizeof(buf)-1, errnum) - sizeof(unknown);
  1248. memcpy(s, unknown, sizeof(unknown));
  1249. GOT_MESG:
  1250. if (!strerrbuf) { /* SUSv3 */
  1251. buflen = 0;
  1252. }
  1253. i = strlen(s) + 1;
  1254. if (i > buflen) {
  1255. i = buflen;
  1256. retval = ERANGE;
  1257. }
  1258. if (i) {
  1259. memcpy(strerrbuf, s, i);
  1260. strerrbuf[i-1] = 0; /* In case buf was too small. */
  1261. }
  1262. if (retval) {
  1263. __set_errno(retval);
  1264. }
  1265. return retval;
  1266. }
  1267. #endif
  1268. /**********************************************************************/
  1269. /* GNU extension functions. */
  1270. /**********************************************************************/
  1271. #ifdef L__glibc_strerror_r
  1272. weak_alias(_glibc_strerror_r,__strerror_r);
  1273. char *_glibc_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1274. {
  1275. _susv3_strerror_r(errnum, strerrbuf, buflen);
  1276. return strerrbuf;
  1277. }
  1278. #endif
  1279. /**********************************************************************/
  1280. #ifdef L_wmempcpy
  1281. #define L_mempcpy
  1282. #define Wmempcpy wmempcpy
  1283. #else
  1284. #define Wmempcpy mempcpy
  1285. #endif
  1286. #ifdef L_mempcpy
  1287. #ifndef L_wmempcpy
  1288. /* uClibc's old string implementation did this to cater to some app. */
  1289. weak_alias(mempcpy,__mempcpy);
  1290. #endif
  1291. Wvoid *Wmempcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  1292. {
  1293. register Wchar *r1 = s1;
  1294. register const Wchar *r2 = s2;
  1295. #ifdef __BCC__
  1296. while (n--) {
  1297. *r1++ = *r2++;
  1298. }
  1299. #else
  1300. while (n) {
  1301. *r1++ = *r2++;
  1302. --n;
  1303. }
  1304. #endif
  1305. return r1;
  1306. }
  1307. #endif
  1308. /**********************************************************************/
  1309. #ifdef L_memrchr
  1310. void *memrchr(const void *s, int c, size_t n)
  1311. {
  1312. register const unsigned char *r;
  1313. #ifdef __BCC__
  1314. /* bcc can optimize the counter if it thinks it is a pointer... */
  1315. register const char *np = (const char *) n;
  1316. #else
  1317. #define np n
  1318. #endif
  1319. r = ((unsigned char *)s) + ((size_t) np);
  1320. while (np) {
  1321. if (*--r == ((unsigned char)c)) {
  1322. return (void *) r; /* silence the warning */
  1323. }
  1324. --np;
  1325. }
  1326. return NULL;
  1327. }
  1328. #undef np
  1329. #endif
  1330. /**********************************************************************/
  1331. #ifdef L_wcpcpy
  1332. #define L_stpcpy
  1333. #define Wstpcpy wcpcpy
  1334. #else
  1335. #define Wstpcpy stpcpy
  1336. #endif
  1337. #ifdef L_stpcpy
  1338. Wchar *Wstpcpy(register Wchar * __restrict s1, const Wchar * __restrict s2)
  1339. {
  1340. #ifdef __BCC__
  1341. do {
  1342. *s1 = *s2++;
  1343. } while (*s1++ != 0);
  1344. #else
  1345. while ( (*s1++ = *s2++) != 0 );
  1346. #endif
  1347. return s1 - 1;
  1348. }
  1349. #endif
  1350. /**********************************************************************/
  1351. #ifdef L_wcpncpy
  1352. #define L_stpncpy
  1353. #define Wstpncpy wcpncpy
  1354. #else
  1355. #define Wstpncpy stpncpy
  1356. #endif
  1357. #ifdef L_stpncpy
  1358. Wchar *Wstpncpy(register Wchar * __restrict s1,
  1359. register const Wchar * __restrict s2,
  1360. size_t n)
  1361. {
  1362. Wchar *s = s1;
  1363. const Wchar *p = s2;
  1364. #ifdef __BCC__
  1365. while (n--) {
  1366. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1367. ++s;
  1368. }
  1369. return s1 + (s2 - p);
  1370. #else
  1371. while (n) {
  1372. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1373. ++s;
  1374. --n;
  1375. }
  1376. return s1 + (s2 - p);
  1377. #endif
  1378. }
  1379. #endif
  1380. /**********************************************************************/
  1381. #ifdef L_bzero
  1382. void bzero(void *s, size_t n)
  1383. {
  1384. register unsigned char *p = s;
  1385. #ifdef __BCC__
  1386. /* bcc can optimize the counter if it thinks it is a pointer... */
  1387. register const char *np = (const char *) n;
  1388. #else
  1389. #define np n
  1390. #endif
  1391. while (np) {
  1392. *p++ = 0;
  1393. --np;
  1394. }
  1395. }
  1396. #undef np
  1397. #endif
  1398. /**********************************************************************/
  1399. #ifdef L_bcopy
  1400. void bcopy(const void *s2, void *s1, size_t n)
  1401. {
  1402. #if 1
  1403. memmove(s1, s2, n);
  1404. #else
  1405. #ifdef __BCC__
  1406. register char *s;
  1407. register const char *p;
  1408. s = s1;
  1409. p = s2;
  1410. if (p >= s) {
  1411. while (n--) {
  1412. *s++ = *p++;
  1413. }
  1414. } else {
  1415. s += n;
  1416. p += n;
  1417. while (n--) {
  1418. *--s = *--p;
  1419. }
  1420. }
  1421. #else
  1422. register char *s;
  1423. register const char *p;
  1424. s = s1;
  1425. p = s2;
  1426. if (p >= s) {
  1427. while (n) {
  1428. *s++ = *p++;
  1429. --n;
  1430. }
  1431. } else {
  1432. while (n) {
  1433. --n;
  1434. s[n] = p[n];
  1435. }
  1436. }
  1437. #endif
  1438. #endif
  1439. }
  1440. #endif
  1441. /**********************************************************************/
  1442. #ifdef L_strcasestr
  1443. char *strcasestr(const char *s1, const char *s2)
  1444. {
  1445. register const char *s = s1;
  1446. register const char *p = s2;
  1447. #if 1
  1448. do {
  1449. if (!*p) {
  1450. return (char *) s1;;
  1451. }
  1452. if ((*p == *s)
  1453. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1454. ) {
  1455. ++p;
  1456. ++s;
  1457. } else {
  1458. p = s2;
  1459. if (!*s) {
  1460. return NULL;
  1461. }
  1462. s = ++s1;
  1463. }
  1464. } while (1);
  1465. #else
  1466. while (*p && *s) {
  1467. if ((*p == *s)
  1468. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1469. ) {
  1470. ++p;
  1471. ++s;
  1472. } else {
  1473. p = s2;
  1474. s = ++s1;
  1475. }
  1476. }
  1477. return (*p) ? NULL : (char *) s1;
  1478. #endif
  1479. }
  1480. #endif
  1481. /**********************************************************************/
  1482. #ifdef L_strndup
  1483. char *strndup(register const char *s1, size_t n)
  1484. {
  1485. register char *s;
  1486. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  1487. if ((s = malloc(n + 1)) != NULL) {
  1488. memcpy(s, s1, n);
  1489. s[n] = 0;
  1490. }
  1491. return s;
  1492. }
  1493. #endif
  1494. /**********************************************************************/
  1495. #ifdef L_strsep
  1496. char *strsep(char ** __restrict s1, const char * __restrict s2)
  1497. {
  1498. register char *s = *s1;
  1499. register char *p;
  1500. #if 1
  1501. p = NULL;
  1502. if (s && *s && (p = strpbrk(s, s2))) {
  1503. *p++ = 0;
  1504. }
  1505. #else
  1506. if (s && *s && *(p = s + strcspn(s, s2))) {
  1507. *p++ = 0;
  1508. } else {
  1509. p = NULL;
  1510. }
  1511. #endif
  1512. *s1 = p;
  1513. return s;
  1514. }
  1515. #endif
  1516. /**********************************************************************/
  1517. #ifdef L_wcschrnul
  1518. #define L_strchrnul
  1519. #define __Wstrchrnul __wcschrnul
  1520. #define Wstrchrnul wcschrnul
  1521. #else
  1522. #define __Wstrchrnul __strchrnul
  1523. #define Wstrchrnul strchrnul
  1524. #endif
  1525. #ifdef L_strchrnul
  1526. extern Wchar *__Wstrchrnul(register const Wchar *s, Wint c);
  1527. weak_alias(__Wstrchrnul, Wstrchrnul);
  1528. Wchar *__Wstrchrnul(register const Wchar *s, Wint c)
  1529. {
  1530. --s;
  1531. while (*++s && (*s != ((Wchar)c)));
  1532. return (Wchar *) s;
  1533. }
  1534. #endif
  1535. /**********************************************************************/
  1536. #ifdef L_rawmemchr
  1537. void *rawmemchr(const void *s, int c)
  1538. {
  1539. register const unsigned char *r = s;
  1540. while (*r != ((unsigned char)c)) ++r;
  1541. return (void *) r; /* silence the warning */
  1542. }
  1543. #endif
  1544. /**********************************************************************/
  1545. #ifdef L_basename
  1546. char *basename(const char *path)
  1547. {
  1548. register const char *s;
  1549. register const char *p;
  1550. p = s = path;
  1551. while (*s) {
  1552. if (*s++ == '/') {
  1553. p = s;
  1554. }
  1555. }
  1556. return (char *) p;
  1557. }
  1558. #endif
  1559. /**********************************************************************/
  1560. #ifdef L___xpg_basename
  1561. char *__xpg_basename(register char *path)
  1562. {
  1563. static const char null_or_empty[] = ".";
  1564. char *first;
  1565. register char *last;
  1566. first = (char *) null_or_empty;
  1567. if (path && *path) {
  1568. first = path;
  1569. last = path - 1;
  1570. do {
  1571. if ((*path != '/') && (path > ++last)) {
  1572. last = first = path;
  1573. }
  1574. } while (*++path);
  1575. if (*first == '/') {
  1576. last = first;
  1577. }
  1578. last[1] = 0;
  1579. }
  1580. return first;
  1581. }
  1582. #endif
  1583. /**********************************************************************/
  1584. #ifdef L_dirname
  1585. char *dirname(char *path)
  1586. {
  1587. static const char null_or_empty_or_noslash[] = ".";
  1588. register char *s;
  1589. register char *last;
  1590. char *first;
  1591. last = s = path;
  1592. if (s != NULL) {
  1593. LOOP:
  1594. while (*s && (*s != '/')) ++s;
  1595. first = s;
  1596. while (*s == '/') ++s;
  1597. if (*s) {
  1598. last = first;
  1599. goto LOOP;
  1600. }
  1601. if (last == path) {
  1602. if (*last != '/') {
  1603. goto DOT;
  1604. }
  1605. if ((*++last == '/') && (last[1] == 0)) {
  1606. ++last;
  1607. }
  1608. }
  1609. *last = 0;
  1610. return path;
  1611. }
  1612. DOT:
  1613. return (char *) null_or_empty_or_noslash;
  1614. }
  1615. #endif
  1616. /**********************************************************************/
  1617. #ifdef L_strlcat
  1618. /* OpenBSD function:
  1619. * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst.
  1620. * Returns strlen(src) + strlen({original} dst), so truncation occurred if the
  1621. * return val is >= n.
  1622. * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is
  1623. * taken as n. */
  1624. size_t strlcat(register char *__restrict dst,
  1625. register const char *__restrict src,
  1626. size_t n)
  1627. {
  1628. size_t len;
  1629. char dummy[1];
  1630. len = 0;
  1631. while (1) {
  1632. if (len >= n) {
  1633. dst = dummy;
  1634. break;
  1635. }
  1636. if (!*dst) {
  1637. break;
  1638. }
  1639. ++dst;
  1640. ++len;
  1641. }
  1642. while ((*dst = *src) != 0) {
  1643. if (++len < n) {
  1644. ++dst;
  1645. }
  1646. ++src;
  1647. }
  1648. return len;
  1649. }
  1650. #endif
  1651. /**********************************************************************/
  1652. #ifdef WANT_WIDE
  1653. extern size_t __wcslcpy(wchar_t *__restrict dst,
  1654. const wchar_t *__restrict src,
  1655. size_t n);
  1656. #endif
  1657. #ifdef L___wcslcpy
  1658. #define L_strlcpy
  1659. #define Wstrlcpy __wcslcpy
  1660. #ifdef __LOCALE_C_ONLY
  1661. weak_alias(__wcslcpy,wcsxfrm);
  1662. #endif
  1663. #endif
  1664. #ifdef L_strlcpy
  1665. #ifndef L___wcslcpy
  1666. #define Wstrlcpy strlcpy
  1667. #ifdef __LOCALE_C_ONLY
  1668. weak_alias(strlcpy,strxfrm);
  1669. #endif
  1670. #endif
  1671. /* OpenBSD function:
  1672. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  1673. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  1674. size_t Wstrlcpy(register Wchar *__restrict dst,
  1675. register const Wchar *__restrict src,
  1676. size_t n)
  1677. {
  1678. const Wchar *src0 = src;
  1679. Wchar dummy[1];
  1680. if (!n) {
  1681. dst = dummy;
  1682. } else {
  1683. --n;
  1684. }
  1685. while ((*dst = *src) != 0) {
  1686. if (n) {
  1687. --n;
  1688. ++dst;
  1689. }
  1690. ++src;
  1691. }
  1692. return src - src0;
  1693. }
  1694. #endif
  1695. /**********************************************************************/
  1696. #ifdef L__string_syssigmsgs
  1697. const char _string_syssigmsgs[] = {
  1698. /* 0: 0, 1 */ "\0"
  1699. /* 1: 1, 7 */ "Hangup\0"
  1700. /* 2: 8, 10 */ "Interrupt\0"
  1701. /* 3: 18, 5 */ "Quit\0"
  1702. /* 4: 23, 20 */ "Illegal instruction\0"
  1703. /* 5: 43, 22 */ "Trace/breakpoint trap\0"
  1704. /* 6: 65, 8 */ "Aborted\0"
  1705. /* 7: 73, 10 */ "Bus error\0"
  1706. /* 8: 83, 25 */ "Floating point exception\0"
  1707. /* 9: 108, 7 */ "Killed\0"
  1708. /* 10: 115, 22 */ "User defined signal 1\0"
  1709. /* 11: 137, 19 */ "Segmentation fault\0"
  1710. /* 12: 156, 22 */ "User defined signal 2\0"
  1711. /* 13: 178, 12 */ "Broken pipe\0"
  1712. /* 14: 190, 12 */ "Alarm clock\0"
  1713. /* 15: 202, 11 */ "Terminated\0"
  1714. /* 16: 213, 12 */ "Stack fault\0"
  1715. /* 17: 225, 13 */ "Child exited\0"
  1716. /* 18: 238, 10 */ "Continued\0"
  1717. /* 19: 248, 17 */ "Stopped (signal)\0"
  1718. /* 20: 265, 8 */ "Stopped\0"
  1719. /* 21: 273, 20 */ "Stopped (tty input)\0"
  1720. /* 22: 293, 21 */ "Stopped (tty output)\0"
  1721. /* 23: 314, 21 */ "Urgent I/O condition\0"
  1722. /* 24: 335, 24 */ "CPU time limit exceeded\0"
  1723. /* 25: 359, 25 */ "File size limit exceeded\0"
  1724. /* 26: 384, 22 */ "Virtual timer expired\0"
  1725. /* 27: 406, 24 */ "Profiling timer expired\0"
  1726. /* 28: 430, 15 */ "Window changed\0"
  1727. /* 29: 445, 13 */ "I/O possible\0"
  1728. /* 30: 458, 14 */ "Power failure\0"
  1729. /* 31: 472, 16 */ "Bad system call"
  1730. };
  1731. #endif
  1732. /**********************************************************************/
  1733. #ifdef L_sys_siglist
  1734. const char *const sys_siglist[_NSIG] = {
  1735. NULL,
  1736. _string_syssigmsgs + 1,
  1737. _string_syssigmsgs + 8,
  1738. _string_syssigmsgs + 18,
  1739. _string_syssigmsgs + 23,
  1740. _string_syssigmsgs + 43,
  1741. _string_syssigmsgs + 65,
  1742. _string_syssigmsgs + 73,
  1743. _string_syssigmsgs + 83,
  1744. _string_syssigmsgs + 108,
  1745. _string_syssigmsgs + 115,
  1746. _string_syssigmsgs + 137,
  1747. _string_syssigmsgs + 156,
  1748. _string_syssigmsgs + 178,
  1749. _string_syssigmsgs + 190,
  1750. _string_syssigmsgs + 202,
  1751. _string_syssigmsgs + 213,
  1752. _string_syssigmsgs + 225,
  1753. _string_syssigmsgs + 238,
  1754. _string_syssigmsgs + 248,
  1755. _string_syssigmsgs + 265,
  1756. _string_syssigmsgs + 273,
  1757. _string_syssigmsgs + 293,
  1758. _string_syssigmsgs + 314,
  1759. _string_syssigmsgs + 335,
  1760. _string_syssigmsgs + 359,
  1761. _string_syssigmsgs + 384,
  1762. _string_syssigmsgs + 406,
  1763. _string_syssigmsgs + 430,
  1764. _string_syssigmsgs + 445,
  1765. _string_syssigmsgs + 458,
  1766. _string_syssigmsgs + 472,
  1767. NULL,
  1768. NULL,
  1769. NULL,
  1770. NULL,
  1771. NULL,
  1772. NULL,
  1773. NULL,
  1774. NULL,
  1775. NULL,
  1776. NULL,
  1777. NULL,
  1778. NULL,
  1779. NULL,
  1780. NULL,
  1781. NULL,
  1782. NULL,
  1783. NULL,
  1784. NULL,
  1785. NULL,
  1786. NULL,
  1787. NULL,
  1788. NULL,
  1789. NULL,
  1790. NULL,
  1791. NULL,
  1792. NULL,
  1793. NULL,
  1794. NULL,
  1795. NULL,
  1796. NULL,
  1797. NULL,
  1798. NULL,
  1799. };
  1800. #endif
  1801. /**********************************************************************/
  1802. #ifdef L_strsignal
  1803. /* TODO: make a threadsafe version? */
  1804. char *strsignal(int signum)
  1805. {
  1806. register char *s;
  1807. int i;
  1808. static char buf[_STRSIGNAL_BUFSIZE];
  1809. static const char unknown[15] = {
  1810. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  1811. };
  1812. if (((unsigned int) signum) < _SYS_NSIG) {
  1813. /* Trade time for space. This function should rarely be called
  1814. * so rather than keeping an array of pointers for the different
  1815. * messages, just run through the buffer until we find the
  1816. * correct string. */
  1817. for (s = (char *) _string_syssigmsgs, i = signum ; i ; ++s) {
  1818. if (!*s) {
  1819. --i;
  1820. }
  1821. }
  1822. if (*s) { /* Make sure we have an actual message. */
  1823. goto DONE;
  1824. }
  1825. }
  1826. s = _int10tostr(buf+sizeof(buf)-1, signum) - sizeof(unknown);
  1827. memcpy(s, unknown, sizeof(unknown));
  1828. DONE:
  1829. return s;
  1830. }
  1831. #endif
  1832. /**********************************************************************/
  1833. #ifdef L_psignal
  1834. /* TODO: make this threadsafe with a reentrant version of strsignal? */
  1835. void psignal(int signum, register const char *message)
  1836. {
  1837. /* If the program is calling psignal, it's a safe bet that printf and
  1838. * friends are used as well. It is also possible that the calling
  1839. * program could buffer stderr, or reassign it. */
  1840. register const char *sep;
  1841. sep = ": ";
  1842. if (!(message && *message)) { /* Caller did not supply a prefix message */
  1843. message = (sep += 2); /* or passed an empty string. */
  1844. }
  1845. #if 1
  1846. fprintf(stderr, "%s%s%s\n", message, sep, strsignal(signum));
  1847. #else
  1848. /* Note: Assumes stderr not closed or buffered. */
  1849. __STDIO_THREADLOCK(stderr);
  1850. _stdio_fdout(STDERR_FILENO, message, sep, strsignal(signum));
  1851. __STDIO_THREADUNLOCK(stderr);
  1852. #endif
  1853. }
  1854. #endif
  1855. /**********************************************************************/
  1856. #ifndef __LOCALE_C_ONLY
  1857. #ifdef L_strxfrm
  1858. #ifndef WANT_WIDE
  1859. #error WANT_WIDE should be defined for L_strxfrm
  1860. #endif
  1861. #ifdef L_wcsxfrm
  1862. #error L_wcsxfrm already defined for L_strxfrm
  1863. #endif
  1864. #define wcscoll strcoll
  1865. #define L_wcsxfrm
  1866. #undef WANT_WIDE
  1867. #undef Wvoid
  1868. #undef Wchar
  1869. #undef Wuchar
  1870. #undef Wint
  1871. #define Wchar char
  1872. #endif /* L_strxfrm */
  1873. #ifdef L_wcsxfrm
  1874. #define CUR_COLLATE (&__global_locale.collate)
  1875. #define MAX_PENDING 8
  1876. typedef struct {
  1877. const Wchar *s;
  1878. const Wchar *eob; /* end of backward */
  1879. __uwchar_t weight;
  1880. __uwchar_t ui_weight; /* undefined or invalid */
  1881. int colitem;
  1882. int weightidx;
  1883. int rule;
  1884. size_t position;
  1885. /* should be wchar_t. if wchar < 0 do EILSEQ? */
  1886. __uwchar_t *cip;
  1887. __uwchar_t ci_pending[MAX_PENDING]; /* nul-terminated */
  1888. char *back_buf;
  1889. char *bbe; /* end of back_buf (actual last... not 1 past end) */
  1890. char *bp; /* ptr into backbuf, NULL if not in backward mode */
  1891. char ibb[128];
  1892. size_t bb_size;
  1893. int ru_pushed;
  1894. } col_state_t;
  1895. #define WEIGHT_MASK 0x3fffU
  1896. #define RULE_MASK 0xc000U
  1897. #define RULE_FORWARD (1 << 14)
  1898. #define RULE_POSITION (1 << 15)
  1899. #define UI_IDX (WEIGHT_MASK-6)
  1900. #define POSIT_IDX (WEIGHT_MASK-5)
  1901. #define RANGE_IDX (WEIGHT_MASK-4)
  1902. #define UNDEF_IDX (WEIGHT_MASK-3)
  1903. #define INVAL_IDX (WEIGHT_MASK-2)
  1904. #define DITTO_IDX (WEIGHT_MASK-1)
  1905. #undef TRACE
  1906. #if 0
  1907. #define TRACE(X) printf##X
  1908. #else
  1909. #define TRACE(X) ((void)0)
  1910. #endif
  1911. static int lookup(wchar_t wc)
  1912. {
  1913. unsigned int sc, n, i0, i1;
  1914. if (((__uwchar_t) wc) > 0xffffU) {
  1915. return 0;
  1916. }
  1917. sc = wc & CUR_COLLATE->ti_mask;
  1918. wc >>= CUR_COLLATE->ti_shift;
  1919. n = wc & CUR_COLLATE->ii_mask;
  1920. wc >>= CUR_COLLATE->ii_shift;
  1921. i0 = CUR_COLLATE->wcs2colidt_tbl[wc];
  1922. i0 <<= CUR_COLLATE->ii_shift;
  1923. i1 = CUR_COLLATE->wcs2colidt_tbl[CUR_COLLATE->ii_len + i0 + n];
  1924. i1 <<= CUR_COLLATE->ti_shift;
  1925. return CUR_COLLATE->wcs2colidt_tbl[CUR_COLLATE->ii_len + CUR_COLLATE->ti_len + i1 + sc];
  1926. }
  1927. static void init_col_state(col_state_t *cs, const Wchar *wcs)
  1928. {
  1929. memset(cs, 0, sizeof(col_state_t));
  1930. cs->s = wcs;
  1931. cs->bp = cs->back_buf = cs->ibb;
  1932. cs->bb_size = 128;
  1933. cs->bbe = cs->back_buf + (cs->bb_size -1);
  1934. }
  1935. static void next_weight(col_state_t *cs, int pass)
  1936. {
  1937. int r, w, ru, ri, popping_backup_stack;
  1938. ssize_t n;
  1939. const uint16_t *p;
  1940. #ifdef WANT_WIDE
  1941. #define WC (*cs->s)
  1942. #define N (1)
  1943. #else /* WANT_WIDE */
  1944. mbstate_t mbstate;
  1945. wchar_t WC;
  1946. size_t n0, nx;
  1947. #define N n0
  1948. mbstate.mask = 0;
  1949. #endif /* WANT_WIDE */
  1950. do {
  1951. if (cs->ru_pushed) {
  1952. ru = cs->ru_pushed;
  1953. TRACE(("ru_pushed = %d\n", ru));
  1954. cs->ru_pushed = 0;
  1955. goto POSITION_SKIP;
  1956. }
  1957. #ifdef __UCLIBC_MJN3_ONLY__
  1958. #warning should we walk pendings backwards?
  1959. #endif
  1960. if (cs->cip) { /* possible pending weight */
  1961. if ((r = *(cs->cip++)) == 0) {
  1962. cs->cip = NULL;
  1963. continue;
  1964. }
  1965. cs->weightidx = r & WEIGHT_MASK;
  1966. assert(cs->weightidx);
  1967. /* assert(cs->weightidx != WEIGHT_MASK); */
  1968. } else { /* get the next collation item from the string */
  1969. TRACE(("clearing popping flag\n"));
  1970. popping_backup_stack = 0;
  1971. IGNORE_LOOP:
  1972. /* keep first pos as 0 for a sentinal */
  1973. if (*cs->bp) { /* pending backward chars */
  1974. POP_BACKUP:
  1975. popping_backup_stack = 1;
  1976. TRACE(("setting popping flag\n"));
  1977. n = 0;
  1978. if (*cs->bp > 0) { /* singles pending */
  1979. cs->s -= 1;
  1980. if ((*cs->bp -= 1) == 0) {
  1981. cs->bp -= 1;
  1982. }
  1983. } else { /* last was a multi */
  1984. cs->s += *cs->bp;
  1985. cs->bp -= 1;
  1986. }
  1987. } else if (!*cs->s) { /* not in backward mode and end of string */
  1988. cs->weight = 0;
  1989. return;
  1990. } else {
  1991. cs->position += 1;
  1992. }
  1993. BACK_LOOP:
  1994. #ifdef WANT_WIDE
  1995. n = 1;
  1996. cs->colitem = r = lookup(*cs->s);
  1997. #else /* WANT_WIDE */
  1998. n = n0 = mbrtowc(&WC, cs->s, SIZE_MAX, &mbstate);
  1999. if (n < 0) {
  2000. __set_errno(EILSEQ);
  2001. cs->weight = 0;
  2002. return;
  2003. }
  2004. cs->colitem = r = lookup(WC);
  2005. #endif /* WANT_WIDE */
  2006. TRACE((" r=%d WC=%#lx\n", r, (unsigned long)(WC)));
  2007. if (r > CUR_COLLATE->max_col_index) { /* starting char for one or more sequences */
  2008. p = CUR_COLLATE->multistart_tbl;
  2009. p += p[r-CUR_COLLATE->max_col_index -1];
  2010. do {
  2011. n = N;
  2012. r = *p++;
  2013. do {
  2014. if (!*p) { /* found it */
  2015. cs->colitem = r;
  2016. TRACE((" found multi %d\n", n));
  2017. goto FOUND;
  2018. }
  2019. #ifdef WANT_WIDE
  2020. /* the lookup check here is safe since we're assured that *p is a valid colidx */
  2021. if (!cs->s[n] || (lookup(cs->s[n]) != *p)) {
  2022. do {} while (*p++);
  2023. break;
  2024. }
  2025. ++p;
  2026. ++n;
  2027. #else /* WANT_WIDE */
  2028. if (cs->s[n]) {
  2029. nx = mbrtowc(&WC, cs->s + n, SIZE_MAX, &mbstate);
  2030. if (nx < 0) {
  2031. __set_errno(EILSEQ);
  2032. cs->weight = 0;
  2033. return;
  2034. }
  2035. }
  2036. if (!cs->s[n] || (lookup(WC) != *p)) {
  2037. do {} while (*p++);
  2038. break;
  2039. }
  2040. ++p;
  2041. n += nx;
  2042. #endif /* WANT_WIDE */
  2043. } while (1);
  2044. } while (1);
  2045. } else if (r == 0) { /* illegal, undefined, or part of a range */
  2046. if ((CUR_COLLATE->range_count)
  2047. #ifdef __UCLIBC_MJN3_ONLY__
  2048. #warning .. need to introduce range as a collating item?
  2049. #endif
  2050. && (((__uwchar_t)(WC - CUR_COLLATE->range_low)) <= CUR_COLLATE->range_count)
  2051. ) { /* part of a range */
  2052. /* Note: cs->colitem = 0 already. */
  2053. TRACE((" found range\n"));
  2054. ru = CUR_COLLATE->ruletable[CUR_COLLATE->range_rule_offset*CUR_COLLATE->MAX_WEIGHTS + pass];
  2055. assert((ru & WEIGHT_MASK) != DITTO_IDX);
  2056. if ((ru & WEIGHT_MASK) == WEIGHT_MASK) {
  2057. ru = (ru & RULE_MASK) | RANGE_IDX;
  2058. cs->weight = CUR_COLLATE->range_base_weight + (WC - CUR_COLLATE->range_low);
  2059. }
  2060. goto RANGE_SKIP_TO;
  2061. } else if (((__uwchar_t)(WC)) <= 0x7fffffffUL) { /* legal but undefined */
  2062. UNDEFINED:
  2063. /* Note: cs->colitem = 0 already. */
  2064. ri = CUR_COLLATE->undefined_idx;
  2065. assert(ri != 0); /* implicit undefined isn't supported */
  2066. TRACE((" found explicit UNDEFINED\n"));
  2067. #ifdef __UCLIBC_MJN3_ONLY__
  2068. #warning right now single weight locales do not support ..
  2069. #endif
  2070. if (CUR_COLLATE->num_weights == 1) {
  2071. TRACE((" single weight UNDEFINED\n"));
  2072. cs->weightidx = RANGE_IDX;
  2073. cs->weight = ri;
  2074. cs->s += n;
  2075. goto PROCESS_WEIGHT;
  2076. }
  2077. ri = CUR_COLLATE->index2ruleidx[ri - 1];
  2078. ru = CUR_COLLATE->ruletable[ri * CUR_COLLATE->MAX_WEIGHTS + pass];
  2079. assert((ru & WEIGHT_MASK) != WEIGHT_MASK); /* TODO: handle ".." */
  2080. if ((ru & WEIGHT_MASK) == DITTO_IDX) {
  2081. cs->colitem = CUR_COLLATE->undefined_idx;
  2082. }
  2083. goto RANGE_SKIP_TO;
  2084. } else { /* illegal */
  2085. TRACE((" found illegal\n"));
  2086. __set_errno(EINVAL);
  2087. /* We put all illegals in the same equiv class with maximal weight,
  2088. * and ignore them after the first pass. */
  2089. if (pass > 0) {
  2090. cs->s += n;
  2091. goto IGNORE_LOOP;
  2092. }
  2093. ru = (RULE_FORWARD | RANGE_IDX);
  2094. cs->weight = 0xffffU;
  2095. goto RANGE_SKIP_TO;
  2096. }
  2097. } else if (CUR_COLLATE->num_weights == 1) {
  2098. TRACE((" single weight\n"));
  2099. cs->weightidx = RANGE_IDX;
  2100. cs->weight = cs->colitem;
  2101. cs->s += n;
  2102. goto PROCESS_WEIGHT;
  2103. } else {
  2104. TRACE((" normal\n"));
  2105. }
  2106. /* if we get here, it is a normal char either singlely weighted, undefined, or in a range */
  2107. FOUND:
  2108. ri = CUR_COLLATE->index2ruleidx[cs->colitem - 1];
  2109. TRACE((" ri=%d ", ri));
  2110. #ifdef __UCLIBC_MJN3_ONLY__
  2111. #warning make sure this is correct
  2112. #endif
  2113. if (!ri) {
  2114. TRACE(("NOT IN THIS LOCALE\n"));
  2115. goto UNDEFINED;
  2116. }
  2117. ru = CUR_COLLATE->ruletable[ri * CUR_COLLATE->MAX_WEIGHTS + pass];
  2118. RANGE_SKIP_TO:
  2119. #ifdef __UCLIBC_MJN3_ONLY__
  2120. #warning ignoreables probably should not interrupt backwards processing, but this is wrong
  2121. #endif
  2122. /* if (!(ru & WEIGHT_MASK)) { */
  2123. /* TRACE(("IGNORE\n")); */
  2124. /* cs->s += n; */
  2125. /* continue; */
  2126. /* } */
  2127. TRACE((" rule = %#x weight = %#x popping = %d s = %p eob = %p\n",
  2128. ru & RULE_MASK, ru & WEIGHT_MASK, popping_backup_stack,
  2129. cs->s, cs->eob));
  2130. /* now we need to check if we're going backwards... */
  2131. if (!popping_backup_stack) {
  2132. if (!(ru & RULE_MASK)) { /* backward */
  2133. TRACE(("backwards\n"));
  2134. assert(cs->bp <= cs->bbe);
  2135. if (cs->bp == cs->bbe) {
  2136. if (cs->back_buf == cs->ibb) { /* was using internal buffer */
  2137. cs->bp = malloc(cs->bb_size + 128);
  2138. if (!cs->bp) {
  2139. __set_errno(ENOMEM);
  2140. #ifdef __UCLIBC_MJN3_ONLY__
  2141. #warning what to do here?
  2142. #endif
  2143. cs->weight = 0;
  2144. return;
  2145. }
  2146. memcpy(cs->bp, cs->back_buf, cs->bb_size);
  2147. } else {
  2148. cs->bp = realloc(cs->back_buf, cs->bb_size + 128);
  2149. if (!cs->bp) {
  2150. __set_errno(ENOMEM);
  2151. #ifdef __UCLIBC_MJN3_ONLY__
  2152. #warning what to do here?
  2153. #endif
  2154. cs->weight = 0;
  2155. return;
  2156. }
  2157. }
  2158. cs->bb_size += 128;
  2159. cs->bbe = cs->bp + (cs->bbe - cs->back_buf);
  2160. cs->back_buf = cs->bp;
  2161. cs->bp = cs->bbe;
  2162. }
  2163. if (n==1) { /* single char */
  2164. if (*cs->bp && (((unsigned char)(*cs->bp)) < CHAR_MAX)) {
  2165. *cs->bp += 1; /* increment last single's count */
  2166. } else { /* last was a multi, or just starting */
  2167. if (!cs->bp) {
  2168. cs->bp = cs->back_buf;
  2169. } else {
  2170. assert(cs->bp < cs->bbe);
  2171. ++cs->bp;
  2172. }
  2173. *cs->bp = 1;
  2174. }
  2175. } else { /* multichar */
  2176. assert(n>1);
  2177. assert(cs->bp < cs->bbe);
  2178. *++cs->bp = -n;
  2179. }
  2180. cs->s += n;
  2181. if (*cs->s) {
  2182. goto BACK_LOOP;
  2183. }
  2184. /* end-of-string so start popping */
  2185. cs->eob = cs->s;
  2186. TRACE(("popping\n"));
  2187. goto POP_BACKUP;
  2188. } else if (*cs->bp) { /* was going backward but this element isn't */
  2189. /* discard current and use previous backward element */
  2190. assert(!cs->cip);
  2191. cs->eob = cs->s;
  2192. TRACE(("popping\n"));
  2193. goto POP_BACKUP;
  2194. } else { /* was and still going forward */
  2195. TRACE(("forwards\n"));
  2196. if ((ru & (RULE_POSITION|WEIGHT_MASK)) > RULE_POSITION) {
  2197. assert(ru & WEIGHT_MASK);
  2198. cs->ru_pushed = ru;
  2199. cs->weight = cs->position;
  2200. #ifdef __UCLIBC_MJN3_ONLY__
  2201. #warning devel code
  2202. #endif
  2203. cs->position = 0; /* reset to reduce size for strcoll? */
  2204. cs->s += n;
  2205. cs->weightidx = RANGE_IDX;
  2206. goto PROCESS_WEIGHT;
  2207. }
  2208. }
  2209. } else { /* popping backwards stack */
  2210. TRACE(("popping (continued)\n"));
  2211. if (!*cs->bp) {
  2212. cs->s = cs->eob;
  2213. }
  2214. cs->s -= n;
  2215. }
  2216. cs->s += n;
  2217. POSITION_SKIP:
  2218. cs->weightidx = ru & WEIGHT_MASK;
  2219. cs->rule = ru & RULE_MASK;
  2220. }
  2221. #ifdef __UCLIBC_MJN3_ONLY__
  2222. #warning for pending we only want the weight... _not_ the rule
  2223. #endif
  2224. if (!cs->weightidx) { /* ignore */
  2225. continue;
  2226. }
  2227. PROCESS_WEIGHT:
  2228. assert(cs->weightidx);
  2229. if (((unsigned int)(cs->weightidx - UI_IDX)) <= (INVAL_IDX-UI_IDX)) {
  2230. if (cs->weightidx == UI_IDX) {
  2231. cs->weight = cs->ui_weight;
  2232. }
  2233. return;
  2234. }
  2235. assert(cs->weightidx != WEIGHT_MASK);
  2236. if (cs->weightidx == DITTO_IDX) { /* want the weight of the current collating item */
  2237. TRACE(("doing ditto\n"));
  2238. w = CUR_COLLATE->index2weight[cs->colitem -1];
  2239. } else if (cs->weightidx <= CUR_COLLATE->max_col_index) { /* normal */
  2240. TRACE(("doing normal\n"));
  2241. w = CUR_COLLATE->index2weight[cs->weightidx -1];
  2242. } else { /* a string */
  2243. TRACE(("doing string\n"));
  2244. assert(!(cs->weightidx & RULE_MASK));
  2245. /* note: iso14561 allows null string here */
  2246. p = CUR_COLLATE->weightstr + (cs->weightidx - (CUR_COLLATE->max_col_index + 2));
  2247. if (*p & WEIGHT_MASK) {
  2248. r = 0;
  2249. do {
  2250. assert(r < MAX_PENDING);
  2251. cs->ci_pending[r++] = *p++;
  2252. } while (*p & WEIGHT_MASK);
  2253. cs->cip = cs->ci_pending;
  2254. }
  2255. continue;
  2256. }
  2257. cs->weight = w;
  2258. return;
  2259. } while (1);
  2260. }
  2261. int wcscoll (const Wchar *s0, const Wchar *s1)
  2262. {
  2263. col_state_t ws[2];
  2264. int pass;
  2265. if (!CUR_COLLATE->num_weights) { /* C locale */
  2266. #ifdef WANT_WIDE
  2267. return wcscmp(s0, s1);
  2268. #else /* WANT_WIDE */
  2269. return strcmp(s0, s1);
  2270. #endif /* WANT_WIDE */
  2271. }
  2272. pass = 0;
  2273. do { /* loop through the weights levels */
  2274. init_col_state(ws, s0);
  2275. init_col_state(ws+1, s1);
  2276. do { /* loop through the strings */
  2277. /* for each string, get the next weight */
  2278. next_weight(ws, pass);
  2279. next_weight(ws+1, pass);
  2280. TRACE(("w0=%lu w1=%lu\n",
  2281. (unsigned long) ws[0].weight,
  2282. (unsigned long) ws[1].weight));
  2283. if (ws[0].weight != ws[1].weight) {
  2284. return ws[0].weight - ws[1].weight;
  2285. }
  2286. } while (ws[0].weight);
  2287. } while (++pass < CUR_COLLATE->num_weights);
  2288. return 0;
  2289. }
  2290. #ifdef WANT_WIDE
  2291. size_t wcsxfrm(wchar_t *__restrict ws1, const wchar_t *__restrict ws2, size_t n)
  2292. {
  2293. col_state_t cs;
  2294. size_t count;
  2295. int pass;
  2296. if (!CUR_COLLATE->num_weights) { /* C locale */
  2297. return __wcslcpy(ws1, ws2, n);
  2298. }
  2299. #ifdef __UCLIBC_MJN3_ONLY__
  2300. #warning handle empty string as a special case
  2301. #endif
  2302. count = pass = 0;
  2303. do { /* loop through the weights levels */
  2304. init_col_state(&cs, ws2);
  2305. do { /* loop through the string */
  2306. next_weight(&cs, pass);
  2307. TRACE(("weight=%lu (%#lx)\n", (unsigned long) cs.weight, (unsigned long) cs.weight));
  2308. if (count < n) {
  2309. ws1[count] = cs.weight +1;
  2310. }
  2311. ++count;
  2312. TRACE(("--------------------------------------------\n"));
  2313. } while (cs.weight);
  2314. if (count <= n) { /* overwrite the trailing 0 end-of-pass marker */
  2315. ws1[count-1] = 1;
  2316. }
  2317. TRACE(("-------------------- pass %d --------------------\n", pass));
  2318. } while (++pass < CUR_COLLATE->num_weights);
  2319. if (count <= n) { /* oops... change it back */
  2320. ws1[count-1] = 0;
  2321. }
  2322. return count-1;
  2323. }
  2324. #else /* WANT_WIDE */
  2325. static const unsigned long bound[] = {
  2326. 1UL << 7,
  2327. 1UL << 11,
  2328. 1UL << 16,
  2329. 1UL << 21,
  2330. 1UL << 26,
  2331. };
  2332. static unsigned char first[] = {
  2333. 0x0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
  2334. };
  2335. /* Use an extension of UTF-8 to store a 32 bit val in max 6 bytes. */
  2336. static size_t store(unsigned char *s, size_t count, size_t n, __uwchar_t weight)
  2337. {
  2338. int i, r;
  2339. i = 0;
  2340. do {
  2341. if (weight < bound[i]) {
  2342. break;
  2343. }
  2344. } while (++i < sizeof(bound)/sizeof(bound[0]));
  2345. r = i+1;
  2346. if (i + count < n) {
  2347. s += count;
  2348. s[0] = first[i];
  2349. while (i) {
  2350. s[i] = 0x80 | (weight & 0x3f);
  2351. weight >>= 6;
  2352. --i;
  2353. }
  2354. s[0] |= weight;
  2355. }
  2356. return r;
  2357. }
  2358. size_t strxfrm(char *__restrict ws1, const char *__restrict ws2, size_t n)
  2359. {
  2360. col_state_t cs;
  2361. size_t count, inc;
  2362. int pass;
  2363. if (!CUR_COLLATE->num_weights) { /* C locale */
  2364. return strlcpy(ws1, ws2, n);
  2365. }
  2366. #ifdef __UCLIBC_MJN3_ONLY__
  2367. #warning handle empty string as a special case
  2368. #endif
  2369. inc = count = pass = 0;
  2370. do { /* loop through the weights levels */
  2371. init_col_state(&cs, ws2);
  2372. do { /* loop through the string */
  2373. next_weight(&cs, pass);
  2374. TRACE(("weight=%lu (%#lx)\n", (unsigned long) cs.weight, (unsigned long) cs.weight));
  2375. inc = store((unsigned char *)ws1, count, n, cs.weight + 1);
  2376. count += inc;
  2377. TRACE(("--------------------------------------------\n"));
  2378. } while (cs.weight);
  2379. /* overwrite the trailing 0 end-of-pass marker */
  2380. assert(inc == 1);
  2381. if (count <= n) {
  2382. ws1[count-1] = 1;
  2383. }
  2384. TRACE(("-------------------- pass %d --------------------\n", pass));
  2385. } while (++pass < CUR_COLLATE->num_weights);
  2386. if (count <= n) { /* oops... change it back */
  2387. ws1[count-1] = 0;
  2388. }
  2389. return count-1;
  2390. }
  2391. #endif /* WANT_WIDE */
  2392. #endif /* wcscoll */
  2393. #endif /* __LOCALE_C_ONLY */
  2394. /**********************************************************************/