stdlib.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. long int labs(long int j)
  175. {
  176. return (j >= 0) ? j : -j;
  177. }
  178. #if UINT_MAX == ULONG_MAX
  179. strong_alias(labs,abs)
  180. #endif
  181. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  182. strong_alias(labs,llabs)
  183. #endif
  184. #if ULONG_MAX == UINTMAX_MAX
  185. strong_alias(labs,imaxabs)
  186. #endif
  187. #endif
  188. /**********************************************************************/
  189. #ifdef L_llabs
  190. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  191. long long int llabs(long long int j)
  192. {
  193. return (j >= 0) ? j : -j;
  194. }
  195. #if (ULLONG_MAX == UINTMAX_MAX)
  196. strong_alias(llabs,imaxabs)
  197. #endif
  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. long atol(const char *nptr)
  212. {
  213. return strtol(nptr, (char **) NULL, 10);
  214. }
  215. #if UINT_MAX == ULONG_MAX
  216. strong_alias(atol,atoi)
  217. #endif
  218. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  219. strong_alias(atol,atoll)
  220. #endif
  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. long __XL(strtol)(const char * __restrict str, char ** __restrict endptr,
  234. int base __LOCALE_PARAM )
  235. {
  236. return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 1 __LOCALE_ARG );
  237. }
  238. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtol_l)
  239. strong_alias(strtol,strtoimax)
  240. #endif
  241. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  242. strong_alias(__XL(strtol),__XL(strtoll))
  243. #endif
  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. long long __XL(strtoll)(const char * __restrict str,
  250. char ** __restrict endptr, int base
  251. __LOCALE_PARAM )
  252. {
  253. return (long long) __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 1
  254. __LOCALE_ARG );
  255. }
  256. #if !defined(L_strtoll_l)
  257. #if (ULLONG_MAX == UINTMAX_MAX)
  258. strong_alias(strtoll,strtoimax)
  259. #endif
  260. strong_alias(strtoll,strtoq)
  261. #endif
  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. unsigned long __XL(strtoul)(const char * __restrict str,
  268. char ** __restrict endptr, int base
  269. __LOCALE_PARAM )
  270. {
  271. return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 0 __LOCALE_ARG );
  272. }
  273. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtoul_l)
  274. strong_alias(strtoul,strtoumax)
  275. #endif
  276. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  277. strong_alias(__XL(strtoul),__XL(strtoull))
  278. #endif
  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. unsigned long long __XL(strtoull)(const char * __restrict str,
  285. char ** __restrict endptr, int base
  286. __LOCALE_PARAM )
  287. {
  288. return __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 0 __LOCALE_ARG );
  289. }
  290. #if !defined(L_strtoull_l)
  291. #if (ULLONG_MAX == UINTMAX_MAX)
  292. strong_alias(strtoull,strtoumax)
  293. #endif
  294. strong_alias(strtoull,strtouq)
  295. #endif
  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. #ifndef L__stdlib_strto_l
  318. #define L__stdlib_strto_l
  319. #endif
  320. #endif
  321. #if defined(L__stdlib_strto_l) || defined(L__stdlib_strto_l_l)
  322. #if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)
  323. #define _stdlib_strto_l _stdlib_wcsto_l
  324. #define _stdlib_strto_l_l _stdlib_wcsto_l_l
  325. #define Wchar wchar_t
  326. #define Wuchar __uwchar_t
  327. #ifdef __UCLIBC_DO_XLOCALE
  328. #define ISSPACE(C) iswspace_l((C), locale_arg)
  329. #else
  330. #define ISSPACE(C) iswspace((C))
  331. #endif
  332. #else /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
  333. #define Wchar char
  334. #define Wuchar unsigned char
  335. #ifdef __UCLIBC_DO_XLOCALE
  336. #define ISSPACE(C) isspace_l((C), locale_arg)
  337. #else
  338. #define ISSPACE(C) isspace((C))
  339. #endif
  340. #endif /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
  341. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  342. unsigned long _stdlib_strto_l(register const Wchar * __restrict str,
  343. Wchar ** __restrict endptr, int base,
  344. int sflag)
  345. {
  346. return _stdlib_strto_l_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
  347. }
  348. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  349. /* This is the main work fuction which handles both strtol (sflag = 1) and
  350. * strtoul (sflag = 0). */
  351. unsigned long __XL_NPP(_stdlib_strto_l)(register const Wchar * __restrict str,
  352. Wchar ** __restrict endptr, int base,
  353. int sflag __LOCALE_PARAM )
  354. {
  355. unsigned long number, cutoff;
  356. #if _STRTO_ENDPTR
  357. const Wchar *fail_char;
  358. #define SET_FAIL(X) fail_char = (X)
  359. #else
  360. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  361. #endif
  362. unsigned char negative, digit, cutoff_digit;
  363. assert(((unsigned int)sflag) <= 1);
  364. SET_FAIL(str);
  365. while (ISSPACE(*str)) { /* Skip leading whitespace. */
  366. ++str;
  367. }
  368. /* Handle optional sign. */
  369. negative = 0;
  370. switch(*str) {
  371. case '-': negative = 1; /* Fall through to increment str. */
  372. case '+': ++str;
  373. }
  374. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  375. base += 10; /* Default is 10 (26). */
  376. if (*str == '0') {
  377. SET_FAIL(++str);
  378. base -= 2; /* Now base is 8 or 16 (24). */
  379. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  380. ++str;
  381. base += base; /* Base is 16 (16 or 48). */
  382. }
  383. }
  384. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  385. base = 16;
  386. }
  387. }
  388. number = 0;
  389. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  390. cutoff_digit = ULONG_MAX % base;
  391. cutoff = ULONG_MAX / base;
  392. do {
  393. digit = (((Wuchar)(*str - '0')) <= 9)
  394. ? (*str - '0')
  395. : ((*str >= 'A')
  396. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  397. : 40);
  398. if (digit >= base) {
  399. break;
  400. }
  401. SET_FAIL(++str);
  402. if ((number > cutoff)
  403. || ((number == cutoff) && (digit > cutoff_digit))) {
  404. number = ULONG_MAX;
  405. negative &= sflag;
  406. SET_ERRNO(ERANGE);
  407. } else {
  408. number = number * base + digit;
  409. }
  410. } while (1);
  411. }
  412. #if _STRTO_ENDPTR
  413. if (endptr) {
  414. *endptr = (Wchar *) fail_char;
  415. }
  416. #endif
  417. {
  418. unsigned long tmp = ((negative)
  419. ? ((unsigned long)(-(1+LONG_MIN)))+1
  420. : LONG_MAX);
  421. if (sflag && (number > tmp)) {
  422. number = tmp;
  423. SET_ERRNO(ERANGE);
  424. }
  425. }
  426. return negative ? (unsigned long)(-((long)number)) : number;
  427. }
  428. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  429. #endif
  430. /**********************************************************************/
  431. #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
  432. #ifndef L__stdlib_strto_ll
  433. #define L__stdlib_strto_ll
  434. #endif
  435. #endif
  436. #if defined(L__stdlib_strto_ll) || defined(L__stdlib_strto_ll_l)
  437. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  438. #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
  439. #define _stdlib_strto_ll _stdlib_wcsto_ll
  440. #define _stdlib_strto_ll_l _stdlib_wcsto_ll_l
  441. #define Wchar wchar_t
  442. #define Wuchar __uwchar_t
  443. #ifdef __UCLIBC_DO_XLOCALE
  444. #define ISSPACE(C) iswspace_l((C), locale_arg)
  445. #else
  446. #define ISSPACE(C) iswspace((C))
  447. #endif
  448. #else /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
  449. #define Wchar char
  450. #define Wuchar unsigned char
  451. #ifdef __UCLIBC_DO_XLOCALE
  452. #define ISSPACE(C) isspace_l((C), locale_arg)
  453. #else
  454. #define ISSPACE(C) isspace((C))
  455. #endif
  456. #endif /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
  457. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  458. unsigned long long _stdlib_strto_ll(register const Wchar * __restrict str,
  459. Wchar ** __restrict endptr, int base,
  460. int sflag)
  461. {
  462. return _stdlib_strto_ll_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
  463. }
  464. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  465. /* This is the main work fuction which handles both strtoll (sflag = 1) and
  466. * strtoull (sflag = 0). */
  467. unsigned long long __XL_NPP(_stdlib_strto_ll)(register const Wchar * __restrict str,
  468. Wchar ** __restrict endptr, int base,
  469. int sflag __LOCALE_PARAM )
  470. {
  471. unsigned long long number;
  472. #if _STRTO_ENDPTR
  473. const Wchar *fail_char;
  474. #define SET_FAIL(X) fail_char = (X)
  475. #else
  476. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  477. #endif
  478. unsigned int n1;
  479. unsigned char negative, digit;
  480. assert(((unsigned int)sflag) <= 1);
  481. SET_FAIL(str);
  482. while (ISSPACE(*str)) { /* Skip leading whitespace. */
  483. ++str;
  484. }
  485. /* Handle optional sign. */
  486. negative = 0;
  487. switch(*str) {
  488. case '-': negative = 1; /* Fall through to increment str. */
  489. case '+': ++str;
  490. }
  491. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  492. base += 10; /* Default is 10 (26). */
  493. if (*str == '0') {
  494. SET_FAIL(++str);
  495. base -= 2; /* Now base is 8 or 16 (24). */
  496. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  497. ++str;
  498. base += base; /* Base is 16 (16 or 48). */
  499. }
  500. }
  501. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  502. base = 16;
  503. }
  504. }
  505. number = 0;
  506. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  507. do {
  508. digit = (((Wuchar)(*str - '0')) <= 9)
  509. ? (*str - '0')
  510. : ((*str >= 'A')
  511. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  512. : 40);
  513. if (digit >= base) {
  514. break;
  515. }
  516. SET_FAIL(++str);
  517. #if 1
  518. /* Optional, but speeds things up in the usual case. */
  519. if (number <= (ULLONG_MAX >> 6)) {
  520. number = number * base + digit;
  521. } else
  522. #endif
  523. {
  524. n1 = ((unsigned char) number) * base + digit;
  525. number = (number >> CHAR_BIT) * base;
  526. if (number + (n1 >> CHAR_BIT) <= (ULLONG_MAX >> CHAR_BIT)) {
  527. number = (number << CHAR_BIT) + n1;
  528. } else { /* Overflow. */
  529. number = ULLONG_MAX;
  530. negative &= sflag;
  531. SET_ERRNO(ERANGE);
  532. }
  533. }
  534. } while (1);
  535. }
  536. #if _STRTO_ENDPTR
  537. if (endptr) {
  538. *endptr = (Wchar *) fail_char;
  539. }
  540. #endif
  541. {
  542. unsigned long long tmp = ((negative)
  543. ? ((unsigned long long)(-(1+LLONG_MIN)))+1
  544. : LLONG_MAX);
  545. if (sflag && (number > tmp)) {
  546. number = tmp;
  547. SET_ERRNO(ERANGE);
  548. }
  549. }
  550. return negative ? (unsigned long long)(-((long long)number)) : number;
  551. }
  552. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  553. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  554. #endif
  555. /**********************************************************************/
  556. /* Made _Exit() an alias for _exit(), as per C99. */
  557. /* #ifdef L__Exit */
  558. /* void _Exit(int status) */
  559. /* { */
  560. /* _exit(status); */
  561. /* } */
  562. /* #endif */
  563. /**********************************************************************/
  564. #ifdef L_bsearch
  565. void *bsearch(const void *key, const void *base, size_t /* nmemb */ high,
  566. size_t size, int (*compar)(const void *, const void *))
  567. {
  568. register char *p;
  569. size_t low;
  570. size_t mid;
  571. int r;
  572. if (size > 0) { /* TODO: change this to an assert?? */
  573. low = 0;
  574. while (low < high) {
  575. mid = low + ((high - low) >> 1); /* Avoid possible overflow here. */
  576. p = ((char *)base) + mid * size; /* Could overflow here... */
  577. r = (*compar)(key, p); /* but that's an application problem! */
  578. if (r > 0) {
  579. low = mid + 1;
  580. } else if (r < 0) {
  581. high = mid;
  582. } else {
  583. return p;
  584. }
  585. }
  586. }
  587. return NULL;
  588. }
  589. #endif
  590. /**********************************************************************/
  591. #ifdef L_qsort
  592. /* This code is derived from a public domain shell sort routine by
  593. * Ray Gardner and found in Bob Stout's snippets collection. The
  594. * original code is included below in an #if 0/#endif block.
  595. *
  596. * I modified it to avoid the possibility of overflow in the wgap
  597. * calculation, as well as to reduce the generated code size with
  598. * bcc and gcc. */
  599. void qsort (void *base,
  600. size_t nel,
  601. size_t width,
  602. int (*comp)(const void *, const void *))
  603. {
  604. size_t wgap, i, j, k;
  605. char tmp;
  606. if ((nel > 1) && (width > 0)) {
  607. assert( nel <= ((size_t)(-1)) / width ); /* check for overflow */
  608. wgap = 0;
  609. do {
  610. wgap = 3 * wgap + 1;
  611. } while (wgap < (nel-1)/3);
  612. /* From the above, we know that either wgap == 1 < nel or */
  613. /* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap < nel. */
  614. wgap *= width; /* So this can not overflow if wnel doesn't. */
  615. nel *= width; /* Convert nel to 'wnel' */
  616. do {
  617. i = wgap;
  618. do {
  619. j = i;
  620. do {
  621. register char *a;
  622. register char *b;
  623. j -= wgap;
  624. a = j + ((char *)base);
  625. b = a + wgap;
  626. if ( (*comp)(a, b) <= 0 ) {
  627. break;
  628. }
  629. k = width;
  630. do {
  631. tmp = *a;
  632. *a++ = *b;
  633. *b++ = tmp;
  634. } while ( --k );
  635. } while (j >= wgap);
  636. i += width;
  637. } while (i < nel);
  638. wgap = (wgap - width)/3;
  639. } while (wgap);
  640. }
  641. }
  642. /* ---------- original snippets version below ---------- */
  643. #if 0
  644. /*
  645. ** ssort() -- Fast, small, qsort()-compatible Shell sort
  646. **
  647. ** by Ray Gardner, public domain 5/90
  648. */
  649. #include <stddef.h>
  650. void ssort (void *base,
  651. size_t nel,
  652. size_t width,
  653. int (*comp)(const void *, const void *))
  654. {
  655. size_t wnel, gap, wgap, i, j, k;
  656. char *a, *b, tmp;
  657. wnel = width * nel;
  658. for (gap = 0; ++gap < nel;)
  659. gap *= 3;
  660. while ( gap /= 3 )
  661. {
  662. wgap = width * gap;
  663. for (i = wgap; i < wnel; i += width)
  664. {
  665. for (j = i - wgap; ;j -= wgap)
  666. {
  667. a = j + (char *)base;
  668. b = a + wgap;
  669. if ( (*comp)(a, b) <= 0 )
  670. break;
  671. k = width;
  672. do
  673. {
  674. tmp = *a;
  675. *a++ = *b;
  676. *b++ = tmp;
  677. } while ( --k );
  678. if (j < wgap)
  679. break;
  680. }
  681. }
  682. }
  683. }
  684. #endif
  685. #endif
  686. /**********************************************************************/
  687. #ifdef L__stdlib_mb_cur_max
  688. size_t _stdlib_mb_cur_max(void)
  689. {
  690. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  691. return __UCLIBC_CURLOCALE_DATA.mb_cur_max;
  692. #else
  693. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  694. #ifdef __UCLIBC_MJN3_ONLY__
  695. #warning need to change this when/if transliteration is implemented
  696. #endif
  697. #endif
  698. return 1;
  699. #endif
  700. }
  701. #endif
  702. /**********************************************************************/
  703. #ifdef L_mblen
  704. int mblen(register const char *s, size_t n)
  705. {
  706. static mbstate_t state;
  707. size_t r;
  708. if (!s) {
  709. state.__mask = 0;
  710. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  711. return ENCODING == __ctype_encoding_utf8;
  712. #else
  713. return 0;
  714. #endif
  715. }
  716. if ((r = mbrlen(s, n, &state)) == (size_t) -2) {
  717. /* TODO: Should we set an error state? */
  718. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  719. return (size_t) -1; /* TODO: Change error code above? */
  720. }
  721. return r;
  722. }
  723. #endif
  724. /**********************************************************************/
  725. #ifdef L_mbtowc
  726. int mbtowc(wchar_t *__restrict pwc, register const char *__restrict s, size_t n)
  727. {
  728. static mbstate_t state;
  729. size_t r;
  730. if (!s) {
  731. state.__mask = 0;
  732. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  733. return ENCODING == __ctype_encoding_utf8;
  734. #else
  735. return 0;
  736. #endif
  737. }
  738. if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
  739. /* TODO: Should we set an error state? */
  740. state.__wc = 0xffffU; /* Make sure we're in an error state. */
  741. return (size_t) -1; /* TODO: Change error code above? */
  742. }
  743. return r;
  744. }
  745. #endif
  746. /**********************************************************************/
  747. #ifdef L_wctomb
  748. /* Note: We completely ignore state in all currently supported conversions. */
  749. int wctomb(register char *__restrict s, wchar_t swc)
  750. {
  751. return (!s)
  752. ?
  753. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  754. (ENCODING == __ctype_encoding_utf8)
  755. #else
  756. 0 /* Encoding is stateless. */
  757. #endif
  758. : ((ssize_t) wcrtomb(s, swc, NULL));
  759. }
  760. #endif
  761. /**********************************************************************/
  762. #ifdef L_mbstowcs
  763. size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
  764. {
  765. mbstate_t state;
  766. const char *e = s; /* Needed because of restrict. */
  767. state.__mask = 0; /* Always start in initial shift state. */
  768. return mbsrtowcs(pwcs, &e, n, &state);
  769. }
  770. #endif
  771. /**********************************************************************/
  772. #ifdef L_wcstombs
  773. /* Note: We completely ignore state in all currently supported conversions. */
  774. size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
  775. {
  776. const wchar_t *e = pwcs; /* Needed because of restrict. */
  777. return wcsrtombs(s, &e, n, NULL);
  778. }
  779. #endif
  780. /**********************************************************************/
  781. #if defined(L_wcstol) || defined(L_wcstol_l)
  782. long __XL(wcstol)(const wchar_t * __restrict str,
  783. wchar_t ** __restrict endptr, int base __LOCALE_PARAM )
  784. {
  785. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 1 __LOCALE_ARG );
  786. }
  787. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstol_l)
  788. strong_alias(wcstol,wcstoimax)
  789. #endif
  790. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  791. strong_alias(__XL(wcstol),__XL(wcstoll))
  792. #endif
  793. __XL_ALIAS(wcstol)
  794. #endif
  795. /**********************************************************************/
  796. #if defined(L_wcstoll) || defined(L_wcstoll_l)
  797. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  798. long long __XL(wcstoll)(const wchar_t * __restrict str,
  799. wchar_t ** __restrict endptr, int base
  800. __LOCALE_PARAM )
  801. {
  802. return (long long) __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 1
  803. __LOCALE_ARG );
  804. }
  805. #if !defined(L_wcstoll_l)
  806. #if (ULLONG_MAX == UINTMAX_MAX)
  807. strong_alias(wcstoll,wcstoimax)
  808. #endif
  809. strong_alias(wcstoll,wcstoq)
  810. #endif
  811. __XL_ALIAS(wcstoll)
  812. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  813. #endif
  814. /**********************************************************************/
  815. #if defined(L_wcstoul) || defined(L_wcstoul_l)
  816. unsigned long __XL(wcstoul)(const wchar_t * __restrict str,
  817. wchar_t ** __restrict endptr, int base
  818. __LOCALE_PARAM )
  819. {
  820. return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 0 __LOCALE_ARG );
  821. }
  822. #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstoul_l)
  823. strong_alias(wcstoul,wcstoumax)
  824. #endif
  825. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  826. strong_alias(__XL(wcstoul),__XL(wcstoull))
  827. #endif
  828. __XL_ALIAS(wcstoul)
  829. #endif
  830. /**********************************************************************/
  831. #if defined(L_wcstoull) || defined(L_wcstoull_l)
  832. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  833. unsigned long long __XL(wcstoull)(const wchar_t * __restrict str,
  834. wchar_t ** __restrict endptr, int base
  835. __LOCALE_PARAM )
  836. {
  837. return __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 0 __LOCALE_ARG );
  838. }
  839. #if !defined(L_wcstoull_l)
  840. #if (ULLONG_MAX == UINTMAX_MAX)
  841. strong_alias(wcstoull,wcstoumax)
  842. #endif
  843. strong_alias(wcstoull,wcstouq)
  844. #endif
  845. __XL_ALIAS(wcstoull)
  846. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  847. #endif
  848. /**********************************************************************/