wstring.c 60 KB

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