wstring.c 73 KB

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