stdlib.c 26 KB

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