stdlib.c 25 KB

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