wstring.c 66 KB

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