stdlib.c 25 KB

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