stdlib.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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 attribute_hidden __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. strong_alias(__qsort,qsort)
  647. /* ---------- original snippets version below ---------- */
  648. #if 0
  649. /*
  650. ** ssort() -- Fast, small, qsort()-compatible Shell sort
  651. **
  652. ** by Ray Gardner, public domain 5/90
  653. */
  654. #include <stddef.h>
  655. void ssort (void *base,
  656. size_t nel,
  657. size_t width,
  658. int (*comp)(const void *, const void *))
  659. {
  660. size_t wnel, gap, wgap, i, j, k;
  661. char *a, *b, tmp;
  662. wnel = width * nel;
  663. for (gap = 0; ++gap < nel;)
  664. gap *= 3;
  665. while ( gap /= 3 )
  666. {
  667. wgap = width * gap;
  668. for (i = wgap; i < wnel; i += width)
  669. {
  670. for (j = i - wgap; ;j -= wgap)
  671. {
  672. a = j + (char *)base;
  673. b = a + wgap;
  674. if ( (*comp)(a, b) <= 0 )
  675. break;
  676. k = width;
  677. do
  678. {
  679. tmp = *a;
  680. *a++ = *b;
  681. *b++ = tmp;
  682. } while ( --k );
  683. if (j < wgap)
  684. break;
  685. }
  686. }
  687. }
  688. }
  689. #endif
  690. #endif
  691. /**********************************************************************/
  692. #ifdef L__stdlib_mb_cur_max
  693. size_t _stdlib_mb_cur_max(void)
  694. {
  695. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  696. return __UCLIBC_CURLOCALE_DATA.mb_cur_max;
  697. #else
  698. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  699. #ifdef __UCLIBC_MJN3_ONLY__
  700. #warning need to change this when/if transliteration is implemented
  701. #endif
  702. #endif
  703. return 1;
  704. #endif
  705. }
  706. #endif
  707. /**********************************************************************/
  708. #ifdef L_mblen
  709. int mblen(register const char *s, size_t n)
  710. {
  711. static mbstate_t state;
  712. size_t r;
  713. if (!s) {
  714. state.__mask = 0;
  715. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  716. return ENCODING == __ctype_encoding_utf8;
  717. #else
  718. return 0;
  719. #endif
  720. }
  721. if ((r = mbrlen(s, n, &state)) == (size_t) -2) {
  722. /* TODO: Should we set an error state? */
  723. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  724. return (size_t) -1; /* TODO: Change error code above? */
  725. }
  726. return r;
  727. }
  728. #endif
  729. /**********************************************************************/
  730. #ifdef L_mbtowc
  731. int mbtowc(wchar_t *__restrict pwc, register const char *__restrict s, size_t n)
  732. {
  733. static mbstate_t state;
  734. size_t r;
  735. if (!s) {
  736. state.__mask = 0;
  737. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  738. return ENCODING == __ctype_encoding_utf8;
  739. #else
  740. return 0;
  741. #endif
  742. }
  743. if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
  744. /* TODO: Should we set an error state? */
  745. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  746. return (size_t) -1; /* TODO: Change error code above? */
  747. }
  748. return r;
  749. }
  750. #endif
  751. /**********************************************************************/
  752. #ifdef L_wctomb
  753. /* Note: We completely ignore state in all currently supported conversions. */
  754. int wctomb(register char *__restrict s, wchar_t swc)
  755. {
  756. return (!s)
  757. ?
  758. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  759. (ENCODING == __ctype_encoding_utf8)
  760. #else
  761. 0 /* Encoding is stateless. */
  762. #endif
  763. : ((ssize_t) wcrtomb(s, swc, NULL));
  764. }
  765. #endif
  766. /**********************************************************************/
  767. #ifdef L_mbstowcs
  768. size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
  769. {
  770. mbstate_t state;
  771. const char *e = s; /* Needed because of restrict. */
  772. state.__mask = 0; /* Always start in initial shift state. */
  773. return mbsrtowcs(pwcs, &e, n, &state);
  774. }
  775. #endif
  776. /**********************************************************************/
  777. #ifdef L_wcstombs
  778. /* Note: We completely ignore state in all currently supported conversions. */
  779. size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
  780. {
  781. const wchar_t *e = pwcs; /* Needed because of restrict. */
  782. return wcsrtombs(s, &e, n, NULL);
  783. }
  784. #endif
  785. /**********************************************************************/
  786. #if defined(L_wcstol) || defined(L_wcstol_l)
  787. long __XL(wcstol)(const wchar_t * __restrict str,
  788. wchar_t ** __restrict endptr, int base __LOCALE_PARAM )
  789. {
  790. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 1 __LOCALE_ARG );
  791. }
  792. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstol_l)
  793. strong_alias(wcstol,wcstoimax)
  794. #endif
  795. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  796. strong_alias(__XL(wcstol),__XL(wcstoll))
  797. #endif
  798. __XL_ALIAS(wcstol)
  799. #endif
  800. /**********************************************************************/
  801. #if defined(L_wcstoll) || defined(L_wcstoll_l)
  802. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  803. long long __XL(wcstoll)(const wchar_t * __restrict str,
  804. wchar_t ** __restrict endptr, int base
  805. __LOCALE_PARAM )
  806. {
  807. return (long long) __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 1
  808. __LOCALE_ARG );
  809. }
  810. #if !defined(L_wcstoll_l)
  811. #if (ULLONG_MAX == UINTMAX_MAX)
  812. strong_alias(wcstoll,wcstoimax)
  813. #endif
  814. strong_alias(wcstoll,wcstoq)
  815. #endif
  816. __XL_ALIAS(wcstoll)
  817. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  818. #endif
  819. /**********************************************************************/
  820. #if defined(L_wcstoul) || defined(L_wcstoul_l)
  821. unsigned long __XL(wcstoul)(const wchar_t * __restrict str,
  822. wchar_t ** __restrict endptr, int base
  823. __LOCALE_PARAM )
  824. {
  825. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 0 __LOCALE_ARG );
  826. }
  827. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstoul_l)
  828. strong_alias(wcstoul,wcstoumax)
  829. #endif
  830. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  831. strong_alias(__XL(wcstoul),__XL(wcstoull))
  832. #endif
  833. __XL_ALIAS(wcstoul)
  834. #endif
  835. /**********************************************************************/
  836. #if defined(L_wcstoull) || defined(L_wcstoull_l)
  837. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  838. unsigned long long __XL(wcstoull)(const wchar_t * __restrict str,
  839. wchar_t ** __restrict endptr, int base
  840. __LOCALE_PARAM )
  841. {
  842. return __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 0 __LOCALE_ARG );
  843. }
  844. #if !defined(L_wcstoull_l)
  845. #if (ULLONG_MAX == UINTMAX_MAX)
  846. strong_alias(wcstoull,wcstoumax)
  847. #endif
  848. strong_alias(wcstoull,wcstouq)
  849. #endif
  850. __XL_ALIAS(wcstoull)
  851. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  852. #endif
  853. /**********************************************************************/