stdlib.c 27 KB

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