wstring.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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. #ifdef L_wcstok
  769. #define L_strtok_r
  770. #define Wstrtok_r wcstok
  771. #define Wstrspn wcsspn
  772. #define Wstrpbrk wcspbrk
  773. #else
  774. #define Wstrtok_r strtok_r
  775. #define Wstrspn strspn
  776. #define Wstrpbrk strpbrk
  777. #endif
  778. #ifdef L_strtok_r
  779. Wchar *Wstrtok_r(Wchar * __restrict s1, const Wchar * __restrict s2,
  780. Wchar ** __restrict next_start)
  781. {
  782. register Wchar *s;
  783. register Wchar *p;
  784. #if 1
  785. if (((s = s1) != NULL) || ((s = *next_start) != NULL)) {
  786. if (*(s += Wstrspn(s, s2))) {
  787. if ((p = Wstrpbrk(s, s2)) != NULL) {
  788. *p++ = 0;
  789. }
  790. } else {
  791. p = s = NULL;
  792. }
  793. *next_start = p;
  794. }
  795. return s;
  796. #else
  797. if (!(s = s1)) {
  798. s = *next_start;
  799. }
  800. if (s && *(s += Wstrspn(s, s2))) {
  801. if (*(p = s + Wstrcspn(s, s2))) {
  802. *p++ = 0;
  803. }
  804. *next_start = p;
  805. return s;
  806. }
  807. return NULL; /* TODO: set *next_start = NULL for safety? */
  808. #endif
  809. }
  810. #endif
  811. /**********************************************************************/
  812. /* #ifdef L_wcstok */
  813. /* #define L_strtok */
  814. /* #define Wstrtok wcstok */
  815. /* #define Wstrtok_r wcstok_r */
  816. /* #else */
  817. /* #define Wstrtok strtok */
  818. /* #define Wstrtok_r strtok_r */
  819. /* #endif */
  820. #ifdef L_strtok
  821. #define Wstrtok strtok
  822. #define Wstrtok_r strtok_r
  823. Wchar *Wstrtok(Wchar * __restrict s1, const Wchar * __restrict s2)
  824. {
  825. static Wchar *next_start; /* Initialized to 0 since in bss. */
  826. return Wstrtok_r(s1, s2, &next_start);
  827. }
  828. #endif
  829. /**********************************************************************/
  830. #ifdef L_wmemset
  831. #define L_memset
  832. #define Wmemset wmemset
  833. #else
  834. #define Wmemset memset
  835. #endif
  836. #ifdef L_memset
  837. Wvoid *Wmemset(Wvoid *s, Wint c, size_t n)
  838. {
  839. register Wuchar *p = (Wuchar *) s;
  840. #ifdef __BCC__
  841. /* bcc can optimize the counter if it thinks it is a pointer... */
  842. register const char *np = (const char *) n;
  843. #else
  844. #define np n
  845. #endif
  846. while (np) {
  847. *p++ = (Wuchar) c;
  848. --np;
  849. }
  850. return s;
  851. }
  852. #undef np
  853. #endif
  854. /**********************************************************************/
  855. #ifdef L_wcslen
  856. #define L_strlen
  857. #define Wstrlen wcslen
  858. #else
  859. #define Wstrlen strlen
  860. #endif
  861. #ifdef L_strlen
  862. size_t Wstrlen(const Wchar *s)
  863. {
  864. register const Wchar *p;
  865. for (p=s ; *p ; p++);
  866. return p - s;
  867. }
  868. #endif
  869. /**********************************************************************/
  870. /* ANSI/ISO end here */
  871. /**********************************************************************/
  872. #ifdef L_ffs
  873. int ffs(int i)
  874. {
  875. #if 1
  876. /* inlined binary search method */
  877. char n = 1;
  878. #if UINT_MAX == 0xffffU
  879. /* nothing to do here -- just trying to avoiding possible problems */
  880. #elif UINT_MAX == 0xffffffffU
  881. if (!(i & 0xffff)) {
  882. n += 16;
  883. i >>= 16;
  884. }
  885. #else
  886. #error ffs needs rewriting!
  887. #endif
  888. if (!(i & 0xff)) {
  889. n += 8;
  890. i >>= 8;
  891. }
  892. if (!(i & 0x0f)) {
  893. n += 4;
  894. i >>= 4;
  895. }
  896. if (!(i & 0x03)) {
  897. n += 2;
  898. i >>= 2;
  899. }
  900. return (i) ? (n + ((i+1) & 0x01)) : 0;
  901. #else
  902. /* linear search -- slow, but small */
  903. int n;
  904. for (n = 0 ; i ; ++n) {
  905. i >>= 1;
  906. }
  907. return n;
  908. #endif
  909. }
  910. #endif
  911. /**********************************************************************/
  912. #ifdef L_wcscasecmp
  913. #define L_strcasecmp
  914. #define Wstrcasecmp wcscasecmp
  915. #else
  916. #define Wstrcasecmp strcasecmp
  917. #endif
  918. #ifdef L_strcasecmp
  919. int Wstrcasecmp(register const Wchar *s1, register const Wchar *s2)
  920. {
  921. #ifdef WANT_WIDE
  922. while ((*s1 == *s2) || (towlower(*s1) == towlower(*s2))) {
  923. if (!*s1++) {
  924. return 0;
  925. }
  926. ++s2;
  927. }
  928. return (((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1;
  929. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  930. #else
  931. int r = 0;
  932. while ( ((s1 == s2) ||
  933. !(r = ((int)( tolower(*((Wuchar *)s1))))
  934. - tolower(*((Wuchar *)s2))))
  935. && (++s2, *s1++));
  936. return r;
  937. #endif
  938. }
  939. #endif
  940. /**********************************************************************/
  941. #ifdef L_wcsncasecmp
  942. #define L_strncasecmp
  943. #define Wstrncasecmp wcsncasecmp
  944. #else
  945. #define Wstrncasecmp strncasecmp
  946. #endif
  947. #ifdef L_strncasecmp
  948. int Wstrncasecmp(register const Wchar *s1, register const Wchar *s2, size_t n)
  949. {
  950. #ifdef WANT_WIDE
  951. while (n && ((*s1 == *s2) || (towlower(*s1) == towlower(*s2)))) {
  952. if (!*s1++) {
  953. return 0;
  954. }
  955. ++s2;
  956. --n;
  957. }
  958. return (n == 0)
  959. ? 0
  960. : ((((Wuchar)towlower(*s1)) < ((Wuchar)towlower(*s2))) ? -1 : 1);
  961. /* TODO -- should wide cmp funcs do wchar or Wuchar compares? */
  962. #else
  963. int r = 0;
  964. while ( n
  965. && ((s1 == s2) ||
  966. !(r = ((int)( tolower(*((unsigned char *)s1))))
  967. - tolower(*((unsigned char *)s2))))
  968. && (--n, ++s2, *s1++));
  969. return r;
  970. #endif
  971. }
  972. #endif
  973. /**********************************************************************/
  974. #ifdef L_wcsnlen
  975. #define L_strnlen
  976. #define Wstrnlen wcsnlen
  977. #else
  978. #define Wstrnlen strnlen
  979. #endif
  980. #ifdef L_strnlen
  981. size_t Wstrnlen(const Wchar *s, size_t max)
  982. {
  983. register const Wchar *p = s;
  984. #ifdef __BCC__
  985. /* bcc can optimize the counter if it thinks it is a pointer... */
  986. register const char *maxp = (const char *) max;
  987. #else
  988. #define maxp max
  989. #endif
  990. while (maxp && *p) {
  991. ++p;
  992. --maxp;
  993. }
  994. return p - s;
  995. }
  996. #undef maxp
  997. #endif
  998. /**********************************************************************/
  999. /* No wide analog. */
  1000. #ifdef L_memccpy
  1001. void *memccpy(void * __restrict s1, const void * __restrict s2, int c, size_t n)
  1002. {
  1003. register char *r1 = s1;
  1004. register const char *r2 = s2;
  1005. while (n-- && (((unsigned char)(*r1++ = *r2++)) != ((unsigned char) c)));
  1006. return (n == (size_t) -1) ? NULL : r1;
  1007. }
  1008. #endif
  1009. /**********************************************************************/
  1010. #ifdef L_wcsdup
  1011. #define L_strdup
  1012. #define Wstrdup wcsdup
  1013. #define Wstrlen wcslen
  1014. #define Wstrcpy wcscpy
  1015. #else
  1016. #define Wstrdup strdup
  1017. #define Wstrlen strlen
  1018. #define Wstrcpy strcpy
  1019. #endif
  1020. #ifdef L_strdup
  1021. Wchar *Wstrdup(register const Wchar *s1)
  1022. {
  1023. register Wchar *s;
  1024. if ((s = malloc((Wstrlen(s1) + 1) * sizeof(Wchar))) != NULL) {
  1025. Wstrcpy(s, s1);
  1026. }
  1027. return s;
  1028. }
  1029. #endif
  1030. /**********************************************************************/
  1031. #ifdef L_strerror
  1032. char *strerror(int errnum)
  1033. {
  1034. static char buf[_SYS_ERRMSG_MAXLEN];
  1035. return (_susv3_strerror_r(errnum, buf, sizeof(buf)) == 0) ? buf : NULL;
  1036. }
  1037. #endif
  1038. /**********************************************************************/
  1039. /* SUSv3 functions. */
  1040. /**********************************************************************/
  1041. #ifdef L__susv3_strerror_r
  1042. int _susv3_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1043. {
  1044. register char *s;
  1045. int i, retval;
  1046. char buf[_SYS_ERRMSG_MAXLEN];
  1047. static const char unknown[14] = {
  1048. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'e', 'r', 'r', 'o', 'r', ' '
  1049. };
  1050. retval = EINVAL;
  1051. if (((unsigned int) errnum) < _SYS_NERR) {
  1052. /* Trade time for space. This function should rarely be called
  1053. * so rather than keeping an array of pointers for the different
  1054. * messages, just run through the buffer until we find the
  1055. * correct string. */
  1056. for (s = (char *) _string_syserrmsgs, i = errnum ; i ; ++s) {
  1057. if (!*s) {
  1058. --i;
  1059. }
  1060. }
  1061. if (*s) { /* Make sure we have an actual message. */
  1062. retval = 0;
  1063. goto GOT_MESG;
  1064. }
  1065. }
  1066. s = _int10tostr(buf+sizeof(buf)-1, errnum) - sizeof(unknown);
  1067. memcpy(s, unknown, sizeof(unknown));
  1068. GOT_MESG:
  1069. if (!strerrbuf) { /* SUSv3 */
  1070. buflen = 0;
  1071. }
  1072. i = strlen(s) + 1;
  1073. if (i > buflen) {
  1074. i = buflen;
  1075. retval = ERANGE;
  1076. }
  1077. if (i) {
  1078. memcpy(strerrbuf, s, i);
  1079. strerrbuf[i-1] = 0; /* In case buf was too small. */
  1080. }
  1081. if (retval) {
  1082. __set_errno(retval);
  1083. }
  1084. return retval;
  1085. }
  1086. #endif
  1087. /**********************************************************************/
  1088. /* GNU extension functions. */
  1089. /**********************************************************************/
  1090. #ifdef L__glibc_strerror_r
  1091. weak_alias(_glibc_strerror_r,__strerror_r);
  1092. char *_glibc_strerror_r(int errnum, char *strerrbuf, size_t buflen)
  1093. {
  1094. _susv3_strerror_r(errnum, strerrbuf, buflen);
  1095. return strerrbuf;
  1096. }
  1097. #endif
  1098. /**********************************************************************/
  1099. #ifdef L_wmempcpy
  1100. #define L_mempcpy
  1101. #define Wmempcpy wmempcpy
  1102. #else
  1103. #define Wmempcpy mempcpy
  1104. #endif
  1105. #ifdef L_mempcpy
  1106. #ifndef L_wmempcpy
  1107. /* uClibc's old string implementation did this to cater to some app. */
  1108. weak_alias(mempcpy,__mempcpy);
  1109. #endif
  1110. Wvoid *Wmempcpy(Wvoid * __restrict s1, const Wvoid * __restrict s2, size_t n)
  1111. {
  1112. register Wchar *r1 = s1;
  1113. register const Wchar *r2 = s2;
  1114. #ifdef __BCC__
  1115. while (n--) {
  1116. *r1++ = *r2++;
  1117. }
  1118. #else
  1119. while (n) {
  1120. *r1++ = *r2++;
  1121. --n;
  1122. }
  1123. #endif
  1124. return r1;
  1125. }
  1126. #endif
  1127. /**********************************************************************/
  1128. #ifdef L_memrchr
  1129. void *memrchr(const void *s, int c, size_t n)
  1130. {
  1131. register const unsigned char *r;
  1132. #ifdef __BCC__
  1133. /* bcc can optimize the counter if it thinks it is a pointer... */
  1134. register const char *np = (const char *) n;
  1135. #else
  1136. #define np n
  1137. #endif
  1138. r = ((unsigned char *)s) + ((size_t) np);
  1139. while (np) {
  1140. if (*--r == ((unsigned char)c)) {
  1141. return (void *) r; /* silence the warning */
  1142. }
  1143. --np;
  1144. }
  1145. return NULL;
  1146. }
  1147. #undef np
  1148. #endif
  1149. /**********************************************************************/
  1150. #ifdef L_wcpcpy
  1151. #define L_stpcpy
  1152. #define Wstpcpy wcpcpy
  1153. #else
  1154. #define Wstpcpy stpcpy
  1155. #endif
  1156. #ifdef L_stpcpy
  1157. Wchar *Wstpcpy(register Wchar * __restrict s1, const Wchar * __restrict s2)
  1158. {
  1159. #ifdef __BCC__
  1160. do {
  1161. *s1 = *s2++;
  1162. } while (*s1++ != 0);
  1163. #else
  1164. while ( (*s1++ = *s2++) != 0 );
  1165. #endif
  1166. return s1 - 1;
  1167. }
  1168. #endif
  1169. /**********************************************************************/
  1170. #ifdef L_wcpncpy
  1171. #define L_stpncpy
  1172. #define Wstpncpy wcpncpy
  1173. #else
  1174. #define Wstpncpy stpncpy
  1175. #endif
  1176. #ifdef L_stpncpy
  1177. Wchar *Wstpncpy(register Wchar * __restrict s1,
  1178. register const Wchar * __restrict s2,
  1179. size_t n)
  1180. {
  1181. Wchar *s = s1;
  1182. const Wchar *p = s2;
  1183. #ifdef __BCC__
  1184. while (n--) {
  1185. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1186. ++s;
  1187. }
  1188. return s1 + (s2 - p);
  1189. #else
  1190. while (n) {
  1191. if ((*s = *s2) != 0) s2++; /* Need to fill tail with 0s. */
  1192. ++s;
  1193. --n;
  1194. }
  1195. return s1 + (s2 - p);
  1196. #endif
  1197. }
  1198. #endif
  1199. /**********************************************************************/
  1200. #ifdef L_bzero
  1201. void bzero(void *s, size_t n)
  1202. {
  1203. register unsigned char *p = s;
  1204. #ifdef __BCC__
  1205. /* bcc can optimize the counter if it thinks it is a pointer... */
  1206. register const char *np = (const char *) n;
  1207. #else
  1208. #define np n
  1209. #endif
  1210. while (np) {
  1211. *p++ = 0;
  1212. --np;
  1213. }
  1214. }
  1215. #undef np
  1216. #endif
  1217. /**********************************************************************/
  1218. #ifdef L_bcopy
  1219. void bcopy(const void *s2, void *s1, size_t n)
  1220. {
  1221. #if 1
  1222. memmove(s1, s2, n);
  1223. #else
  1224. #ifdef __BCC__
  1225. register char *s;
  1226. register const char *p;
  1227. s = s1;
  1228. p = s2;
  1229. if (p >= s) {
  1230. while (n--) {
  1231. *s++ = *p++;
  1232. }
  1233. } else {
  1234. s += n;
  1235. p += n;
  1236. while (n--) {
  1237. *--s = *--p;
  1238. }
  1239. }
  1240. #else
  1241. register char *s;
  1242. register const char *p;
  1243. s = s1;
  1244. p = s2;
  1245. if (p >= s) {
  1246. while (n) {
  1247. *s++ = *p++;
  1248. --n;
  1249. }
  1250. } else {
  1251. while (n) {
  1252. --n;
  1253. s[n] = p[n];
  1254. }
  1255. }
  1256. #endif
  1257. #endif
  1258. }
  1259. #endif
  1260. /**********************************************************************/
  1261. #ifdef L_strcasestr
  1262. char *strcasestr(const char *s1, const char *s2)
  1263. {
  1264. register const char *s = s1;
  1265. register const char *p = s2;
  1266. #if 1
  1267. do {
  1268. if (!*p) {
  1269. return (char *) s1;;
  1270. }
  1271. if ((*p == *s)
  1272. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1273. ) {
  1274. ++p;
  1275. ++s;
  1276. } else {
  1277. p = s2;
  1278. if (!*s) {
  1279. return NULL;
  1280. }
  1281. s = ++s1;
  1282. }
  1283. } while (1);
  1284. #else
  1285. while (*p && *s) {
  1286. if ((*p == *s)
  1287. || (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
  1288. ) {
  1289. ++p;
  1290. ++s;
  1291. } else {
  1292. p = s2;
  1293. s = ++s1;
  1294. }
  1295. }
  1296. return (*p) ? NULL : (char *) s1;
  1297. #endif
  1298. }
  1299. #endif
  1300. /**********************************************************************/
  1301. #ifdef L_strndup
  1302. char *strndup(register const char *s1, size_t n)
  1303. {
  1304. register char *s;
  1305. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  1306. if ((s = malloc(n + 1)) != NULL) {
  1307. memcpy(s, s1, n);
  1308. s[n] = 0;
  1309. }
  1310. return s;
  1311. }
  1312. #endif
  1313. /**********************************************************************/
  1314. #ifdef L_strsep
  1315. char *strsep(char ** __restrict s1, const char * __restrict s2)
  1316. {
  1317. register char *s = *s1;
  1318. register char *p;
  1319. #if 1
  1320. p = NULL;
  1321. if (s && *s && (p = strpbrk(s, s2))) {
  1322. *p++ = 0;
  1323. }
  1324. #else
  1325. if (s && *s && *(p = s + strcspn(s, s2))) {
  1326. *p++ = 0;
  1327. } else {
  1328. p = NULL;
  1329. }
  1330. #endif
  1331. *s1 = p;
  1332. return s;
  1333. }
  1334. #endif
  1335. /**********************************************************************/
  1336. #ifdef L_wcschrnul
  1337. #define L_strchrnul
  1338. #define Wstrchrnul wcschrnul
  1339. #else
  1340. #define Wstrchrnul strchrnul
  1341. #endif
  1342. #ifdef L_strchrnul
  1343. Wchar *Wstrchrnul(register const Wchar *s, Wint c)
  1344. {
  1345. --s;
  1346. while (*++s && (*s != ((Wchar)c)));
  1347. return (Wchar *) s;
  1348. }
  1349. #endif
  1350. /**********************************************************************/
  1351. #ifdef L_rawmemchr
  1352. void *rawmemchr(const void *s, int c)
  1353. {
  1354. register const unsigned char *r = s;
  1355. while (*r != ((unsigned char)c)) ++r;
  1356. return (void *) r; /* silence the warning */
  1357. }
  1358. #endif
  1359. /**********************************************************************/
  1360. #ifdef L_basename
  1361. char *basename(const char *path)
  1362. {
  1363. register const char *s;
  1364. register const char *p;
  1365. p = s = path;
  1366. while (*s) {
  1367. if (*s++ == '/') {
  1368. p = s;
  1369. }
  1370. }
  1371. return (char *) p;
  1372. }
  1373. #endif
  1374. /**********************************************************************/
  1375. #ifdef L___xpg_basename
  1376. char *__xpg_basename(register char *path)
  1377. {
  1378. static const char null_or_empty[] = ".";
  1379. char *first;
  1380. register char *last;
  1381. first = (char *) null_or_empty;
  1382. if (path && *path) {
  1383. first = path;
  1384. last = path - 1;
  1385. do {
  1386. if ((*path != '/') && (path > ++last)) {
  1387. last = first = path;
  1388. }
  1389. } while (*++path);
  1390. if (*first == '/') {
  1391. last = first;
  1392. }
  1393. last[1] = 0;
  1394. }
  1395. return first;
  1396. }
  1397. #endif
  1398. /**********************************************************************/
  1399. #ifdef L_dirname
  1400. char *dirname(char *path)
  1401. {
  1402. static const char null_or_empty_or_noslash[] = ".";
  1403. register char *s;
  1404. register char *last;
  1405. char *first;
  1406. last = s = path;
  1407. if (s != NULL) {
  1408. LOOP:
  1409. while (*s && (*s != '/')) ++s;
  1410. first = s;
  1411. while (*s == '/') ++s;
  1412. if (*s) {
  1413. last = first;
  1414. goto LOOP;
  1415. }
  1416. if (last == path) {
  1417. if (*last != '/') {
  1418. goto DOT;
  1419. }
  1420. if ((*++last == '/') && (last[1] == 0)) {
  1421. ++last;
  1422. }
  1423. }
  1424. *last = 0;
  1425. return path;
  1426. }
  1427. DOT:
  1428. return (char *) null_or_empty_or_noslash;
  1429. }
  1430. #endif
  1431. /**********************************************************************/
  1432. #ifdef L_strlcat
  1433. /* OpenBSD function:
  1434. * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst.
  1435. * Returns strlen(src) + strlen({original} dst), so truncation occurred if the
  1436. * return val is >= n.
  1437. * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is
  1438. * taken as n. */
  1439. size_t strlcat(register char *__restrict dst,
  1440. register const char *__restrict src,
  1441. size_t n)
  1442. {
  1443. size_t len;
  1444. char dummy[1];
  1445. len = 0;
  1446. while (1) {
  1447. if (len >= n) {
  1448. dst = dummy;
  1449. break;
  1450. }
  1451. if (!*dst) {
  1452. break;
  1453. }
  1454. ++dst;
  1455. ++len;
  1456. }
  1457. while ((*dst = *src) != 0) {
  1458. if (++len < n) {
  1459. ++dst;
  1460. }
  1461. ++src;
  1462. }
  1463. return len;
  1464. }
  1465. #endif
  1466. /**********************************************************************/
  1467. #ifdef L_strlcpy
  1468. /* OpenBSD function:
  1469. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  1470. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  1471. size_t strlcpy(register char *__restrict dst,
  1472. register const char *__restrict src,
  1473. size_t n)
  1474. {
  1475. const char *src0 = src;
  1476. char dummy[1];
  1477. if (!n) {
  1478. dst = dummy;
  1479. } else {
  1480. --n;
  1481. }
  1482. while ((*dst = *src) != 0) {
  1483. if (n) {
  1484. --n;
  1485. ++dst;
  1486. }
  1487. ++src;
  1488. }
  1489. return src - src0;
  1490. }
  1491. #endif
  1492. /**********************************************************************/
  1493. #ifdef L__string_syssigmsgs
  1494. const char _string_syssigmsgs[] = {
  1495. /* 0: 0, 1 */ "\0"
  1496. /* 1: 1, 7 */ "Hangup\0"
  1497. /* 2: 8, 10 */ "Interrupt\0"
  1498. /* 3: 18, 5 */ "Quit\0"
  1499. /* 4: 23, 20 */ "Illegal instruction\0"
  1500. /* 5: 43, 22 */ "Trace/breakpoint trap\0"
  1501. /* 6: 65, 8 */ "Aborted\0"
  1502. /* 7: 73, 10 */ "Bus error\0"
  1503. /* 8: 83, 25 */ "Floating point exception\0"
  1504. /* 9: 108, 7 */ "Killed\0"
  1505. /* 10: 115, 22 */ "User defined signal 1\0"
  1506. /* 11: 137, 19 */ "Segmentation fault\0"
  1507. /* 12: 156, 22 */ "User defined signal 2\0"
  1508. /* 13: 178, 12 */ "Broken pipe\0"
  1509. /* 14: 190, 12 */ "Alarm clock\0"
  1510. /* 15: 202, 11 */ "Terminated\0"
  1511. /* 16: 213, 12 */ "Stack fault\0"
  1512. /* 17: 225, 13 */ "Child exited\0"
  1513. /* 18: 238, 10 */ "Continued\0"
  1514. /* 19: 248, 17 */ "Stopped (signal)\0"
  1515. /* 20: 265, 8 */ "Stopped\0"
  1516. /* 21: 273, 20 */ "Stopped (tty input)\0"
  1517. /* 22: 293, 21 */ "Stopped (tty output)\0"
  1518. /* 23: 314, 21 */ "Urgent I/O condition\0"
  1519. /* 24: 335, 24 */ "CPU time limit exceeded\0"
  1520. /* 25: 359, 25 */ "File size limit exceeded\0"
  1521. /* 26: 384, 22 */ "Virtual timer expired\0"
  1522. /* 27: 406, 24 */ "Profiling timer expired\0"
  1523. /* 28: 430, 15 */ "Window changed\0"
  1524. /* 29: 445, 13 */ "I/O possible\0"
  1525. /* 30: 458, 14 */ "Power failure\0"
  1526. /* 31: 472, 16 */ "Bad system call"
  1527. };
  1528. #endif
  1529. /**********************************************************************/
  1530. #ifdef L_sys_siglist
  1531. const char *const sys_siglist[_NSIG] = {
  1532. NULL,
  1533. _string_syssigmsgs + 1,
  1534. _string_syssigmsgs + 8,
  1535. _string_syssigmsgs + 18,
  1536. _string_syssigmsgs + 23,
  1537. _string_syssigmsgs + 43,
  1538. _string_syssigmsgs + 65,
  1539. _string_syssigmsgs + 73,
  1540. _string_syssigmsgs + 83,
  1541. _string_syssigmsgs + 108,
  1542. _string_syssigmsgs + 115,
  1543. _string_syssigmsgs + 137,
  1544. _string_syssigmsgs + 156,
  1545. _string_syssigmsgs + 178,
  1546. _string_syssigmsgs + 190,
  1547. _string_syssigmsgs + 202,
  1548. _string_syssigmsgs + 213,
  1549. _string_syssigmsgs + 225,
  1550. _string_syssigmsgs + 238,
  1551. _string_syssigmsgs + 248,
  1552. _string_syssigmsgs + 265,
  1553. _string_syssigmsgs + 273,
  1554. _string_syssigmsgs + 293,
  1555. _string_syssigmsgs + 314,
  1556. _string_syssigmsgs + 335,
  1557. _string_syssigmsgs + 359,
  1558. _string_syssigmsgs + 384,
  1559. _string_syssigmsgs + 406,
  1560. _string_syssigmsgs + 430,
  1561. _string_syssigmsgs + 445,
  1562. _string_syssigmsgs + 458,
  1563. _string_syssigmsgs + 472,
  1564. NULL,
  1565. NULL,
  1566. NULL,
  1567. NULL,
  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. };
  1597. #endif
  1598. /**********************************************************************/
  1599. #ifdef L_strsignal
  1600. /* TODO: make a threadsafe version? */
  1601. char *strsignal(int signum)
  1602. {
  1603. register char *s;
  1604. int i;
  1605. static char buf[_STRSIGNAL_BUFSIZE];
  1606. static const char unknown[15] = {
  1607. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  1608. };
  1609. if (((unsigned int) signum) < _SYS_NSIG) {
  1610. /* Trade time for space. This function should rarely be called
  1611. * so rather than keeping an array of pointers for the different
  1612. * messages, just run through the buffer until we find the
  1613. * correct string. */
  1614. for (s = (char *) _string_syssigmsgs, i = signum ; i ; ++s) {
  1615. if (!*s) {
  1616. --i;
  1617. }
  1618. }
  1619. if (*s) { /* Make sure we have an actual message. */
  1620. goto DONE;
  1621. }
  1622. }
  1623. s = _int10tostr(buf+sizeof(buf)-1, signum) - sizeof(unknown);
  1624. memcpy(s, unknown, sizeof(unknown));
  1625. DONE:
  1626. return s;
  1627. }
  1628. #endif
  1629. /**********************************************************************/
  1630. #ifdef L_psignal
  1631. /* TODO: make this threadsafe with a reentrant version of strsignal? */
  1632. void psignal(int signum, register const char *message)
  1633. {
  1634. /* If the program is calling psignal, it's a safe bet that printf and
  1635. * friends are used as well. It is also possible that the calling
  1636. * program could buffer stderr, or reassign it. */
  1637. register const char *sep;
  1638. sep = ": ";
  1639. if (!(message && *message)) { /* Caller did not supply a prefix message */
  1640. message = (sep += 2); /* or passed an empty string. */
  1641. }
  1642. #if 1
  1643. fprintf(stderr, "%s%s%s\n", message, sep, strsignal(signum));
  1644. #else
  1645. /* Note: Assumes stderr not closed or buffered. */
  1646. __STDIO_THREADLOCK(stderr);
  1647. _stdio_fdout(STDERR_FILENO, message, sep, strsignal(signum));
  1648. __STDIO_THREADUNLOCK(stderr);
  1649. #endif
  1650. }
  1651. #endif
  1652. /**********************************************************************/