stdlib.c 26 KB

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