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