wstring.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. #define _STDIO_UTILITY
  28. #define _GNU_SOURCE
  29. #include <string.h>
  30. #include <strings.h>
  31. #include <stdio.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #include <errno.h>
  36. #include <signal.h>
  37. #ifdef WANT_WIDE
  38. #include <wchar.h>
  39. #include <wctype.h>
  40. #include <locale.h>
  41. #define Wvoid wchar_t
  42. #define Wchar wchar_t
  43. #define Wuchar __uwchar_t
  44. #define Wint wchar_t
  45. #else
  46. #define Wvoid void
  47. #define Wchar char
  48. typedef unsigned char __string_uchar_t;
  49. #define Wuchar __string_uchar_t
  50. #define Wint int
  51. #endif
  52. /**********************************************************************/
  53. /* NOTE: If we ever do internationalized syserr messages, this will
  54. * have to be changed! */
  55. #define _SYS_NERR 125
  56. #define _SYS_ERRMSG_MAXLEN 50
  57. extern const char _string_syserrmsgs[];
  58. #define _SYS_NSIG 32
  59. #define _SYS_SIGMSG_MAXLEN 25
  60. extern const char _string_syssigmsgs[];
  61. #if _SYS_ERRMSG_MAXLEN < __UIM_BUFLEN_INT + 14
  62. #define _STRERROR_BUFSIZE (__UIM_BUFLEN_INT + 14)
  63. #else
  64. #define _STRERROR_BUFSIZE _SYS_ERRMSG_MAXLEN
  65. #endif
  66. #if _SYS_SIGMSG_MAXLEN < __UIM_BUFLEN_INT + 14
  67. #define _STRSIGNAL_BUFSIZE (__UIM_BUFLEN_INT + 14)
  68. #else
  69. #define _STRSIGNAL_BUFSIZE _SYS_SIGMSG_MAXLEN
  70. #endif
  71. /**********************************************************************/
  72. #ifdef L__string_syserrmsgs
  73. const char _string_syserrmsgs[] = {
  74. /* 0: 0, 8 */ "Success\0"
  75. /* 1: 8, 24 */ "Operation not permitted\0"
  76. /* 2: 32, 26 */ "No such file or directory\0"
  77. /* 3: 58, 16 */ "No such process\0"
  78. /* 4: 74, 24 */ "Interrupted system call\0"
  79. /* 5: 98, 19 */ "Input/output error\0"
  80. /* 6: 117, 26 */ "No such device or address\0"
  81. /* 7: 143, 23 */ "Argument list too long\0"
  82. /* 8: 166, 18 */ "Exec format error\0"
  83. /* 9: 184, 20 */ "Bad file descriptor\0"
  84. /* 10: 204, 19 */ "No child processes\0"
  85. /* 11: 223, 33 */ "Resource temporarily unavailable\0"
  86. /* 12: 256, 23 */ "Cannot allocate memory\0"
  87. /* 13: 279, 18 */ "Permission denied\0"
  88. /* 14: 297, 12 */ "Bad address\0"
  89. /* 15: 309, 22 */ "Block device required\0"
  90. /* 16: 331, 24 */ "Device or resource busy\0"
  91. /* 17: 355, 12 */ "File exists\0"
  92. /* 18: 367, 26 */ "Invalid cross-device link\0"
  93. /* 19: 393, 15 */ "No such device\0"
  94. /* 20: 408, 16 */ "Not a directory\0"
  95. /* 21: 424, 15 */ "Is a directory\0"
  96. /* 22: 439, 17 */ "Invalid argument\0"
  97. /* 23: 456, 30 */ "Too many open files in system\0"
  98. /* 24: 486, 20 */ "Too many open files\0"
  99. /* 25: 506, 31 */ "Inappropriate ioctl for device\0"
  100. /* 26: 537, 15 */ "Text file busy\0"
  101. /* 27: 552, 15 */ "File too large\0"
  102. /* 28: 567, 24 */ "No space left on device\0"
  103. /* 29: 591, 13 */ "Illegal seek\0"
  104. /* 30: 604, 22 */ "Read-only file system\0"
  105. /* 31: 626, 15 */ "Too many links\0"
  106. /* 32: 641, 12 */ "Broken pipe\0"
  107. /* 33: 653, 33 */ "Numerical argument out of domain\0"
  108. /* 34: 686, 30 */ "Numerical result out of range\0"
  109. /* 35: 716, 26 */ "Resource deadlock avoided\0"
  110. /* 36: 742, 19 */ "File name too long\0"
  111. /* 37: 761, 19 */ "No locks available\0"
  112. /* 38: 780, 25 */ "Function not implemented\0"
  113. /* 39: 805, 20 */ "Directory not empty\0"
  114. /* 40: 825, 34 */ "Too many levels of symbolic links\0"
  115. /* 41: 859, 1 */ "\0"
  116. /* 42: 860, 27 */ "No message of desired type\0"
  117. /* 43: 887, 19 */ "Identifier removed\0"
  118. /* 44: 906, 28 */ "Channel number out of range\0"
  119. /* 45: 934, 25 */ "Level 2 not synchronized\0"
  120. /* 46: 959, 15 */ "Level 3 halted\0"
  121. /* 47: 974, 14 */ "Level 3 reset\0"
  122. /* 48: 988, 25 */ "Link number out of range\0"
  123. /* 49: 1013, 29 */ "Protocol driver not attached\0"
  124. /* 50: 1042, 27 */ "No CSI structure available\0"
  125. /* 51: 1069, 15 */ "Level 2 halted\0"
  126. /* 52: 1084, 17 */ "Invalid exchange\0"
  127. /* 53: 1101, 27 */ "Invalid request descriptor\0"
  128. /* 54: 1128, 14 */ "Exchange full\0"
  129. /* 55: 1142, 9 */ "No anode\0"
  130. /* 56: 1151, 21 */ "Invalid request code\0"
  131. /* 57: 1172, 13 */ "Invalid slot\0"
  132. /* 58: 1185, 1 */ "\0"
  133. /* 59: 1186, 21 */ "Bad font file format\0"
  134. /* 60: 1207, 20 */ "Device not a stream\0"
  135. /* 61: 1227, 18 */ "No data available\0"
  136. /* 62: 1245, 14 */ "Timer expired\0"
  137. /* 63: 1259, 25 */ "Out of streams resources\0"
  138. /* 64: 1284, 30 */ "Machine is not on the network\0"
  139. /* 65: 1314, 22 */ "Package not installed\0"
  140. /* 66: 1336, 17 */ "Object is remote\0"
  141. /* 67: 1353, 22 */ "Link has been severed\0"
  142. /* 68: 1375, 16 */ "Advertise error\0"
  143. /* 69: 1391, 14 */ "Srmount error\0"
  144. /* 70: 1405, 28 */ "Communication error on send\0"
  145. /* 71: 1433, 15 */ "Protocol error\0"
  146. /* 72: 1448, 19 */ "Multihop attempted\0"
  147. /* 73: 1467, 19 */ "RFS specific error\0"
  148. /* 74: 1486, 12 */ "Bad message\0"
  149. /* 75: 1498, 38 */ "Value too large for defined data type\0"
  150. /* 76: 1536, 27 */ "Name not unique on network\0"
  151. /* 77: 1563, 29 */ "File descriptor in bad state\0"
  152. /* 78: 1592, 23 */ "Remote address changed\0"
  153. /* 79: 1615, 39 */ "Can not access a needed shared library\0"
  154. /* 80: 1654, 37 */ "Accessing a corrupted shared library\0"
  155. /* 81: 1691, 32 */ ".lib section in a.out corrupted\0"
  156. /* 82: 1723, 48 */ "Attempting to link in too many shared libraries\0"
  157. /* 83: 1771, 38 */ "Cannot exec a shared library directly\0"
  158. /* 84: 1809, 50 */ "Invalid or incomplete multibyte or wide character\0"
  159. /* 85: 1859, 44 */ "Interrupted system call should be restarted\0"
  160. /* 86: 1903, 19 */ "Streams pipe error\0"
  161. /* 87: 1922, 15 */ "Too many users\0"
  162. /* 88: 1937, 31 */ "Socket operation on non-socket\0"
  163. /* 89: 1968, 29 */ "Destination address required\0"
  164. /* 90: 1997, 17 */ "Message too long\0"
  165. /* 91: 2014, 31 */ "Protocol wrong type for socket\0"
  166. /* 92: 2045, 23 */ "Protocol not available\0"
  167. /* 93: 2068, 23 */ "Protocol not supported\0"
  168. /* 94: 2091, 26 */ "Socket type not supported\0"
  169. /* 95: 2117, 24 */ "Operation not supported\0"
  170. /* 96: 2141, 30 */ "Protocol family not supported\0"
  171. /* 97: 2171, 41 */ "Address family not supported by protocol\0"
  172. /* 98: 2212, 23 */ "Address already in use\0"
  173. /* 99: 2235, 32 */ "Cannot assign requested address\0"
  174. /* 100: 2267, 16 */ "Network is down\0"
  175. /* 101: 2283, 23 */ "Network is unreachable\0"
  176. /* 102: 2306, 36 */ "Network dropped connection on reset\0"
  177. /* 103: 2342, 33 */ "Software caused connection abort\0"
  178. /* 104: 2375, 25 */ "Connection reset by peer\0"
  179. /* 105: 2400, 26 */ "No buffer space available\0"
  180. /* 106: 2426, 40 */ "Transport endpoint is already connected\0"
  181. /* 107: 2466, 36 */ "Transport endpoint is not connected\0"
  182. /* 108: 2502, 46 */ "Cannot send after transport endpoint shutdown\0"
  183. /* 109: 2548, 35 */ "Too many references: cannot splice\0"
  184. /* 110: 2583, 21 */ "Connection timed out\0"
  185. /* 111: 2604, 19 */ "Connection refused\0"
  186. /* 112: 2623, 13 */ "Host is down\0"
  187. /* 113: 2636, 17 */ "No route to host\0"
  188. /* 114: 2653, 30 */ "Operation already in progress\0"
  189. /* 115: 2683, 26 */ "Operation now in progress\0"
  190. /* 116: 2709, 22 */ "Stale NFS file handle\0"
  191. /* 117: 2731, 25 */ "Structure needs cleaning\0"
  192. /* 118: 2756, 28 */ "Not a XENIX named type file\0"
  193. /* 119: 2784, 30 */ "No XENIX semaphores available\0"
  194. /* 120: 2814, 21 */ "Is a named type file\0"
  195. /* 121: 2835, 17 */ "Remote I/O error\0"
  196. /* 122: 2852, 20 */ "Disk quota exceeded\0"
  197. /* 123: 2872, 16 */ "No medium found\0"
  198. /* 124: 2888, 18 */ "Wrong medium type"
  199. };
  200. #endif
  201. /**********************************************************************/
  202. #ifdef L_sys_errlist
  203. 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.")
  204. int sys_nerr = _SYS_NERR;
  205. const char *const sys_errlist[] = {
  206. _string_syserrmsgs + 0,
  207. _string_syserrmsgs + 8,
  208. _string_syserrmsgs + 32,
  209. _string_syserrmsgs + 58,
  210. _string_syserrmsgs + 74,
  211. _string_syserrmsgs + 98,
  212. _string_syserrmsgs + 117,
  213. _string_syserrmsgs + 143,
  214. _string_syserrmsgs + 166,
  215. _string_syserrmsgs + 184,
  216. _string_syserrmsgs + 204,
  217. _string_syserrmsgs + 223,
  218. _string_syserrmsgs + 256,
  219. _string_syserrmsgs + 279,
  220. _string_syserrmsgs + 297,
  221. _string_syserrmsgs + 309,
  222. _string_syserrmsgs + 331,
  223. _string_syserrmsgs + 355,
  224. _string_syserrmsgs + 367,
  225. _string_syserrmsgs + 393,
  226. _string_syserrmsgs + 408,
  227. _string_syserrmsgs + 424,
  228. _string_syserrmsgs + 439,
  229. _string_syserrmsgs + 456,
  230. _string_syserrmsgs + 486,
  231. _string_syserrmsgs + 506,
  232. _string_syserrmsgs + 537,
  233. _string_syserrmsgs + 552,
  234. _string_syserrmsgs + 567,
  235. _string_syserrmsgs + 591,
  236. _string_syserrmsgs + 604,
  237. _string_syserrmsgs + 626,
  238. _string_syserrmsgs + 641,
  239. _string_syserrmsgs + 653,
  240. _string_syserrmsgs + 686,
  241. _string_syserrmsgs + 716,
  242. _string_syserrmsgs + 742,
  243. _string_syserrmsgs + 761,
  244. _string_syserrmsgs + 780,
  245. _string_syserrmsgs + 805,
  246. _string_syserrmsgs + 825,
  247. /* _string_syserrmsgs + 859, */
  248. NULL, /* glibc compatiblity :-( */
  249. _string_syserrmsgs + 860,
  250. _string_syserrmsgs + 887,
  251. _string_syserrmsgs + 906,
  252. _string_syserrmsgs + 934,
  253. _string_syserrmsgs + 959,
  254. _string_syserrmsgs + 974,
  255. _string_syserrmsgs + 988,
  256. _string_syserrmsgs + 1013,
  257. _string_syserrmsgs + 1042,
  258. _string_syserrmsgs + 1069,
  259. _string_syserrmsgs + 1084,
  260. _string_syserrmsgs + 1101,
  261. _string_syserrmsgs + 1128,
  262. _string_syserrmsgs + 1142,
  263. _string_syserrmsgs + 1151,
  264. _string_syserrmsgs + 1172,
  265. /* _string_syserrmsgs + 1185, */
  266. NULL, /* glibc compatiblity :-( */
  267. _string_syserrmsgs + 1186,
  268. _string_syserrmsgs + 1207,
  269. _string_syserrmsgs + 1227,
  270. _string_syserrmsgs + 1245,
  271. _string_syserrmsgs + 1259,
  272. _string_syserrmsgs + 1284,
  273. _string_syserrmsgs + 1314,
  274. _string_syserrmsgs + 1336,
  275. _string_syserrmsgs + 1353,
  276. _string_syserrmsgs + 1375,
  277. _string_syserrmsgs + 1391,
  278. _string_syserrmsgs + 1405,
  279. _string_syserrmsgs + 1433,
  280. _string_syserrmsgs + 1448,
  281. _string_syserrmsgs + 1467,
  282. _string_syserrmsgs + 1486,
  283. _string_syserrmsgs + 1498,
  284. _string_syserrmsgs + 1536,
  285. _string_syserrmsgs + 1563,
  286. _string_syserrmsgs + 1592,
  287. _string_syserrmsgs + 1615,
  288. _string_syserrmsgs + 1654,
  289. _string_syserrmsgs + 1691,
  290. _string_syserrmsgs + 1723,
  291. _string_syserrmsgs + 1771,
  292. _string_syserrmsgs + 1809,
  293. _string_syserrmsgs + 1859,
  294. _string_syserrmsgs + 1903,
  295. _string_syserrmsgs + 1922,
  296. _string_syserrmsgs + 1937,
  297. _string_syserrmsgs + 1968,
  298. _string_syserrmsgs + 1997,
  299. _string_syserrmsgs + 2014,
  300. _string_syserrmsgs + 2045,
  301. _string_syserrmsgs + 2068,
  302. _string_syserrmsgs + 2091,
  303. _string_syserrmsgs + 2117,
  304. _string_syserrmsgs + 2141,
  305. _string_syserrmsgs + 2171,
  306. _string_syserrmsgs + 2212,
  307. _string_syserrmsgs + 2235,
  308. _string_syserrmsgs + 2267,
  309. _string_syserrmsgs + 2283,
  310. _string_syserrmsgs + 2306,
  311. _string_syserrmsgs + 2342,
  312. _string_syserrmsgs + 2375,
  313. _string_syserrmsgs + 2400,
  314. _string_syserrmsgs + 2426,
  315. _string_syserrmsgs + 2466,
  316. _string_syserrmsgs + 2502,
  317. _string_syserrmsgs + 2548,
  318. _string_syserrmsgs + 2583,
  319. _string_syserrmsgs + 2604,
  320. _string_syserrmsgs + 2623,
  321. _string_syserrmsgs + 2636,
  322. _string_syserrmsgs + 2653,
  323. _string_syserrmsgs + 2683,
  324. _string_syserrmsgs + 2709,
  325. _string_syserrmsgs + 2731,
  326. _string_syserrmsgs + 2756,
  327. _string_syserrmsgs + 2784,
  328. _string_syserrmsgs + 2814,
  329. _string_syserrmsgs + 2835,
  330. _string_syserrmsgs + 2852,
  331. _string_syserrmsgs + 2872,
  332. _string_syserrmsgs + 2888,
  333. };
  334. #endif
  335. /**********************************************************************/
  336. #ifdef L_wmemcpy
  337. #define L_memcpy
  338. #define Wmemcpy wmemcpy
  339. #else
  340. #define Wmemcpy memcpy
  341. #endif
  342. #ifdef L_memcpy
  343. Wvoid *Wmemcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  344. {
  345. register Wchar *r1 = s1;
  346. register const Wchar *r2 = s2;
  347. #ifdef __BCC__
  348. while (n--) {
  349. *r1++ = *r2++;
  350. }
  351. #else
  352. while (n) {
  353. *r1++ = *r2++;
  354. --n;
  355. }
  356. #endif
  357. return s1;
  358. }
  359. #endif
  360. /**********************************************************************/
  361. #ifdef L_wmemmove
  362. #define L_memmove
  363. #define Wmemmove wmemmove
  364. #else
  365. #define Wmemmove memmove
  366. #endif
  367. #ifdef L_memmove
  368. Wvoid *Wmemmove(Wvoid *s1, const Wvoid *s2, size_t n)
  369. {
  370. #ifdef __BCC__
  371. register Wchar *s = (Wchar *) s1;
  372. register const Wchar *p = (const Wchar *) s2;
  373. if (p >= s) {
  374. while (n--) {
  375. *s++ = *p++;
  376. }
  377. } else {
  378. s += n;
  379. p += n;
  380. while (n--) {
  381. *--s = *--p;
  382. }
  383. }
  384. return s1;
  385. #else
  386. register Wchar *s = (Wchar *) s1;
  387. register const Wchar *p = (const Wchar *) s2;
  388. if (p >= s) {
  389. while (n) {
  390. *s++ = *p++;
  391. --n;
  392. }
  393. } else {
  394. while (n) {
  395. --n;
  396. s[n] = p[n];
  397. }
  398. }
  399. return s1;
  400. #endif
  401. }
  402. #endif
  403. /**********************************************************************/
  404. #ifdef L_wcscpy
  405. #define L_strcpy
  406. #define Wstrcpy wcscpy
  407. #else
  408. #define Wstrcpy strcpy
  409. #endif
  410. #ifdef L_strcpy
  411. Wchar *Wstrcpy(Wchar * __restrict s1, const Wchar * __restrict s2)
  412. {
  413. register Wchar *s = s1;
  414. #ifdef __BCC__
  415. do {
  416. *s = *s2++;
  417. } while (*s++ != 0);
  418. #else
  419. while ( (*s++ = *s2++) != 0 );
  420. #endif
  421. return s1;
  422. }
  423. #endif
  424. /**********************************************************************/
  425. #ifdef L_wcsncpy
  426. #define L_strncpy
  427. #define Wstrncpy wcsncpy
  428. #else
  429. #define Wstrncpy strncpy
  430. #endif
  431. #ifdef L_strncpy
  432. Wchar *Wstrncpy(Wchar * __restrict s1, register const Wchar * __restrict s2,
  433. size_t n)
  434. {
  435. register Wchar *s = s1;
  436. #ifdef __BCC__
  437. while (n--) {
  438. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  439. ++s;
  440. }
  441. #else
  442. while (n) {
  443. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  444. ++s;
  445. --n;
  446. }
  447. #endif
  448. return s1;
  449. }
  450. #endif
  451. /**********************************************************************/
  452. #ifdef L_wcscat
  453. #define L_strcat
  454. #define Wstrcat wcscat
  455. #else
  456. #define Wstrcat strcat
  457. #endif
  458. #ifdef L_strcat
  459. Wchar *Wstrcat(Wchar * __restrict s1, register const Wchar * __restrict s2)
  460. {
  461. register Wchar *s = s1;
  462. while (*s++);
  463. --s;
  464. while ((*s++ = *s2++) != 0);
  465. return s1;
  466. }
  467. #endif
  468. /**********************************************************************/
  469. #ifdef L_wcsncat
  470. #define L_strncat
  471. #define Wstrncat wcsncat
  472. #else
  473. #define Wstrncat strncat
  474. #endif
  475. #ifdef L_strncat
  476. Wchar *Wstrncat(Wchar * __restrict s1, register const Wchar * __restrict s2,
  477. size_t n)
  478. {
  479. register Wchar *s = s1;
  480. while (*s++);
  481. --s;
  482. #if __BCC__
  483. while (n-- && ((*s = *s2++) != 0)) ++s;
  484. #else
  485. while (n && ((*s = *s2++) != 0)) {
  486. --n;
  487. ++s;
  488. }
  489. #endif
  490. *s = 0;
  491. return s1;
  492. }
  493. #endif
  494. /**********************************************************************/
  495. #ifdef L_wmemcmp
  496. #define L_memcmp
  497. #define Wmemcmp wmemcmp
  498. #else
  499. #define Wmemcmp memcmp
  500. #endif
  501. #ifdef L_memcmp
  502. #ifndef L_wmemcmp
  503. weak_alias(memcmp,bcmp);
  504. #endif
  505. int Wmemcmp(const Wvoid *s1, const Wvoid *s2, size_t n)
  506. {
  507. register const Wuchar *r1 = (const Wuchar *) s1;
  508. register const Wuchar *r2 = (const Wuchar *) s2;
  509. #ifdef WANT_WIDE
  510. while (n && (*r1 == *r2)) {
  511. ++r1;
  512. ++r2;
  513. --n;
  514. }
  515. return (n == 0) ? 0 : ((*r1 < *r2) ? -1 : 1);
  516. #else
  517. int r = 0;
  518. while (n-- && ((r = ((int)(*r1++)) - *r2++) == 0));
  519. return r;
  520. #endif
  521. }
  522. #endif
  523. /**********************************************************************/
  524. #ifdef L_wcscmp
  525. #define L_strcmp
  526. #define Wstrcmp wcscmp
  527. #else
  528. #define Wstrcmp strcmp
  529. #endif
  530. #ifdef L_strcmp
  531. #ifndef L_wcscmp
  532. #warning implement strcoll and remove weak alias (or enable for C locale only)
  533. weak_alias(strcmp,strcoll);
  534. #endif
  535. int Wstrcmp(register const Wchar *s1, register const Wchar *s2)
  536. {
  537. #ifdef WANT_WIDE
  538. while (*((Wuchar *)s1) == *((Wuchar *)s2)) {
  539. if (!*s1++) {
  540. return 0;
  541. }
  542. ++s2;
  543. }
  544. return (*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1;
  545. #else
  546. int r;
  547. while (((r = ((int)(*((Wuchar *)s1))) - *((Wuchar *)s2++))
  548. == 0) && *s1++);
  549. return r;
  550. #endif
  551. }
  552. #endif
  553. /**********************************************************************/
  554. #ifdef L_strcoll
  555. #error implement strcoll and remove weak_alias!!
  556. #if 0
  557. extern unsigned char *_ctype_collate;
  558. int strcoll(register const char *s1, const char *s2)
  559. {
  560. int r;
  561. while (!(r = (_ctype_collate[(int)(*s1++)]-_ctype_collate[(int)(*s2++)])));
  562. return r;
  563. }
  564. #endif
  565. #endif
  566. /**********************************************************************/
  567. #ifdef L_wcsncmp
  568. #define L_strncmp
  569. #define Wstrncmp wcsncmp
  570. #else
  571. #define Wstrncmp strncmp
  572. #endif
  573. #ifdef L_strncmp
  574. int Wstrncmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  575. {
  576. #ifdef WANT_WIDE
  577. while (n && (*((Wuchar *)s1) == *((Wuchar *)s2))) {
  578. if (!*s1++) {
  579. return 0;
  580. }
  581. ++s2;
  582. --n;
  583. }
  584. return (n == 0) ? 0 : ((*((Wuchar *)s1) < *((Wuchar *)s2)) ? -1 : 1);
  585. #else
  586. int r = 0;
  587. while (n--
  588. && ((r = ((int)(*((unsigned char *)s1))) - *((unsigned char *)s2++))
  589. == 0)
  590. && *s1++);
  591. return r;
  592. #endif
  593. }
  594. #endif
  595. /**********************************************************************/
  596. #ifdef L_strxfrm
  597. #error implement strxfrm
  598. /* size_t strxfrm(char *dst, const char *src, size_t len); */
  599. #endif
  600. /**********************************************************************/
  601. #ifdef L_wmemchr
  602. #define L_memchr
  603. #define Wmemchr wmemchr
  604. #else
  605. #define Wmemchr memchr
  606. #endif
  607. #ifdef L_memchr
  608. Wvoid *Wmemchr(const Wvoid *s, Wint c, size_t n)
  609. {
  610. register const Wuchar *r = (const Wuchar *) s;
  611. #ifdef __BCC__
  612. /* bcc can optimize the counter if it thinks it is a pointer... */
  613. register const char *np = (const char *) n;
  614. #else
  615. #define np n
  616. #endif
  617. while (np) {
  618. if (*r == ((Wuchar)c)) {
  619. return (Wvoid *) r; /* silence the warning */
  620. }
  621. ++r;
  622. --np;
  623. }
  624. return NULL;
  625. }
  626. #undef np
  627. #endif
  628. /**********************************************************************/
  629. #ifdef L_wcschr
  630. #define L_strchr
  631. #define Wstrchr wcschr
  632. #else
  633. #define Wstrchr strchr
  634. #endif
  635. #ifdef L_strchr
  636. #ifndef L_wcschr
  637. weak_alias(strchr,index);
  638. #endif
  639. Wchar *Wstrchr(register const Wchar *s, Wint c)
  640. {
  641. do {
  642. if (*s == ((Wchar)c)) {
  643. return (Wchar *) s; /* silence the warning */
  644. }
  645. } while (*s++);
  646. return NULL;
  647. }
  648. #endif
  649. /**********************************************************************/
  650. #ifdef L_wcscspn
  651. #define L_strcspn
  652. #define Wstrcspn wcscspn
  653. #else
  654. #define Wstrcspn strcspn
  655. #endif
  656. #ifdef L_strcspn
  657. size_t Wstrcspn(const Wchar *s1, const Wchar *s2)
  658. {
  659. register const Wchar *s;
  660. register const Wchar *p;
  661. for ( s=s1 ; *s ; s++ ) {
  662. for ( p=s2 ; *p ; p++ ) {
  663. if (*p == *s) goto done;
  664. }
  665. }
  666. done:
  667. return s - s1;
  668. }
  669. #endif
  670. /**********************************************************************/
  671. #ifdef L_wcspbrk
  672. #define L_strpbrk
  673. #define Wstrpbrk wcspbrk
  674. #else
  675. #define Wstrpbrk strpbrk
  676. #endif
  677. #ifdef L_strpbrk
  678. Wchar *Wstrpbrk(const Wchar *s1, const Wchar *s2)
  679. {
  680. register const Wchar *s;
  681. register const Wchar *p;
  682. for ( s=s1 ; *s ; s++ ) {
  683. for ( p=s2 ; *p ; p++ ) {
  684. if (*p == *s) return (Wchar *) s; /* silence the warning */
  685. }
  686. }
  687. return NULL;
  688. }
  689. #endif
  690. /**********************************************************************/
  691. #ifdef L_wcsrchr
  692. #define L_strrchr
  693. #define Wstrrchr wcsrchr
  694. #else
  695. #define Wstrrchr strrchr
  696. #endif
  697. #ifdef L_strrchr
  698. #ifndef L_wcsrchr
  699. weak_alias(strrchr,rindex);
  700. #endif
  701. Wchar *Wstrrchr(register const Wchar *s, Wint c)
  702. {
  703. register const Wchar *p;
  704. p = NULL;
  705. do {
  706. if (*s == (Wchar) c) {
  707. p = s;
  708. }
  709. } while (*s++);
  710. return (Wchar *) p; /* silence the warning */
  711. }
  712. #endif
  713. /**********************************************************************/
  714. #ifdef L_wcsspn
  715. #define L_strspn
  716. #define Wstrspn wcsspn
  717. #else
  718. #define Wstrspn strspn
  719. #endif
  720. #ifdef L_strspn
  721. size_t Wstrspn(const Wchar *s1, const Wchar *s2)
  722. {
  723. register const Wchar *s = s1;
  724. register const Wchar *p = s2;
  725. while (*p) {
  726. if (*p++ == *s) {
  727. ++s;
  728. p = s2;
  729. }
  730. }
  731. return s - s1;
  732. }
  733. #endif
  734. /**********************************************************************/
  735. #ifdef L_wcsstr
  736. #define L_strstr
  737. #define Wstrstr wcsstr
  738. #else
  739. #define Wstrstr strstr
  740. #endif
  741. #ifdef L_strstr
  742. /* NOTE: This is the simple-minded O(len(s1) * len(s2)) worst-case approach. */
  743. #ifdef L_wcsstr
  744. weak_alias(wcsstr,wcswcs);
  745. #endif
  746. Wchar *Wstrstr(const Wchar *s1, const Wchar *s2)
  747. {
  748. register const Wchar *s = s1;
  749. register const Wchar *p = s2;
  750. do {
  751. if (!*p) {
  752. return (Wchar *) s1;;
  753. }
  754. if (*p == *s) {
  755. ++p;
  756. ++s;
  757. } else {
  758. p = s2;
  759. if (!*s) {
  760. return NULL;
  761. }
  762. s = ++s1;
  763. }
  764. } while (1);
  765. }
  766. #endif
  767. /**********************************************************************/
  768. #undef Wstrspn
  769. #undef Wstrpbrk
  770. #ifdef L_wcstok
  771. #define L_strtok_r
  772. #define Wstrtok_r wcstok
  773. #define Wstrspn wcsspn
  774. #define Wstrpbrk wcspbrk
  775. #else
  776. #define Wstrtok_r strtok_r
  777. #define Wstrspn strspn
  778. #define Wstrpbrk strpbrk
  779. #endif
  780. #ifdef L_strtok_r
  781. Wchar *Wstrtok_r(Wchar * __restrict s1, const Wchar * __restrict s2,
  782. Wchar ** __restrict next_start)
  783. {
  784. register Wchar *s;
  785. register Wchar *p;
  786. #if 1
  787. if (((s = s1) != NULL) || ((s = *next_start) != NULL)) {
  788. if (*(s += Wstrspn(s, s2))) {
  789. if ((p = Wstrpbrk(s, s2)) != NULL) {
  790. *p++ = 0;
  791. }
  792. } else {
  793. p = s = NULL;
  794. }
  795. *next_start = p;
  796. }
  797. return s;
  798. #else
  799. if (!(s = s1)) {
  800. s = *next_start;
  801. }
  802. if (s && *(s += Wstrspn(s, s2))) {
  803. if (*(p = s + Wstrcspn(s, s2))) {
  804. *p++ = 0;
  805. }
  806. *next_start = p;
  807. return s;
  808. }
  809. return NULL; /* TODO: set *next_start = NULL for safety? */
  810. #endif
  811. }
  812. #endif
  813. /**********************************************************************/
  814. /* #ifdef L_wcstok */
  815. /* #define L_strtok */
  816. /* #define Wstrtok wcstok */
  817. /* #define Wstrtok_r wcstok_r */
  818. /* #else */
  819. /* #define Wstrtok strtok */
  820. /* #define Wstrtok_r strtok_r */
  821. /* #endif */
  822. #ifdef L_strtok
  823. #define Wstrtok strtok
  824. #define Wstrtok_r strtok_r
  825. Wchar *Wstrtok(Wchar * __restrict s1, const Wchar * __restrict s2)
  826. {
  827. static Wchar *next_start; /* Initialized to 0 since in bss. */
  828. return Wstrtok_r(s1, s2, &next_start);
  829. }
  830. #endif
  831. /**********************************************************************/
  832. #ifdef L_wmemset
  833. #define L_memset
  834. #define Wmemset wmemset
  835. #else
  836. #define Wmemset memset
  837. #endif
  838. #ifdef L_memset
  839. Wvoid *Wmemset(Wvoid *s, Wint c, size_t n)
  840. {
  841. register Wuchar *p = (Wuchar *) s;
  842. #ifdef __BCC__
  843. /* bcc can optimize the counter if it thinks it is a pointer... */
  844. register const char *np = (const char *) n;
  845. #else
  846. #define np n
  847. #endif
  848. while (np) {
  849. *p++ = (Wuchar) c;
  850. --np;
  851. }
  852. return s;
  853. }
  854. #undef np
  855. #endif
  856. /**********************************************************************/
  857. #ifdef L_wcslen
  858. #define L_strlen
  859. #define Wstrlen wcslen
  860. #else
  861. #define Wstrlen strlen
  862. #endif
  863. #ifdef L_strlen
  864. size_t Wstrlen(const Wchar *s)
  865. {
  866. register const Wchar *p;
  867. for (p=s ; *p ; p++);
  868. return p - s;
  869. }
  870. #endif
  871. /**********************************************************************/
  872. /* ANSI/ISO end here */
  873. /**********************************************************************/
  874. #ifdef L_ffs
  875. int ffs(int i)
  876. {
  877. #if 1
  878. /* inlined binary search method */
  879. char n = 1;
  880. #if UINT_MAX == 0xffffU
  881. /* nothing to do here -- just trying to avoiding possible problems */
  882. #elif UINT_MAX == 0xffffffffU
  883. if (!(i & 0xffff)) {
  884. n += 16;
  885. i >>= 16;
  886. }
  887. #else
  888. #error ffs needs rewriting!
  889. #endif
  890. if (!(i & 0xff)) {
  891. n += 8;
  892. i >>= 8;
  893. }
  894. if (!(i & 0x0f)) {
  895. n += 4;
  896. i >>= 4;
  897. }
  898. if (!(i & 0x03)) {
  899. n += 2;
  900. i >>= 2;
  901. }
  902. return (i) ? (n + ((i+1) & 0x01)) : 0;
  903. #else
  904. /* linear search -- slow, but small */
  905. int n;
  906. for (n = 0 ; i ; ++n) {
  907. i >>= 1;
  908. }
  909. return n;
  910. #endif
  911. }
  912. #endif
  913. /**********************************************************************/
  914. #ifdef L_wcscasecmp
  915. #define L_strcasecmp
  916. #define Wstrcasecmp wcscasecmp
  917. #else
  918. #define Wstrcasecmp strcasecmp
  919. #endif
  920. #ifdef L_strcasecmp
  921. int Wstrcasecmp(register const Wchar *s1, register const Wchar *s2)
  922. {
  923. #ifdef WANT_WIDE
  924. while ((*s1 == *s2) || (towlower(*s1) == towlower(*s2))) {
  925. if (!*s1++) {
  926. return 0;
  927. }
  928. ++s2;
  929. }
  930. return (((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1;
  931. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  932. #else
  933. int r = 0;
  934. while ( ((s1 == s2) ||
  935. !(r = ((int)( tolower(*((Wuchar *)s1))))
  936. - tolower(*((Wuchar *)s2))))
  937. && (++s2, *s1++));
  938. return r;
  939. #endif
  940. }
  941. #endif
  942. /**********************************************************************/
  943. #ifdef L_wcsncasecmp
  944. #define L_strncasecmp
  945. #define Wstrncasecmp wcsncasecmp
  946. #else
  947. #define Wstrncasecmp strncasecmp
  948. #endif
  949. #ifdef L_strncasecmp
  950. int Wstrncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  951. {
  952. #ifdef WANT_WIDE
  953. while (n && ((*s1 == *s2) || (towlower(*s1) == towlower(*s2)))) {
  954. if (!*s1++) {
  955. return 0;
  956. }
  957. ++s2;
  958. --n;
  959. }
  960. return (n == 0)
  961. ? 0
  962. : ((((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1);
  963. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  964. #else
  965. int r = 0;
  966. while ( n
  967. && ((s1 == s2) ||
  968. !(r = ((int)( tolower(*((unsigned char *)s1))))
  969. - tolower(*((unsigned char *)s2))))
  970. && (--n, ++s2, *s1++));
  971. return r;
  972. #endif
  973. }
  974. #endif
  975. /**********************************************************************/
  976. #ifdef L_wcsnlen
  977. #define L_strnlen
  978. #define Wstrnlen wcsnlen
  979. #else
  980. #define Wstrnlen strnlen
  981. #endif
  982. #ifdef L_strnlen
  983. size_t Wstrnlen(const Wchar *s, size_t max)
  984. {
  985. register const Wchar *p = s;
  986. #ifdef __BCC__
  987. /* bcc can optimize the counter if it thinks it is a pointer... */
  988. register const char *maxp = (const char *) max;
  989. #else
  990. #define maxp max
  991. #endif
  992. while (maxp && *p) {
  993. ++p;
  994. --maxp;
  995. }
  996. return p - s;
  997. }
  998. #undef maxp
  999. #endif
  1000. /**********************************************************************/
  1001. /* No wide analog. */
  1002. #ifdef L_memccpy
  1003. void *memccpy(void * __restrict s1, const void * __restrict s2, int c, size_t n)
  1004. {
  1005. register char *r1 = s1;
  1006. register const char *r2 = s2;
  1007. while (n-- && (((unsigned char)(*r1++ = *r2++)) != ((unsigned char) c)));
  1008. return (n == (size_t) -1) ? NULL : r1;
  1009. }
  1010. #endif
  1011. /**********************************************************************/
  1012. #undef Wstrlen
  1013. #undef Wstrcpy
  1014. #ifdef L_wcsdup
  1015. #define L_strdup
  1016. #define Wstrdup wcsdup
  1017. #define Wstrlen wcslen
  1018. #define Wstrcpy wcscpy
  1019. #else
  1020. #define Wstrdup strdup
  1021. #define Wstrlen strlen
  1022. #define Wstrcpy strcpy
  1023. #endif
  1024. #ifdef L_strdup
  1025. Wchar *Wstrdup(register const Wchar *s1)
  1026. {
  1027. register Wchar *s;
  1028. if ((s = malloc((Wstrlen(s1) + 1) * sizeof(Wchar))) != NULL) {
  1029. Wstrcpy(s, s1);
  1030. }
  1031. return s;
  1032. }
  1033. #endif
  1034. /**********************************************************************/
  1035. #ifdef L_strerror
  1036. char *strerror(int errnum)
  1037. {
  1038. static char buf[_SYS_ERRMSG_MAXLEN];
  1039. return (_susv3_strerror_r(errnum, buf, sizeof(buf)) == 0) ? buf : NULL;
  1040. }
  1041. #endif
  1042. /**********************************************************************/
  1043. /* SUSv3 functions. */
  1044. /**********************************************************************/
  1045. #ifdef L__susv3_strerror_r
  1046. int _susv3_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1047. {
  1048. register char *s;
  1049. int i, retval;
  1050. char buf[_SYS_ERRMSG_MAXLEN];
  1051. static const char unknown[14] = {
  1052. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'e', 'r', 'r', 'o', 'r', ' '
  1053. };
  1054. retval = EINVAL;
  1055. if (((unsigned int) errnum) < _SYS_NERR) {
  1056. /* Trade time for space. This function should rarely be called
  1057. * so rather than keeping an array of pointers for the different
  1058. * messages, just run through the buffer until we find the
  1059. * correct string. */
  1060. for (s = (char *) _string_syserrmsgs, i = errnum ; i ; ++s) {
  1061. if (!*s) {
  1062. --i;
  1063. }
  1064. }
  1065. if (*s) { /* Make sure we have an actual message. */
  1066. retval = 0;
  1067. goto GOT_MESG;
  1068. }
  1069. }
  1070. s = _int10tostr(buf+sizeof(buf)-1, errnum) - sizeof(unknown);
  1071. memcpy(s, unknown, sizeof(unknown));
  1072. GOT_MESG:
  1073. if (!strerrbuf) { /* SUSv3 */
  1074. buflen = 0;
  1075. }
  1076. i = strlen(s) + 1;
  1077. if (i > buflen) {
  1078. i = buflen;
  1079. retval = ERANGE;
  1080. }
  1081. if (i) {
  1082. memcpy(strerrbuf, s, i);
  1083. strerrbuf[i-1] = 0; /* In case buf was too small. */
  1084. }
  1085. if (retval) {
  1086. __set_errno(retval);
  1087. }
  1088. return retval;
  1089. }
  1090. #endif
  1091. /**********************************************************************/
  1092. /* GNU extension functions. */
  1093. /**********************************************************************/
  1094. #ifdef L__glibc_strerror_r
  1095. weak_alias(_glibc_strerror_r,__strerror_r);
  1096. char *_glibc_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1097. {
  1098. _susv3_strerror_r(errnum, strerrbuf, buflen);
  1099. return strerrbuf;
  1100. }
  1101. #endif
  1102. /**********************************************************************/
  1103. #ifdef L_wmempcpy
  1104. #define L_mempcpy
  1105. #define Wmempcpy wmempcpy
  1106. #else
  1107. #define Wmempcpy mempcpy
  1108. #endif
  1109. #ifdef L_mempcpy
  1110. #ifndef L_wmempcpy
  1111. /* uClibc's old string implementation did this to cater to some app. */
  1112. weak_alias(mempcpy,__mempcpy);
  1113. #endif
  1114. Wvoid *Wmempcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  1115. {
  1116. register Wchar *r1 = s1;
  1117. register const Wchar *r2 = s2;
  1118. #ifdef __BCC__
  1119. while (n--) {
  1120. *r1++ = *r2++;
  1121. }
  1122. #else
  1123. while (n) {
  1124. *r1++ = *r2++;
  1125. --n;
  1126. }
  1127. #endif
  1128. return r1;
  1129. }
  1130. #endif
  1131. /**********************************************************************/
  1132. #ifdef L_memrchr
  1133. void *memrchr(const void *s, int c, size_t n)
  1134. {
  1135. register const unsigned char *r;
  1136. #ifdef __BCC__
  1137. /* bcc can optimize the counter if it thinks it is a pointer... */
  1138. register const char *np = (const char *) n;
  1139. #else
  1140. #define np n
  1141. #endif
  1142. r = ((unsigned char *)s) + ((size_t) np);
  1143. while (np) {
  1144. if (*--r == ((unsigned char)c)) {
  1145. return (void *) r; /* silence the warning */
  1146. }
  1147. --np;
  1148. }
  1149. return NULL;
  1150. }
  1151. #undef np
  1152. #endif
  1153. /**********************************************************************/
  1154. #ifdef L_wcpcpy
  1155. #define L_stpcpy
  1156. #define Wstpcpy wcpcpy
  1157. #else
  1158. #define Wstpcpy stpcpy
  1159. #endif
  1160. #ifdef L_stpcpy
  1161. Wchar *Wstpcpy(register Wchar * __restrict s1, const Wchar * __restrict s2)
  1162. {
  1163. #ifdef __BCC__
  1164. do {
  1165. *s1 = *s2++;
  1166. } while (*s1++ != 0);
  1167. #else
  1168. while ( (*s1++ = *s2++) != 0 );
  1169. #endif
  1170. return s1 - 1;
  1171. }
  1172. #endif
  1173. /**********************************************************************/
  1174. #ifdef L_wcpncpy
  1175. #define L_stpncpy
  1176. #define Wstpncpy wcpncpy
  1177. #else
  1178. #define Wstpncpy stpncpy
  1179. #endif
  1180. #ifdef L_stpncpy
  1181. Wchar *Wstpncpy(register Wchar * __restrict s1,
  1182. register const Wchar * __restrict s2,
  1183. size_t n)
  1184. {
  1185. Wchar *s = s1;
  1186. const Wchar *p = s2;
  1187. #ifdef __BCC__
  1188. while (n--) {
  1189. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1190. ++s;
  1191. }
  1192. return s1 + (s2 - p);
  1193. #else
  1194. while (n) {
  1195. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1196. ++s;
  1197. --n;
  1198. }
  1199. return s1 + (s2 - p);
  1200. #endif
  1201. }
  1202. #endif
  1203. /**********************************************************************/
  1204. #ifdef L_bzero
  1205. void bzero(void *s, size_t n)
  1206. {
  1207. register unsigned char *p = s;
  1208. #ifdef __BCC__
  1209. /* bcc can optimize the counter if it thinks it is a pointer... */
  1210. register const char *np = (const char *) n;
  1211. #else
  1212. #define np n
  1213. #endif
  1214. while (np) {
  1215. *p++ = 0;
  1216. --np;
  1217. }
  1218. }
  1219. #undef np
  1220. #endif
  1221. /**********************************************************************/
  1222. #ifdef L_bcopy
  1223. void bcopy(const void *s2, void *s1, size_t n)
  1224. {
  1225. #if 1
  1226. memmove(s1, s2, n);
  1227. #else
  1228. #ifdef __BCC__
  1229. register char *s;
  1230. register const char *p;
  1231. s = s1;
  1232. p = s2;
  1233. if (p >= s) {
  1234. while (n--) {
  1235. *s++ = *p++;
  1236. }
  1237. } else {
  1238. s += n;
  1239. p += n;
  1240. while (n--) {
  1241. *--s = *--p;
  1242. }
  1243. }
  1244. #else
  1245. register char *s;
  1246. register const char *p;
  1247. s = s1;
  1248. p = s2;
  1249. if (p >= s) {
  1250. while (n) {
  1251. *s++ = *p++;
  1252. --n;
  1253. }
  1254. } else {
  1255. while (n) {
  1256. --n;
  1257. s[n] = p[n];
  1258. }
  1259. }
  1260. #endif
  1261. #endif
  1262. }
  1263. #endif
  1264. /**********************************************************************/
  1265. #ifdef L_strcasestr
  1266. char *strcasestr(const char *s1, const char *s2)
  1267. {
  1268. register const char *s = s1;
  1269. register const char *p = s2;
  1270. #if 1
  1271. do {
  1272. if (!*p) {
  1273. return (char *) s1;;
  1274. }
  1275. if ((*p == *s)
  1276. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1277. ) {
  1278. ++p;
  1279. ++s;
  1280. } else {
  1281. p = s2;
  1282. if (!*s) {
  1283. return NULL;
  1284. }
  1285. s = ++s1;
  1286. }
  1287. } while (1);
  1288. #else
  1289. while (*p && *s) {
  1290. if ((*p == *s)
  1291. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1292. ) {
  1293. ++p;
  1294. ++s;
  1295. } else {
  1296. p = s2;
  1297. s = ++s1;
  1298. }
  1299. }
  1300. return (*p) ? NULL : (char *) s1;
  1301. #endif
  1302. }
  1303. #endif
  1304. /**********************************************************************/
  1305. #ifdef L_strndup
  1306. char *strndup(register const char *s1, size_t n)
  1307. {
  1308. register char *s;
  1309. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  1310. if ((s = malloc(n + 1)) != NULL) {
  1311. memcpy(s, s1, n);
  1312. s[n] = 0;
  1313. }
  1314. return s;
  1315. }
  1316. #endif
  1317. /**********************************************************************/
  1318. #ifdef L_strsep
  1319. char *strsep(char ** __restrict s1, const char * __restrict s2)
  1320. {
  1321. register char *s = *s1;
  1322. register char *p;
  1323. #if 1
  1324. p = NULL;
  1325. if (s && *s && (p = strpbrk(s, s2))) {
  1326. *p++ = 0;
  1327. }
  1328. #else
  1329. if (s && *s && *(p = s + strcspn(s, s2))) {
  1330. *p++ = 0;
  1331. } else {
  1332. p = NULL;
  1333. }
  1334. #endif
  1335. *s1 = p;
  1336. return s;
  1337. }
  1338. #endif
  1339. /**********************************************************************/
  1340. #ifdef L_wcschrnul
  1341. #define L_strchrnul
  1342. #define Wstrchrnul wcschrnul
  1343. #else
  1344. #define Wstrchrnul strchrnul
  1345. #endif
  1346. #ifdef L_strchrnul
  1347. Wchar *Wstrchrnul(register const Wchar *s, Wint c)
  1348. {
  1349. --s;
  1350. while (*++s && (*s != ((Wchar)c)));
  1351. return (Wchar *) s;
  1352. }
  1353. #endif
  1354. /**********************************************************************/
  1355. #ifdef L_rawmemchr
  1356. void *rawmemchr(const void *s, int c)
  1357. {
  1358. register const unsigned char *r = s;
  1359. while (*r != ((unsigned char)c)) ++r;
  1360. return (void *) r; /* silence the warning */
  1361. }
  1362. #endif
  1363. /**********************************************************************/
  1364. #ifdef L_basename
  1365. char *basename(const char *path)
  1366. {
  1367. register const char *s;
  1368. register const char *p;
  1369. p = s = path;
  1370. while (*s) {
  1371. if (*s++ == '/') {
  1372. p = s;
  1373. }
  1374. }
  1375. return (char *) p;
  1376. }
  1377. #endif
  1378. /**********************************************************************/
  1379. #ifdef L___xpg_basename
  1380. char *__xpg_basename(register char *path)
  1381. {
  1382. static const char null_or_empty[] = ".";
  1383. char *first;
  1384. register char *last;
  1385. first = (char *) null_or_empty;
  1386. if (path && *path) {
  1387. first = path;
  1388. last = path - 1;
  1389. do {
  1390. if ((*path != '/') && (path > ++last)) {
  1391. last = first = path;
  1392. }
  1393. } while (*++path);
  1394. if (*first == '/') {
  1395. last = first;
  1396. }
  1397. last[1] = 0;
  1398. }
  1399. return first;
  1400. }
  1401. #endif
  1402. /**********************************************************************/
  1403. #ifdef L_dirname
  1404. char *dirname(char *path)
  1405. {
  1406. static const char null_or_empty_or_noslash[] = ".";
  1407. register char *s;
  1408. register char *last;
  1409. char *first;
  1410. last = s = path;
  1411. if (s != NULL) {
  1412. LOOP:
  1413. while (*s && (*s != '/')) ++s;
  1414. first = s;
  1415. while (*s == '/') ++s;
  1416. if (*s) {
  1417. last = first;
  1418. goto LOOP;
  1419. }
  1420. if (last == path) {
  1421. if (*last != '/') {
  1422. goto DOT;
  1423. }
  1424. if ((*++last == '/') && (last[1] == 0)) {
  1425. ++last;
  1426. }
  1427. }
  1428. *last = 0;
  1429. return path;
  1430. }
  1431. DOT:
  1432. return (char *) null_or_empty_or_noslash;
  1433. }
  1434. #endif
  1435. /**********************************************************************/
  1436. #ifdef L_strlcat
  1437. /* OpenBSD function:
  1438. * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst.
  1439. * Returns strlen(src) + strlen({original} dst), so truncation occurred if the
  1440. * return val is >= n.
  1441. * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is
  1442. * taken as n. */
  1443. size_t strlcat(register char *__restrict dst,
  1444. register const char *__restrict src,
  1445. size_t n)
  1446. {
  1447. size_t len;
  1448. char dummy[1];
  1449. len = 0;
  1450. while (1) {
  1451. if (len >= n) {
  1452. dst = dummy;
  1453. break;
  1454. }
  1455. if (!*dst) {
  1456. break;
  1457. }
  1458. ++dst;
  1459. ++len;
  1460. }
  1461. while ((*dst = *src) != 0) {
  1462. if (++len < n) {
  1463. ++dst;
  1464. }
  1465. ++src;
  1466. }
  1467. return len;
  1468. }
  1469. #endif
  1470. /**********************************************************************/
  1471. #ifdef L_strlcpy
  1472. /* OpenBSD function:
  1473. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  1474. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  1475. size_t strlcpy(register char *__restrict dst,
  1476. register const char *__restrict src,
  1477. size_t n)
  1478. {
  1479. const char *src0 = src;
  1480. char dummy[1];
  1481. if (!n) {
  1482. dst = dummy;
  1483. } else {
  1484. --n;
  1485. }
  1486. while ((*dst = *src) != 0) {
  1487. if (n) {
  1488. --n;
  1489. ++dst;
  1490. }
  1491. ++src;
  1492. }
  1493. return src - src0;
  1494. }
  1495. #endif
  1496. /**********************************************************************/
  1497. #ifdef L__string_syssigmsgs
  1498. const char _string_syssigmsgs[] = {
  1499. /* 0: 0, 1 */ "\0"
  1500. /* 1: 1, 7 */ "Hangup\0"
  1501. /* 2: 8, 10 */ "Interrupt\0"
  1502. /* 3: 18, 5 */ "Quit\0"
  1503. /* 4: 23, 20 */ "Illegal instruction\0"
  1504. /* 5: 43, 22 */ "Trace/breakpoint trap\0"
  1505. /* 6: 65, 8 */ "Aborted\0"
  1506. /* 7: 73, 10 */ "Bus error\0"
  1507. /* 8: 83, 25 */ "Floating point exception\0"
  1508. /* 9: 108, 7 */ "Killed\0"
  1509. /* 10: 115, 22 */ "User defined signal 1\0"
  1510. /* 11: 137, 19 */ "Segmentation fault\0"
  1511. /* 12: 156, 22 */ "User defined signal 2\0"
  1512. /* 13: 178, 12 */ "Broken pipe\0"
  1513. /* 14: 190, 12 */ "Alarm clock\0"
  1514. /* 15: 202, 11 */ "Terminated\0"
  1515. /* 16: 213, 12 */ "Stack fault\0"
  1516. /* 17: 225, 13 */ "Child exited\0"
  1517. /* 18: 238, 10 */ "Continued\0"
  1518. /* 19: 248, 17 */ "Stopped (signal)\0"
  1519. /* 20: 265, 8 */ "Stopped\0"
  1520. /* 21: 273, 20 */ "Stopped (tty input)\0"
  1521. /* 22: 293, 21 */ "Stopped (tty output)\0"
  1522. /* 23: 314, 21 */ "Urgent I/O condition\0"
  1523. /* 24: 335, 24 */ "CPU time limit exceeded\0"
  1524. /* 25: 359, 25 */ "File size limit exceeded\0"
  1525. /* 26: 384, 22 */ "Virtual timer expired\0"
  1526. /* 27: 406, 24 */ "Profiling timer expired\0"
  1527. /* 28: 430, 15 */ "Window changed\0"
  1528. /* 29: 445, 13 */ "I/O possible\0"
  1529. /* 30: 458, 14 */ "Power failure\0"
  1530. /* 31: 472, 16 */ "Bad system call"
  1531. };
  1532. #endif
  1533. /**********************************************************************/
  1534. #ifdef L_sys_siglist
  1535. const char *const sys_siglist[_NSIG] = {
  1536. NULL,
  1537. _string_syssigmsgs + 1,
  1538. _string_syssigmsgs + 8,
  1539. _string_syssigmsgs + 18,
  1540. _string_syssigmsgs + 23,
  1541. _string_syssigmsgs + 43,
  1542. _string_syssigmsgs + 65,
  1543. _string_syssigmsgs + 73,
  1544. _string_syssigmsgs + 83,
  1545. _string_syssigmsgs + 108,
  1546. _string_syssigmsgs + 115,
  1547. _string_syssigmsgs + 137,
  1548. _string_syssigmsgs + 156,
  1549. _string_syssigmsgs + 178,
  1550. _string_syssigmsgs + 190,
  1551. _string_syssigmsgs + 202,
  1552. _string_syssigmsgs + 213,
  1553. _string_syssigmsgs + 225,
  1554. _string_syssigmsgs + 238,
  1555. _string_syssigmsgs + 248,
  1556. _string_syssigmsgs + 265,
  1557. _string_syssigmsgs + 273,
  1558. _string_syssigmsgs + 293,
  1559. _string_syssigmsgs + 314,
  1560. _string_syssigmsgs + 335,
  1561. _string_syssigmsgs + 359,
  1562. _string_syssigmsgs + 384,
  1563. _string_syssigmsgs + 406,
  1564. _string_syssigmsgs + 430,
  1565. _string_syssigmsgs + 445,
  1566. _string_syssigmsgs + 458,
  1567. _string_syssigmsgs + 472,
  1568. NULL,
  1569. NULL,
  1570. NULL,
  1571. NULL,
  1572. NULL,
  1573. NULL,
  1574. NULL,
  1575. NULL,
  1576. NULL,
  1577. NULL,
  1578. NULL,
  1579. NULL,
  1580. NULL,
  1581. NULL,
  1582. NULL,
  1583. NULL,
  1584. NULL,
  1585. NULL,
  1586. NULL,
  1587. NULL,
  1588. NULL,
  1589. NULL,
  1590. NULL,
  1591. NULL,
  1592. NULL,
  1593. NULL,
  1594. NULL,
  1595. NULL,
  1596. NULL,
  1597. NULL,
  1598. NULL,
  1599. NULL,
  1600. };
  1601. #endif
  1602. /**********************************************************************/
  1603. #ifdef L_strsignal
  1604. /* TODO: make a threadsafe version? */
  1605. char *strsignal(int signum)
  1606. {
  1607. register char *s;
  1608. int i;
  1609. static char buf[_STRSIGNAL_BUFSIZE];
  1610. static const char unknown[15] = {
  1611. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  1612. };
  1613. if (((unsigned int) signum) < _SYS_NSIG) {
  1614. /* Trade time for space. This function should rarely be called
  1615. * so rather than keeping an array of pointers for the different
  1616. * messages, just run through the buffer until we find the
  1617. * correct string. */
  1618. for (s = (char *) _string_syssigmsgs, i = signum ; i ; ++s) {
  1619. if (!*s) {
  1620. --i;
  1621. }
  1622. }
  1623. if (*s) { /* Make sure we have an actual message. */
  1624. goto DONE;
  1625. }
  1626. }
  1627. s = _int10tostr(buf+sizeof(buf)-1, signum) - sizeof(unknown);
  1628. memcpy(s, unknown, sizeof(unknown));
  1629. DONE:
  1630. return s;
  1631. }
  1632. #endif
  1633. /**********************************************************************/
  1634. #ifdef L_psignal
  1635. /* TODO: make this threadsafe with a reentrant version of strsignal? */
  1636. void psignal(int signum, register const char *message)
  1637. {
  1638. /* If the program is calling psignal, it's a safe bet that printf and
  1639. * friends are used as well. It is also possible that the calling
  1640. * program could buffer stderr, or reassign it. */
  1641. register const char *sep;
  1642. sep = ": ";
  1643. if (!(message && *message)) { /* Caller did not supply a prefix message */
  1644. message = (sep += 2); /* or passed an empty string. */
  1645. }
  1646. #if 1
  1647. fprintf(stderr, "%s%s%s\n", message, sep, strsignal(signum));
  1648. #else
  1649. /* Note: Assumes stderr not closed or buffered. */
  1650. __STDIO_THREADLOCK(stderr);
  1651. _stdio_fdout(STDERR_FILENO, message, sep, strsignal(signum));
  1652. __STDIO_THREADUNLOCK(stderr);
  1653. #endif
  1654. }
  1655. #endif
  1656. /**********************************************************************/