stdlib.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. /* Standard not very clear here, so do like glibc. */
  731. if (pwc != NULL)
  732. *pwc = L'\0';
  733. return 0;
  734. }
  735. if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
  736. /* TODO: Should we set an error state? */
  737. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  738. return -1; /* TODO: Change error code above? */
  739. }
  740. return r;
  741. }
  742. #endif
  743. /**********************************************************************/
  744. #ifdef L_wctomb
  745. /* Note: We completely ignore state in all currently supported conversions. */
  746. int wctomb(register char *__restrict s, wchar_t swc)
  747. {
  748. return (!s)
  749. ?
  750. /*
  751. In this case we have to return 0 because the only multibyte supported encoding
  752. is utf-8, that is a stateless encoding. See wctomb() documentation.
  753. */
  754. is_stateful(ENCODING)
  755. : ((ssize_t) wcrtomb(s, swc, NULL));
  756. }
  757. #endif
  758. /**********************************************************************/
  759. #ifdef L_mbstowcs
  760. size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
  761. {
  762. mbstate_t state;
  763. const char *e = s; /* Needed because of restrict. */
  764. state.__mask = 0; /* Always start in initial shift state. */
  765. return mbsrtowcs(pwcs, &e, n, &state);
  766. }
  767. #endif
  768. /**********************************************************************/
  769. #ifdef L_wcstombs
  770. /* Note: We completely ignore state in all currently supported conversions. */
  771. size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
  772. {
  773. const wchar_t *e = pwcs; /* Needed because of restrict. */
  774. return wcsrtombs(s, &e, n, NULL);
  775. }
  776. #endif
  777. /**********************************************************************/
  778. #if defined(L_wcstol) || defined(L_wcstol_l)
  779. long __XL_NPP(wcstol)(const wchar_t * __restrict str,
  780. wchar_t ** __restrict endptr, int base __LOCALE_PARAM)
  781. {
  782. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 1 __LOCALE_ARG);
  783. }
  784. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstol_l)
  785. strong_alias(wcstol,wcstoimax)
  786. #endif
  787. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  788. strong_alias_untyped(__XL_NPP(wcstol),__XL_NPP(wcstoll))
  789. #endif
  790. #endif
  791. /**********************************************************************/
  792. #if defined(L_wcstoll) || defined(L_wcstoll_l)
  793. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  794. long long __XL_NPP(wcstoll)(const wchar_t * __restrict str,
  795. wchar_t ** __restrict endptr, int base
  796. __LOCALE_PARAM)
  797. {
  798. return (long long) __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 1 __LOCALE_ARG);
  799. }
  800. #if !defined(L_wcstoll_l)
  801. #if (ULLONG_MAX == UINTMAX_MAX)
  802. strong_alias(wcstoll,wcstoimax)
  803. #endif
  804. strong_alias(wcstoll,wcstoq)
  805. #endif
  806. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  807. #endif
  808. /**********************************************************************/
  809. #if defined(L_wcstoul) || defined(L_wcstoul_l)
  810. unsigned long __XL_NPP(wcstoul)(const wchar_t * __restrict str,
  811. wchar_t ** __restrict endptr, int base
  812. __LOCALE_PARAM)
  813. {
  814. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 0 __LOCALE_ARG);
  815. }
  816. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstoul_l)
  817. strong_alias(wcstoul,wcstoumax)
  818. #endif
  819. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  820. strong_alias_untyped(__XL_NPP(wcstoul),__XL_NPP(wcstoull))
  821. #endif
  822. #endif
  823. /**********************************************************************/
  824. #if defined(L_wcstoull) || defined(L_wcstoull_l)
  825. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  826. unsigned long long __XL_NPP(wcstoull)(const wchar_t * __restrict str,
  827. wchar_t ** __restrict endptr, int base
  828. __LOCALE_PARAM)
  829. {
  830. return __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 0 __LOCALE_ARG);
  831. }
  832. #if !defined(L_wcstoull_l)
  833. #if (ULLONG_MAX == UINTMAX_MAX)
  834. strong_alias(wcstoull,wcstoumax)
  835. #endif
  836. strong_alias(wcstoull,wcstouq)
  837. #endif
  838. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  839. #endif
  840. /**********************************************************************/