wstring.c 67 KB

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