stdlib.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. * From my (incomplete) stdlib library for linux and (soon) elks.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  19. *
  20. * This code is currently under development. Also, I plan to port
  21. * it to elks which is a 16-bit environment with a fairly limited
  22. * compiler. Therefore, please refrain from modifying this code
  23. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  24. *
  25. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  26. /* Oct 29, 2002
  27. * Fix a couple of 'restrict' bugs in mbstowcs and wcstombs.
  28. *
  29. * Nov 21, 2002
  30. * Add wscto{inttype} functions.
  31. */
  32. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  33. #include <limits.h>
  34. /* Work around gcc's refusal to create aliases.
  35. * TODO: Add in a define to disable the aliases? */
  36. #if UINT_MAX == ULONG_MAX
  37. #ifdef L_labs
  38. #define abs __ignore_abs
  39. #endif
  40. #ifdef L_atol
  41. #define atoi __ignore_atoi
  42. #endif
  43. #endif
  44. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  45. #ifdef L_labs
  46. #define llabs __ignore_llabs
  47. #endif
  48. #ifdef L_atol
  49. #define atoll __ignore_atoll
  50. #endif
  51. #ifdef L_strtol
  52. #define strtoll __ignore_strtoll
  53. #endif
  54. #ifdef L_strtoul
  55. #define strtoull __ignore_strtoull
  56. #endif
  57. #ifdef L_wcstol
  58. #define wcstoll __ignore_wcstoll
  59. #endif
  60. #ifdef L_wcstoul
  61. #define wcstoull __ignore_wcstoull
  62. #endif
  63. #ifdef strtol_l
  64. #define strtoll_l __ignore_strtoll_l
  65. #endif
  66. #ifdef L_strtoul_l
  67. #define strtoull_l __ignore_strtoull_l
  68. #endif
  69. #ifdef L_wcstol_l
  70. #define wcstoll_l __ignore_wcstoll_l
  71. #endif
  72. #ifdef L_wcstoul_l
  73. #define wcstoull_l __ignore_wcstoull_l
  74. #endif
  75. #endif
  76. #if defined(ULLONG_MAX) && (ULLONG_MAX == UINTMAX_MAX)
  77. #if defined L_labs || defined L_llabs
  78. #define imaxabs __ignore_imaxabs
  79. #endif
  80. #endif
  81. #include <stdint.h>
  82. #include <inttypes.h>
  83. #include <ctype.h>
  84. #include <errno.h>
  85. #include <assert.h>
  86. #include <unistd.h>
  87. #include <stdlib.h>
  88. #include <locale.h>
  89. #ifdef __UCLIBC_HAS_WCHAR__
  90. #include <wchar.h>
  91. #include <wctype.h>
  92. #include <bits/uClibc_uwchar.h>
  93. #ifdef __UCLIBC_HAS_XLOCALE__
  94. #include <xlocale.h>
  95. #endif /* __UCLIBC_HAS_XLOCALE__ */
  96. /* TODO: clean up the following... */
  97. #if WCHAR_MAX > 0xffffUL
  98. #define UTF_8_MAX_LEN 6
  99. #else
  100. #define UTF_8_MAX_LEN 3
  101. #endif
  102. #ifdef __UCLIBC_HAS_LOCALE__
  103. #define ENCODING ((__UCLIBC_CURLOCALE_DATA).encoding)
  104. #ifndef __CTYPE_HAS_UTF_8_LOCALES
  105. #ifdef L_mblen
  106. /* emit only once */
  107. #warning __CTYPE_HAS_UTF_8_LOCALES not set!
  108. #endif
  109. #endif
  110. #else /* __UCLIBC_HAS_LOCALE__ */
  111. #ifdef __UCLIBC_MJN3_ONLY__
  112. #ifdef L_mblen
  113. /* emit only once */
  114. #warning devel checks
  115. #endif
  116. #endif
  117. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  118. #error __CTYPE_HAS_8_BIT_LOCALES is defined!
  119. #endif
  120. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  121. #error __CTYPE_HAS_UTF_8_LOCALES is defined!
  122. #endif
  123. #endif
  124. #endif /* __UCLIBC_HAS_LOCALE__ */
  125. /**********************************************************************/
  126. #ifdef __UCLIBC_HAS_XLOCALE__
  127. extern unsigned long
  128. _stdlib_strto_l_l(register const char * __restrict str,
  129. char ** __restrict endptr, int base, int sflag,
  130. __locale_t locale_arg) attribute_hidden;
  131. #if defined(ULLONG_MAX)
  132. extern unsigned long long
  133. _stdlib_strto_ll_l(register const char * __restrict str,
  134. char ** __restrict endptr, int base, int sflag,
  135. __locale_t locale_arg) attribute_hidden;
  136. #endif
  137. #ifdef __UCLIBC_HAS_WCHAR__
  138. extern unsigned long
  139. _stdlib_wcsto_l_l(register const wchar_t * __restrict str,
  140. wchar_t ** __restrict endptr, int base, int sflag,
  141. __locale_t locale_arg) attribute_hidden;
  142. #if defined(ULLONG_MAX)
  143. extern unsigned long long
  144. _stdlib_wcsto_ll_l(register const wchar_t * __restrict str,
  145. wchar_t ** __restrict endptr, int base, int sflag,
  146. __locale_t locale_arg) attribute_hidden;
  147. #endif
  148. #endif /* __UCLIBC_HAS_WCHAR__ */
  149. #endif /* __UCLIBC_HAS_XLOCALE__ */
  150. extern unsigned long
  151. _stdlib_strto_l(register const char * __restrict str,
  152. char ** __restrict endptr, int base, int sflag) attribute_hidden;
  153. #if defined(ULLONG_MAX)
  154. extern unsigned long long
  155. _stdlib_strto_ll(register const char * __restrict str,
  156. char ** __restrict endptr, int base, int sflag) attribute_hidden;
  157. #endif
  158. #ifdef __UCLIBC_HAS_WCHAR__
  159. extern unsigned long
  160. _stdlib_wcsto_l(register const wchar_t * __restrict str,
  161. wchar_t ** __restrict endptr, int base, int sflag) attribute_hidden;
  162. #if defined(ULLONG_MAX)
  163. extern unsigned long long
  164. _stdlib_wcsto_ll(register const wchar_t * __restrict str,
  165. wchar_t ** __restrict endptr, int base, int sflag) attribute_hidden;
  166. #endif
  167. #endif /* __UCLIBC_HAS_WCHAR__ */
  168. /**********************************************************************/
  169. #ifdef L_atof
  170. libc_hidden_proto(strtod)
  171. double atof(const char *nptr)
  172. {
  173. return strtod(nptr, (char **) NULL);
  174. }
  175. #endif
  176. /**********************************************************************/
  177. #ifdef L_abs
  178. #if INT_MAX < LONG_MAX
  179. int abs(int j)
  180. {
  181. return (j >= 0) ? j : -j;
  182. }
  183. #endif /* INT_MAX < LONG_MAX */
  184. #endif
  185. /**********************************************************************/
  186. #ifdef L_labs
  187. long int labs(long int j)
  188. {
  189. return (j >= 0) ? j : -j;
  190. }
  191. #if UINT_MAX == ULONG_MAX
  192. #undef abs
  193. extern __typeof(labs) abs;
  194. strong_alias(labs,abs)
  195. #endif
  196. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  197. #undef llabs
  198. extern __typeof(labs) llabs;
  199. strong_alias(labs,llabs)
  200. #endif
  201. #if ULONG_MAX == UINTMAX_MAX
  202. #undef imaxabs
  203. extern __typeof(labs) imaxabs;
  204. strong_alias(labs,imaxabs)
  205. #endif
  206. #endif
  207. /**********************************************************************/
  208. #ifdef L_llabs
  209. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  210. long long int llabs(long long int j)
  211. {
  212. return (j >= 0) ? j : -j;
  213. }
  214. #if (ULLONG_MAX == UINTMAX_MAX)
  215. #undef imaxabs
  216. extern __typeof(llabs) imaxabs;
  217. strong_alias(llabs,imaxabs)
  218. #endif
  219. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  220. #endif
  221. /**********************************************************************/
  222. #ifdef L_atoi
  223. #if INT_MAX < LONG_MAX
  224. libc_hidden_proto(strtol)
  225. libc_hidden_proto(atoi)
  226. int atoi(const char *nptr)
  227. {
  228. return (int) strtol(nptr, (char **) NULL, 10);
  229. }
  230. libc_hidden_def(atoi)
  231. #endif /* INT_MAX < LONG_MAX */
  232. #endif
  233. /**********************************************************************/
  234. #ifdef L_atol
  235. libc_hidden_proto(strtol)
  236. libc_hidden_proto(atol)
  237. long atol(const char *nptr)
  238. {
  239. return strtol(nptr, (char **) NULL, 10);
  240. }
  241. libc_hidden_def(atol)
  242. #if UINT_MAX == ULONG_MAX
  243. #undef atoi
  244. extern __typeof(atol) atoi;
  245. libc_hidden_proto(atoi)
  246. strong_alias(atol,atoi)
  247. libc_hidden_def(atoi)
  248. #endif
  249. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  250. #undef atoll
  251. extern __typeof(atol) atoll;
  252. strong_alias(atol,atoll)
  253. #endif
  254. #endif
  255. /**********************************************************************/
  256. #ifdef L_atoll
  257. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  258. libc_hidden_proto(strtoll)
  259. long long atoll(const char *nptr)
  260. {
  261. return strtoll(nptr, (char **) NULL, 10);
  262. }
  263. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  264. #endif
  265. /**********************************************************************/
  266. #if defined(L_strtol) || defined(L_strtol_l)
  267. libc_hidden_proto(__XL_NPP(strtol))
  268. long __XL_NPP(strtol)(const char * __restrict str, char ** __restrict endptr,
  269. int base __LOCALE_PARAM )
  270. {
  271. return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 1 __LOCALE_ARG );
  272. }
  273. libc_hidden_def(__XL_NPP(strtol))
  274. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtol_l)
  275. strong_alias(strtol,strtoimax)
  276. #endif
  277. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  278. #ifdef L_strtol_l
  279. #undef strtoll_l
  280. #else
  281. #undef strtoll
  282. #endif
  283. extern __typeof(strtol) __XL_NPP(strtoll);
  284. libc_hidden_proto(__XL_NPP(strtoll))
  285. strong_alias(__XL_NPP(strtol),__XL_NPP(strtoll))
  286. libc_hidden_def(__XL_NPP(strtoll))
  287. #endif
  288. #endif
  289. /**********************************************************************/
  290. #if defined(L_strtoll) || defined(L_strtoll_l)
  291. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  292. libc_hidden_proto(__XL_NPP(strtoll))
  293. long long __XL_NPP(strtoll)(const char * __restrict str,
  294. char ** __restrict endptr, int base
  295. __LOCALE_PARAM )
  296. {
  297. return (long long) __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 1
  298. __LOCALE_ARG );
  299. }
  300. libc_hidden_def(__XL_NPP(strtoll))
  301. #if !defined(L_strtoll_l)
  302. #if (ULLONG_MAX == UINTMAX_MAX)
  303. strong_alias(strtoll,strtoimax)
  304. #endif
  305. strong_alias(strtoll,strtoq)
  306. #endif
  307. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  308. #endif
  309. /**********************************************************************/
  310. #if defined(L_strtoul) || defined(L_strtoul_l)
  311. libc_hidden_proto(__XL_NPP(strtoul))
  312. unsigned long __XL_NPP(strtoul)(const char * __restrict str,
  313. char ** __restrict endptr, int base
  314. __LOCALE_PARAM )
  315. {
  316. return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 0 __LOCALE_ARG );
  317. }
  318. libc_hidden_def(__XL_NPP(strtoul))
  319. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtoul_l)
  320. strong_alias(strtoul,strtoumax)
  321. #endif
  322. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  323. #ifdef L_strtoul_l
  324. #undef strtoull_l
  325. #else
  326. #undef strtoull
  327. #endif
  328. extern __typeof(strtoul) __XL_NPP(strtoull);
  329. libc_hidden_proto(__XL_NPP(strtoull))
  330. strong_alias(__XL_NPP(strtoul),__XL_NPP(strtoull))
  331. libc_hidden_def(__XL_NPP(strtoull))
  332. #endif
  333. #endif
  334. /**********************************************************************/
  335. #if defined(L_strtoull) || defined(L_strtoull_l)
  336. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  337. libc_hidden_proto(__XL_NPP(strtoull))
  338. unsigned long long __XL_NPP(strtoull)(const char * __restrict str,
  339. char ** __restrict endptr, int base
  340. __LOCALE_PARAM )
  341. {
  342. return __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 0 __LOCALE_ARG );
  343. }
  344. libc_hidden_def(__XL_NPP(strtoull))
  345. #if !defined(L_strtoull_l)
  346. #if (ULLONG_MAX == UINTMAX_MAX)
  347. strong_alias(strtoull,strtoumax)
  348. #endif
  349. strong_alias(strtoull,strtouq)
  350. #endif
  351. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  352. #endif
  353. /**********************************************************************/
  354. /* Support routines follow */
  355. /**********************************************************************/
  356. /* Set if we want errno set appropriately. */
  357. /* NOTE: Implies _STRTO_ENDPTR below */
  358. #define _STRTO_ERRNO 1
  359. /* Set if we want support for the endptr arg. */
  360. /* Implied by _STRTO_ERRNO. */
  361. #define _STRTO_ENDPTR 1
  362. #if _STRTO_ERRNO
  363. #undef _STRTO_ENDPTR
  364. #define _STRTO_ENDPTR 1
  365. #define SET_ERRNO(X) __set_errno(X)
  366. #else
  367. #define SET_ERRNO(X) ((void)(X)) /* keep side effects */
  368. #endif
  369. /**********************************************************************/
  370. #if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)
  371. #ifndef L__stdlib_strto_l
  372. #define L__stdlib_strto_l
  373. #endif
  374. #endif
  375. #if defined(L__stdlib_strto_l) || defined(L__stdlib_strto_l_l)
  376. #if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)
  377. #define _stdlib_strto_l _stdlib_wcsto_l
  378. #define _stdlib_strto_l_l _stdlib_wcsto_l_l
  379. #define Wchar wchar_t
  380. #define Wuchar __uwchar_t
  381. #ifdef __UCLIBC_DO_XLOCALE
  382. #define ISSPACE(C) iswspace_l((C), locale_arg)
  383. libc_hidden_proto(iswspace_l)
  384. #else
  385. #define ISSPACE(C) iswspace((C))
  386. libc_hidden_proto(iswspace)
  387. #endif
  388. #else /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
  389. #define Wchar char
  390. #define Wuchar unsigned char
  391. #ifdef __UCLIBC_DO_XLOCALE
  392. #define ISSPACE(C) isspace_l((C), locale_arg)
  393. libc_hidden_proto(isspace_l)
  394. #else
  395. #define ISSPACE(C) isspace((C))
  396. libc_hidden_proto(isspace)
  397. #endif
  398. #endif /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
  399. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  400. unsigned long attribute_hidden _stdlib_strto_l(register const Wchar * __restrict str,
  401. Wchar ** __restrict endptr, int base,
  402. int sflag)
  403. {
  404. return _stdlib_strto_l_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
  405. }
  406. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  407. /* This is the main work fuction which handles both strtol (sflag = 1) and
  408. * strtoul (sflag = 0). */
  409. #ifdef __UCLIBC_HAS_XLOCALE__
  410. libc_hidden_proto(__ctype_b_loc)
  411. #elif __UCLIBC_HAS_CTYPE_TABLES__
  412. libc_hidden_proto(__ctype_b)
  413. #endif
  414. unsigned long attribute_hidden __XL_NPP(_stdlib_strto_l)(register const Wchar * __restrict str,
  415. Wchar ** __restrict endptr, int base,
  416. int sflag __LOCALE_PARAM )
  417. {
  418. unsigned long number, cutoff;
  419. #if _STRTO_ENDPTR
  420. const Wchar *fail_char;
  421. #define SET_FAIL(X) fail_char = (X)
  422. #else
  423. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  424. #endif
  425. unsigned char negative, digit, cutoff_digit;
  426. assert(((unsigned int)sflag) <= 1);
  427. SET_FAIL(str);
  428. while (ISSPACE(*str)) { /* Skip leading whitespace. */
  429. ++str;
  430. }
  431. /* Handle optional sign. */
  432. negative = 0;
  433. switch(*str) {
  434. case '-': negative = 1; /* Fall through to increment str. */
  435. case '+': ++str;
  436. }
  437. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  438. base += 10; /* Default is 10 (26). */
  439. if (*str == '0') {
  440. SET_FAIL(++str);
  441. base -= 2; /* Now base is 8 or 16 (24). */
  442. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  443. ++str;
  444. base += base; /* Base is 16 (16 or 48). */
  445. }
  446. }
  447. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  448. base = 16;
  449. }
  450. }
  451. number = 0;
  452. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  453. cutoff_digit = ULONG_MAX % base;
  454. cutoff = ULONG_MAX / base;
  455. do {
  456. digit = (((Wuchar)(*str - '0')) <= 9)
  457. ? (*str - '0')
  458. : ((*str >= 'A')
  459. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  460. : 40);
  461. if (digit >= base) {
  462. break;
  463. }
  464. SET_FAIL(++str);
  465. if ((number > cutoff)
  466. || ((number == cutoff) && (digit > cutoff_digit))) {
  467. number = ULONG_MAX;
  468. negative &= sflag;
  469. SET_ERRNO(ERANGE);
  470. } else {
  471. number = number * base + digit;
  472. }
  473. } while (1);
  474. }
  475. #if _STRTO_ENDPTR
  476. if (endptr) {
  477. *endptr = (Wchar *) fail_char;
  478. }
  479. #endif
  480. {
  481. unsigned long tmp = ((negative)
  482. ? ((unsigned long)(-(1+LONG_MIN)))+1
  483. : LONG_MAX);
  484. if (sflag && (number > tmp)) {
  485. number = tmp;
  486. SET_ERRNO(ERANGE);
  487. }
  488. }
  489. return negative ? (unsigned long)(-((long)number)) : number;
  490. }
  491. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  492. #endif
  493. /**********************************************************************/
  494. #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
  495. #ifndef L__stdlib_strto_ll
  496. #define L__stdlib_strto_ll
  497. #endif
  498. #endif
  499. #if defined(L__stdlib_strto_ll) || defined(L__stdlib_strto_ll_l)
  500. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  501. #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
  502. #define _stdlib_strto_ll _stdlib_wcsto_ll
  503. #define _stdlib_strto_ll_l _stdlib_wcsto_ll_l
  504. #define Wchar wchar_t
  505. #define Wuchar __uwchar_t
  506. #ifdef __UCLIBC_DO_XLOCALE
  507. #define ISSPACE(C) iswspace_l((C), locale_arg)
  508. libc_hidden_proto(iswspace_l)
  509. #else
  510. #define ISSPACE(C) iswspace((C))
  511. libc_hidden_proto(iswspace)
  512. #endif
  513. #else /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
  514. #define Wchar char
  515. #define Wuchar unsigned char
  516. #ifdef __UCLIBC_DO_XLOCALE
  517. #define ISSPACE(C) isspace_l((C), locale_arg)
  518. libc_hidden_proto(isspace_l)
  519. #else
  520. #define ISSPACE(C) isspace((C))
  521. libc_hidden_proto(isspace)
  522. #endif
  523. #endif /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
  524. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  525. unsigned long long attribute_hidden _stdlib_strto_ll(register const Wchar * __restrict str,
  526. Wchar ** __restrict endptr, int base,
  527. int sflag)
  528. {
  529. return _stdlib_strto_ll_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
  530. }
  531. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  532. #if !defined __UCLIBC_HAS_XLOCALE__ && defined __UCLIBC_HAS_CTYPE_TABLES__
  533. libc_hidden_proto(__ctype_b)
  534. #endif
  535. /* This is the main work fuction which handles both strtoll (sflag = 1) and
  536. * strtoull (sflag = 0). */
  537. unsigned long long attribute_hidden __XL_NPP(_stdlib_strto_ll)(register const Wchar * __restrict str,
  538. Wchar ** __restrict endptr, int base,
  539. int sflag __LOCALE_PARAM )
  540. {
  541. unsigned long long number;
  542. #if _STRTO_ENDPTR
  543. const Wchar *fail_char;
  544. #define SET_FAIL(X) fail_char = (X)
  545. #else
  546. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  547. #endif
  548. unsigned int n1;
  549. unsigned char negative, digit;
  550. assert(((unsigned int)sflag) <= 1);
  551. SET_FAIL(str);
  552. while (ISSPACE(*str)) { /* Skip leading whitespace. */
  553. ++str;
  554. }
  555. /* Handle optional sign. */
  556. negative = 0;
  557. switch(*str) {
  558. case '-': negative = 1; /* Fall through to increment str. */
  559. case '+': ++str;
  560. }
  561. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  562. base += 10; /* Default is 10 (26). */
  563. if (*str == '0') {
  564. SET_FAIL(++str);
  565. base -= 2; /* Now base is 8 or 16 (24). */
  566. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  567. ++str;
  568. base += base; /* Base is 16 (16 or 48). */
  569. }
  570. }
  571. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  572. base = 16;
  573. }
  574. }
  575. number = 0;
  576. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  577. do {
  578. digit = (((Wuchar)(*str - '0')) <= 9)
  579. ? (*str - '0')
  580. : ((*str >= 'A')
  581. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  582. : 40);
  583. if (digit >= base) {
  584. break;
  585. }
  586. SET_FAIL(++str);
  587. #if 1
  588. /* Optional, but speeds things up in the usual case. */
  589. if (number <= (ULLONG_MAX >> 6)) {
  590. number = number * base + digit;
  591. } else
  592. #endif
  593. {
  594. n1 = ((unsigned char) number) * base + digit;
  595. number = (number >> CHAR_BIT) * base;
  596. if (number + (n1 >> CHAR_BIT) <= (ULLONG_MAX >> CHAR_BIT)) {
  597. number = (number << CHAR_BIT) + n1;
  598. } else { /* Overflow. */
  599. number = ULLONG_MAX;
  600. negative &= sflag;
  601. SET_ERRNO(ERANGE);
  602. }
  603. }
  604. } while (1);
  605. }
  606. #if _STRTO_ENDPTR
  607. if (endptr) {
  608. *endptr = (Wchar *) fail_char;
  609. }
  610. #endif
  611. {
  612. unsigned long long tmp = ((negative)
  613. ? ((unsigned long long)(-(1+LLONG_MIN)))+1
  614. : LLONG_MAX);
  615. if (sflag && (number > tmp)) {
  616. number = tmp;
  617. SET_ERRNO(ERANGE);
  618. }
  619. }
  620. return negative ? (unsigned long long)(-((long long)number)) : number;
  621. }
  622. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  623. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  624. #endif
  625. /**********************************************************************/
  626. /* Made _Exit() an alias for _exit(), as per C99. */
  627. /* #ifdef L__Exit */
  628. /* libc_hidden_proto(_exit) */
  629. /* void _Exit(int status) */
  630. /* { */
  631. /* _exit(status); */
  632. /* } */
  633. /* #endif */
  634. /**********************************************************************/
  635. #ifdef L_bsearch
  636. void *bsearch(const void *key, const void *base, size_t /* nmemb */ high,
  637. size_t size, int (*compar)(const void *, const void *))
  638. {
  639. register char *p;
  640. size_t low;
  641. size_t mid;
  642. int r;
  643. if (size > 0) { /* TODO: change this to an assert?? */
  644. low = 0;
  645. while (low < high) {
  646. mid = low + ((high - low) >> 1); /* Avoid possible overflow here. */
  647. p = ((char *)base) + mid * size; /* Could overflow here... */
  648. r = (*compar)(key, p); /* but that's an application problem! */
  649. if (r > 0) {
  650. low = mid + 1;
  651. } else if (r < 0) {
  652. high = mid;
  653. } else {
  654. return p;
  655. }
  656. }
  657. }
  658. return NULL;
  659. }
  660. #endif
  661. /**********************************************************************/
  662. #ifdef L_qsort
  663. /* This code is derived from a public domain shell sort routine by
  664. * Ray Gardner and found in Bob Stout's snippets collection. The
  665. * original code is included below in an #if 0/#endif block.
  666. *
  667. * I modified it to avoid the possibility of overflow in the wgap
  668. * calculation, as well as to reduce the generated code size with
  669. * bcc and gcc. */
  670. libc_hidden_proto(qsort)
  671. void qsort (void *base,
  672. size_t nel,
  673. size_t width,
  674. int (*comp)(const void *, const void *))
  675. {
  676. size_t wgap, i, j, k;
  677. char tmp;
  678. if ((nel > 1) && (width > 0)) {
  679. assert( nel <= ((size_t)(-1)) / width ); /* check for overflow */
  680. wgap = 0;
  681. do {
  682. wgap = 3 * wgap + 1;
  683. } while (wgap < (nel-1)/3);
  684. /* From the above, we know that either wgap == 1 < nel or */
  685. /* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap < nel. */
  686. wgap *= width; /* So this can not overflow if wnel doesn't. */
  687. nel *= width; /* Convert nel to 'wnel' */
  688. do {
  689. i = wgap;
  690. do {
  691. j = i;
  692. do {
  693. register char *a;
  694. register char *b;
  695. j -= wgap;
  696. a = j + ((char *)base);
  697. b = a + wgap;
  698. if ( (*comp)(a, b) <= 0 ) {
  699. break;
  700. }
  701. k = width;
  702. do {
  703. tmp = *a;
  704. *a++ = *b;
  705. *b++ = tmp;
  706. } while ( --k );
  707. } while (j >= wgap);
  708. i += width;
  709. } while (i < nel);
  710. wgap = (wgap - width)/3;
  711. } while (wgap);
  712. }
  713. }
  714. libc_hidden_def(qsort)
  715. /* ---------- original snippets version below ---------- */
  716. #if 0
  717. /*
  718. ** ssort() -- Fast, small, qsort()-compatible Shell sort
  719. **
  720. ** by Ray Gardner, public domain 5/90
  721. */
  722. #include <stddef.h>
  723. void ssort (void *base,
  724. size_t nel,
  725. size_t width,
  726. int (*comp)(const void *, const void *))
  727. {
  728. size_t wnel, gap, wgap, i, j, k;
  729. char *a, *b, tmp;
  730. wnel = width * nel;
  731. for (gap = 0; ++gap < nel;)
  732. gap *= 3;
  733. while ( gap /= 3 )
  734. {
  735. wgap = width * gap;
  736. for (i = wgap; i < wnel; i += width)
  737. {
  738. for (j = i - wgap; ;j -= wgap)
  739. {
  740. a = j + (char *)base;
  741. b = a + wgap;
  742. if ( (*comp)(a, b) <= 0 )
  743. break;
  744. k = width;
  745. do
  746. {
  747. tmp = *a;
  748. *a++ = *b;
  749. *b++ = tmp;
  750. } while ( --k );
  751. if (j < wgap)
  752. break;
  753. }
  754. }
  755. }
  756. }
  757. #endif
  758. #endif
  759. /**********************************************************************/
  760. #ifdef L__stdlib_mb_cur_max
  761. libc_hidden_proto(_stdlib_mb_cur_max)
  762. size_t _stdlib_mb_cur_max(void)
  763. {
  764. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  765. return __UCLIBC_CURLOCALE_DATA.mb_cur_max;
  766. #else
  767. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  768. #ifdef __UCLIBC_MJN3_ONLY__
  769. #warning need to change this when/if transliteration is implemented
  770. #endif
  771. #endif
  772. return 1;
  773. #endif
  774. }
  775. libc_hidden_def(_stdlib_mb_cur_max)
  776. #endif
  777. /**********************************************************************/
  778. #ifdef L_mblen
  779. libc_hidden_proto(mbrlen)
  780. int mblen(register const char *s, size_t n)
  781. {
  782. static mbstate_t state;
  783. size_t r;
  784. if (!s) {
  785. state.__mask = 0;
  786. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  787. return ENCODING == __ctype_encoding_utf8;
  788. #else
  789. return 0;
  790. #endif
  791. }
  792. if ((r = mbrlen(s, n, &state)) == (size_t) -2) {
  793. /* TODO: Should we set an error state? */
  794. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  795. return -1; /* TODO: Change error code above? */
  796. }
  797. return r;
  798. }
  799. #endif
  800. /**********************************************************************/
  801. #ifdef L_mbtowc
  802. libc_hidden_proto(mbrtowc)
  803. int mbtowc(wchar_t *__restrict pwc, register const char *__restrict s, size_t n)
  804. {
  805. static mbstate_t state;
  806. size_t r;
  807. if (!s) {
  808. state.__mask = 0;
  809. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  810. return ENCODING == __ctype_encoding_utf8;
  811. #else
  812. return 0;
  813. #endif
  814. }
  815. if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
  816. /* TODO: Should we set an error state? */
  817. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  818. return -1; /* TODO: Change error code above? */
  819. }
  820. return r;
  821. }
  822. #endif
  823. /**********************************************************************/
  824. #ifdef L_wctomb
  825. /* Note: We completely ignore state in all currently supported conversions. */
  826. libc_hidden_proto(wcrtomb)
  827. int wctomb(register char *__restrict s, wchar_t swc)
  828. {
  829. return (!s)
  830. ?
  831. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  832. (ENCODING == __ctype_encoding_utf8)
  833. #else
  834. 0 /* Encoding is stateless. */
  835. #endif
  836. : ((ssize_t) wcrtomb(s, swc, NULL));
  837. }
  838. #endif
  839. /**********************************************************************/
  840. #ifdef L_mbstowcs
  841. libc_hidden_proto(mbsrtowcs)
  842. size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
  843. {
  844. mbstate_t state;
  845. const char *e = s; /* Needed because of restrict. */
  846. state.__mask = 0; /* Always start in initial shift state. */
  847. return mbsrtowcs(pwcs, &e, n, &state);
  848. }
  849. #endif
  850. /**********************************************************************/
  851. #ifdef L_wcstombs
  852. /* Note: We completely ignore state in all currently supported conversions. */
  853. libc_hidden_proto(wcsrtombs)
  854. size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
  855. {
  856. const wchar_t *e = pwcs; /* Needed because of restrict. */
  857. return wcsrtombs(s, &e, n, NULL);
  858. }
  859. #endif
  860. /**********************************************************************/
  861. #if defined(L_wcstol) || defined(L_wcstol_l)
  862. libc_hidden_proto(__XL_NPP(wcstol))
  863. long __XL_NPP(wcstol)(const wchar_t * __restrict str,
  864. wchar_t ** __restrict endptr, int base __LOCALE_PARAM )
  865. {
  866. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 1 __LOCALE_ARG );
  867. }
  868. libc_hidden_def(__XL_NPP(wcstol))
  869. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstol_l)
  870. strong_alias(wcstol,wcstoimax)
  871. #endif
  872. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  873. #ifdef L_wcstol_l
  874. #undef wcstoll_l
  875. #else
  876. #undef wcstoll
  877. #endif
  878. extern __typeof(wcstol) __XL_NPP(wcstoll);
  879. libc_hidden_proto(__XL_NPP(wcstoll))
  880. strong_alias(__XL_NPP(wcstol),__XL_NPP(wcstoll))
  881. libc_hidden_def(__XL_NPP(wcstoll))
  882. #endif
  883. #endif
  884. /**********************************************************************/
  885. #if defined(L_wcstoll) || defined(L_wcstoll_l)
  886. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  887. libc_hidden_proto(__XL_NPP(wcstoll))
  888. long long __XL_NPP(wcstoll)(const wchar_t * __restrict str,
  889. wchar_t ** __restrict endptr, int base
  890. __LOCALE_PARAM )
  891. {
  892. return (long long) __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 1
  893. __LOCALE_ARG );
  894. }
  895. libc_hidden_def(__XL_NPP(wcstoll))
  896. #if !defined(L_wcstoll_l)
  897. #if (ULLONG_MAX == UINTMAX_MAX)
  898. strong_alias(wcstoll,wcstoimax)
  899. #endif
  900. strong_alias(wcstoll,wcstoq)
  901. #endif
  902. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  903. #endif
  904. /**********************************************************************/
  905. #if defined(L_wcstoul) || defined(L_wcstoul_l)
  906. libc_hidden_proto(__XL_NPP(wcstoul))
  907. unsigned long __XL_NPP(wcstoul)(const wchar_t * __restrict str,
  908. wchar_t ** __restrict endptr, int base
  909. __LOCALE_PARAM )
  910. {
  911. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 0 __LOCALE_ARG );
  912. }
  913. libc_hidden_def(__XL_NPP(wcstoul))
  914. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstoul_l)
  915. strong_alias(wcstoul,wcstoumax)
  916. #endif
  917. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  918. #ifdef L_wcstoul_l
  919. #undef wcstoull_l
  920. #else
  921. #undef wcstoull
  922. #endif
  923. extern __typeof(wcstoul) __XL_NPP(wcstoull);
  924. libc_hidden_proto(__XL_NPP(wcstoull))
  925. strong_alias(__XL_NPP(wcstoul),__XL_NPP(wcstoull))
  926. libc_hidden_def(__XL_NPP(wcstoull))
  927. #endif
  928. #endif
  929. /**********************************************************************/
  930. #if defined(L_wcstoull) || defined(L_wcstoull_l)
  931. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  932. libc_hidden_proto(__XL_NPP(wcstoull))
  933. unsigned long long __XL_NPP(wcstoull)(const wchar_t * __restrict str,
  934. wchar_t ** __restrict endptr, int base
  935. __LOCALE_PARAM )
  936. {
  937. return __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 0 __LOCALE_ARG );
  938. }
  939. libc_hidden_def(__XL_NPP(wcstoull))
  940. #if !defined(L_wcstoull_l)
  941. #if (ULLONG_MAX == UINTMAX_MAX)
  942. strong_alias(wcstoull,wcstoumax)
  943. #endif
  944. strong_alias(wcstoull,wcstouq)
  945. #endif
  946. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  947. #endif
  948. /**********************************************************************/