wstring.c 36 KB

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