wstring.c 60 KB

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