wstring.c 70 KB

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