stdlib.c 25 KB

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